Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Problem with the blockreassurance module in case of a site in a subfolder #712

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions blockreassurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class blockreassurance extends Module implements WidgetInterface
public $folder_file_upload;
/** @var string */
private $templateFile;
public static $static_img_path;
public static $static_img_path_perso;
public static $static_folder_file_upload;

public function __construct()
{
Expand Down Expand Up @@ -123,6 +126,9 @@ public function __construct()
$this->logo_path = $this->_path . 'logo.png';
$this->module_path = $this->_path;
$this->folder_file_upload = _PS_MODULE_DIR_ . $this->name . '/views/img/img_perso/';
self::$static_img_path = $this->img_path;
self::$static_img_path_perso = $this->img_path_perso;
self::$static_folder_file_upload = $this->folder_file_upload;

// Confirm uninstall
$this->confirmUninstall = $this->trans('Are you sure you want to uninstall this module?', [], 'Modules.Blockreassurance.Admin');
Expand Down Expand Up @@ -162,9 +168,9 @@ public function install()
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8;';

$sqlQueries[] = 'INSERT INTO ' . _DB_PREFIX_ . 'psreassurance (icon, custom_icon, status, position, type_link, id_cms, date_add) VALUES '
. "('" . $this->img_path . "reassurance/pack2/security.svg', null, 1, 1, null, null, now()),"
. "('" . $this->img_path . "reassurance/pack2/carrier.svg', null, 1, 2, null, null, now()),"
. "('" . $this->img_path . "reassurance/pack2/parcel.svg', null, 1, 3, null, null, now())";
. "('reassurance/pack2/security.svg', null, 1, 1, null, null, now()),"
. "('reassurance/pack2/carrier.svg', null, 1, 2, null, null, now()),"
. "('reassurance/pack2/parcel.svg', null, 1, 3, null, null, now())";
foreach (Language::getLanguages(false) as $lang) {
$sqlQueries[] = 'INSERT INTO ' . _DB_PREFIX_ . 'psreassurance_lang (id_psreassurance, id_lang, title, description, link) VALUES '
. '(1, ' . $lang['id_lang'] . ", '" . $this->trans('Security policy', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', '" . $this->trans('(edit with the Customer Reassurance module)', [], 'Modules.Blockreassurance.Shop', $lang['locale']) . "', ''),"
Expand Down Expand Up @@ -286,6 +292,15 @@ public function getContent()
'actions' => $this->trans('Actions', [], 'Modules.Blockreassurance.Admin'),
];

$allblock = $reassuranceRepository->getAllBlock();
foreach ($allblock as &$block) {
if ($block['icon']) {
$block['icon'] = $this->img_path . $block['icon'];
} elseif ($block['custom_icon']) {
$block['custom_icon'] = $this->img_path_perso . '/' . $block['custom_icon'];
}
}

$this->context->smarty->assign([
'psr_hook_header' => (int) Configuration::get('PSR_HOOK_HEADER'),
'psr_hook_footer' => (int) Configuration::get('PSR_HOOK_FOOTER'),
Expand All @@ -295,7 +310,7 @@ public function getContent()
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
'logo_path' => $this->logo_path,
'languages' => Language::getLanguages(false),
'allblock' => $reassuranceRepository->getAllBlock(),
'allblock' => $allblock,
'currentPage' => $currentPage,
'moduleAdminLink' => $moduleAdminLink,
'img_path' => $this->img_path,
Expand Down
17 changes: 16 additions & 1 deletion controllers/admin/AdminBlockListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,17 @@ public function displayAjaxSaveBlockContent()
}

if (strpos($picto, $this->module->img_path_perso) !== false) {
if ($picto != '') {
$picto = basename($picto);
}
$blockPsr->setIcon('');
$blockPsr->setCustomIcon($picto);
} else {
if ($picto != '') {
$parts = explode('/', $picto);
$parts = array_slice($parts , -3);
$picto = implode('/', $parts);
}
$blockPsr->setIcon($picto);
$blockPsr->setCustomIcon('');
}
Expand All @@ -188,8 +196,15 @@ public function displayAjaxSaveBlockContent()
);

if (is_bool($validUpload) && $validUpload === false) {
// Remove Custom icon
if ($blockPsr->getCustomIcon() != '') {
$filePath = blockreassurance::$static_folder_file_upload . '/' . basename($blockPsr->getCustomIcon());
if (file_exists($filePath)) {
unlink($filePath);
}
}
move_uploaded_file($fileTmpName, $this->module->folder_file_upload . $filename);
$blockPsr->setCustomIcon($this->module->img_path_perso . '/' . $filename);
$blockPsr->setCustomIcon($filename);
$blockPsr->setIcon('');
} else {
$errors[] = $validUpload;
Expand Down
8 changes: 7 additions & 1 deletion src/Repository/PsreassuranceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ public function getAllBlockByStatus($id_lang = 1)
$xmlMimes = ['image/svg', 'image/svg+xml'];
foreach ($result as &$item) {
$item['is_svg'] = !empty($item['custom_icon'])
&& in_array(\ImageManager::getMimeType(_PS_ROOT_DIR_ . $item['custom_icon']), $xmlMimes);
&& in_array(\ImageManager::getMimeType(\blockreassurance::$static_folder_file_upload . $item['custom_icon']), $xmlMimes);

if ($item['custom_icon'] != '') {
$item['custom_icon'] = \blockreassurance::$static_img_path_perso . '/' . $item['custom_icon'];
} elseif ($item['icon'] != '') {
$item['icon'] = \blockreassurance::$static_img_path . $item['icon'];
}
}

return $result;
Expand Down
59 changes: 59 additions & 0 deletions upgrade/upgrade-6.0.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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
*/
if (!defined('_PS_VERSION_')) {
exit;
}

/**
* @param blockreassurance $module
*
* @return bool|string
*/
function upgrade_module_6_0_0($module)
{
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'psreassurance';

$rows = Db::getInstance()->executeS($sql);

foreach ($rows as $row) {

$data = [];

if ($row['icon']) {

$parts = explode('/', $row['icon']);
$parts = array_slice($parts , -3);

$data = [
'icon' => implode('/', $parts)
];
} elseif ($row['custom_icon']) {
$data = [
'custom_icon' => basename($row['custom_icon'])
];
}

if ($data) {
Db::getInstance()->update('psreassurance', $data, '`id_psreassurance` = ' . (int) $row['id_psreassurance']);
}
}

return true;
}