Skip to content

Commit

Permalink
Merge pull request #622 from leemyongpakvn/migrate-ObjectModel-to-Doc…
Browse files Browse the repository at this point in the history
…trine

Migrate ObjectModel to Doctrine
  • Loading branch information
leemyongpakvn authored Dec 5, 2023
2 parents 4b8f66d + aa009ef commit 5e49526
Show file tree
Hide file tree
Showing 20 changed files with 942 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
uses: actions/[email protected]

- name: PHP syntax checker 7.1
uses: prestashop/github-action-php-lint/5.6@master
uses: prestashop/github-action-php-lint/7.1@master

- name: PHP syntax checker 7.2
uses: prestashop/github-action-php-lint/7.2@master
Expand Down
25 changes: 15 additions & 10 deletions blockreassurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_once $autoloadPath;
}

use PrestaShop\Module\BlockReassurance\Entity\Psreassurance;
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

class blockreassurance extends Module implements WidgetInterface
Expand Down Expand Up @@ -273,6 +274,8 @@ public function getContent()

$moduleAdminLink = Context::getContext()->link->getAdminLink('AdminModules', true) . '&configure=' . $this->name . '&module_name=' . $this->name;

$reassuranceRepository = $this->get('block_reassurance_repository');

$allCms = CMS::listCms($id_lang);
$fields_captions = [
'position' => $this->trans('Position', [], 'Modules.Blockreassurance.Admin'),
Expand All @@ -292,7 +295,7 @@ public function getContent()
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
'logo_path' => $this->logo_path,
'languages' => Language::getLanguages(false),
'allblock' => ReassuranceActivity::getAllBlock(),
'allblock' => $reassuranceRepository->getAllBlock(),
'currentPage' => $currentPage,
'moduleAdminLink' => $moduleAdminLink,
'img_path' => $this->img_path,
Expand All @@ -303,9 +306,9 @@ public function getContent()
'folderIsWritable' => $this->folderUploadFilesHasGoodRights(),
'folderPath' => $this->img_path_perso,
// constants
'LINK_TYPE_NONE' => ReassuranceActivity::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => ReassuranceActivity::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => ReassuranceActivity::TYPE_LINK_URL,
'LINK_TYPE_NONE' => Psreassurance::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => Psreassurance::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => Psreassurance::TYPE_LINK_URL,
'fields_captions' => $fields_captions,
]);

Expand Down Expand Up @@ -432,7 +435,8 @@ public function renderWidget($hookName = null, array $configuration = [])
*/
public function getWidgetVariables($hookName = null, array $configuration = [])
{
$blocks = ReassuranceActivity::getAllBlockByStatus(
$reassuranceRepository = $this->get('block_reassurance_repository');
$blocks = $reassuranceRepository->getAllBlockByStatus(
$this->context->language->id
);

Expand All @@ -455,7 +459,7 @@ public function getWidgetVariables($hookName = null, array $configuration = [])

return [
'elements' => $elements,
'LINK_TYPE_NONE' => ReassuranceActivity::TYPE_LINK_NONE,
'LINK_TYPE_NONE' => Psreassurance::TYPE_LINK_NONE,
];
}

Expand Down Expand Up @@ -492,16 +496,17 @@ private function shouldWeDisplayOnBlockProduct($enableCheckout, $enableProduct,
*/
private function renderTemplateInHook($template)
{
$reassuranceRepository = $this->get('block_reassurance_repository');
$id_lang = $this->context->language->id;

$this->context->smarty->assign([
'blocks' => ReassuranceActivity::getAllBlockByStatus($id_lang),
'blocks' => $reassuranceRepository->getAllBlockByStatus($id_lang),
'iconColor' => Configuration::get('PSR_ICON_COLOR'),
'textColor' => Configuration::get('PSR_TEXT_COLOR'),
// constants
'LINK_TYPE_NONE' => ReassuranceActivity::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => ReassuranceActivity::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => ReassuranceActivity::TYPE_LINK_URL,
'LINK_TYPE_NONE' => Psreassurance::TYPE_LINK_NONE,
'LINK_TYPE_CMS' => Psreassurance::TYPE_LINK_CMS_PAGE,
'LINK_TYPE_URL' => Psreassurance::TYPE_LINK_URL,
]);

return $this->display(__FILE__, 'views/templates/hook/' . $template);
Expand Down
8 changes: 8 additions & 0 deletions classes/ReassuranceActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class ReassuranceActivity extends ObjectModel
* @param array $psr_languages
* @param int $type_link
* @param int $id_cms
*
* @deprecated 6.0.0 - migrated to src/Form/PsreassuranceFormDataHandler as createLangs and updateLangs
*/
public function handleBlockValues($psr_languages, $type_link, $id_cms)
{
Expand Down Expand Up @@ -115,6 +117,8 @@ public function handleBlockValues($psr_languages, $type_link, $id_cms)
* @return array
*
* @throws PrestaShopDatabaseException
*
* @deprecated 6.0.0 - migrated to src/Repository/PsreassuranceRepository
*/
public static function getAllBlock()
{
Expand Down Expand Up @@ -147,6 +151,8 @@ public static function getAllBlock()
* @return array
*
* @throws PrestaShopDatabaseException
*
* @deprecated 6.0.0 - migrated to src/Repository/PsreassuranceRepository
*/
public static function getAllBlockByStatus($id_lang = 1)
{
Expand All @@ -169,6 +175,8 @@ public static function getAllBlockByStatus($id_lang = 1)

/**
* @return string|bool
*
* @deprecated 6.0.0 - migrated to ImageManager since PS 1.7.7.0
*/
public static function getMimeType(string $filename)
{
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"PrestaShop\\Module\\BlockReassurance\\": "src/"
},
"classmap": [
"blockreassurance.php",
"classes/ReassuranceActivity.php"
"blockreassurance.php"
],
"exclude-from-classmap": []
},
Expand Down
4 changes: 2 additions & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<author><![CDATA[PrestaShop]]></author>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
<limited_countries></limited_countries>
</module>
28 changes: 28 additions & 0 deletions config/admin/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 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;
11 changes: 11 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
imports:
- { resource: ../common.yml }

services:
block_reassurance_form_data_handler:
class: 'PrestaShop\Module\BlockReassurance\Form\PsreassuranceFormDataHandler'
public: true
arguments:
- '@block_reassurance_repository'
- '@prestashop.core.admin.lang.repository'
- '@doctrine.orm.default_entity_manager'
8 changes: 8 additions & 0 deletions config/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
block_reassurance_repository:
class: PrestaShop\Module\BlockReassurance\Repository\PsreassuranceRepository
public: true
arguments:
- '@doctrine'
- '@doctrine.dbal.default_connection'
- '%database_prefix%'
28 changes: 28 additions & 0 deletions config/front/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 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;
2 changes: 2 additions & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: ../common.yml }
28 changes: 28 additions & 0 deletions config/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 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;
75 changes: 33 additions & 42 deletions controllers/admin/AdminBlockListingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
use PrestaShop\Module\BlockReassurance\Entity\Psreassurance;

class AdminBlockListingController extends ModuleAdminController
{
/** @var blockreassurance */
Expand Down Expand Up @@ -139,9 +141,9 @@ public function displayAjaxSaveBlockContent()
$errors = [];

$picto = Tools::getValue('picto');
$id_block = empty(Tools::getValue('id_block')) ? null : Tools::getValue('id_block');
$id_block = empty(Tools::getValue('id_block')) ? 0 : (int) Tools::getValue('id_block');
$type_link = (int) Tools::getValue('typelink');
$id_cms = Tools::getValue('id_cms');
$id_cms = (int) Tools::getValue('id_cms');
$psr_languages = (array) json_decode(Tools::getValue('lang_values'));
$authExtensions = ['gif', 'jpg', 'jpeg', 'jpe', 'png', 'svg'];
$authMimeType = ['image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/svg', 'image/svg+xml'];
Expand All @@ -152,66 +154,55 @@ public function displayAjaxSaveBlockContent()
return $this->ajaxRenderJson(empty($errors) ? 'success' : 'error');
}

$blockPsr = new ReassuranceActivity($id_block);
if (!$id_block) {
$reassuranceRepository = $this->context->controller->getContainer()->get('block_reassurance_repository');
$reassuranceFormHandler = $this->context->controller->getContainer()->get('block_reassurance_form_data_handler');

if ($id_block) {
$blockPsr = $reassuranceRepository->find($id_block);
} else {
$blockPsr = new Psreassurance();
// Last position
$blockPsr->position = (int) Db::getInstance()->getValue('SELECT MAX(position) AS max FROM ' . _DB_PREFIX_ . 'psreassurance');
$blockPsr->position = $blockPsr->position ? $blockPsr->position + 1 : 1;
$blockPsr->status = false;
$blockPsr->setPosition((int) Db::getInstance()->getValue('SELECT MAX(position) AS max FROM ' . _DB_PREFIX_ . 'psreassurance'));
$blockPsr->setPosition($blockPsr->getPosition() ? $blockPsr->getPosition() + 1 : 1);
$blockPsr->setStatus(0);
}
$blockPsr->handleBlockValues($psr_languages, $type_link, $id_cms);

if (strpos($picto, $this->module->img_path_perso) !== false) {
$blockPsr->icon = '';
$blockPsr->custom_icon = $picto;
$blockPsr->setIcon('');
$blockPsr->setCustomIcon($picto);
} else {
$blockPsr->icon = $picto;
$blockPsr->custom_icon = '';
$blockPsr->setIcon($picto);
$blockPsr->setCustomIcon('');
}
$blockPsr->date_add = date('Y-m-d H:i:s');
$blockPsr->date_upd = date('Y-m-d H:i:s');

if (!empty($_FILES)) {
$customImage = $_FILES['file'];
$fileTmpName = $customImage['tmp_name'];
$filename = $customImage['name'];

// validateUpload return false if no error (false -> OK)
if (version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
// PrestaShop 1.7.7.0+
$validUpload = ImageManager::validateUpload(
$customImage,
0,
$authExtensions,
$authMimeType
);
} else {
// PrestaShop < 1.7.7
$validUpload = false;
$mimeType = ReassuranceActivity::getMimeType($customImage['tmp_name']);
if ($mimeType && (
!in_array($mimeType, $authMimeType)
|| !ImageManager::isCorrectImageFileExt($customImage['name'], $authExtensions)
|| preg_match('/\%00/', $customImage['name'])
)) {
$validUpload = Context::getContext()->getTranslator()->trans('Image format not recognized, allowed formats are: .gif, .jpg, .png', [], 'Admin.Notifications.Error');
}
if ($customImage['error']) {
$validUpload = Context::getContext()->getTranslator()->trans('Error while uploading image; please change your server\'s settings. (Error code: %s)', [$customImage['error']], 'Admin.Notifications.Error');
}
}
$validUpload = ImageManager::validateUpload(
$customImage,
0,
$authExtensions,
$authMimeType
);

if (is_bool($validUpload) && $validUpload === false) {
move_uploaded_file($fileTmpName, $this->module->folder_file_upload . $filename);
$blockPsr->custom_icon = $this->module->img_path_perso . '/' . $filename;
$blockPsr->icon = '';
$blockPsr->setCustomIcon($this->module->img_path_perso . '/' . $filename);
$blockPsr->setIcon('');
} else {
$errors[] = $validUpload;
}
}

if (empty($errors)) {
$blockPsr->setDateUpd(new \DateTime('now', new \DateTimeZone('UTC')));
if ($id_block) {
$blockPsr->update();
$reassuranceFormHandler->updateLangs($blockPsr, $psr_languages, $type_link, $id_cms);
} else {
$blockPsr->add();
$blockPsr->setDateAdd(new \DateTime('now', new \DateTimeZone('UTC')));
$reassuranceFormHandler->createLangs($blockPsr, $psr_languages, $type_link, $id_cms);
}
}

Expand Down
Loading

0 comments on commit 5e49526

Please sign in to comment.