Skip to content
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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@
"optimize-autoloader": true,
"prepend-autoloader": false
},
"autoload": {
"psr-4": {
"PrestaShop\\Module\\DashProducts\\": "src/"
}
},
"type": "prestashop-module"
}
8 changes: 4 additions & 4 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<module>
<name>dashproducts</name>
<displayName><![CDATA[Dashboard Products]]></displayName>
<version><![CDATA[2.2.1]]></version>
<description><![CDATA[Adds a block with a table of your latest orders and a ranking of your products]]></description>
<version><![CDATA[3.0.0]]></version>
<description><![CDATA[Enrich your stats: display a table of your latest orders and a ranking of your products.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[dashboard]]></tab>
<is_configurable>0</is_configurable>
<tab><![CDATA[administration]]></tab>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
34 changes: 34 additions & 0 deletions config/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 3.0 (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
* 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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @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 3.0 (AFL-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;
7 changes: 7 additions & 0 deletions config/routes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dashproducts_configuration:
path: /dashproducts/configuration
methods: [GET, POST]
defaults:
_controller: PrestaShop\Module\DashProducts\Controller\ConfigurationController::indexAction
_legacy_controller: AdminDashproductsConfiguration
_legacy_link: AdminDashproductsConfiguration
10 changes: 10 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
PrestaShop\Module\DashProducts\Controller\ConfigurationController:
public: true
autowire: true
autoconfigure: true

PrestaShop\Module\DashProducts\Type\ConfigurationType:
autowire: true
autoconfigure: true
tags: ['form.type']
98 changes: 93 additions & 5 deletions dashproducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use Twig\Environment;

if (!defined('_PS_VERSION_')) {
exit;
}
Expand All @@ -33,7 +36,7 @@ public function __construct()
{
$this->name = 'dashproducts';
$this->tab = 'administration';
$this->version = '2.2.1';
$this->version = '3.0.0';
$this->author = 'PrestaShop';

parent::__construct();
Expand All @@ -50,11 +53,54 @@ public function install()
Configuration::updateValue('DASHPRODUCT_NBR_SHOW_TOP_SEARCH', 10);

return parent::install()
&& $this->createConfigurationTab()
&& $this->registerHook('dashboardZoneTwo')
&& $this->registerHook('dashboardData')
// Modern counterpart of dashboardZoneTwo, registered alongside it (#41971).
&& $this->registerHook('displayAdminDashboardZoneTwo')
;
}

public function uninstall()
{
$idTab = (int) Tab::getIdFromClassName('AdminDashproductsConfiguration');
if ($idTab) {
(new Tab($idTab))->delete();
}

return parent::uninstall();
}

/**
* Hidden tab (id_parent -1): only used to back the settings route's ACL. Shared between
* install() and the 3.0.0 upgrade script so shops upgrading from an earlier version get it too.
*/
public function createConfigurationTab(): bool
{
if (Tab::getIdFromClassName('AdminDashproductsConfiguration')) {
return true;
}

$tab = new Tab();
$tab->active = true;
$tab->class_name = 'AdminDashproductsConfiguration';
$tab->name = [];
foreach (Language::getLanguages(true) as $lang) {
$tab->name[$lang['id_lang']] = 'Dashproducts configuration';
}
$tab->id_parent = -1;
$tab->module = $this->name;

return $tab->add();
}

public function getContent()
{
Tools::redirectAdmin(
SymfonyContainer::getInstance()->get('router')->generate('dashproducts_configuration')
);
}

public function hookDashboardZoneTwo($params)
{
$this->context->smarty->assign(
Expand Down Expand Up @@ -90,6 +136,46 @@ public function hookDashboardData($params)
];
}

/**
* Modern counterpart of hookDashboardZoneTwo(). Tables are plain server-rendered HTML.
*/
public function hookDisplayAdminDashboardZoneTwo(array $params)
{
$data = $this->hookDashboardData($params);

return $this->render('zone_two.html.twig', [
'configUrl' => $this->getConfigUrl(),
'tables' => [
['title' => $this->trans('Recent Orders', [], 'Modules.Dashproducts.Admin'), 'table' => $data['data_table']['table_recent_orders']],
['title' => $this->trans('Best Sellers', [], 'Modules.Dashproducts.Admin'), 'table' => $data['data_table']['table_best_sellers']],
['title' => $this->trans('Most Viewed', [], 'Modules.Dashproducts.Admin'), 'table' => $data['data_table']['table_most_viewed']],
['title' => $this->trans('Top Searches', [], 'Modules.Dashproducts.Admin'), 'table' => $data['data_table']['table_top_10_most_search']],
],
]);
}

private function render(string $template, array $params = []): string
{
$twig = $this->get('twig');
if (!$twig instanceof Environment) {
return '';
}

return $twig->render('@Modules/dashproducts/views/templates/admin/' . $template, $params);
}

/**
* Null (no "Configure" link shown) when the current employee can't configure this module.
*/
private function getConfigUrl(): ?string
{
if (!Tab::checkTabRights((int) Tab::getIdFromClassName('AdminDashproductsConfiguration'))) {
return null;
}

return SymfonyContainer::getInstance()->get('router')->generate('dashproducts_configuration');
}

public function getTableRecentOrders()
{
$header = [
Expand Down Expand Up @@ -132,7 +218,7 @@ public function getTableRecentOrders()
'value' => $this->context->getCurrentLocale()->formatPrice((float) $order['total_paid_tax_excl'], $currency['iso_code']),
'class' => 'text-center',
'wrapper_start' => $order['valid'] ? '<span class="badge badge-success">' : '',
'wrapper_end' => '<span>',
'wrapper_end' => $order['valid'] ? '</span>' : '',
];
$tr[] = [
'id' => 'date_add',
Expand All @@ -148,7 +234,7 @@ public function getTableRecentOrders()
'id' => 'details',
'value' => '',
'class' => 'text-right',
'wrapper_start' => '<a class="btn btn-default" href="' . $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => (int) $order['id_order'], 'vieworder' => 1]) . '" title="' . $this->trans('Details', [], 'Modules.Dashproducts.Admin') . '"><i class="icon-search"></i>',
'wrapper_start' => '<a class="btn btn-default" href="' . $this->context->link->getAdminLink('AdminOrders', true, [], ['id_order' => (int) $order['id_order'], 'vieworder' => 1]) . '" title="' . $this->trans('Details', [], 'Modules.Dashproducts.Admin') . '"><i class="material-icons">search</i>',
'wrapper_end' => '</a>',
];

Expand Down Expand Up @@ -361,7 +447,7 @@ public function getTableMostViewed($date_from, $date_to)
$purchased = $this->getTotalProductPurchased($date_from, $date_to, (int) $product_obj->id);
$tr[] = [
'id' => 'purchased',
'value' => $this->getTotalProductPurchased($date_from, $date_to, (int) $product_obj->id),
'value' => $purchased,
'class' => 'text-center',
];
$tr[] = [
Expand Down Expand Up @@ -406,7 +492,9 @@ public function getTableTop10MostSearch($date_from, $date_to)
$tr = [];
$tr[] = [
'id' => 'product',
'value' => $term['keywords'],
// Customer-supplied search terms: escaped, unlike other columns in this
// table which are trusted server-computed numbers.
'value' => Tools::htmlentitiesUTF8($term['keywords']),
'class' => 'text-left',
];
$tr[] = [
Expand Down
77 changes: 77 additions & 0 deletions src/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* This source file is subject to the Academic Free License 3.0 (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
*
* @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 3.0 (AFL-3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\DashProducts\Controller;

use Configuration;
use PrestaShop\Module\DashProducts\Type\ConfigurationType;
// FrameworkBundleAdminController is deprecated since 9.0 in favor of PrestaShopAdminController,
// which doesn't exist on PS 8.2 — kept for the module's declared 8.2+ compatibility.
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ConfigurationController extends FrameworkBundleAdminController
{
private const FIELDS = [
'DASHPRODUCT_NBR_SHOW_LAST_ORDER',
'DASHPRODUCT_NBR_SHOW_BEST_SELLER',
'DASHPRODUCT_NBR_SHOW_MOST_VIEWED',
'DASHPRODUCT_NBR_SHOW_TOP_SEARCH',
];

/**
* @AdminSecurity("is_granted('read', request.get('_legacy_controller'))")
*/
public function indexAction(Request $request): Response
Comment thread
mattgoud marked this conversation as resolved.
Comment thread
mattgoud marked this conversation as resolved.
{
$data = [];
foreach (self::FIELDS as $field) {
$data[$field] = (int) Configuration::get($field);
}

$form = $this->createForm(ConfigurationType::class, $data);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->denyAccessUnlessGranted('update', $request->attributes->get('_legacy_controller'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not to handle in @adminsecurity ?


// No @DemoRestricted attribute here: its listener only recognizes the PHP 8
// attribute form (PrestaShopBundle\Security\Attribute\DemoRestricted), which
// doesn't exist on PS 8.2 — this module's declared minimum version.
if ($this->isDemoModeEnabled()) {
$this->addFlash('error', $this->trans('This functionality has been disabled.', 'Admin.Notifications.Error'));

return $this->redirectToRoute('dashproducts_configuration');
}

foreach ($form->getData() as $field => $value) {
Configuration::updateValue($field, (int) $value);
}
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));

return $this->redirectToRoute('dashproducts_configuration');
}

return $this->render('@Modules/dashproducts/views/templates/admin/configuration.html.twig', [
'configurationForm' => $form->createView(),
'enableSidebar' => true,
'help_link' => $this->generateSidebarLink('AdminDashproductsConfiguration'),
]);
}
}
34 changes: 34 additions & 0 deletions src/Controller/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 3.0 (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
* 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.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @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 3.0 (AFL-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;
Loading
Loading