Skip to content

Commit

Permalink
Save images inside /img/homeslider directory
Browse files Browse the repository at this point in the history
Module directory is not the right place to store slide image files.
That is because tbupdater module  always wipes out module directory
during module update. This causes data loss.

This new implementation uploads images into /img/homeslider/ directory
instead.

Closes #3
Related to thirtybees/thirtybees#922
  • Loading branch information
getdatakick committed Dec 12, 2022
1 parent a800903 commit 73c1fa1
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 139 deletions.
25 changes: 13 additions & 12 deletions HomeSlide.php
Expand Up @@ -121,32 +121,33 @@ public function add($autodate = true, $null_values = false)
}

/**
* @return int
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function delete()
{
$res = true;

$images = $this->image;
foreach ($images as $image) {
if (preg_match('/sample/', $image) === 0) {
if ($image && file_exists(dirname(__FILE__) . '/images/' . $image)) {
$res &= @unlink(dirname(__FILE__) . '/images/' . $image);
}
// delete image files
foreach ($this->image as $image) {
$filepath = HomeSlider::getImageDir() . $image;
if ($image && file_exists($filepath)) {
$res = unlink($filepath) && $res;
}
}

$res &= $this->reOrderPositions();
// reorder positions
$res = $this->reOrderPositions() && $res;

$res &= Db::getInstance()->execute('
// delete slides
$res = Db::getInstance()->execute('
DELETE FROM `' . _DB_PREFIX_ . 'homeslider`
WHERE `id_homeslider_slides` = ' . (int)$this->id
);
) && $res;

$res &= parent::delete();
return $res;
// delete record
return parent::delete() && $res;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions fixtures/fixtures.json
@@ -0,0 +1,20 @@
[
{
"image": "sample-1.jpg",
"title": "Shop Tea",
"legend": "Shop Tea",
"description": "<h3 style=\"float:left;clear:both;font-size:30px;\">Shop Tea</h3><p style=\"text-align:center;margin-top:20px;\"><button class=\"btn btn-default\" type=\"button\">Shop now !</button></p>"
},
{
"image": "sample-2.jpg",
"title": "Soaps",
"legend": "Soaps",
"description": "<h3 style=\"float:left;clear:both;font-size:30px;\">View All Soaps</h3><p style=\"text-align:center;margin-top:20px;\"><button class=\"btn btn-default\" type=\"button\">Shop now !</button></p>"
},
{
"image": "sample-3.jpg",
"title": "Gifts",
"legend": "Gifts",
"description": "<h3 style=\"float:left;clear:both;font-size:30px;\">Shop Gifts</h3><p style=\"text-align:center;margin-top:20px;\"><button class=\"btn btn-default\" type=\"button\">Shop now !</button></p>"
}
]
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
281 changes: 175 additions & 106 deletions homeslider.php

Large diffs are not rendered by default.

Binary file removed images/gray_next.png
Binary file not shown.
Binary file removed images/gray_pager.png
Binary file not shown.
Binary file removed images/gray_prev.png
Binary file not shown.
Binary file removed images/sample-1.jpg
Binary file not shown.
Binary file removed images/sample-2.jpg
Binary file not shown.
Binary file removed images/sample-3.jpg
Binary file not shown.
35 changes: 35 additions & 0 deletions upgrade/index.php
@@ -0,0 +1,35 @@
<?php
/**
* Copyright (C) 2017-2019 thirty bees
* Copyright (C) 2007-2016 PrestaShop SA
*
* thirty bees is an extension to the PrestaShop software by PrestaShop SA.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (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.php
* 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@thirtybees.com so we can send you a copy immediately.
*
* @author thirty bees <modules@thirtybees.com>
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2017-2019 thirty bees
* @copyright 2007-2016 PrestaShop SA
* @license Academic Free License (AFL 3.0)
* PrestaShop is an internationally registered trademark of PrestaShop SA.
*/

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;
60 changes: 60 additions & 0 deletions upgrade/upgrade-2.1.0.php
@@ -0,0 +1,60 @@
<?php
/**
* 2007-2016 PrestaShop
*
* thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
* Copyright (C) 2017-2018 thirty bees
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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@thirtybees.com so we can send you a copy immediately.
*
* @author thirty bees <modules@thirtybees.com>
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2017-2018 thirty bees
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* PrestaShop is an internationally registered trademark & property of PrestaShop SA
*/

if (!defined('_TB_VERSION_')) {
exit;
}

/**
* Copy all image files referenced in the database to the /img/homeslider/ directory
*
* @return bool
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
function upgrade_module_2_1_0()
{
$sourceDir = __DIR__ . '/../images/';
$targetDir = HomeSlider::getImageDir();
if (file_exists($sourceDir)) {

// find images from the database
$images = array_column(Db::getInstance()->executeS((new DbQuery())
->select('DISTINCT(image) as image')
->from('homeslider_slides_lang')
), 'image');

// copy them to destination
foreach ($images as $image) {
if (file_exists($sourceDir . $image)) {
if (! file_exists($targetDir . $image)) {
copy($sourceDir . $image, $targetDir . $image);
}
}
}
}
return true;
}
44 changes: 23 additions & 21 deletions views/templates/hook/homeslider.tpl
Expand Up @@ -22,26 +22,28 @@
* PrestaShop is an internationally registered trademark of PrestaShop SA.
*}

{if $page_name =='index'}
<!-- Module HomeSlider -->
{if $page_name == 'index'}
{if isset($homeslider_slides)}
<div id="homepage-slider">
{if isset($homeslider_slides.0) && isset($homeslider_slides.0.sizes.1)}{capture name='height'}{$homeslider_slides.0.sizes.1}{/capture}{/if}
<ul id="homeslider"{if isset($smarty.capture.height) && $smarty.capture.height} style="max-height:{$smarty.capture.height}px;"{/if}>
{foreach from=$homeslider_slides item=slide}
{if $slide.active}
<li class="homeslider-container">
<a href="{$slide.url|escape:'html':'UTF-8'}" title="{$slide.legend|escape:'html':'UTF-8'}">
<img src="{$link->getMediaLink("`$smarty.const._MODULE_DIR_`homeslider/images/`$slide.image|escape:'htmlall':'UTF-8'`")}"{if isset($slide.size) && $slide.size} {$slide.size}{else} width="100%" height="100%"{/if} alt="{$slide.legend|escape:'htmlall':'UTF-8'}" />
</a>
{if isset($slide.description) && trim($slide.description) != ''}
<div class="homeslider-description">{$slide.description}</div>
{/if}
</li>
{/if}
{/foreach}
</ul>
</div>
{/if}
<!-- /Module HomeSlider -->
<div id="homepage-slider">
{if isset($homeslider_slides.0) && isset($homeslider_slides.0.sizes.1)}{capture name='height'}{$homeslider_slides.0.sizes.1}{/capture}{/if}
<ul id="homeslider"{if isset($smarty.capture.height) && $smarty.capture.height} style="max-height:{$smarty.capture.height}px;"{/if}>
{foreach from=$homeslider_slides item=slide}
{if $slide.active}
<li class="homeslider-container">
<a href="{$slide.url|escape:'html':'UTF-8'}" title="{$slide.legend|escape:'html':'UTF-8'}">
<img src="{$link->getMediaLink($slide.imageUrl)|escape:'htmlall':'UTF-8'}"
{if isset($slide.size) && $slide.size}{$slide.size}{else}width="100%"
height="100%"{/if}
alt="{$slide.legend|escape:'htmlall':'UTF-8'}"
/>
</a>
{if isset($slide.description) && trim($slide.description) != ''}
<div class="homeslider-description">{$slide.description}</div>
{/if}
</li>
{/if}
{/foreach}
</ul>
</div>
{/if}
{/if}

0 comments on commit 73c1fa1

Please sign in to comment.