local/modules/HookAdminHome/Hook/AdminHook.php line 51

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace HookAdminHome\Hook;
  12. use HookAdminHome\HookAdminHome;
  13. use Symfony\Component\Cache\Adapter\AdapterInterface;
  14. use Thelia\Core\Event\Hook\HookRenderBlockEvent;
  15. use Thelia\Core\Event\Hook\HookRenderEvent;
  16. use Thelia\Core\Hook\BaseHook;
  17. /**
  18.  * Class AdminHook.
  19.  *
  20.  * @author Gilles Bourgeat <gilles@thelia.net>
  21.  */
  22. class AdminHook extends BaseHook
  23. {
  24.     protected $theliaCache null;
  25.     public function __construct(AdapterInterface $theliaCache null)
  26.     {
  27.         $this->theliaCache $theliaCache;
  28.     }
  29.     public function blockStatistics(HookRenderEvent $event): void
  30.     {
  31.         if (== HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_STATS1)) {
  32.             $event->add($this->render('block-statistics.html'));
  33.         }
  34.         $event->add($this->render('hook-admin-home-config.html'));
  35.     }
  36.     public function blockStatisticsJs(HookRenderEvent $event): void
  37.     {
  38.         if (== HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_STATS1)) {
  39.             $event->add($this->render('block-statistics-js.html'));
  40.         }
  41.     }
  42.     public function blockSalesStatistics(HookRenderBlockEvent $event): void
  43.     {
  44.         if (== HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_SALES1)) {
  45.             $content trim($this->render('block-sales-statistics.html'));
  46.             if (!empty($content)) {
  47.                 $event->add([
  48.                     'id' => 'block-sales-statistics',
  49.                     'title' => $this->trans('Sales statistics', [], HookAdminHome::DOMAIN_NAME),
  50.                     'content' => $content,
  51.                 ]);
  52.             }
  53.         }
  54.     }
  55.     public function blockNews(HookRenderBlockEvent $event): void
  56.     {
  57.         if (== HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_NEWS1)) {
  58.             $content trim($this->render('block-news.html'));
  59.             if (!empty($content)) {
  60.                 $event->add([
  61.                     'id' => 'block-news',
  62.                     'title' => $this->trans('Thelia Github activity', [], HookAdminHome::DOMAIN_NAME),
  63.                     'content' => $content,
  64.                 ]);
  65.             }
  66.         }
  67.     }
  68.     public function blockTheliaInformation(HookRenderBlockEvent $event): void
  69.     {
  70.         $releases $this->getGithubReleases();
  71.         if (== HookAdminHome::getConfigValue(HookAdminHome::ACTIVATE_INFO1)) {
  72.             $content trim(
  73.                 $this->render(
  74.                     'block-thelia-information.html',
  75.                     [
  76.                         'latestStableRelease' => $releases['latestStableRelease'],
  77.                         'latestPreRelease' => $releases['latestPreRelease'],
  78.                     ]
  79.                 )
  80.             );
  81.             if (!empty($content)) {
  82.                 $event->add([
  83.                     'id' => 'block-thelia-information',
  84.                     'title' => $this->trans('Thelia news', [], HookAdminHome::DOMAIN_NAME),
  85.                     'content' => $content,
  86.                 ]);
  87.             }
  88.         }
  89.     }
  90.     private function getGithubReleases(): array
  91.     {
  92.         $cachedReleases $this->theliaCache->getItem('thelia_github_releases');
  93.         if (!$cachedReleases->isHit()) {
  94.             try {
  95.                 $resource curl_init();
  96.                 curl_setopt($resource\CURLOPT_URL'https://api.github.com/repos/thelia/thelia/releases');
  97.                 curl_setopt($resource\CURLOPT_RETURNTRANSFER1);
  98.                 curl_setopt($resource\CURLOPT_HTTPHEADER, ['accept: application/vnd.github.v3+json']);
  99.                 curl_setopt($resource\CURLOPT_USERAGENT'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  100.                 $results curl_exec($resource);
  101.                 curl_close($resource);
  102.                 $theliaReleases json_decode($resultstrue);
  103.                 $publishedAtSort = function ($a$b) {return (new \DateTime($a['published_at'])) < (new \DateTime($b['published_at'])); };
  104.                 $stableReleases array_filter($theliaReleases, function ($theliaRelease) { return !$theliaRelease['prerelease']; });
  105.                 usort($stableReleases$publishedAtSort);
  106.                 $latestStableRelease $stableReleases[0] ?? null;
  107.                 $preReleases array_filter($theliaReleases, function ($theliaRelease) { return $theliaRelease['prerelease']; });
  108.                 usort($preReleases$publishedAtSort);
  109.                 $latestPreRelease $preReleases[0] ?? null;
  110.                 // Don't display pre-release if they are < than stable release
  111.                 if (version_compare($latestPreRelease['tag_name'], $latestStableRelease['tag_name'], '<')) {
  112.                     $latestPreRelease null;
  113.                 }
  114.             } catch (\Exception $exception) {
  115.                 $latestPreRelease null;
  116.                 $latestStableRelease null;
  117.             }
  118.             $cachedReleases->expiresAfter(3600);
  119.             $cachedReleases->set(
  120.                 [
  121.                     'latestStableRelease' => $latestStableRelease,
  122.                     'latestPreRelease' => $latestPreRelease,
  123.                 ]
  124.             );
  125.             $this->theliaCache->save($cachedReleases);
  126.         }
  127.         return $cachedReleases->get();
  128.     }
  129. }