custom/plugins/TdsMerware/src/TdsMerware.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tds\Merware;
  3. use League\Flysystem\FilesystemInterface;
  4. use Shopware\Core\Content\Category\CategoryDefinition;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Tds\Merware\Service\Core\FileService;
  15. /**
  16.  * Class TdsMerware
  17.  *
  18.  * @package Tds\Merware
  19.  */
  20. class TdsMerware extends Plugin
  21. {
  22.     /**
  23.      * @var FilesystemInterface
  24.      */
  25.     private $fileSystem;
  26.     /**
  27.      * {@inheritDoc}
  28.      */
  29.     public function install(InstallContext $installContext): void
  30.     {
  31.         /**
  32.          * @var EntityRepositoryInterface $customFieldSetRepository
  33.          */
  34.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  35.         $customFieldSetRepository->create($this->getCustomFieldSets(), $installContext->getContext());
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      *
  40.      * @param UninstallContext $uninstallContext
  41.      *
  42.      * @throws InconsistentCriteriaIdsException
  43.      */
  44.     public function uninstall(UninstallContext $uninstallContext): void
  45.     {
  46.         $this->deleteCustomFieldSet($this->getCustomFieldSets(), $uninstallContext->getContext());
  47.         $this->deleteCustomFields($this->getCustomFieldSets(), $uninstallContext->getContext());
  48.     }
  49.     /**
  50.      * Helper-function to trigger the deletion of plugin custom field sets
  51.      *
  52.      * @param $customFieldSets
  53.      * @param $context
  54.      * @throws InconsistentCriteriaIdsException
  55.      */
  56.     private function deleteCustomFieldSet($customFieldSets$context): void
  57.     {
  58.         /**
  59.          * @var EntityRepositoryInterface $customFieldSetRepository
  60.          */
  61.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  62.         $ids $this->getCustomFieldSetIds($customFieldSets$context);
  63.         if (!empty($ids)) {
  64.             $customFieldSetRepository->delete($ids$context);
  65.         }
  66.     }
  67.     /**
  68.      * Helper-function to get custom field set ids
  69.      *
  70.      * @param $customFieldSets
  71.      * @param $context
  72.      *
  73.      * @return array
  74.      *
  75.      * @throws InconsistentCriteriaIdsException
  76.      */
  77.     private function getCustomFieldSetIds($customFieldSets$context)
  78.     {
  79.         /**
  80.          * @var EntityRepositoryInterface $customFieldSetRepository
  81.          */
  82.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  83.         $ids = [];
  84.         foreach ($customFieldSets as $customFieldSet) {
  85.             $criteria = new Criteria();
  86.             $criteria->addFilter(
  87.                 new EqualsFilter('name'$customFieldSet['name'])
  88.             );
  89.             $customFieldSetId $customFieldSetRepository->searchIds($criteria$context)->firstId();
  90.             if (!empty($customFieldSetId)) {
  91.                 $ids[] = [
  92.                     'id' => $customFieldSetId
  93.                 ];
  94.             }
  95.         }
  96.         return $ids;
  97.     }
  98.     /**
  99.      * Helper-function to delete custom fields
  100.      *
  101.      * @param $customFieldSets
  102.      * @param $context
  103.      *
  104.      * @throws InconsistentCriteriaIdsException
  105.      */
  106.     private function deleteCustomFields($customFieldSets$context): void
  107.     {
  108.         /**
  109.          * @var EntityRepositoryInterface $customFieldRepository
  110.          */
  111.         $customFieldRepository $this->container->get('custom_field.repository');
  112.         $ids $this->getCustomFieldIds($customFieldSets$context);
  113.         if (!empty($ids)) {
  114.             $customFieldRepository->delete($ids$context);
  115.         }
  116.     }
  117.     /**
  118.      * Helper-function to get a list of custom field ids
  119.      *
  120.      * @param $customFieldSets
  121.      * @param $context
  122.      *
  123.      * @return array
  124.      *
  125.      * @throws InconsistentCriteriaIdsException
  126.      */
  127.     private function getCustomFieldIds($customFieldSets$context)
  128.     {
  129.         /**
  130.          * @var EntityRepositoryInterface $customFieldRepository
  131.          */
  132.         $customFieldRepository $this->container->get('custom_field.repository');
  133.         $ids = [];
  134.         foreach ($customFieldSets as $customFieldSet) {
  135.             if (!empty($customFieldSet['customFields'])) {
  136.                 foreach ($customFieldSet['customFields'] as $customField) {
  137.                     $criteria = new Criteria();
  138.                     $criteria->addFilter(
  139.                         new EqualsFilter('name'$customField['name'])
  140.                     );
  141.                     $customFieldId $customFieldRepository->searchIds($criteria$context)->firstId();
  142.                     if (!empty($customFieldId)) {
  143.                         $ids[] = [
  144.                             'id' => $customFieldId
  145.                         ];
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         return $ids;
  151.     }
  152.     /**
  153.      * Helper-function to get a list of plugin-required custom field data
  154.      *
  155.      * @return array
  156.      */
  157.     private function getCustomFieldSets()
  158.     {
  159.         return [
  160.             [
  161.                 'name' => 'tds_merware_category',
  162.                 'config' => [
  163.                     'label' => [
  164.                         'de-DE' => 'MerWare Kategorien',
  165.                         'en-GB' => 'MerWare categories',
  166.                     ],
  167.                 ],
  168.                 'customFields' => [
  169.                     [
  170.                         'name' => 'tds_merware_category_id',
  171.                         'type' => CustomFieldTypes::TEXT,
  172.                         'config' => [
  173.                             'customFieldPosition' => 1,
  174.                             'label' => [
  175.                                 'de-DE' => 'Interne ID',
  176.                                 'en-GB' => 'Internal ID',
  177.                             ],
  178.                         ],
  179.                     ]
  180.                 ],
  181.                 'relations' => [
  182.                     [
  183.                         'entityName' => $this->container->get(CategoryDefinition::class)->getEntityName()
  184.                     ]
  185.                 ]
  186.             ]
  187.         ];
  188.     }
  189.     /**
  190.      * {@inheritDoc}
  191.      *
  192.      * @param ActivateContext $activateContext
  193.      */
  194.     public function activate(ActivateContext $activateContext): void
  195.     {
  196.         parent::activate($activateContext);
  197.         $defaultDirectories FileService::getDefaultDirectories();
  198.         $this->createPluginDirectories($defaultDirectories);
  199.     }
  200.     /**
  201.      * Helper-function to create plugin directories
  202.      *
  203.      * @param array $directories
  204.      */
  205.     private function createPluginDirectories($directories = [])
  206.     {
  207.         foreach ($directories as $directory) {
  208.             if (!$this->fileSystem->has($directory)) {
  209.                 $this->fileSystem->createDir($directory);
  210.             }
  211.         }
  212.     }
  213.     /**
  214.      * Custom setter to set the required file system
  215.      *
  216.      * @param FilesystemInterface $fileSystemInterface
  217.      */
  218.     public function setFileSystem(FilesystemInterface $fileSystemInterface): void
  219.     {
  220.         $this->fileSystem $fileSystemInterface;
  221.     }
  222. }