<?php
namespace App\Smarty\Functions;

class TaskCategoryLabel
{
	static public function create($params, $smarty)
	{
		// CHECK INPUT --------------------------------------------------------------------------------
		$defaultOutput = '';
		if ($params['default']) {
			$defaultOutput = $params['default'];
		}

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

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

		// CHECK OUTPUT FORMAT ------------------------------------------------------------------------
		if (!$params['format']) {
			return $categ->category ? $categ->category : $defaultOutput;
		}
		else {
			$replaceArray = [];
			$replaceArray['%name%'] = $categ->category;

			return str_replace(array_keys($replaceArray), array_values($replaceArray), $params['format']);
		}
	}
}
