<?php
namespace App\Smarty\Functions;

class ImportedOrdersSystemLabel
{

	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\ExtResSystem') {
			$importedOrdersSystems = $params['ref'];
		}
		elseif (is_numeric($params['ref']) and $params['ref'] > 0) {
			$importedOrdersSystems = \Entity\ExtResSystem::select($params['ref']);
			if (!$importedOrdersSystems->recordExists()) {
				return $defaultOutput;
			}
		}
		else {
			return $defaultOutput;
		}



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

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

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

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

}