class_name = $class_name; $this->table_name = $table_name; $this->identifier = $identifier; $this->form_action = $form_action; } public function setTitle($title) { $this->title = $title; } public function setSubmitButton($title) { $this->submit_button_title = $title; } public function setFields($fields) { $this->form_inputs = $fields; } public function display(&$smarty, $langs, $obj) { global $cookie; $this->prepareFieldsToDisplay($obj, $langs); $smarty_params = [ 'current_id_lang' => $cookie->id_lang, 'langs' => $langs, 'page_title' => $this->title, 'form_action'=> $this->urlFormAction($this->form_action), 'form_inputs'=> $this->form_inputs, 'id_name' => $this->identifier, 'id' => $obj->id, 'module_dir' => _MODULE_DIR_, 'button_submit' => [ 'name' => $this->submitName($this->form_action), 'value' => $this->submit_button_title ] ]; $smarty->assign($smarty_params); echo $smarty->fetch(self::FORM_DIR.'form_edit.tpl'); } private function prepareFieldsToDisplay($obj, $langs) { global $cookie; foreach ($this->form_inputs as $key => &$field) { $field['name'] = $key; $field['value'] = ''; $field['html_attrs'] = ''; $this->prepareTemplatePath($field); $this->prepareInputHtmlAttributes($field); $this->prepareSelectOptions($field); $this->prepareTinyMCE($field, $cookie); $this->updateValueFields($field, $obj, $key, $langs); } } private function prepareTemplatePath(&$field) { $field['template'] = self::FORM_DIR.$field['type'].'.tpl'; } private function prepareInputHtmlAttributes(&$field) { if (isset($field['attrs']) && is_array($field['attrs'])) { $html_attrs = ''; foreach($field['attrs'] as $key => $value) { $html_attrs .= " $key = \"".$value."\""; } $field['html_attrs'] = $html_attrs; } } private function prepareSelectOptions(&$field) { if (isset($field['options_raw']) && isset($field['options_map'])) { $level_prefix = '—'; if (isset($field['level_prefix'])) { $level_prefix = $field['level_prefix']; } $rows = $field['options_raw']; $keys = $field['options_map']; $options = []; foreach($rows as $row) { $options_row['value'] = $row[$keys[0]]; $options_row['label'] = $row[$keys[1]]; if (!empty($level_prefix) && $row[$keys[2]]) { $options_row['label'] = str_repeat($level_prefix, $row[$keys[2]]) .' '.$options_row['label']; } $options[] = $options_row; } $field['options'] = $options; } } private function updateValueFields(&$field, $obj, $key, $langs) { if (!isset($field['initial_value'])) { $field['initial_value'] = ''; } $value = &$field['value']; if (isset($field['translatable']) && $field['translatable']==true) { foreach ($langs as $lang) { $val = $this->getFieldValue($obj, $key, $field['initial_value'], $lang['id_lang']); $value[$lang['id_lang']] = htmlentities( $val, ENT_COMPAT, 'UTF-8' ); } } else { $field['value'] = $this->getFieldValue($obj, $key, $field['initial_value'], null); } } private function prepareTinyMCE(&$field, $cookie) { if ('textarea_tinymce' == $field['type']) { $iso = Language::getIsoById((int)($cookie->id_lang)); $field['iso'] = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en'); $field['ad'] = dirname($_SERVER["PHP_SELF"]); $field['path_theme_css'] = _THEME_CSS_DIR_; $field['base_uri'] = __PS_BASE_URI__; } } private function urlFormAction($submit_action) { global $currentIndex; return $currentIndex .'&token='.Tools::getAdminTokenLite($this->class_name) .'&submit'.$submit_action.$this->table_name.'=1' ; } private function submitName($submit_action) { return 'submit'.$submit_action.$this->table_name.$this->bt_action_suffix; } public function enableSubmitAndBackToParent() { $this->bt_action_suffix = 'AndBackToParent'; } private function getFieldValue($obj, $key, $initial_value, $id_lang = NULL) { if ($id_lang) $defaultValue = ($obj->id AND isset($obj->{$key}[$id_lang])) ? $obj->{$key}[$id_lang] : $initial_value; else $defaultValue = isset($obj->{$key}) ? $obj->{$key} : $initial_value; return Tools::getValue($key.($id_lang ? '_'.$id_lang : ''), $defaultValue); } }