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

Introducing reusable way to display KPIs blocks in Back Office modern pages #9242

38 changes: 36 additions & 2 deletions admin-dev/themes/new-theme/scss/components/layout/_kpi.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
.kpi-container {
position: relative;
text-decoration: none;
background: $gray-soft;
padding-top: .9375rem;
padding-bottom: .9375rem;
text-align: center;
display: block;

&:hover {
text-decoration: none;
Expand All @@ -15,6 +20,8 @@

.kpi-content {
position: relative;
text-align: left;
padding-left: 40px;

> .material-icons {
position: absolute;
Expand All @@ -24,20 +31,47 @@
color: $primary;
}

&.-color2 {
> .value,
> .material-icons {
color: $danger;
}
}

&.-color3 {
> .value,
> .material-icons {
color: $success;
}
}

> .title,
> .subtitle,
> .value {
font-size: 0.75rem;
padding-left: 3.125rem;
display: block;
padding-left: .125rem;
color: $gray-dark;
}

> .title {
display: inline-block;
}

> .subtitle {
text-transform: uppercase;
color: $gray-medium;
display: block;
}
> .value {
font-size: 1.25rem;
color: $primary;
display: block;
}
}

.container-fluid {
> .kpi-container {
padding-left: 1rem;
padding-right: 1rem;
}
}
55 changes: 55 additions & 0 deletions src/Adapter/Configuration/KpiConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-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 http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Configuration;

use ConfigurationKPI;
use PrestaShop\PrestaShop\Adapter\Configuration;

/**
* Class KpiConfiguration provides access to legacy ConfigurationKpi methods
*/
class KpiConfiguration extends Configuration
{
/**
* Changes configuration definition before calling it's methods
*
* @param $name
* @param $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
if (is_callable([$this, $name])) {
ConfigurationKPI::setKpiDefinition();
$result = call_user_func([$this, $name], $arguments);
ConfigurationKPI::unsetKpiDefinition();

return $result;
}
}
}
99 changes: 99 additions & 0 deletions src/Adapter/Kpi/EnabledLanguagesKpi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-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 http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Kpi;

use HelperKpi;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Kpi\KpiInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class EnabledLanguagesKpi is an implementation for enabled languages KPI
*/
final class EnabledLanguagesKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $legacyContext;

/**
* @param LegacyContext $legacyContext
* @param TranslatorInterface $translator
* @param ConfigurationInterface $configuration
*/
public function __construct(
LegacyContext $legacyContext,
TranslatorInterface $translator,
ConfigurationInterface $configuration
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->legacyContext = $legacyContext;
}

/**
* {@inheritdoc}
*/
public function render()
{
$enabledLanguages = $this->configuration->get('ENABLED_LANGUAGES');

$kpi = new HelperKpi();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't want to increase the scope of this PR so I'm still relying on legacy HelperKpi to render the view, but the overall kpi implementation is not dependent on legacy code.

Copy link
Contributor

Choose a reason for hiding this comment

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

you're right :) it's the main purpose of using adapters

$kpi->context->smarty->setTemplateDir(_PS_BO_ALL_THEMES_DIR_.'new-theme/template/');
$kpi->id = 'box-languages';
$kpi->icon = 'mic';
$kpi->color = 'color1';
$kpi->href = $this->legacyContext->getAdminLink('AdminLanguages');
$kpi->title = $this->translator->trans('Enabled Languages', [], 'Admin.International.Feature');

if (false !== $enabledLanguages) {
$kpi->value = $enabledLanguages;
}

$params = [
'ajax' => 1,
'action' => 'getKpi',
'kpi' => 'enabled_languages',
];
$kpi->source = $this->legacyContext->getAdminLink('AdminStats', true, $params);
$kpi->refresh = $this->configuration->get('ENABLED_LANGUAGES_EXPIRE') < time();

return $kpi->generate();
}
}
99 changes: 99 additions & 0 deletions src/Adapter/Kpi/MainCountryKpi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-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 http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Adapter\Kpi;

use HelperKpi;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Kpi\KpiInterface;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Class MainCountryKpi is an implementation for main countries KPI
*/
final class MainCountryKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $legacyContext;

/**
* @param LegacyContext $legacyContext
* @param TranslatorInterface $translator
* @param ConfigurationInterface $configuration
*/
public function __construct(
LegacyContext $legacyContext,
TranslatorInterface $translator,
ConfigurationInterface $configuration
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->legacyContext = $legacyContext;
}

/**
* {@inheritdoc}
*/
public function render()
{
$mainCountry = $this->configuration->get('MAIN_COUNTRY');

$kpi = new HelperKpi();
$kpi->context->smarty->setTemplateDir(_PS_BO_ALL_THEMES_DIR_.'new-theme/template/');
$kpi->id = 'box-country';
$kpi->icon = 'home';
$kpi->color = 'color2';
$kpi->title = $this->translator->trans('Main Country', [], 'Admin.International.Feature');
$kpi->subtitle = $this->translator->trans('30 Days', [], 'Admin.Global');

if (false !== $mainCountry) {
$kpi->value = $mainCountry;
}

$params = [
'ajax' => 1,
'action' => 'getKpi',
'kpi' => 'main_country',
];
$kpi->source = $this->legacyContext->getAdminLink('AdminStats', true, $params);
Copy link
Contributor

Choose a reason for hiding this comment

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

if you only need legacy context to render an url, how about making this url an argument of your constructor instead? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yes, good idea :) no need to inject the whole context

Copy link
Contributor

Choose a reason for hiding this comment

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

my secret (not anymore!) challenge is to remove LegacyContext at some point, so when I can remove it from a class I'm happy 💃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hehe now everyone knows! 🙈

$kpi->refresh = $this->configuration->get('MAIN_COUNTRY_EXPIRE') < time();

return $kpi->generate();
}
}