<?php
namespace App\Smarty\Functions;

/**
 * Display document series details in various ways.
 * The parameter 'ref' is mandatory and it represents the document id or an \Entity\Document instantiation object.
 * The 'default' parameter indicates what the function will output in case no data is retrieved.
 * The parameter 'assign' represents the variable that will store the \Entity\Document object after instantiation.
 * The parameter 'format' represents the format of the function output - this may include HTML format and some variables.
 * Available variables: %code%, %nextNumber%.
 */
class DocumentLabel
{

	/**
	 * @param Array $params
	 * @return String
	 */
	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\Document') {
			$document = $params['ref'];
		}
		elseif (is_numeric($params['ref']) and $params['ref'] > 0) {
			$document = \Entity\Document::select($params['ref']);
			if (!$document->recordExists()) {
				return $defaultOutput;
			}
		}
		else {
			return $defaultOutput;
		}


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

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

		//--------------------CHECK OUTPUT FORMAT--------------------------------------------------
		// CHECK OUTPUT FORMAT --------------------------------------------------------------------
		if (!$params['format']) {
			if ($params['hideLink']) {
				return "#{$document->getId()}";
			}
			else{
				$documentLabel = sprintf("<span class=\"remoteLoadLink\" onclick=\"RemoteLoad.load(this, MVC.appUrl('RemoteData/InfoBox', '%s', { 'id': '%s'}), null); return false;\">%s</span>",
					'document',
					$document->getId(),
					($document->getId() ? "#{$document->getId()}" : $defaultOutput));
				return $documentLabel;
			}
		}
		return '';
	}

}