custom/plugins/CoeRechnungsMailSw6/src/CoeRechnungsMailSw6.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeRechnungsMailSw6;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. /**
  12.  * Class CoeRechnungsMailSw6
  13.  * @package CoeRechnungsMailSw6
  14.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  15.  */
  16. class CoeRechnungsMailSw6 extends Plugin
  17. {
  18.     public static $field_set_name "coe_rechnungsmail";
  19.     public static $field_name_mail "coe_rechnungsmail_mail";
  20.     public static $field_name_active "coe_rechnungsmail_active";
  21.     /**
  22.      * @param InstallContext $context
  23.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  24.      */
  25.     public function install(InstallContext $context): void{
  26.         parent::install($context);
  27.         if(!$this->hasCustomField($context->getContext())){
  28.             $this->createCustomField($context->getContext());
  29.         }
  30.     }
  31.     /**
  32.      * @param UninstallContext $context
  33.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  34.      */
  35.     public function uninstall(UninstallContext $context): void{
  36.         if($context->keepUserData()){
  37.             return;
  38.         }
  39.         parent::uninstall($context);
  40.         if($this->hasCustomField($context->getContext())){
  41.             $this->deleteCustomField($context->getContext());
  42.         }
  43.     }
  44.     /**
  45.      * @param Context $context
  46.      * @return bool
  47.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  48.      */
  49.     private function hasCustomField(Context $context){
  50.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  51.         $customFieldSetRepository $this->container->get("custom_field_set.repository");
  52.         $criteria = new Criteria();
  53.         $criteria->addFilter(new EqualsFilter('name'self::$field_set_name));
  54.         /** @var IdSearchResult $result */
  55.         $result $customFieldSetRepository->searchIds($criteria$context);
  56.         return $result->getTotal() > 0;
  57.     }
  58.     /**
  59.      * @param Context $context
  60.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  61.      */
  62.     private function createCustomField(Context $context){
  63.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  64.         $customFieldSetRepository $this->container->get("custom_field_set.repository");
  65.         $customFieldSetRepository->create([
  66.             [
  67.                 'name' => self::$field_set_name,
  68.                 'global' => true,
  69.                 'config' => [
  70.                     'label' => [
  71.                         'en-GB' => 'Alternative email address for documents',
  72.                         'de-DE' => 'Alternative E-Mail Adresse für Dokumente'
  73.                     ]
  74.                 ],
  75.                 'customFields' => [
  76.                     [
  77.                         'name' => self::$field_name_mail,
  78.                         'type' => 'text',
  79.                         'config' => [
  80.                             'type' => 'text',
  81.                             'label' => [
  82.                                 'en-GB' => 'Email address',
  83.                                 'de-DE' => 'E-Mail Addresse'
  84.                             ],
  85.                             'customFieldPosition' => 1
  86.                         ]
  87.                     ],
  88.                     [
  89.                         'name' => self::$field_name_active,
  90.                         'type' => 'bool',
  91.                         'config' => [
  92.                             'type' => 'checkbox',
  93.                             'label' => [
  94.                                 'en-GB' => 'Is active',
  95.                                 'de-DE' => 'Ist aktiv'
  96.                             ],
  97.                             'customFieldPosition' => 2
  98.                         ]
  99.                     ]
  100.                 ],
  101.                 'relations' => [
  102.                     [
  103.                         "entityName" => "customer"
  104.                     ]
  105.                 ]
  106.             ]
  107.         ], $context);
  108.     }
  109.     /**
  110.      * @param Context $context
  111.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  112.      */
  113.     private function deleteCustomField(Context $context){
  114.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  115.         $customFieldSetRepository $this->container->get("custom_field_set.repository");
  116.         $criteria = new Criteria();
  117.         $criteria->addFilter(new EqualsFilter('name'self::$field_set_name));
  118.         $result $customFieldSetRepository->searchIds($criteria$context);
  119.         $customFieldSetRepository->delete([
  120.             [
  121.                 'id' => $result->firstId(),
  122.             ],
  123.         ], $context);
  124.     }
  125. }