maxImageBytes = (Configuration::get('PS_LIMIT_UPLOAD_IMAGE_VALUE') * 1000000); $this->table = 'guide_category'; $this->className = 'GuideCategory'; $this->lang = true; $this->edit = true; $this->delete = true; $this->view = true; $this->fieldsDisplay = array( 'id_guide_category' => array( 'title' => $this->l('ID'), 'align' => 'center', 'width' => 30 ), 'name' => array( 'title' => $this->l('Title'), 'width' => 100 ), 'description' => array( 'title' => $this->l('Description'), 'width' => 500, 'maxlength' => 90, 'orderby' => false ), 'position' => array( 'title' => $this->l('Position'), 'width' => 40, 'filter_key' => 'position', 'align' => 'center', 'position' => 'position' ), 'active' => array( 'title' => $this->l('Displayed'), 'active' => 'status', 'align' => 'center', 'type' => 'bool', 'orderby' => false ) ); if (isset($_GET['delete'.$this->table])) { self::$current_category = new GuideCategory( (int)(Tools::getValue('id_guide_category_parent', 0)) ); } else { self::$current_category = new GuideCategory( (int)( Tools::getValue('id_guide_category', Tools::getValue('id_guide_category_parent', 0)) ) ); } $this->_select = 'position '; $this->_filter = 'AND a.`id_parent` = '.(int)(self::$current_category->id); $this->_orderBy = 'position'; $this->_orderWay = 'DESC'; $this->identifiersDnd['id_guide_category'] = 'id_guide_category'; parent::__construct(); } public static function getCurrentCategory() { return self::$current_category; } public function displayForm($token = NULL) { global $currentIndex, $cookie, $smarty; parent::displayForm(); if (!($obj = $this->loadObject(true))) return; $langs = Language::getLanguages(false); $form = new FormBuilder('AdminGuideCategories', $this->table, 'id_guide_category', 'Add'); $this->fieldsForm = array( 'id_parent' => array( 'title' => $this->l('Catégorie parent'), 'type' => 'select', 'options_raw' => GuideCategory::findCategoriesTree($cookie->id_lang, $obj->id), 'options_map' => ['id_guide_category', 'name', 'level'], 'empty_option' => ['value'=>'0', 'label'=>$this->l('No parent category')], 'initial_value' => $obj->id_parent?$obj->id_parent:self::$current_category->id ), 'id_category_family' => array( 'title' => $this->l('Famille'), 'type' => 'select', 'options_raw' => GuideCategory::findFamiliesTree($cookie->id_lang), 'options_map' => ['id_category_family', 'name', 'level'], 'initial_value' => $obj->id_category_family ), 'name' => array( 'title' => $this->l('Title'), 'type' => 'text', 'required' => true, 'translatable' => true, 'attrs' => [ 'onkeyup' => 'copy2friendlyURL();' ] ), 'description' => array( 'title' => $this->l('Description'), 'type' => 'textarea', 'translatable' => true, ), 'image' => array( 'title' => $this->l('Image'), 'type' => 'file', 'file_attrs' => [ 'max_bytes' => $this->maxImageBytes, 'allowed_format' => 'JPEG', 'html_file' => "img", 'path' => $obj->getImageFileUrl().'?rand='.time() ] ), 'active' => array( 'title' => $this->l('Displayed'), 'type' => 'yesno', 'required' => true, ), 'link_rewrite' => array( 'title' => $this->l('Simplified URL'), 'type' => 'text', 'required' => true, ), 'meta_title' => array( 'title' => $this->l('Title (META)'), 'type' => 'text', 'translatable' => true, ), 'meta_description' => array( 'title' => $this->l('Description (META)'), 'type' => 'textarea', 'translatable' => true, ), ); $form->setTitle($this->l('Purchase guide category')); $form->setFields($this->fieldsForm); $form->enableSubmitAndBackToParent(); $form->setSubmitButton($this->l('Save')); $form->display($smarty, $langs, $obj); $currentIndex .= '&id_guide_category='.($obj->id?$obj->id_parent:self::$current_category->id); } public function displayTop() { global $currentIndex, $cookie; $clean_currentIndex = $currentIndex; $parent_category = 0; if (Validate::isLoadedObject(self::$current_category)) { $parent_category = new GuideCategory(self::$current_category->id_parent); if (Validate::isLoadedObject($parent_category)) { echo ' '.$this->l('back to').' '.$parent_category->name[$cookie->id_lang].'

'; } else { echo ' '.$this->l('back').'

'; } echo $this->l('Current category').' : '.self::$current_category->name[$cookie->id_lang].'

'; } } public function displayList() { global $currentIndex; $this->displayTop(); if ($this->edit AND (!isset($this->noAdd) OR !$this->noAdd)) echo '
'.$this->l('Add new').'

'; // Append when we get a syntax error in SQL query if ($this->_list === false) { $this->displayWarning($this->l('Bad SQL query')); return false; } // Display list header (filtering, pagination and column names) $this->displayListHeader(); if (!sizeof($this->_list)) echo ''.$this->l('No items found').''; // Show the content of the table $this->displayListContent(); // Close list table and submit button $this->displayListFooter(); } public function viewguide_category() { global $cookie; $this->getList((int)($cookie->id_lang)); $this->displayList(); $this->displayOptionsList(); $this->displayRequiredFields(); $this->includeSubTab('display'); } protected function _displayEditLink($token = NULL, $id) { global $currentIndex; $_cacheLang['Edit'] = $this->l('Edit'); echo ' '; } protected function _displayDeleteLink($token = NULL, $id) { global $currentIndex; $_cacheLang['Delete'] = $this->l('Delete'); $_cacheLang['DeleteItem'] = $this->l('Delete item #', __CLASS__, TRUE, FALSE); echo ' '.$_cacheLang['Delete'].''; } public function postProcess() { if (isset($_GET['delete'.$this->table]) || Tools::getValue('submitDel'.$this->table)) { $this->postProcessDelete(); return; } parent::postProcess(); } protected function afterUpdate() { return $this->afterAdd(); } protected function afterAdd() { if (isset($_FILES['image']['tmp_name']) && $_FILES['image']['tmp_name'] != NULL) { $object = $this->loadObject(); $error = checkImage( $_FILES['image'], $this->maxImageSize); if ($error) { $this->_errors[] = $error; return false; } $tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS'); if (!$tmpName OR !move_uploaded_file($_FILES['image']['tmp_name'], $tmpName)) { $this->_errors[] = Tools::displayError('An error occurred during the image upload'); return false; } $new_path = $object->getImageFilePath(); if (file_exists($new_path)) { @unlink($new_path); } if (!imageResize($tmpName, $new_path, self::IMG_WIDTH, self::IMG_HEIGHT)) { $this->_errors[] = Tools::displayError('An error occurred while copying image.'); return false; } @unlink($tmpName); } return true; } private function postProcessDelete() { global $currentIndex; if ($this->tabAccess['delete'] === '1') { if (isset($_GET['delete'.$this->table])) { $object = $this->loadObject(); if (Validate::isLoadedObject($object)) { $result = $object->delete(); if (!$result) { if ($object->getDeleteError()==1) { $this->_errors[] = Tools::displayError($this->l('The category has at least one sub category')); } else if ($object->getDeleteError()==2) { $this->_errors[] = Tools::displayError($this->l('The category contains at least one page')); } else { $this->_errors[] = Tools::displayError('An error occurred during deletion.'); } } else { Tools::redirectAdmin($currentIndex.'&id_guide_category_parent='.self::$current_category->id.'&conf=1&token='.$this->token); } } else { $this->_errors[] = Tools::displayError('An error occurred while deleting object.').' '.$this->table.' '.Tools::displayError('(cannot load object)'); } return; } elseif (Tools::getValue('submitDel'.$this->table)) { $object = new GuideCategory(); $result = $object->deleteSelection(Tools::getValue($this->table.'Box')); if (!$result) { if ($object->getDeleteError()==1) { $this->_errors[] = Tools::displayError($this->l('One of the selected categories has at least one sub category')); } elseif ($object->getDeleteError()==2) { $this->_errors[] = Tools::displayError($this->l('One of the selected categories contains at least one page')); } else { $this->_errors[] = Tools::displayError('An error occurred while deleting selection.'); } } else { Tools::redirectAdmin($currentIndex.'&id_guide_category_parent='.self::$current_category->id.'&conf=1&token='.$this->token); } return; } } } }