custom/plugins/RhiemRentalProducts/src/RhiemRentalProducts.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rhiem\RhiemRentalProducts;
  4. use Doctrine\DBAL\Connection;
  5. use Rhiem\RhiemRentalProducts\Installer\PayloadUpdater;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. if (file_exists(__DIR__ '/../vendor/autoload.php')) {
  14.     require_once __DIR__ '/../vendor/autoload.php';
  15. }
  16. class RhiemRentalProducts extends Plugin
  17. {
  18.     public function deactivate(DeactivateContext $deactivateContext): void
  19.     {
  20.         $this->deactivateCustomfields($deactivateContext);
  21.     }
  22.     public function activate(ActivateContext $activateContext): void
  23.     {
  24.         $this->activateCustomfields($activateContext);
  25.     }
  26.     public function uninstall(UninstallContext $uninstallContext): void
  27.     {
  28.         parent::uninstall($uninstallContext);
  29.         if ($uninstallContext->keepUserData()) {
  30.             return;
  31.         }
  32.         $this->removeTables();
  33.     }
  34.     public function postUpdate(UpdateContext $updateContext): void
  35.     {
  36.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.2.2''<')) {
  37.             /** @var Connection $connection */
  38.             $connection $this->container->get(Connection::class);
  39.             $payloadUpdater = new PayloadUpdater($connection$this->container);
  40.             $payloadUpdater->update();
  41.         }
  42.         parent::postUpdate($updateContext);
  43.     }
  44.     private function removeTables(): void
  45.     {
  46.         /** @var Connection $connection */
  47.         $connection $this->container->get(Connection::class);
  48.         $connection->executeUpdate('DROP TABLE IF EXISTS `rental_product_price`');
  49.         $connection->executeUpdate('DROP TABLE IF EXISTS `rental_product_deposit_price`');
  50.         $connection->executeUpdate('DROP TABLE IF EXISTS `rental_product`');
  51.     }
  52.     private function deactivateCustomfields(DeactivateContext $context): void
  53.     {
  54.         /**
  55.          * Custom Fields should only ever be deactivated because they are used in order documents,emails, etc. which have to be
  56.          * historically accurate and therefore have to be archived instead of deleted
  57.          */
  58.         /** @var EntityRepositoryInterface $customFieldRepository */
  59.         $customFieldRepository $this->container->get('custom_field.repository');
  60.         $customFieldRepository->upsert(
  61.             [
  62.                 [
  63.                     'id' => '6cd39a8435e3432b92d9e7da7ec4c5b1',
  64.                     'name' => 'rhiem_rental_products_rent_start',
  65.                     'type' => CustomFieldTypes::DATETIME,
  66.                     'active' => false,
  67.                 ],
  68.                 [
  69.                     'id' => 'e8cf727fda8440139d68d300c6790d7a',
  70.                     'name' => 'rhiem_rental_products_rent_end',
  71.                     'type' => CustomFieldTypes::DATETIME,
  72.                     'active' => false,
  73.                 ],
  74.             ],
  75.             $context->getContext()
  76.         );
  77.     }
  78.     private function activateCustomfields(ActivateContext $context): void
  79.     {
  80.         /** @var EntityRepositoryInterface $customFieldRepository */
  81.         $customFieldRepository $this->container->get('custom_field.repository');
  82.         $customFieldRepository->upsert(
  83.             [
  84.                 [
  85.                     'id' => '6cd39a8435e3432b92d9e7da7ec4c5b1',
  86.                     'name' => 'rhiem_rental_products_rent_start',
  87.                     'type' => CustomFieldTypes::DATETIME,
  88.                     'active' => true,
  89.                 ],
  90.                 [
  91.                     'id' => 'e8cf727fda8440139d68d300c6790d7a',
  92.                     'name' => 'rhiem_rental_products_rent_end',
  93.                     'type' => CustomFieldTypes::DATETIME,
  94.                     'active' => true,
  95.                 ],
  96.             ],
  97.             $context->getContext()
  98.         );
  99.     }
  100. }