local/modules/VirtualProductControl/Hook/VirtualProductHook.php line 40

  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 VirtualProductControl\Hook;
  12. use Thelia\Core\Event\Hook\HookRenderEvent;
  13. use Thelia\Core\Hook\BaseHook;
  14. use Thelia\Core\Security\AccessManager;
  15. use Thelia\Core\Security\Resource\AdminResources;
  16. use Thelia\Core\Security\SecurityContext;
  17. use Thelia\Model\ModuleQuery;
  18. use Thelia\Model\ProductQuery;
  19. /**
  20.  * Class VirtualProductHook.
  21.  *
  22.  * @author Manuel Raynaud <manu@raynaud.io>
  23.  */
  24. class VirtualProductHook extends BaseHook
  25. {
  26.     /**
  27.      * @var SecurityContext
  28.      */
  29.     protected $securityContext;
  30.     public function __construct(SecurityContext $securityContext)
  31.     {
  32.         $this->securityContext $securityContext;
  33.     }
  34.     public function onMainBeforeContent(HookRenderEvent $event): void
  35.     {
  36.         if ($this->securityContext->isGranted(
  37.             ['ADMIN'],
  38.             [AdminResources::PRODUCT],
  39.             [],
  40.             [AccessManager::VIEW]
  41.         )) {
  42.             $products ProductQuery::create()
  43.                 ->filterByVirtual(1)
  44.                 ->filterByVisible(1)
  45.                 ->count();
  46.             if ($products 0) {
  47.                 $deliveryModule ModuleQuery::create()->retrieveVirtualProductDelivery();
  48.                 if (false === $deliveryModule) {
  49.                     $event->add($this->render('virtual-delivery-warning.html'));
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }