<?php
namespace App\Smarty\Functions;

class ImportedDocumentLabel
{

	static public function create($params, $smarty)
	{
		$defaultOutput = '';

		if ($params['default']) {
			$defaultOutput = $params['default'];
		}

		//---------------------GET PRODUCT INSTANCE DETAILS----------------------------------------
		if (is_object($params['ref']) and get_class($params['ref']) == 'Entity\ImportedDocument') {
			$importedDocument = $params['ref'];
		}
		elseif (is_numeric($params['ref']) and $params['ref'] > 0) {
			$importedDocument = \Entity\ImportedDocument::select($params['ref']);
			if (!$importedDocument->recordExists()) {
				return $defaultOutput;
			}
		}
		else {
			return $defaultOutput;
		}



		//------STORE PRODUCT INSTANCE DETAILS IN AN OUTPUT PARAMETERS ($params['assign'])---------

		if ($params['assign']) {
			$smarty->assign($params['assign'], $importedDocument);
			return '';
		}

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

}