custom/plugins/EwCustomRegister/src/EwCustomRegister.php line 13

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ew\CustomRegister;
  3. use Doctrine\DBAL\DBALException;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. class EwCustomRegister extends Plugin
  11. {
  12.     protected const BODY_WEIGHT 'bodyWeight';
  13.     protected const BODY_DESIRED_WEIGHT 'bodyDesiredWeight';
  14.     protected const BODY_HEIGHT 'bodyHeight';
  15.     protected const GENDER_TYPE 'genderType';
  16.     protected const BIRTHDAY 'birthDay';
  17.     protected const HOUSE_NUMBER 'houseNumber';
  18.     /**
  19.      * @param InstallContext $context
  20.      * @throws DBALException
  21.      */
  22.     public function install(InstallContext $context) : void
  23.     {
  24.         parent::install($context);
  25.         $customerFieldsRepository $this->container->get('custom_field_set.repository');
  26.         $this->addCustomFieldsToCustomer($context$customerFieldsRepository);
  27.     }
  28.     /**
  29.      * @return array[]
  30.      */
  31.     private function getCustomFields(): array
  32.     {
  33.         return [
  34.             [
  35.                 'name' => self::BODY_WEIGHT,
  36.                 'type' => CustomFieldTypes::FLOAT,
  37.                 'config' => [
  38.                     'type' => 'text',
  39.                     'label' => [
  40.                         'de-DE' => 'Body weight',
  41.                         'en-GB' => 'Body weight'
  42.                     ],
  43.                     'customFieldType' => 'text',
  44.                     'customFieldPosition' => 1
  45.                 ]
  46.             ],
  47.             [
  48.                 'name' => self::BODY_HEIGHT,
  49.                 'type' => CustomFieldTypes::FLOAT,
  50.                 'config' => [
  51.                     'type' => 'text',
  52.                     'label' => [
  53.                         'de-DE' => 'Body height',
  54.                         'en-GB' => 'Body height'
  55.                     ],
  56.                     'customFieldType' => 'text',
  57.                     'customFieldPosition' => 2
  58.                 ]
  59.             ],
  60.             [
  61.                 'name' => self::GENDER_TYPE,
  62.                 'type' => CustomFieldTypes::TEXT,
  63.                 'config' => [
  64.                     'type' => 'text',
  65.                     'label' => [
  66.                         'de-DE' => 'Gender type',
  67.                         'en-GB' => 'Gender type'
  68.                     ],
  69.                     'customFieldType' => 'text',
  70.                     'customFieldPosition' => 3
  71.                 ]
  72.             ],
  73.             [
  74.                 'name' => self::BIRTHDAY,
  75.                 'type' => CustomFieldTypes::DATETIME,
  76.                 'config' => [
  77.                     'type' => 'text',
  78.                     'label' => [
  79.                         'de-DE' => 'Birthday',
  80.                         'en-GB' => 'Birthday'
  81.                     ],
  82.                     'customFieldType' => 'text',
  83.                     'customFieldPosition' => 4
  84.                 ]
  85.             ],
  86.             [
  87.                 'name' => self::BODY_DESIRED_WEIGHT,
  88.                 'type' => CustomFieldTypes::FLOAT,
  89.                 'config' => [
  90.                     'type' => 'text',
  91.                     'label' => [
  92.                         'de-DE' => 'Desired weight',
  93.                         'en-GB' => 'Desired weight'
  94.                     ],
  95.                     'customFieldType' => 'text',
  96.                     'customFieldPosition' => 5
  97.                 ]
  98.             ],
  99.         ];
  100.     }
  101.     private function getCustomFieldsAddress(): array
  102.     {
  103.         return [
  104.             [
  105.                 'name' => self::HOUSE_NUMBER,
  106.                 'type' => CustomFieldTypes::TEXT,
  107.                 'config' => [
  108.                     'type' => 'text',
  109.                     'label' => [
  110.                         'de-DE' => 'Hausnummer',
  111.                         'en-GB' => 'House No.'
  112.                     ],
  113.                     'customFieldType' => 'text',
  114.                     'customFieldPosition' => 5
  115.                 ]
  116.             ],
  117.         ];
  118.     }
  119.     /**
  120.      * @throws DBALException
  121.      */
  122.     private function addCustomFieldsToCustomer($context, ?object $customFieldSetRepository): void
  123.     {
  124.         $this->uninstallCustomFieldset($context->getContext());
  125.         $customFieldSetRepository->create([[
  126.             'name' => 'ew-custom-register',
  127.             'position' => 0,
  128.             'global' => true,
  129.             'active' => true,
  130.             'config' => [
  131.                 'label' => [
  132.                     'en-GB' => 'EW Customer Fields',
  133.                     'de-DE' => 'EW Customer Fields'
  134.                 ],
  135.             ],
  136.             'customFields' => $this->getCustomFields(),
  137.             'relations' => [
  138.                 [
  139.                     'entityName' => 'customer'
  140.                 ]
  141.             ]
  142.         ]], $context->getContext());
  143.         $customFieldSetRepository->create([[
  144.             'name' => 'ew-custom-register-address',
  145.             'position' => 0,
  146.             'global' => true,
  147.             'active' => true,
  148.             'config' => [
  149.                 'label' => [
  150.                     'en-GB' => 'EW Customer Address Fields',
  151.                     'de-DE' => 'EW Customer Address Fields'
  152.                 ],
  153.             ],
  154.             'customFields' => $this->getCustomFieldsAddress(),
  155.             'relations' => [
  156.                 [
  157.                     'entityName' => 'customer_address'
  158.                 ]
  159.             ]
  160.         ]], $context->getContext());
  161.     }
  162.     public function uninstall(UninstallContext $uninstallContext): void
  163.     {
  164.         if ($uninstallContext->keepUserData()) {
  165.             parent::uninstall($uninstallContext);
  166.             return;
  167.         }
  168.         $this->uninstallCustomFieldset($uninstallContext->getContext());
  169.     }
  170.     private function uninstallCustomFieldset($context){
  171.         $cfsRepo $this->container->get('custom_field_set.repository');
  172.         $cfRepo $this->container->get('custom_field.repository');
  173.         foreach (['ew-custom-register''ew-custom-register-address'] as $value) {
  174.             //delete custom_field_set entry
  175.             $cfsId $cfsRepo->search((new Criteria())
  176.                 ->addFilter(new EqualsFilter('name'$value)), $context
  177.             )->first();
  178.             if ($cfsId) {
  179.                 $cfsRepo->delete([
  180.                     ['id' => $cfsId->getId()]
  181.                 ], $context);
  182.                 //delete custom_field entries
  183.                 $cfIds $cfRepo->search((new Criteria())
  184.                     ->addFilter(
  185.                         new EqualsFilter('customFieldSetId'$cfsId->getId())
  186.                     ), $context)->getIds();
  187.                 $ids = [];
  188.                 foreach ($cfIds as $id) {
  189.                     $ids[] = ['id' => $id];
  190.                 }
  191.                 $cfRepo->delete($ids$context);
  192.             }
  193.         }
  194.     }
  195. }