Skip to content

Commit

Permalink
Merge pull request #96 from matks/use-frontcontroller
Browse files Browse the repository at this point in the history
Use AdminController
  • Loading branch information
nicosomb authored Sep 12, 2024
2 parents 8f62eda + c01ac75 commit 060a79f
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
presta-versions: ['1.7.4.4', '1.7.5.1', '1.7.6', '1.7.7', '1.7.8', 'latest']
presta-versions: ['1.7.5.1', '1.7.6', '1.7.7', '1.7.8', 'latest']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Add sliding images to your homepage to welcome your visitors in a visual and fri

## Compatibility

PrestaShop: `1.7.4.0` or later
PrestaShop: `1.7.5.0` or later

## Multistore compatibility

Expand Down
42 changes: 0 additions & 42 deletions ajax_ps_imageslider.php

This file was deleted.

2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_imageslider</name>
<displayName><![CDATA[Image slider]]></displayName>
<version><![CDATA[3.1.4]]></version>
<version><![CDATA[3.2.0]]></version>
<description><![CDATA[Adds an image slider to your site.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab>
Expand Down
48 changes: 48 additions & 0 deletions controllers/admin/AdminConfigureSlidesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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 function ajaxProcessUpdateSlidesPosition()
{
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;
}

// 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();

ob_end_clean();
header('Content-Type: application/json');
$this->ajaxRender(json_encode(['success' => true]));
exit;
}
}
98 changes: 75 additions & 23 deletions ps_imageslider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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);
Expand All @@ -62,7 +62,7 @@ public function __construct()

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

$this->templateFile = 'module:ps_imageslider/views/templates/hook/slider.tpl';
}
Expand All @@ -75,6 +75,7 @@ public function install()
/* Adds Module */
if (
parent::install() &&
$this->installTab() &&
$this->registerHook('displayHeader') &&
$this->registerHook('displayHome') &&
$this->registerHook('actionShopDataDuplication')
Expand Down Expand Up @@ -125,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
*/
Expand Down Expand Up @@ -158,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');
Expand All @@ -169,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
*/
Expand Down Expand Up @@ -613,31 +644,52 @@ public function hookActionShopDataDuplication($params)

public function headerHTML()
{
if ('AdminModules' !== Tools::getValue('controller') ||
Tools::getValue('configure') !== $this->name ||
Tools::getIsset('id_slide') ||
Tools::getIsset('addSlide')) {
// Run only on module configuration page
if (Tools::getValue('controller') != 'AdminModules' || Tools::getValue('configure') !== $this->name) {
return;
}

$this->context->controller->addJS($this->_path . 'js/Sortable.min.js');
/* Style & js for fieldset 'slides configuration' */
$html = '<script type="text/javascript">
$(function() {
var $mySlides = $("#slides");
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);
}
});
$mySlides.hover(function() {
$(this).css("cursor","move");
},
function() {
$(this).css("cursor","auto");
// Add sortable library
$this->context->controller->addJS($this->_path . 'js/Sortable.min.js?v=' . $this->version);

// Add sorting scripts
$html = '
<script type="text/javascript">
$(function () {
var slideList = $("#slides");
// Check if the list exists, so we dont run it on edit page
if (!slideList.length) {
return;
}
new Sortable(slideList[0], {
animation: 150,
onUpdate: function (event) {
var sortableIdsAsTableString = this.toArray();
var sortableIdsAsData = sortableIdsAsTableString.map((x) => x.slice(-1));
var ajaxCallParameters = {
ajax: true,
action: "updateSlidesPosition",
slides: sortableIdsAsData
};
$.ajax({
type: "POST",
cache: false,
url: "' . $this->context->link->getAdminLink('AdminConfigureSlides') . '",
data: ajaxCallParameters
});
}
});
slideList.hover(
function () {
$(this).css("cursor", "move");
},
function () {
$(this).css("cursor", "auto");
}
);
});
</script>';

Expand Down
27 changes: 27 additions & 0 deletions upgrade/upgrade-3.2.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_3_2_0($object)
{
return $object->installTab();
}

0 comments on commit 060a79f

Please sign in to comment.