* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ /** * Use this helper to generate preferences forms, with values stored in the configuration table. */ class HelperOptionsCore extends Helper { public $required = false; /** @var int */ public $id; public function __construct() { $this->base_folder = 'helpers/options/'; $this->base_tpl = 'options.tpl'; parent::__construct(); } /** * Generate a form for options. * * @param array $option_list * * @return string html */ public function generateOptions($option_list) { $this->tpl = $this->createTemplate($this->base_tpl); $tab = Tab::getTab($this->context->language->id, $this->id); $languages = Language::getLanguages(false); $has_color_field = false; $use_multishop = false; $hide_multishop_checkbox = (Shop::getTotalShops(false, null) < 2) ? true : false; foreach ($option_list as $category => $category_data) { if (!is_array($category_data)) { continue; } if (!isset($category_data['image'])) { $category_data['image'] = (!empty($tab['module']) && file_exists($_SERVER['DOCUMENT_ROOT'] . _MODULE_DIR_ . $tab['module'] . '/' . $tab['class_name'] . '.gif') ? _MODULE_DIR_ . $tab['module'] . '/' : '../img/t/') . $tab['class_name'] . '.gif'; } if (!isset($category_data['fields'])) { $category_data['fields'] = []; } $category_data['hide_multishop_checkbox'] = true; if (isset($category_data['tabs'])) { $tabs[$category] = $category_data['tabs']; $tabs[$category]['misc'] = Context::getContext()->getTranslator()->trans('Miscellaneous', [], 'Admin.Global'); } foreach ($category_data['fields'] as $key => $field) { if (empty($field['no_multishop_checkbox']) && !$hide_multishop_checkbox) { $category_data['hide_multishop_checkbox'] = false; } // Set field value unless explicitly denied if (!isset($field['auto_value']) || $field['auto_value']) { $field['value'] = $this->getOptionValue($key, $field); } // Check if var is invisible (can't edit it in current shop context), or disable (use default value for multishop) $is_disabled = $is_invisible = false; if (Shop::isFeatureActive()) { if (isset($field['visibility']) && $field['visibility'] > Shop::getContext()) { $is_disabled = true; $is_invisible = true; } elseif (Shop::getContext() != Shop::CONTEXT_ALL && !Configuration::isOverridenByCurrentContext($key)) { $is_disabled = true; } } $field['is_disabled'] = $is_disabled; $field['is_invisible'] = $is_invisible; $field['required'] = isset($field['required']) ? $field['required'] : $this->required; if ($field['type'] === 'checkbox' && isset($field['value_multiple'])) { $multipleValues = is_array($field['value']) ? $field['value'] : explode(',', $field['value']); foreach ($multipleValues as $currentValue) { $field['value_multiple'][$currentValue] = true; } } if ($field['type'] == 'color') { $has_color_field = true; $this->context->controller->addJqueryPlugin('colorpicker'); } if ($field['type'] == 'textarea' || $field['type'] == 'textareaLang') { if (isset($field['autoload_rte']) && $field['autoload_rte'] == true) { $iso = $this->context->language->iso_code; $this->tpl_vars['iso'] = file_exists(_PS_CORE_DIR_ . '/js/tiny_mce/langs/' . $iso . '.js') ? $iso : 'en'; $this->tpl_vars['path_css'] = _THEME_CSS_DIR_; $this->tpl_vars['ad'] = __PS_BASE_URI__ . basename(_PS_ADMIN_DIR_); $this->tpl_vars['tinymce'] = true; $this->context->controller->addJS(_PS_JS_DIR_ . 'tiny_mce/tiny_mce.js'); $this->context->controller->addJS(_PS_JS_DIR_ . 'admin/tinymce.inc.js'); } else { $this->context->controller->addJqueryPlugin('autosize'); } } if ($field['type'] == 'file') { $uploader = new HelperUploader(); $uploader->setId(isset($field['id']) ? $field['id'] : null); $uploader->setName($field['name']); $uploader->setUrl(isset($field['url']) ? $field['url'] : null); $uploader->setMultiple(isset($field['multiple']) ? $field['multiple'] : false); $uploader->setUseAjax(isset($field['ajax']) ? $field['ajax'] : false); $uploader->setMaxFiles(isset($field['max_files']) ? $field['max_files'] : null); if (isset($field['files']) && $field['files']) { $uploader->setFiles($field['files']); } elseif (isset($field['image']) && $field['image']) { // Use for retrocompatibility $uploader->setFiles([ 0 => [ 'type' => HelperUploader::TYPE_IMAGE, 'image' => isset($field['image']) ? $field['image'] : null, 'size' => isset($field['size']) ? $field['size'] : null, 'delete_url' => isset($field['delete_url']) ? $field['delete_url'] : null, ], ]); } if (isset($field['file']) && $field['file']) { // Use for retrocompatibility $uploader->setFiles([ 0 => [ 'type' => HelperUploader::TYPE_FILE, 'size' => isset($field['size']) ? $field['size'] : null, 'delete_url' => isset($field['delete_url']) ? $field['delete_url'] : null, 'download_url' => isset($field['file']) ? $field['file'] : null, ], ]); } if (isset($field['thumb']) && $field['thumb']) { // Use for retrocompatibility $uploader->setFiles([ 0 => [ 'type' => HelperUploader::TYPE_IMAGE, 'image' => isset($field['thumb']) ? '' . $field['title'] . '' : null, ], ]); } $uploader->setTitle(isset($field['title']) ? $field['title'] : null); $field['file'] = $uploader->render(); } // Cast options values if specified if ($field['type'] == 'select' && isset($field['cast'])) { foreach ($field['list'] as $option_key => $option) { $field['list'][$option_key][$field['identifier']] = $field['cast']($option[$field['identifier']]); } } // Fill values for all languages for all lang fields if (substr($field['type'], -4) == 'Lang') { $field['value'] = []; foreach ($languages as $language) { if ($field['type'] == 'textLang') { $value = Tools::getValue($key . '_' . $language['id_lang'], Configuration::get($key, $language['id_lang'])); } elseif ($field['type'] == 'textareaLang') { $value = Configuration::get($key, $language['id_lang']); } elseif ($field['type'] == 'selectLang') { $value = Configuration::get($key, $language['id_lang']); } if (isset($value)) { $field['languages'][$language['id_lang']] = $value; } else { $field['languages'][$language['id_lang']] = ''; } $field['value'][$language['id_lang']] = $this->getOptionValue($key . '_' . strtoupper($language['iso_code']), $field); } } // pre-assign vars to the tpl // @todo move this if ($field['type'] == 'maintenance_ip') { $field['script_ip'] = ' '; $field['link_remove_ip'] = ''; } // Multishop default value $field['multishop_default'] = false; if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_ALL && !$is_invisible) { $field['multishop_default'] = true; $use_multishop = true; } // Assign the modifications back to parent array $category_data['fields'][$key] = $field; // Is at least one required field present? if (isset($field['required']) && $field['required']) { $category_data['required_fields'] = true; } } // Assign the modifications back to parent array $option_list[$category] = $category_data; } $this->tpl->assign([ 'title' => $this->title, 'toolbar_btn' => $this->toolbar_btn, 'show_toolbar' => $this->show_toolbar, 'toolbar_scroll' => $this->toolbar_scroll, 'current' => $this->currentIndex, 'table' => $this->table, 'token' => $this->token, 'tabs' => (isset($tabs)) ? $tabs : null, 'option_list' => $option_list, 'current_id_lang' => $this->context->language->id, 'languages' => $languages, 'currency_left_sign' => $this->context->currency->getSign('left'), 'currency_right_sign' => $this->context->currency->getSign('right'), 'use_multishop' => $use_multishop, 'has_color_field' => $has_color_field, 'controller_name' => $this->controller_name, ]); return parent::generate(); } /** * Type = image. */ public function displayOptionTypeImage($key, $field, $value) { echo ''; echo ''; $i = 0; foreach ($field['list'] as $theme) { echo ''; if (isset($field['max']) && ($i + 1) % $field['max'] == 0) { echo ''; } ++$i; } echo ''; echo '
'; echo ''; echo ''; echo '
'; echo ''; echo '
'; } /** * Type = price. */ public function displayOptionTypePrice($key, $field, $value) { echo $this->context->currency->getSign('left'); if (method_exists($this, 'displayOptionTypeText')) { $this->displayOptionTypeText($key, $field, $value); } echo $this->context->currency->getSign('right') . ' ' . Context::getContext()->getTranslator()->trans('(tax excl.)', [], 'Admin.Global'); } /** * Type = disabled. */ public function displayOptionTypeDisabled($key, $field, $value) { echo $field['disabled']; } public function getOptionValue($key, $field) { $value = Tools::getValue($key, Configuration::get($key)); if (!empty($field['skip_clean_html'])) { return $value; } if (!Validate::isCleanHtml($value)) { $value = Configuration::get($key); } if (isset($field['defaultValue']) && !$value) { $value = $field['defaultValue']; } return Tools::purifyHTML($value); } }