Skip to content

Commit

Permalink
Merge pull request #117 from PrestaShopCorp/3.x
Browse files Browse the repository at this point in the history
Update dev with 3.x
  • Loading branch information
intraordinaire authored Aug 30, 2023
2 parents b725657 + a9a0160 commit 53b8cfe
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
38 changes: 38 additions & 0 deletions classes/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,44 @@ public static function getIdsByHookCalculation($hook_name)
return array_unique($ids);
}

public static function getIdsDailyCalculation()
{
$ids = [];
$in = [];

//advice conditions validation
$sub_query = new DbQuery();
$sub_query->select('id_advice');
$sub_query->from('advice', 'a');

$sub_results = Db::getInstance()->executeS($sub_query);

$in = [];

foreach ($sub_results as $sub_result) {
$in[] = $sub_result['id_advice'];
}

$query = new DbQuery();
$query->select('c.`id_condition`');
$query->from('condition', 'c');
$query->join('LEFT JOIN `' . _DB_PREFIX_ . 'condition_advice` ca ON ca.`id_condition` = c.`id_condition`');
$query->where('c.`calculation_type` = \'time\'');
$query->where('DATEDIFF(NOW(), `date_upd`) >= `calculation_detail`');
$query->where('c.`validated` = 0');
if (count($in)) {
$query->where('ca.`id_advice` IN (' . implode(',', $in) . ')');
}
$query->groupBy('c.`id_condition`');

$result = Db::getInstance()->executeS($query);
foreach ($result as $r) {
$ids[] = $r['id_condition'];
}

return array_unique($ids);
}

public function processCalculation()
{
switch ($this->type) {
Expand Down
22 changes: 18 additions & 4 deletions controllers/admin/AdminGamificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public function initPageHeaderToolbar()

public function ajaxProcessGamificationTasks()
{
if (!Configuration::get('GF_INSTALL_CALC')) {
$this->processRefreshData();
Configuration::updateGlobalValue('GF_INSTALL_CALC', 1);
}
// Refresh data from API, if needed
$this->processRefreshData();

// Recalculate not validated conditions based on time
$this->processMakeDailyCalculation();

// Compute advices validtion/unvalidation based on conditions
$this->processAdviceValidation();

$return['advices_to_display'] = $this->processGetAdvicesToDisplay();
Expand Down Expand Up @@ -113,6 +115,18 @@ public function processGetAdvicesToDisplay($only_premium = false)
return $return;
}

public function processMakeDailyCalculation()
{
$return = true;
$condition_ids = Condition::getIdsDailyCalculation();
foreach ($condition_ids as $id) {
$condition = new Condition((int) $id);
$return &= $condition->processCalculation();
}

return $return;
}

public function processAdviceValidation()
{
$return = true;
Expand Down
23 changes: 17 additions & 6 deletions gamification.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function __construct()
$this->tab = 'administration';
$this->version = '3.0.2';
$this->author = 'PrestaShop';
$this->module_key = 'c1187d1672d2a2d33fbd7d5c29f0d42e';
$this->ps_versions_compliancy = [
'min' => '8.0.0',
];
Expand All @@ -73,7 +74,6 @@ public function install()

return
$this->installDb()
&& Configuration::updateGlobalValue('GF_INSTALL_CALC', 0)
&& parent::install()
&& $this->registerHook('actionAdminControllerSetMedia')
&& $this->registerHook('displayBackOfficeHeader')
Expand All @@ -83,8 +83,7 @@ public function install()

public function uninstall()
{
if (!parent::uninstall() || !$this->uninstallDb() ||
!Configuration::updateGlobalValue('GF_INSTALL_CALC', 0)) {
if (!parent::uninstall() || !$this->uninstallDb()) {
return false;
}

Expand Down Expand Up @@ -114,7 +113,15 @@ public function uninstallDb()

public function enable($force_all = false)
{
return parent::enable($force_all) && Tab::enablingForModule($this->name);
$enableResult = parent::enable($force_all) && Tab::enablingForModule($this->name);

if (php_sapi_name() !== 'cli') {
// If the module is installed/enabled tthrough CLI, we ignore the data refreshing
// because we cannot guess the shop context
$enableResult &= $this->refreshDatas();
}

return $enableResult;
}

public function disable($force_all = false)
Expand Down Expand Up @@ -213,9 +220,9 @@ public function refreshDatas($iso_lang = null)
$this->processCleanAdvices();

$public_key = file_get_contents(__DIR__ . '/prestashop.pub');
$signature = isset($data->signature) ? base64_decode($data->signature) : '';

if (isset($data->conditions)) {
$signature = isset($data->signature) ? base64_decode($data->signature) : '';
if (
function_exists('openssl_verify')
&& self::TEST_MODE === false
Expand All @@ -232,10 +239,12 @@ function_exists('openssl_verify')
}

if (isset($data->advices_lang_16)) {
$signature16 = isset($data->signature_16) ? base64_decode($data->signature_16) : '';
$sslCheck = openssl_verify(json_encode([$data->advices_lang_16]), $signature16, $public_key);
if (
function_exists('openssl_verify')
&& self::TEST_MODE === false
&& !openssl_verify(json_encode([$data->advices_lang_16]), $signature, $public_key)
&& !$sslCheck
) {
return false;
}
Expand All @@ -246,6 +255,8 @@ function_exists('openssl_verify')
}
}
}

return true;
}

public function getData($iso_lang = null)
Expand Down
32 changes: 32 additions & 0 deletions upgrade/install-3.0.1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 Module $module
*
* @return bool
*/
function upgrade_module_3_0_1($module)
{
return Configuration::deleteByName('GF_INSTALL_CALC');
}

0 comments on commit 53b8cfe

Please sign in to comment.