Skip to content

Commit 060a79f

Browse files
authored
Merge pull request #96 from matks/use-frontcontroller
Use AdminController
2 parents 8f62eda + c01ac75 commit 060a79f

File tree

7 files changed

+153
-68
lines changed

7 files changed

+153
-68
lines changed

.github/workflows/php.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
runs-on: ubuntu-latest
5959
strategy:
6060
matrix:
61-
presta-versions: ['1.7.4.4', '1.7.5.1', '1.7.6', '1.7.7', '1.7.8', 'latest']
61+
presta-versions: ['1.7.5.1', '1.7.6', '1.7.7', '1.7.8', 'latest']
6262
steps:
6363
- name: Setup PHP
6464
uses: shivammathur/setup-php@v2

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Add sliding images to your homepage to welcome your visitors in a visual and fri
66

77
## Compatibility
88

9-
PrestaShop: `1.7.4.0` or later
9+
PrestaShop: `1.7.5.0` or later
1010

1111
## Multistore compatibility
1212

ajax_ps_imageslider.php

-42
This file was deleted.

config.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<module>
33
<name>ps_imageslider</name>
44
<displayName><![CDATA[Image slider]]></displayName>
5-
<version><![CDATA[3.1.4]]></version>
5+
<version><![CDATA[3.2.0]]></version>
66
<description><![CDATA[Adds an image slider to your site.]]></description>
77
<author><![CDATA[PrestaShop]]></author>
88
<tab><![CDATA[front_office_features]]></tab>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <[email protected]>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
class AdminConfigureSlidesController extends ModuleAdminController
21+
{
22+
public function ajaxProcessUpdateSlidesPosition()
23+
{
24+
if (empty(Tools::getValue('action')) || Tools::getValue('action') != 'updateSlidesPosition' || empty(Tools::getValue('slides'))) {
25+
ob_end_clean();
26+
header('Content-Type: application/json');
27+
$this->ajaxRender(json_encode(['error' => true]));
28+
exit;
29+
}
30+
31+
// Get slides and update their position
32+
$slides = Tools::getValue('slides');
33+
foreach ($slides as $position => $id_slide) {
34+
Db::getInstance()->execute('
35+
UPDATE `' . _DB_PREFIX_ . 'homeslider_slides` SET `position` = ' . (int) $position . '
36+
WHERE `id_homeslider_slides` = ' . (int) $id_slide
37+
);
38+
}
39+
40+
// Wipe module cache
41+
$this->module->clearCache();
42+
43+
ob_end_clean();
44+
header('Content-Type: application/json');
45+
$this->ajaxRender(json_encode(['success' => true]));
46+
exit;
47+
}
48+
}

ps_imageslider.php

+75-23
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct()
5252
{
5353
$this->name = 'ps_imageslider';
5454
$this->tab = 'front_office_features';
55-
$this->version = '3.1.4';
55+
$this->version = '3.2.0';
5656
$this->author = 'PrestaShop';
5757
$this->need_instance = 0;
5858
$this->secure_key = Tools::hash($this->name);
@@ -62,7 +62,7 @@ public function __construct()
6262

6363
$this->displayName = $this->trans('Image slider', [], 'Modules.Imageslider.Admin');
6464
$this->description = $this->trans('Add sliding images to your homepage to welcome your visitors in a visual and friendly way.', [], 'Modules.Imageslider.Admin');
65-
$this->ps_versions_compliancy = ['min' => '1.7.4.0', 'max' => _PS_VERSION_];
65+
$this->ps_versions_compliancy = ['min' => '1.7.5.0', 'max' => _PS_VERSION_];
6666

6767
$this->templateFile = 'module:ps_imageslider/views/templates/hook/slider.tpl';
6868
}
@@ -75,6 +75,7 @@ public function install()
7575
/* Adds Module */
7676
if (
7777
parent::install() &&
78+
$this->installTab() &&
7879
$this->registerHook('displayHeader') &&
7980
$this->registerHook('displayHome') &&
8081
$this->registerHook('actionShopDataDuplication')
@@ -125,6 +126,21 @@ public function install()
125126
return false;
126127
}
127128

129+
public function installTab()
130+
{
131+
$tab = new Tab();
132+
$tab->class_name = 'AdminConfigureSlides';
133+
$tab->module = $this->name;
134+
$tab->active = true;
135+
$tab->id_parent = -1;
136+
$tab->name = array_fill_keys(
137+
Language::getIDs(false),
138+
$this->displayName
139+
);
140+
141+
return $tab->add();
142+
}
143+
128144
/**
129145
* Adds samples
130146
*/
@@ -158,6 +174,9 @@ public function uninstall()
158174
/* Deletes tables */
159175
$res = $this->deleteTables();
160176

177+
/* Delete hidden tab */
178+
$res &= $this->uninstallTab();
179+
161180
/* Unsets configuration */
162181
$res &= Configuration::deleteByName('HOMESLIDER_SPEED');
163182
$res &= Configuration::deleteByName('HOMESLIDER_PAUSE_ON_HOVER');
@@ -169,6 +188,18 @@ public function uninstall()
169188
return false;
170189
}
171190

191+
public function uninstallTab()
192+
{
193+
$result = true;
194+
$id_tab = (int) Tab::getIdFromClassName('AdminConfigureSlides');
195+
$tab = new Tab($id_tab);
196+
if (Validate::isLoadedObject($tab)) {
197+
$result = $tab->delete();
198+
}
199+
200+
return $result;
201+
}
202+
172203
/**
173204
* Creates tables
174205
*/
@@ -613,31 +644,52 @@ public function hookActionShopDataDuplication($params)
613644

614645
public function headerHTML()
615646
{
616-
if ('AdminModules' !== Tools::getValue('controller') ||
617-
Tools::getValue('configure') !== $this->name ||
618-
Tools::getIsset('id_slide') ||
619-
Tools::getIsset('addSlide')) {
647+
// Run only on module configuration page
648+
if (Tools::getValue('controller') != 'AdminModules' || Tools::getValue('configure') !== $this->name) {
620649
return;
621650
}
622651

623-
$this->context->controller->addJS($this->_path . 'js/Sortable.min.js');
624-
/* Style & js for fieldset 'slides configuration' */
625-
$html = '<script type="text/javascript">
626-
$(function() {
627-
var $mySlides = $("#slides");
628-
new Sortable($mySlides[0], {
629-
animation: 150,
630-
onUpdate: function(event) {
631-
var order = this.toArray().join("&") + "&action=updateSlidesPosition";
632-
$.post("' . $this->context->shop->physical_uri . $this->context->shop->virtual_uri . 'modules/' . $this->name . '/ajax_' . $this->name . '.php?secure_key=' . $this->secure_key . '", order);
633-
}
634-
});
635-
$mySlides.hover(function() {
636-
$(this).css("cursor","move");
637-
},
638-
function() {
639-
$(this).css("cursor","auto");
652+
// Add sortable library
653+
$this->context->controller->addJS($this->_path . 'js/Sortable.min.js?v=' . $this->version);
654+
655+
// Add sorting scripts
656+
$html = '
657+
<script type="text/javascript">
658+
$(function () {
659+
var slideList = $("#slides");
660+
661+
// Check if the list exists, so we dont run it on edit page
662+
if (!slideList.length) {
663+
return;
664+
}
665+
666+
new Sortable(slideList[0], {
667+
animation: 150,
668+
onUpdate: function (event) {
669+
var sortableIdsAsTableString = this.toArray();
670+
var sortableIdsAsData = sortableIdsAsTableString.map((x) => x.slice(-1));
671+
var ajaxCallParameters = {
672+
ajax: true,
673+
action: "updateSlidesPosition",
674+
slides: sortableIdsAsData
675+
};
676+
$.ajax({
677+
type: "POST",
678+
cache: false,
679+
url: "' . $this->context->link->getAdminLink('AdminConfigureSlides') . '",
680+
data: ajaxCallParameters
681+
});
682+
}
640683
});
684+
685+
slideList.hover(
686+
function () {
687+
$(this).css("cursor", "move");
688+
},
689+
function () {
690+
$(this).css("cursor", "auto");
691+
}
692+
);
641693
});
642694
</script>';
643695

upgrade/upgrade-3.2.0.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8+
* that is bundled with this package in the file LICENSE.md.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/AFL-3.0
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to [email protected] so we can send you a copy immediately.
14+
*
15+
* @author PrestaShop SA <[email protected]>
16+
* @copyright Since 2007 PrestaShop SA and Contributors
17+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
18+
* International Registered Trademark & Property of PrestaShop SA
19+
*/
20+
if (!defined('_PS_VERSION_')) {
21+
exit;
22+
}
23+
24+
function upgrade_module_3_2_0($object)
25+
{
26+
return $object->installTab();
27+
}

0 commit comments

Comments
 (0)