custom/plugins/IdeaBooks/src/Storefront/Subscriber/ExportSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace IdeaBooks\Storefront\Subscriber;
  3. use Shopware\Core\Content\ImportExport\Event\ImportExportBeforeExportRecordEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use \Mysqli;
  7. class ExportSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents(): array
  10.     {
  11.         return [
  12.           ImportExportBeforeExportRecordEvent::class => 'convertTags'
  13.         ];
  14.     }
  15.     private function OpenCon()
  16.      {
  17.         //DATABASE_URL="mysql://ideabooks.nl:i9cIbFBadWuxdwgb@localhost:3306/ideabooks_albertvanderveen_nl"
  18.         $databaseUrl getenv('DATABASE_URL');
  19.         $databaseUrlParts explode(':'$databaseUrl);
  20.         $dbuser trim($databaseUrlParts[1],'/');
  21.         $dbpass explode('@'$databaseUrlParts[2])[0];
  22.         $dbhost explode('@'$databaseUrlParts[2])[1];
  23.         $db explode('/'$databaseUrlParts[3])[1];
  24.          /*
  25.          $dbhost = "localhost";
  26.          $dbuser = "app";
  27.          $dbpass = "SAg8XKmloD8cjS2dCvneYFMnH70onxgr";
  28.          $db = "ideanew2";
  29.          */
  30.          $mysqli = new mysqli($dbhost$dbuser$dbpass,$db) or die("Connect failed: %s\n"$conn -> error);
  31.          return $mysqli;
  32.      }
  33.      
  34.     private function CloseCon($mysqli)
  35.      {
  36.          $mysqli -> close();
  37.      }
  38.      public function convertTags(ImportExportBeforeExportRecordEvent $event): void
  39.     {
  40.         $record $event->getRecord();
  41.         if ($record['status'] == 'notSet'){
  42.             $record = [];//in dit geval record niet exporteren, want niet bevestigde aanmelding
  43.         }
  44.         else {
  45.             //$record['tags'] = 'test';
  46.             $tags explode(','str_replace('|'','$record['tags']));
  47.             $tags substr_replace($tags'0x'00);//voeg '0x' toe aan ieder tag-id (is UUID)
  48.             $mysqli $this->OpenCon();
  49.             $query "SELECT name FROM tag WHERE id IN (" implode(','$tags) . ")";
  50.             file_put_contents(getenv('APP_DIR') . '/var/log/ExportSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'query tags: '.$query.PHP_EOLFILE_APPEND);            
  51.             if ($result $mysqli->query($query)) {
  52.                 $rows mysqli_fetch_all($resultMYSQLI_ASSOC);
  53.                 file_put_contents(getenv('APP_DIR') . '/var/log/ExportSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'rows: '.print_r($rowstrue).PHP_EOLFILE_APPEND);
  54.                 $tagsConvertedArr = [];
  55.                 $country '';
  56.                 foreach ($rows as $key => $row) {
  57.                     if (in_array($row['name'], ['Art''Photography''Design''Architecture'])) $tagsConvertedArr[] = $row['name'];
  58.                     else $country $row['name'];
  59.                 }
  60.                 $record['country'] = $country;
  61.                 $record['newsletterCategories'] = implode('|'$tagsConvertedArr);
  62.             }
  63.             else {
  64.                 $record['country'] = '';//deze twee altijd setten, zodat de col in shopware/core/Content/ImportExport/Processing/Writer/CsvFileWriter.php wordt geset in de header, als $index === 0
  65.                 $record['newsletterCategories'] = '';
  66.             }
  67.             unset($record['tags']);//unsetten, want verspreid over $record['country'] en $record['newsletterCategories']
  68.             //deze ook unsetten, want niet van belang:
  69.             unset($record['id']);
  70.             unset($record['salutation']);
  71.             unset($record['title']);
  72.             unset($record['hash']);
  73.             unset($record['sales_channel_id']);
  74.             unset($record['salesChannelId']);
  75.             $this->CloseCon($mysqli);
  76.         }
  77.         $event->setRecord($record);
  78.         file_put_contents(getenv('APP_DIR') . '/var/log/ExportSubscriber.log'PHP_EOL.date("Y-m-d H:i:s").chr(9).'record: '.print_r($recordtrue).PHP_EOLFILE_APPEND);
  79.     }
  80. }