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

Update: Recommendation section update process #858

Merged
merged 2 commits into from
Apr 9, 2024
Merged
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
11 changes: 3 additions & 8 deletions classes/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -3402,14 +3402,9 @@ public static function addonsRequest($request, $params = array())
$post_data .= '&method=listing&action=install-modules';
$post_data .= defined('_PS_HOST_MODE_') ? '-od' : '';
break;
case 'catalog-recommendation':
$protocols[] = 'http';
$post_data .= '&method=content&action=catalogRecommendation';
$post_data .= defined('_PS_HOST_MODE_') ? '-od' : '';
break;
case 'dashboard-recommendation':
$protocols[] = 'http';
$post_data .= '&method=content&action=dashboardRecommendation';
case 'recommendation':
$post_data .= '&method=recommendation&controller=';
$post_data .= (isset($params['controller']) ? $params['controller'] : Context::getContext()->controller->controller_name);
$post_data .= defined('_PS_HOST_MODE_') ? '-od' : '';
break;
case 'check-version':
Expand Down
72 changes: 64 additions & 8 deletions classes/controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ class AdminControllerCore extends Controller
/** @var int level for permissions View/read */
const LEVEL_VIEW = 1;

/** @var string path for recomendation content */
const RECOMMENDATION_CONTENT_FILE_PATH = '';

public function __construct()
{
global $timer_start;
Expand Down Expand Up @@ -3946,17 +3949,65 @@ protected function ajaxProcessOpenHelp()
public function ajaxProcessGetRecommendationContent()
{
$response = array('success' => false);
if (isset($this->context->cookie->{$this->controller_name.'_closed'}) && $this->context->cookie->{$this->controller_name.'_closed'}) {
$response['success'] = true;
} else {
if (method_exists($this, 'getRecommendationContent')) {
if ($content = $this->getRecommendationContent()) {
$response['success'] = true;
$response['content'] = $content;
if ($this->context->controller->getRecommendationFilePath()) {
$response = $this->getRecommendationContent();
}
$this->context->cookie->write();
$this->ajaxDie(json_encode($response));
}

public function getRecommendationContent()
{
$content = array(
'success' => false,
'cache' => true,
'html' => ''
);

if ($recommendationContent = $this->updateRecommendationContent()) {
$content['cache'] = false;
}
if (file_exists(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath())) {
$content['success'] = true;
if (!isset($this->context->cookie->{$this->controller_name.'_closed'}) || !$this->context->cookie->{$this->controller_name.'_closed'}) {
$content['html'] = file_get_contents(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath());
}
}

return $content;
}

public function updateRecommendationContent()
{
if (!Tools::isFresh($this->context->controller->getRecommendationFilePath(), _TIME_1_DAY_, false)) {
if ($recommendationContent = Tools::addonsRequest(
'recommendation',
array('controller' => $this->context->controller->controller_name)
)) {
$recommendationContent = json_decode($recommendationContent, true);
if (!isset($this->context->cookie->{$this->context->controller->controller_name.'_key'})) {
$this->context->cookie->{$this->context->controller->controller_name.'_key'} = '';
}
if ($this->context->cookie->{$this->context->controller->controller_name.'_key'} != $recommendationContent['key']) {
unset($this->context->cookie->{$this->context->controller->controller_name.'_closed'});
}
$this->context->cookie->{$this->context->controller->controller_name.'_key'} = $recommendationContent['key'];
if (isset($recommendationContent['success']) && $recommendationContent['success']) {
@file_put_contents(
_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath(),
$recommendationContent['html']
);
} elseif (file_exists(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath())) {
unlink(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath());
}
return $recommendationContent;
} else {
if (file_exists(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath())) {
unlink(_PS_ROOT_DIR_.$this->context->controller->getRecommendationFilePath());
}
}
}
$this->ajaxDie(json_encode($response));
return false;
}

public function ajaxProcessRecommendationClosed()
Expand All @@ -3966,6 +4017,11 @@ public function ajaxProcessRecommendationClosed()
$this->ajaxDie(json_encode($response));
}

public function getRecommendationFilePath()
{
return static::RECOMMENDATION_CONTENT_FILE_PATH;
}

/**
* Save list visible columns
*
Expand Down
13 changes: 1 addition & 12 deletions controllers/admin/AdminDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class AdminDashboardControllerCore extends AdminController
{

const DASHBOARD_RECOMMENDATION_CONTENT = '/cache/dashboard_recommendation.html';
const RECOMMENDATION_CONTENT_FILE_PATH = '/cache/dashboard_recommendation.html';

public function __construct()
{
Expand Down Expand Up @@ -493,15 +493,4 @@ public function ajaxProcessSaveDashConfig()

die(json_encode($return));
}

public function getRecommendationContent()
{
if (!Tools::isFresh(self::DASHBOARD_RECOMMENDATION_CONTENT, _TIME_1_DAY_, false)) {
@file_put_contents(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT, Tools::addonsRequest('dashboard-recommendation'));
}
if (file_exists(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT)) {
return Tools::file_get_contents(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT);
}
return false;
}
}
13 changes: 1 addition & 12 deletions controllers/admin/AdminModulesCatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AdminModulesCatalogControllerCore extends AdminController

public $modules;

const CATALOG_RECOMMENDATION_CONTENT = '/cache/catalog_recommendation.html';
const RECOMMENDATION_CONTENT_FILE_PATH = '/cache/catalog_recommendation.html';

const ELEMENT_TYPE_MODULE = 1;
const ELEMENT_TYPE_THEME = 2;
Expand Down Expand Up @@ -240,17 +240,6 @@ public function initToolbar()
);
}

public function getRecommendationContent()
{
if (!Tools::isFresh(self::CATALOG_RECOMMENDATION_CONTENT, _TIME_1_DAY_, false)) {
@file_put_contents(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT, Tools::addonsRequest('catalog-recommendation'));
}
if (file_exists(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT)) {
return Tools::file_get_contents(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT);
}
return false;
}

public function sortList(&$list, $type)
{

Expand Down
4 changes: 2 additions & 2 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1616,9 +1616,9 @@ function loadRecommendation()
token: token
},
success: function(res) {
if (res.success && res.content && res.content.trim()) {
if (res.success && res.html && res.html.trim()) {
$('#recommendation-wrapper-skeleton').fadeIn('slow');
$('#recommendation-wrapper').html(res.content);
$('#recommendation-wrapper').html(res.html);
let images = $('#recommendation-wrapper img');
let loaded = 0;
let total = $(images).length;
Expand Down