* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision$ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ /* * TODO : move HTML code in template files * TODO : phpDoc on two last methods: includeDatepicker() & bindDatepicker() */ class HelperCore { public $currentIndex; public $table; public $identifier; public $token; public $toolbar_btn; public $ps_help_context; public $title; public $show_toolbar = true; public $context; public $toolbar_fix = false; /** @var string Helper tpl folder */ public $base_folder; /** @var string Controller tpl folder */ public $override_folder; /** * @var smartyTemplate base template object */ protected $tpl; /** * @var string base template name */ public $base_tpl = 'content.tpl'; public $tpl_vars = array(); public function __construct() { $this->context = Context::getContext(); } public function setTpl($tpl) { $this->tpl = $this->createTemplate($tpl); } /** * Create a template from the override file, else from the base file. * * @param string $tpl_name filename * @return Template */ public function createTemplate($tpl_name) { // Overrides exists? if ($this->override_folder && file_exists($this->context->smarty->template_dir[0].$this->override_folder.$tpl_name)) return $this->context->smarty->createTemplate($this->override_folder.$tpl_name); return $this->context->smarty->createTemplate($this->base_folder.$tpl_name); } /** * default behaviour for helper is to return a tpl fetched * * @return void */ public function generate() { $this->tpl->assign($this->tpl_vars); return $this->tpl->fetch(); } /** * * @param type $trads values of translations keys * For the moment, translation are not automatic * @param type $selected_cat array of selected categories * Format * Array * ( * [0] => 1 * [1] => 2 * ) * OR * Array * ( * [1] => Array * ( * [id_category] => 1 * [name] => Home page * [link_rewrite] => home * ) * ) * @param type $input_name name of input * @return string */ public static function renderAdminCategorieTree($trads, $selected_cat = array(), $input_name = 'categoryBox', $use_radio = false, $use_search = false, $disabled_categories = array(), $use_in_popup = false) { if (!$use_radio) $input_name = $input_name.'[]'; $context = Context::getContext(); $context->controller->addCSS(_PS_JS_DIR_.'jquery/plugins/treeview/jquery.treeview.css'); $context->controller->addJs(array( _PS_JS_DIR_.'jquery/plugins/treeview/jquery.treeview.js', _PS_JS_DIR_.'jquery/plugins/treeview/jquery.treeview.async.js', _PS_JS_DIR_.'jquery/plugins/treeview/jquery.treeview.edit.js', _PS_JS_DIR_.'admin-categories-tree.js')); if ($use_search) $context->controller->addJs(_PS_JS_DIR_.'jquery/plugins/autocomplete/jquery.autocomplete.js'); $html = ' '; $html .= '
'.$trads['Collapse All'].' | '.$trads['Expand All'].' '.(!$use_radio ? ' | '.$trads['Check All'].' | '.$trads['Uncheck All'].'| ' : '').($use_search ? ''.$trads['search'].' : ' : '').'
'; $home_is_selected = false; foreach($selected_cat AS $cat) { if (is_array($cat)) { $disabled = in_array($cat['id_category'], $disabled_categories); if ($cat['id_category'] != 1) $html .= ''; else $home_is_selected = true; } else { $disabled = in_array($cat, $disabled_categories); if ($cat != 1) $html .= ''; else $home_is_selected = true; } } $html .= ' '; return $html; } /** * use translations files to replace english expression. * * @param mixed $string term or expression in english * @param string $class * @param boolan $addslashes if set to true, the return value will pass through addslashes(). Otherwise, stripslashes(). * @param boolean $htmlentities if set to true(default), the return value will pass through htmlentities($string, ENT_QUOTES, 'utf-8') * @return string the translation if available, or the english default text. */ protected function l($string, $class = 'AdminTab', $addslashes = FALSE, $htmlentities = TRUE) { // if the class is extended by a module, use modules/[module_name]/xx.php lang file $currentClass = get_class($this); if(Module::getModuleNameFromClass($currentClass)) { $string = str_replace('\'', '\\\'', $string); return Module::findTranslation(Module::$classInModule[$currentClass], $string, $currentClass); } global $_LANGADM; if ($class == __CLASS__) $class = 'AdminTab'; $key = md5(str_replace('\'', '\\\'', $string)); $str = (key_exists(get_class($this).$key, $_LANGADM)) ? $_LANGADM[get_class($this).$key] : ((key_exists($class.$key, $_LANGADM)) ? $_LANGADM[$class.$key] : $string); $str = $htmlentities ? htmlentities($str, ENT_QUOTES, 'utf-8') : $str; return str_replace('"', '"', ($addslashes ? addslashes($str) : stripslashes($str))); } /** * Render an area to determinate shop association * * @param string $type 'shop' or 'group_shop' * * @return string */ public function renderAssoShop($type = 'shop') { if (!Shop::isFeatureActive()) return; if ($type != 'shop' && $type != 'group_shop') $type = 'shop'; $assos = array(); if ((int)$this->id) { $sql = 'SELECT `id_'.bqSQL($type).'`, `'.bqSQL($this->identifier).'` FROM `'._DB_PREFIX_.bqSQL($this->table).'_'.bqSQL($type).'` WHERE `'.bqSQL($this->identifier).'` = '.(int)$this->id; foreach (Db::getInstance()->executeS($sql) as $row) $assos[$row['id_'.$type]] = $row['id_'.$type]; } $tpl = $this->createTemplate('helper/assoshop.tpl'); $tpl->assign(array( 'input' => array( 'type' => $type, 'values' => Shop::getTree(), ), 'fields_value' => array( 'shop' => $assos ), 'form_id' => $this->id, 'table' => $this->table )); return $tpl->fetch(); } /** * Render a form with potentials required fields * * @param string $class_name * @param string $identifier * @param array $table_fields * @return string */ public function renderRequiredFields($class_name, $identifier, $table_fields) { $rules = call_user_func_array(array($class_name, 'getValidationRules'), array($class_name)); $required_class_fields = array($identifier); foreach ($rules['required'] as $required) $required_class_fields[] = $required; $object = new $class_name(); $res = $object->getFieldsRequiredDatabase(); $required_fields = array(); foreach ($res as $row) $required_fields[(int)$row['id_required_field']] = $row['field_name']; $this->tpl_vars = array( 'table_fields' => $table_fields, 'irow' => 0, 'required_class_fields' => $required_class_fields, 'required_fields' => $required_fields, 'current' => $this->currentIndex, 'token' => $this->token ); $tpl = $this->createTemplate('helper/required_fields.tpl'); $tpl->assign($this->tpl_vars); return $tpl->fetch(); } }