<?php
namespace App\Smarty\Functions;

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

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

		// GET FREQUENT BUYER CARD DETAILS --------------------------------------------------------
		// frequent buyer card object reference
		if (is_object($params['ref']) and get_class($params['ref'])=='Entity\FrequentBuyerCard') {
			$frequentBuyerCard = $params['ref'];
		}
		// worldspan service id
		elseif ( is_numeric($params['ref']) and $params['ref']>0 ) {
			$frequentBuyerCard = \Entity\FrequentBuyerCard::select($params['ref']);
			if (!$frequentBuyerCard->recordExists()) {
				return $defaultOutput;
			}
		}
		// something invalid
		else {
			return $defaultOutput;
		}

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

		// CHECK OUTPUT FORMAT --------------------------------------------------------------------
		if ($params['hideLink']) {
			return $frequentBuyerCard->cardNumber ? $frequentBuyerCard->cardNumber : "#{$frequentBuyerCard->getId()}";
		}
		else {
			return sprintf("<span class=\"remoteLoadLink\" onclick=\"RemoteLoad.loadEntity(this, '%s', '%s');\">%s</span>",
				'frequentBuyerCard',
				$frequentBuyerCard->getId(),
				($frequentBuyerCard->cardNumber ?: "#{$frequentBuyerCard->getId()}"));
		}
	}

}