* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL * @license https://opensource.org/licenses/MIT The MIT License * * Don't forget to prefix your containers with your own identifier * to avoid any conflicts with others containers. */ abstract class RetailcrmAbstractTemplate { /** @var Module|\RetailCRM */ protected $module; protected $smarty; protected $assets; /** @var string */ protected $template; /** @var array */ protected $data; /** @var array */ private $errors; /** @var array */ private $warnings; /** @var array */ private $informations; /** @var array */ private $confirmations; /** @var Context */ protected $context; /** * RetailcrmAbstractTemplate constructor. * * @param Module $module * @param $smarty * @param $assets */ public function __construct(Module $module, $smarty, $assets) { $this->module = $module; $this->smarty = $smarty; $this->assets = $assets; $this->errors = []; $this->warnings = []; $this->informations = []; $this->confirmations = []; } /** * Returns ISO code of current employee language or default language. * * @return string */ protected function getCurrentLanguageISO() { $langId = 0; global $cookie; if (!empty($this->context) && !empty($this->context->employee)) { $langId = (int) $this->context->employee->id_lang; } elseif ($cookie instanceof Cookie) { $langId = (int) $cookie->id_lang; } else { $langId = (int) Configuration::get('PS_LANG_DEFAULT'); } return (string) Language::getIsoById($langId); } /** * @param $file * * @return mixed * * @throws RuntimeException */ public function render($file) { $this->buildParams(); $this->setTemplate(); if (null === $this->template) { throw new \RuntimeException('Template not be blank'); } // set url post for forms if (empty($this->smarty->getTemplateVars('url_post'))) { $this->data['url_post'] = $this->smarty->getTemplateVars('current') . '&token=' . $this->smarty->getTemplateVars('token'); } $this->smarty->assign(\array_merge($this->data, [ 'moduleErrors' => $this->errors, 'moduleWarnings' => $this->warnings, 'moduleConfirmations' => $this->confirmations, 'moduleInfos' => $this->informations, ])); return $this->module->display($file, "views/templates/admin/$this->template"); } /** * @param $messages * * @return self */ public function setErrors($messages) { if (!empty($messages)) { $this->errors = $messages; } return $this; } /** * @param $messages * * @return self */ public function setWarnings($messages) { if (!empty($messages)) { $this->warnings = $messages; } return $this; } /** * @param $messages * * @return self */ public function setInformations($messages) { if (!empty($messages)) { $this->informations = $messages; } return $this; } /** * @param $messages * * @return self */ public function setConfirmations($messages) { if (!empty($messages)) { $this->confirmations = $messages; } return $this; } /** * @param $context * * @return self */ public function setContext($context) { if (!empty($context)) { $this->context = $context; } return $this; } abstract protected function buildParams(); abstract protected function setTemplate(); }