<?php
namespace App\Smarty\Functions;

class ActivityDomainLabel
{


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

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

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

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

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

}