<?php
namespace App\Smarty\Functions;

class BankLabel
{

	static public function create($params, $smarty)
	{

		// CHECK INPUT --------------------------------------------------------------------------------
		$defaultOutput = '';
		if ($params['default']) {
			$defaultOutput = $params['default'];
		}

		// GET BANK DETAILS ---------------------------------------------------------------------------
		// Bank object reference
		if (is_object($params['ref']) and get_class($params['ref'])=='Entity\Bank') {
			$bank = $params['ref'];
		}
		// bank id
		elseif ( is_numeric($params['ref']) and $params['ref']>0 ) {
			$bank = \Entity\Bank::select($params['ref']);
			if (!$bank->recordExists()) {
				return $defaultOutput;
			}
		}
		// something invalid
		else {
			return $defaultOutput;
		}

		// STORE BANK DETAILS IN AN OUTPUT PARAMETER ($params['assign']) ------------------------------
		if ($params['assign']) {
			$smarty->assign($params['assign'], $bank);
			return '';
		}

		// CHECK OUTPUT FORMAT ------------------------------------------------------------------------
		if (!$params['format']) {
			if ($params['hideLink']) {
				return $bank->name ? $bank->name : $defaultOutput;
			}
			else {
				return sprintf("<span class=\"remoteLoadLink\" onclick=\"RemoteLoad.loadEntity(this, '%s', '%s');\">%s</span>",
					'bank',
					$bank->getId(),
					($bank->name ? $bank->name : $defaultOutput)
					);
			}
		}
		else {
			$replaceArray = [];
			$replaceArray['%name%']			= $bank->name;
			$replaceArray['%name2%']		= $bank->name2;
			$replaceArray['%nameStandard%']	= $bank->nameStandard;
			$replaceArray['%nameLocal%']	= $bank->nameLocal;
			$replaceArray['%code%']			= $bank->code;

			return str_replace(array_keys($replaceArray), array_values($replaceArray), $params['format']);
		}
	}

}