From 1c5205a2a8c2d0a05a1ce96fc777192ac878c0e6 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Fri, 17 Nov 2017 11:42:47 +0100 Subject: [PATCH] Admin --- .../purchaseguide/AdminGuideCategories.php | 221 +++++++++--------- modules/purchaseguide/AdminGuidePosts.php | 28 ++- .../purchaseguide/classes/GuideCategory.php | 82 +++---- .../controllers/front/PostController.php | 6 +- 4 files changed, 180 insertions(+), 157 deletions(-) diff --git a/modules/purchaseguide/AdminGuideCategories.php b/modules/purchaseguide/AdminGuideCategories.php index 8c33d6f5..63da6b4a 100644 --- a/modules/purchaseguide/AdminGuideCategories.php +++ b/modules/purchaseguide/AdminGuideCategories.php @@ -70,6 +70,116 @@ class AdminGuideCategories extends AdminTab parent::__construct(); } + public function postProcess() + { + if (isset($_GET['delete'.$this->table]) || Tools::getValue('submitDel'.$this->table)) { + $this->postProcessDelete(); + return; + } + + if (self::$current_category->id != 0) { + $this->noLink = true; + } + + 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; + } + } + } + protected function _displayEditLink($token = NULL, $id) { global $currentIndex; @@ -275,115 +385,4 @@ class AdminGuideCategories extends AdminTab $this->displayRequiredFields(); $this->includeSubTab('display'); } - - public function postProcess() - { - if (isset($_GET['delete'.$this->table]) || - Tools::getValue('submitDel'.$this->table)) { - $this->postProcessDelete(); - return; - } - - if (self::$current_category->id != 0) { - $this->noLink = true; - } - - 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; - } - } - } } diff --git a/modules/purchaseguide/AdminGuidePosts.php b/modules/purchaseguide/AdminGuidePosts.php index f2a355c9..18206fcf 100644 --- a/modules/purchaseguide/AdminGuidePosts.php +++ b/modules/purchaseguide/AdminGuidePosts.php @@ -3,7 +3,6 @@ require_once(__DIR__.'/classes/GuidePost.php'); require_once(__DIR__.'/classes/GuideCategory.php'); require_once(__DIR__.'/classes/FormBuilder.php'); - class AdminGuidePosts extends AdminTab { public function __construct() @@ -85,6 +84,27 @@ class AdminGuidePosts extends AdminTab parent::postProcess(); } + public function displayTop() + { + global $cookie; + + $id_guide_category = Tools::getValue('id_guide_category', 0); + if ($id_guide_category == 0) { + + } else { + $category = new GuideCategory($id_guide_category); + + $currentIndex = '?tab=AdminGuideCategories'; + $token = Tools::getAdminTokenLite('AdminGuideCategories'); + + echo ' '.$this->l('back to').' '. + $category->name[$cookie->id_lang].'

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

'; + } + } + public function displayList() { $this->_orderBy = 'position'; @@ -96,8 +116,9 @@ class AdminGuidePosts extends AdminTab global $currentIndex, $cookie, $smarty; parent::displayForm(); - if (!($obj = $this->loadObject(true))) - return; + if (!($obj = $this->loadObject(true))) { + return; + } $langs = Language::getLanguages(false); @@ -145,6 +166,5 @@ class AdminGuidePosts extends AdminTab $form->setFields($this->fieldsForm); $form->setSubmitButton($this->l('Save')); $form->display($smarty, $langs, $obj); - } } diff --git a/modules/purchaseguide/classes/GuideCategory.php b/modules/purchaseguide/classes/GuideCategory.php index 389a9be6..05dfef3f 100644 --- a/modules/purchaseguide/classes/GuideCategory.php +++ b/modules/purchaseguide/classes/GuideCategory.php @@ -180,8 +180,10 @@ class GuideCategory extends ObjectModel public function update($autodate = true, $nullValues = false) { - $this->position = self::cleanupPositions((int)$this->id_parent); - return parent::update($autodate, $nullValues); + $this->position = self::getLastPosition((int)$this->id_parent); + $ret = parent::update($autodate, $nullValues); + $this->cleanupPositions((int)$this->id_parent); + return $ret; } public function delete() @@ -191,10 +193,9 @@ class GuideCategory extends ObjectModel return FALSE; } - if ($this->getNbPosts($this->id)) { $this->error = 2; - return FALSE; + return false; } if (!parent::delete()) { @@ -211,11 +212,11 @@ class GuideCategory extends ObjectModel public function deleteSelection($selection) { - if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table)) - die(Tools::displayError()); + if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table)) { + die(Tools::displayError()); + } $result = true; - foreach ($selection AS $id) - { + foreach ($selection AS $id) { $this->id = (int)($id); $result2 = $this->delete(); $result = $result && $this->delete(); @@ -235,15 +236,20 @@ class GuideCategory extends ObjectModel FROM `'._DB_PREFIX_.'guide_category` cp WHERE cp.`id_parent` = '.(int)$this->id_parent.' ORDER BY cp.`position` ASC' - )) - return false; + )) { + return false; + } - foreach ($res AS $category) - if ((int)($category['id_guide_category']) == (int)($this->id)) - $movedCategory = $category; + foreach ($res AS $category) { + if ((int)($category['id_guide_category']) == (int)($this->id)) { + $movedCategory = $category; + } + } + + if (!isset($movedCategory) || !isset($position)) { + return false; + } - if (!isset($movedCategory) || !isset($position)) - return false; // < and > statements rather than BETWEEN operator // since BETWEEN is treated differently according to databases return (Db::getInstance()->Execute(' @@ -260,14 +266,33 @@ class GuideCategory extends ObjectModel WHERE `id_parent` = '.(int)($movedCategory['id_parent']).' AND `id_guide_category`='.(int)($movedCategory['id_guide_category']))); } - + + private function cleanupPositions($id_parent) + { + $result = Db::getInstance()->ExecuteS(' + SELECT `id_guide_category` + FROM `'._DB_PREFIX_.'guide_category` + WHERE `id_parent` = '.(int)($id_parent).' + ORDER BY `position`'); + $position = 0; + foreach ($result as $row) { + $position++; + $sql = ' + UPDATE `'._DB_PREFIX_.'guide_category` + SET `position` = '.(int)($position).' + WHERE `id_parent` = '.(int)($id_parent).' + AND `id_guide_category` = '.(int)($row['id_guide_category']); + Db::getInstance()->Execute($sql); + } + return true; + } public static function getLastPosition($id_parent) { - return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'guide_category` WHERE `id_parent` = '.(int)($id_parent))); + return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_. + 'guide_category` WHERE `id_parent` = '.(int)($id_parent))); } - private function getNbSubCategories($id_guide_category) { $sql = 'SELECT COUNT(*) @@ -276,7 +301,6 @@ class GuideCategory extends ObjectModel return Db::getInstance()->getValue($sql); } - private function getNbPosts($id_guide_category) { $sql = 'SELECT COUNT(*) @@ -284,25 +308,6 @@ class GuideCategory extends ObjectModel WHERE `id_guide_category` = '.intval($id_guide_category); return Db::getInstance()->getValue($sql); } - - private function cleanupPositions($id_parent) - { - $sql = 'SELECT `id_guide_category` - FROM `'._DB_PREFIX_.'guide_category` - WHERE `id_parent` = '.intval($id_parent).' - ORDER BY `position` ASC'; - $rows = Db::getInstance()->ExecuteS($sql); - - $position = 1; - foreach($rows as $row) { - $sql = 'UPDATE `'._DB_PREFIX_.'guide_category` - SET `position` = '.$position.' - WHERE `id_guide_category` = '.$row['id_guide_category']; - Db::getInstance()->Execute($sql); - $position++; - } - return $position; - } public function findByCategoryFamily($id, $id_lang) { @@ -311,7 +316,6 @@ class GuideCategory extends ObjectModel " AND gc.id_category_family=".$id; $row = Db::getInstance()->getRow($sql); - return $row; } diff --git a/modules/purchaseguide/controllers/front/PostController.php b/modules/purchaseguide/controllers/front/PostController.php index 6a4ea6cf..bfdd3d3e 100644 --- a/modules/purchaseguide/controllers/front/PostController.php +++ b/modules/purchaseguide/controllers/front/PostController.php @@ -55,7 +55,7 @@ class PostController extends FrontController $sql = "SELECT * FROM ps_guide_category gc, ps_guide_category_lang gcl WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category AND gc.id_parent=".$id_category." AND gcl.id_lang=".$id_lang. - " ORDER BY position DESC"; + " ORDER BY position ASC"; $category = Db::getInstance()->getRow($sql); } } @@ -65,8 +65,8 @@ class PostController extends FrontController if ($id_post != 0) { $sql = "SELECT * FROM ps_guide_post gp, ps_guide_post_lang gpl WHERE gp.active=1 AND gp.id_guide_post=gpl.id_guide_post - AND gp.id_guide_category=".$category['id_guide_category']." AND gp.id_guide_post=".$id_post. - " AND gpl.id_lang=".$id_lang; + AND gp.id_guide_category=".$category['id_guide_category']. + " AND gp.id_guide_post=".$id_post." AND gpl.id_lang=".$id_lang; $content = Db::getInstance()->getRow($sql); } // Auto Select Post