<?php
namespace App\Smarty\Functions;

class TaskLabel
{

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

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

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

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

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

}