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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neon\Unitarticles;
  3. use Doctrine\DBAL\Connection;
  4. use Neon\Unitarticles\Services\CustomFieldFactory;
  5. use Neon\Unitarticles\UnitarticleSetting\UnitarticleSettingDefinition;
  6. use Shopware\Core\Content\Product\ProductDefinition;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\System\CustomField\CustomFieldTypes;
  10. class NeonPlatformUnitarticles extends Plugin
  11. {
  12.     const PAYLOAD_KEY 'neon_unitarticles';
  13.     const PAYLOAD_KEY_DIMENSION 'dimensions';
  14.     const PAYLOAD_KEY_ISQUANTITYHIDDEN 'isQuantityHidden';
  15.     const PAYLOAD_KEY_UNITLABEL 'unitlabel';
  16.     const CUSTOMFIELD_ISUNITARTICLE 'neon_unitarticles_isunitarticle';
  17.     const CUSTOMFIELD_BASEPRICEVALUE 'neon_unitarticles_basepricevalue';
  18.     const CUSTOMFIELD_HIDEQUANTITYSELECTION 'neon_unitarticles_hidequantityselection';
  19.     const CUSTOMFIELD_ISPRICECALCULATIONDISABLED 'neon_unitarticles_ispricecalculationdisabled';
  20.     const CUSTOMFIELD_DESCRIPTION 'neon_unitarticles_description1';
  21.     const CUSTOMFIELD_UNITLABEL 'neon_unitarticles_unitlabel1';
  22.     const CUSTOMFIELD_STEPZISE 'neon_unitarticles_stepsize1';
  23.     const CUSTOMFIELD_MINVALUE 'neon_unitarticles_minvalue1';
  24.     const CUSTOMFIELD_MAXVALUE 'neon_unitarticles_maxvalue1';
  25.     const CUSTOMFIELD_DEFAULTVALUE 'neon_unitarticles_defaultvalue1';
  26.     const CUSTOMFIELD_EXCLUDEDVALUES 'neon_unitarticles_excludedvalues1';
  27.     const CUSTOMFIELD_HASDIMENSION2 'neon_unitarticles_hasdimension2';
  28.     const CUSTOMFIELD_DESCRIPTION2 'neon_unitarticles_description2';
  29.     const CUSTOMFIELD_STEPZISE2 'neon_unitarticles_stepsize2';
  30.     const CUSTOMFIELD_MINVALUE2 'neon_unitarticles_minvalue2';
  31.     const CUSTOMFIELD_MAXVALUE2 'neon_unitarticles_maxvalue2';
  32.     const CUSTOMFIELD_DEFAULTVALUE2 'neon_unitarticles_defaultvalue2';
  33.     const CUSTOMFIELD_EXCLUDEDVALUES2 'neon_unitarticles_excludedvalues2';
  34.     const CUSTOMFIELD_PRICECALCULATION 'neon_unitarticles_pricecalculation';
  35.     const CUSTOMFIELD_PRICECALCULATION_CUSTOM 'neon_unitarticles_pricecalculation_custom';
  36.     const CUSTOMFIELD_BASEPRICEVALUELABEL 'neon_unitarticles_basepricevaluelabel';
  37.     const CUSTOMFIELD_SURCHARGE1VALUE 'neon_unitarticles_surcharge1value';
  38.     const CUSTOMFIELD_SURCHARGE1LABEL 'neon_unitarticles_surcharge1label';
  39.     const CUSTOMFIELD_SURCHARGE2VALUE 'neon_unitarticles_surcharge2value';
  40.     const CUSTOMFIELD_SURCHARGE2LABEL 'neon_unitarticles_surcharge2label';
  41.     const CUSTOMFIELD_HIDEREFERENCEPRICE 'neon_unitarticles_hidereferenceprice';
  42.     const CUSTOMFIELD_CALCULATEDPRICEFACTOR 'neon_unitarticles_calculated_price_factor';
  43.     const CUSTOMFIELD_PRICECALCULATION_DEFAULTVALUE '{unitvalue1}';
  44.     const PLUGIN_NAME 'NeonPlatformUnitarticles';
  45.     const PAYLOAD_KEY_SURCHARGES 'surcharges';
  46.     const PAYLOAD_KEY_ISMERGEUSERVALUES 'isMergeUserValues';
  47.     const PARAM_UNITVALUE 'neonUnitarticleUnitValue';
  48.     const UNITARTICLE_SETTING_UPDATE_PRIVILEGE 'neon_unitarticles_setting:update';
  49.     const UNITARTICLE_SETTING_READ_PRIVILEGE 'neon_unitarticles_setting:read';
  50.     const UNITARTICLE_SETTING_DELETE_PRIVILEGE 'neon_unitarticles_setting:delete';
  51.     const UNITARTICLE_SETTING_CREATE_PRIVILEGE 'neon_unitarticles_setting:create';
  52.     const CUSTOMFIELD_MERGEUSERVALUES 'neon_unitarticles_mergeuservalues';
  53.     public function activate(Plugin\Context\ActivateContext  $activateContext): void
  54.     {
  55.         parent::activate($activateContext);
  56.         $this->addCustomPrivileges();
  57.     }
  58.     public function deactivate(Plugin\Context\DeactivateContext  $deactivateContext): void
  59.     {
  60.         parent::deactivate($deactivateContext);
  61.         $this->removeCustomPrivileges();
  62.     }
  63.     public function install(Plugin\Context\InstallContext $context): void
  64.     {
  65.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  66.         $this->installCustomFieldsForProduct($context$customFieldSetRepository);
  67.     }
  68.     /**
  69.      * @param Plugin\Context\UninstallContext $context
  70.      * @throws \Doctrine\DBAL\DBALException
  71.      */
  72.     public function uninstall(Plugin\Context\UninstallContext $context): void
  73.     {
  74.         if (!$context->keepUserData()) {
  75.             /** @var Connection $connection */
  76.             $connection $this->container->get('Doctrine\DBAL\Connection');
  77.             $this->uninstallCustomFieldset($connection);
  78.             $this->uninstallSnippets($connection);
  79.             $this->removeTables($connection);
  80.         }
  81.     }
  82.     public function update(Plugin\Context\UpdateContext $context): void
  83.     {
  84.         parent::update($context);
  85.         $this->addCustomPrivileges();
  86.     }
  87.     /**
  88.      * @param Plugin\Context\InstallContext $context
  89.      * @param object|null $customFieldSetRepository
  90.      */
  91.     private function installCustomFieldsForProduct(Plugin\Context\InstallContext $context, ?object $customFieldSetRepository): void
  92.     {
  93.         /** @var Connection $connection */
  94.         $connection $this->container->get('Doctrine\DBAL\Connection');
  95.         $this->uninstallCustomFieldset($connection);
  96.         $this->uninstallSnippets($connection);
  97.         $customFieldSetRepository->create([[
  98.             'name' => 'neon_unitarticles',
  99.             'active' => true,
  100.             'config' => [
  101.                 'label' => [
  102.                     'en-GB' => 'Settings unit article',
  103.                     'de-DE' => 'Konfiguration Einheitenartikel'
  104.                 ],
  105.             ],
  106.             'customFields' => $this->getCustomFields(),
  107.             'relations' => [
  108.                 [
  109.                     'entityName' => 'product'
  110.                 ]
  111.             ]
  112.         ]], $context->getContext());
  113.     }
  114.     /**
  115.      * @param Connection $connection
  116.      * @throws \Doctrine\DBAL\DBALException
  117.      */
  118.     private function uninstallCustomFieldset($connection): void
  119.     {
  120.         $query = new RetryableQuery(
  121.             $connection->prepare("DELETE FROM custom_field_set WHERE `name` = :name;")
  122.         );
  123.         $query->execute([
  124.             'name' => 'neon_unitarticles'
  125.         ]);
  126.         $query->execute([
  127.             'name' => 'neon_unitarticles_additional_dimensions'
  128.         ]);
  129.         //todo: delete product_translation entries
  130.     }
  131.     /**
  132.      * @param Connection $connection
  133.      * @throws \Doctrine\DBAL\DBALException
  134.      */
  135.     private function uninstallSnippets($connection)
  136.     {
  137.         $query = new RetryableQuery(
  138.             $connection->prepare("DELETE FROM snippet WHERE `translation_key` LIKE :translationKey;")
  139.         );
  140.         $query->execute([
  141.             'translationKey' => 'customFields.neon_unitarticles_%'
  142.         ]);
  143.     }
  144.     /**
  145.      * @return array[]
  146.      */
  147.     private function getCustomFields(): array
  148.     {
  149.         /** @var CustomFieldFactory $fieldFactory */
  150.         $fieldFactory = new CustomFieldFactory();
  151.         return [
  152.             [
  153.                 'name' => self::CUSTOMFIELD_ISUNITARTICLE,
  154.                 'type' => CustomFieldTypes::BOOL,
  155.                 'config' => [
  156.                     'bordered' => false,
  157.                     'label' => [
  158.                         'de-DE' => 'Ist Einheitenartikel',
  159.                         'en-GB' => 'Is unit article'
  160.                     ],
  161.                     'customFieldPosition' => 0,
  162.                     'helpText' => [
  163.                         'de-DE' => 'Soll Artikel nach Einheiten verkauft werden? Aktiviert die Plugin-Funktion für diesen Artikel in dieser Sprache',
  164.                         'en-GB' => 'Sell this article by unit.'
  165.                     ],
  166.                 ],
  167.             ],
  168.             [
  169.                 'name' => self::CUSTOMFIELD_BASEPRICEVALUE,
  170.                 'type' => CustomFieldTypes::FLOAT,
  171.                 'config' => [
  172.                     'label' => [
  173.                         'de-DE' => 'Grundeinheit',
  174.                         'en-GB' => 'Base unit'
  175.                     ],
  176.                     'placeholder' => [
  177.                         'de-DE' => 'Einheitenwert des Artikel-Grundpreises (z.B. 1)',
  178.                         'en-GB' => 'Unit value of base price (i.e. 1)'
  179.                     ],
  180.                     'helpText' => [
  181.                         'de-DE' => 'Beispiel: Artikel soll von 1 - 100 m verkauft werden. Den in den Stammdaten angegebenen Preis entspricht dem Preis pro 1 Meter. Dann muss hier 1 eingetrangen werden.',
  182.                         'en-GB' => ''
  183.                     ],
  184.                     'customFieldPosition' => 2
  185.                 ],
  186.             ],
  187.             $fieldFactory->getQuantityHiddenField(self::CUSTOMFIELD_HIDEQUANTITYSELECTION4),
  188.             $fieldFactory->getDescriptionField(self::CUSTOMFIELD_DESCRIPTION99$this->getLabelPrefix(1)),
  189.             $fieldFactory->getUnitField(self::CUSTOMFIELD_UNITLABEL100$this->getLabelPrefix(1)),
  190.             $fieldFactory->getCustomFieldMinValue(self::CUSTOMFIELD_MINVALUE101$this->getLabelPrefix(1)),
  191.             $fieldFactory->getCustomFieldMaxValue(self::CUSTOMFIELD_MAXVALUE102$this->getLabelPrefix(1)),
  192.             $fieldFactory->getCustomFieldStepSize(self::CUSTOMFIELD_STEPZISE103$this->getLabelPrefix(1)),
  193.             $fieldFactory->getCustomFieldDefaultValue(self::CUSTOMFIELD_DEFAULTVALUE104$this->getLabelPrefix(1)),
  194.             $fieldFactory->getCustomFieldExcludedValues(self::CUSTOMFIELD_EXCLUDEDVALUES105$this->getLabelPrefix(1)),
  195.         ];
  196.     }
  197.     /**
  198.      * @return string[]
  199.      */
  200.     private function getLabelPrefix($number): array
  201.     {
  202.         return ['de' => $number '. Dimension: ''en' => ($number == '1st' '2nd') . ' dimension: '];
  203.     }
  204.     private function removeTables(Connection $connection)
  205.     {
  206.         $classNames = [
  207.             UnitarticleSettingDefinition::ENTITY_NAME
  208.         ];
  209.         foreach ($classNames as $className) {
  210.             $connection->executeUpdate(\sprintf('DROP TABLE IF EXISTS `%s`'$className));
  211.         }
  212.     }
  213.     private function addCustomPrivileges(): void
  214.     {
  215.         if (!\method_exists($this'addPrivileges')) {
  216.             return;
  217.         }
  218.         $this->addPrivileges(
  219.             'product.editor',
  220.             [
  221.                 self::UNITARTICLE_SETTING_UPDATE_PRIVILEGE,
  222.             ]
  223.         );
  224.         $this->addPrivileges(
  225.             'product.creator',
  226.             [
  227.                 self::UNITARTICLE_SETTING_CREATE_PRIVILEGE,
  228.             ]
  229.         );
  230.     }
  231.     private function removeCustomPrivileges(): void
  232.     {
  233.         if (!\method_exists($this'removePrivileges')) {
  234.             return;
  235.         }
  236.         $this->removePrivileges([
  237.             self::UNITARTICLE_SETTING_UPDATE_PRIVILEGE,
  238.             self::UNITARTICLE_SETTING_CREATE_PRIVILEGE
  239.         ]);
  240.     }
  241. }