From 780534355d842c5f69f5f0753cc2705bd1a1cfb9 Mon Sep 17 00:00:00 2001 From: matks Date: Tue, 28 Nov 2023 21:51:24 +0100 Subject: [PATCH 1/4] Use admin controller for updating slides with ajax --- ajax_ps_imageslider.php | 42 ---------------- .../admin/AdminConfigureSlidesController.php | 50 +++++++++++++++++++ controllers/admin/index.php | 28 +++++++++++ controllers/index.php | 28 +++++++++++ ps_imageslider.php | 21 +++++++- 5 files changed, 125 insertions(+), 44 deletions(-) delete mode 100644 ajax_ps_imageslider.php create mode 100644 controllers/admin/AdminConfigureSlidesController.php create mode 100644 controllers/admin/index.php create mode 100644 controllers/index.php diff --git a/ajax_ps_imageslider.php b/ajax_ps_imageslider.php deleted file mode 100644 index deb8871..0000000 --- a/ajax_ps_imageslider.php +++ /dev/null @@ -1,42 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ -include_once '../../config/config.inc.php'; -include_once '../../init.php'; -include_once 'ps_imageslider.php'; - -$home_slider = new Ps_ImageSlider(); -$slides = []; - -if (!Tools::isSubmit('secure_key') || Tools::getValue('secure_key') != $home_slider->secure_key || !Tools::getValue('action')) { - exit(1); -} - -if (Tools::getValue('action') == 'updateSlidesPosition' && Tools::getValue('slides')) { - $slides = Tools::getValue('slides'); - - foreach ($slides as $position => $id_slide) { - $res = Db::getInstance()->execute(' - UPDATE `' . _DB_PREFIX_ . 'homeslider_slides` SET `position` = ' . (int) $position . ' - WHERE `id_homeslider_slides` = ' . (int) $id_slide - ); - } - - $home_slider->clearCache(); -} diff --git a/controllers/admin/AdminConfigureSlidesController.php b/controllers/admin/AdminConfigureSlidesController.php new file mode 100644 index 0000000..13a9954 --- /dev/null +++ b/controllers/admin/AdminConfigureSlidesController.php @@ -0,0 +1,50 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ +class AdminConfigureSlidesController extends ModuleAdminController +{ + public $module; + + /** + * This function allow to delete users + */ + public function ajaxProcessUpdateSlidesPosition() + { + $home_slider = $this->module; + $slides = []; + + if (!Tools::isSubmit('secure_key') || Tools::getValue('secure_key') != $home_slider->secure_key || !Tools::getValue('action')) { + $this->ajaxDie(json_encode(['error' => true])); + } + + if (Tools::getValue('action') == 'updateSlidesPosition' && Tools::getValue('slides')) { + $slides = Tools::getValue('slides'); + + foreach ($slides as $position => $id_slide) { + $res = Db::getInstance()->execute(' + UPDATE `' . _DB_PREFIX_ . 'homeslider_slides` SET `position` = ' . (int) $position . ' + WHERE `id_homeslider_slides` = ' . (int) $id_slide + ); + } + + $home_slider->clearCache(); + } + $this->ajaxDie(json_encode(['success' => true])); + } +} diff --git a/controllers/admin/index.php b/controllers/admin/index.php new file mode 100644 index 0000000..36c7d1f --- /dev/null +++ b/controllers/admin/index.php @@ -0,0 +1,28 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/controllers/index.php b/controllers/index.php new file mode 100644 index 0000000..36c7d1f --- /dev/null +++ b/controllers/index.php @@ -0,0 +1,28 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/ps_imageslider.php b/ps_imageslider.php index 2a8c29f..27cf27c 100644 --- a/ps_imageslider.php +++ b/ps_imageslider.php @@ -48,6 +48,10 @@ class Ps_ImageSlider extends Module implements WidgetInterface */ public $secure_key; + public $adminControllers = [ + 'adminConfigureSlides' => 'AdminConfigureSlides', + ]; + public function __construct() { $this->name = 'ps_imageslider'; @@ -628,8 +632,21 @@ public function headerHTML() new Sortable($mySlides[0], { animation: 150, onUpdate: function(event) { - var order = this.toArray().join("&") + "&action=updateSlidesPosition"; - $.post("' . $this->context->shop->physical_uri . $this->context->shop->virtual_uri . 'modules/' . $this->name . '/ajax_' . $this->name . '.php?secure_key=' . $this->secure_key . '", order); + var sortableIdsAsTableString = this.toArray(); + var sortableIdsAsData = sortableIdsAsTableString.map((x) => x.slice(-1)); + var ajaxCallParameters = { + ajax: true, + action: "updateSlidesPosition", + secure_key: "'.$this->secure_key.'", + token: "'.Tools::getAdminTokenLite('AdminConfigureSlides') .'", + slides: sortableIdsAsData + }; + $.ajax({ + type: "POST", + cache: false, + url: "'.$this->context->link->getAdminLink('AdminConfigureSlides', false).'", + data: ajaxCallParameters + }); } }); $mySlides.hover(function() { From 9c1d1e107ff4b6a828fac1bf325f46d6f5f57dbb Mon Sep 17 00:00:00 2001 From: Hlavtox Date: Wed, 11 Sep 2024 16:05:44 +0200 Subject: [PATCH 2/4] Register controller --- config.xml | 2 +- .../admin/AdminConfigureSlidesController.php | 40 ++++++++--------- controllers/index.php | 28 ------------ ps_imageslider.php | 43 +++++++++++++++---- .../index.php => upgrade/upgrade-3.2.0.php | 17 ++++---- 5 files changed, 63 insertions(+), 67 deletions(-) delete mode 100644 controllers/index.php rename controllers/admin/index.php => upgrade/upgrade-3.2.0.php (66%) diff --git a/config.xml b/config.xml index 49fe122..cf023f0 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ ps_imageslider - + diff --git a/controllers/admin/AdminConfigureSlidesController.php b/controllers/admin/AdminConfigureSlidesController.php index 13a9954..5abef0f 100644 --- a/controllers/admin/AdminConfigureSlidesController.php +++ b/controllers/admin/AdminConfigureSlidesController.php @@ -19,32 +19,30 @@ */ class AdminConfigureSlidesController extends ModuleAdminController { - public $module; - - /** - * This function allow to delete users - */ public function ajaxProcessUpdateSlidesPosition() { - $home_slider = $this->module; - $slides = []; - - if (!Tools::isSubmit('secure_key') || Tools::getValue('secure_key') != $home_slider->secure_key || !Tools::getValue('action')) { - $this->ajaxDie(json_encode(['error' => true])); - } + if (empty(Tools::getValue('action')) || Tools::getValue('action') != 'updateSlidesPosition' || empty(Tools::getValue('slides'))) { + ob_end_clean(); + header('Content-Type: application/json'); + $this->ajaxRender(json_encode(['error' => true])); + exit; + } - if (Tools::getValue('action') == 'updateSlidesPosition' && Tools::getValue('slides')) { - $slides = Tools::getValue('slides'); - - foreach ($slides as $position => $id_slide) { - $res = Db::getInstance()->execute(' + // Get slides and update their position + $slides = Tools::getValue('slides'); + foreach ($slides as $position => $id_slide) { + Db::getInstance()->execute(' UPDATE `' . _DB_PREFIX_ . 'homeslider_slides` SET `position` = ' . (int) $position . ' WHERE `id_homeslider_slides` = ' . (int) $id_slide - ); - } + ); + } + + // Wipe module cache + $this->module->clearCache(); - $home_slider->clearCache(); - } - $this->ajaxDie(json_encode(['success' => true])); + ob_end_clean(); + header('Content-Type: application/json'); + $this->ajaxRender(json_encode(['success' => true])); + exit; } } diff --git a/controllers/index.php b/controllers/index.php deleted file mode 100644 index 36c7d1f..0000000 --- a/controllers/index.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) - */ -header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); -header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); - -header('Cache-Control: no-store, no-cache, must-revalidate'); -header('Cache-Control: post-check=0, pre-check=0', false); -header('Pragma: no-cache'); - -header('Location: ../'); -exit; diff --git a/ps_imageslider.php b/ps_imageslider.php index 27cf27c..5d4e5e7 100644 --- a/ps_imageslider.php +++ b/ps_imageslider.php @@ -48,15 +48,11 @@ class Ps_ImageSlider extends Module implements WidgetInterface */ public $secure_key; - public $adminControllers = [ - 'adminConfigureSlides' => 'AdminConfigureSlides', - ]; - public function __construct() { $this->name = 'ps_imageslider'; $this->tab = 'front_office_features'; - $this->version = '3.1.4'; + $this->version = '3.2.0'; $this->author = 'PrestaShop'; $this->need_instance = 0; $this->secure_key = Tools::hash($this->name); @@ -79,6 +75,7 @@ public function install() /* Adds Module */ if ( parent::install() && + $this->installTab() && $this->registerHook('displayHeader') && $this->registerHook('displayHome') && $this->registerHook('actionShopDataDuplication') @@ -129,6 +126,21 @@ public function install() return false; } + public function installTab() + { + $tab = new Tab(); + $tab->class_name = 'AdminConfigureSlides'; + $tab->module = $this->name; + $tab->active = true; + $tab->id_parent = -1; + $tab->name = array_fill_keys( + Language::getIDs(false), + $this->displayName + ); + + return $tab->add(); + } + /** * Adds samples */ @@ -162,6 +174,9 @@ public function uninstall() /* Deletes tables */ $res = $this->deleteTables(); + /* Delete hidden tab */ + $res &= $this->uninstallTab(); + /* Unsets configuration */ $res &= Configuration::deleteByName('HOMESLIDER_SPEED'); $res &= Configuration::deleteByName('HOMESLIDER_PAUSE_ON_HOVER'); @@ -173,6 +188,18 @@ public function uninstall() return false; } + public function uninstallTab() + { + $result = true; + $id_tab = (int) Tab::getIdFromClassName('AdminConfigureSlides'); + $tab = new Tab($id_tab); + if (Validate::isLoadedObject($tab)) { + $result = $tab->delete(); + } + + return $result; + } + /** * Creates tables */ @@ -617,6 +644,7 @@ public function hookActionShopDataDuplication($params) public function headerHTML() { + // Run only on module configuration page if ('AdminModules' !== Tools::getValue('controller') || Tools::getValue('configure') !== $this->name || Tools::getIsset('id_slide') || @@ -625,6 +653,7 @@ public function headerHTML() } $this->context->controller->addJS($this->_path . 'js/Sortable.min.js'); + /* Style & js for fieldset 'slides configuration' */ $html = '';