<?php
namespace App\Smarty\Functions;

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

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

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

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

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

	}

}