custom/plugins/SwagSocialShopping/src/SwagSocialShopping.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. namespace SwagSocialShopping;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. use SwagSocialShopping\Installer\CustomFieldInstaller;
  18. use SwagSocialShopping\Installer\SalesChannelInstaller;
  19. class SwagSocialShopping extends Plugin
  20. {
  21.     public const SALES_CHANNEL_TYPE_SOCIAL_SHOPPING '9ce0868f406d47d98cfe4b281e62f098';
  22.     public const SOCIAL_SHOPPING_SALES_CHANNEL_WRITTEN_EVENT 'swag_social_shopping_sales_channel.written';
  23.     private const SWAG_SOCIAL_SHOPPING_SALES_CHANNEL_PRIVILEGE_KEY 'swag_social_shopping_sales_channel:';
  24.     private const SWAG_SOCIAL_SHOPPING_PRODUCT_ERROR_PRIVILEGE_KEY 'swag_social_shopping_product_error:';
  25.     public function activate(ActivateContext $activateContext): void
  26.     {
  27.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  28.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  29.         (new CustomFieldInstaller($customFieldSetRepository))->activate($activateContext);
  30.         (new SalesChannelInstaller($this->container))->activate($activateContext);
  31.         $this->addCustomPrivileges();
  32.         parent::activate($activateContext);
  33.     }
  34.     public function deactivate(DeactivateContext $deactivateContext): void
  35.     {
  36.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  37.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  38.         (new CustomFieldInstaller($customFieldSetRepository))->deactivate($deactivateContext);
  39.         (new SalesChannelInstaller($this->container))->deactivate($deactivateContext);
  40.         $this->removeCustomPrivileges();
  41.         parent::deactivate($deactivateContext);
  42.     }
  43.     public function install(InstallContext $installContext): void
  44.     {
  45.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  46.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  47.         (new CustomFieldInstaller($customFieldSetRepository))->install($installContext);
  48.         (new SalesChannelInstaller($this->container))->install($installContext);
  49.         parent::install($installContext);
  50.     }
  51.     public function uninstall(UninstallContext $uninstallContext): void
  52.     {
  53.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  54.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  55.         (new CustomFieldInstaller($customFieldSetRepository))->uninstall($uninstallContext);
  56.         (new SalesChannelInstaller($this->container))->uninstall($uninstallContext);
  57.         parent::uninstall($uninstallContext);
  58.     }
  59.     public function update(UpdateContext $updateContext): void
  60.     {
  61.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  62.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  63.         (new CustomFieldInstaller($customFieldSetRepository))->update($updateContext);
  64.         (new SalesChannelInstaller($this->container))->update($updateContext);
  65.         $this->addCustomPrivileges();
  66.         parent::update($updateContext);
  67.     }
  68.     private function addCustomPrivileges(): void
  69.     {
  70.         if (!\method_exists($this'addPrivileges')) {
  71.             return;
  72.         }
  73.         foreach ($this->getCustomPrivileges() as $role => $privileges) {
  74.             $this->addPrivileges($role$privileges);
  75.         }
  76.     }
  77.     private function removeCustomPrivileges(): void
  78.     {
  79.         if (!\method_exists($this'removePrivileges')) {
  80.             return;
  81.         }
  82.         $privilegesToRemove = [];
  83.         foreach ($this->getCustomPrivileges() as $privileges) {
  84.             foreach ($privileges as $privilege) {
  85.                 if (\mb_strpos($privilegeself::SWAG_SOCIAL_SHOPPING_PRODUCT_ERROR_PRIVILEGE_KEY) !== 0
  86.                     && \mb_strpos($privilegeself::SWAG_SOCIAL_SHOPPING_SALES_CHANNEL_PRIVILEGE_KEY) !== 0
  87.                 ) {
  88.                     continue;
  89.                 }
  90.                 $privilegesToRemove[] = $privilege;
  91.             }
  92.         }
  93.         $this->removePrivileges($privilegesToRemove);
  94.     }
  95.     private function getCustomPrivileges(): array
  96.     {
  97.         return [
  98.             'sales_channel.viewer' => [
  99.                 self::SWAG_SOCIAL_SHOPPING_SALES_CHANNEL_PRIVILEGE_KEY 'read',
  100.                 self::SWAG_SOCIAL_SHOPPING_PRODUCT_ERROR_PRIVILEGE_KEY 'read',
  101.                 'seo_url:read',
  102.                 'product_visibility:read',
  103.             ],
  104.             'sales_channel.editor' => [
  105.                 self::SWAG_SOCIAL_SHOPPING_SALES_CHANNEL_PRIVILEGE_KEY 'update',
  106.             ],
  107.             'sales_channel.creator' => [
  108.                 self::SWAG_SOCIAL_SHOPPING_SALES_CHANNEL_PRIVILEGE_KEY 'create',
  109.             ],
  110.         ];
  111.     }
  112. }