custom/plugins/NimbitsTracking/src/NimbitsTracking.php line 10

Open in your IDE?
  1. <?php
  2. namespace Nimbits\NimbitsTracking;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. class NimbitsTracking extends Plugin
  8. {
  9.     /**
  10.      * @param \Shopware\Core\Framework\Plugin\Context\UninstallContext $uninstallContext
  11.      * @throws \Doctrine\DBAL\Exception
  12.      */
  13.     public function uninstall(UninstallContext $uninstallContext): void
  14.     {
  15.         if (!$uninstallContext->keepUserData()) {
  16.             /** @var Connection $connection */
  17.             $connection $this->container->get(Connection::class);
  18.             $connection->executeUpdate('DELETE FROM custom_field_set WHERE name = ?', ['nb_tracking']);
  19.             $this->cleanupCustomFields($connection'customer''nb_tracking_partner');
  20.             $this->cleanupCustomFields($connection'order''nb_tracking_partner');
  21.             try {
  22.                 $connection->executeUpdate('DROP TABLE nb_tracking_visitor_ips');
  23.                 $connection->executeUpdate('DROP TABLE nb_tracking_visitors');
  24.             } catch (Exception $e) {}
  25.         }
  26.         $this->removeMigrations();
  27.     }
  28.     /**
  29.      * @throws \Doctrine\DBAL\Exception
  30.      * @noinspection PhpSameParameterValueInspection
  31.      */
  32.     private function cleanupCustomFields(Connection $connectionstring $tableNamestring $fieldName)
  33.     {
  34.         $entries $connection->executeQuery('SELECT * FROM `' $tableName '` WHERE custom_fields LIKE ?', ['%' $fieldName '%'])->fetchAll();
  35.         foreach ($entries as $entry) {
  36.             $fields json_decode($entry['custom_fields'], true);
  37.             if (in_array('nb_tracking_partner'$fields)) {
  38.                 unset($fields['nb_tracking_partner']);
  39.             }
  40.             $entry['custom_fields'] = json_encode($fields);
  41.             $connection->executeUpdate("
  42.                 UPDATE `$tableName` SET
  43.                     custom_fields = ?
  44.                 WHERE id = ?
  45.             ", [
  46.                 $entry['custom_fields'],
  47.                 $entry['id']
  48.             ]);
  49.         }
  50.     }
  51. }