<?php
namespace Modules\Admin\Config\Currencies;

/**
 * Model class for listing currencies.
 * @author adrian.voicu
 */
class Model extends \App\Mvc\Model_ListingAbstract
{

	public function list_execute(\App\Mvc\View\Listing\Members\Params $listingParams)
	{

		$alternates = (bool) $this->globalCfg['useAlternativeNames']['currencies'];

		$list = [];

		$filters = $listingParams->getFilters();

		foreach (\App\Config\Currencies::getAdmin()->getAll() as $code => $details) {

			// remove elements that are not according to the filters
			if ($filters['currencyName'] && strpos(mb_strtolower($details['currencyName']), mb_strtolower($filters['currencyName']))===false) {
				continue;
			}

			$list[$code] = [
				'id' =>			$code,
				'name' => 		$details['currencyName'],
				'subdivision' =>$details['subdivisionName']
			];

			if($alternates) {
				$list[$code]['nameAlt'] =			$details['currencyName2'];
				$list[$code]['subdivisionAlt'] =	$details['subdivisionName2'];
			}
		}

		$defaultCurrency = \App\Config\Currencies::getAdmin()->getDefaultCurrency();
		$workCurrency = \App\Config\Currencies::getAdmin()->getWorkCurrency();

		// if in $list, add the default value for currency
		if (array_key_exists($defaultCurrency,$list)) {
			$list[$defaultCurrency]['default'] = true;
		}

		// if in $list, add the work currency
		if (array_key_exists($workCurrency,$list)) {
			$list[$workCurrency]['work'] = true;
		}

		$this->totalRecordsCount = count($list);
		$this->recordsList = $list;

	}
}