bundles/ZweiPunkt/ShopwareBundle/Controller/DefaultController.php line 22

Open in your IDE?
  1. <?php
  2. namespace ZweiPunkt\ShopwareBundle\Controller;
  3. use DateInterval;
  4. use DateTime;
  5. use Doctrine\DBAL\Connection;
  6. use Pimcore\Controller\FrontendController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use ZweiPunkt\ShopwareBundle\Command\SyncCommand;
  13. class DefaultController extends FrontendController
  14. {
  15.     private Connection $connection;
  16.     /**
  17.      * @Route("/sync")
  18.      */
  19.     public function indexAction(Request $requestConnection $connection)
  20.     {
  21.         $this->connection $connection;
  22.         if (file_exists(SyncCommand::SYNC_FILE)) {
  23.             unlink(SyncCommand::SYNC_FILE);
  24.         }
  25.         $action $request->query->get('action''none');
  26.         if ('full' == $action) {
  27.             $query '
  28.                 UPDATE object_1
  29.                 SET shopwareSync = 0
  30.                 WHERE shopwareSync IS NOT NULL;
  31.             ';
  32.             $connection->executeStatement($query);
  33.             return new RedirectResponse('/sync');
  34.         }
  35.         return new Response($this->getContent(), Response::HTTP_OK);
  36.     }
  37.     private function getContent(): string
  38.     {
  39.         $links = [
  40.             'Update starten' => '/sync',
  41.             'Vollsynchronisation' => '/sync?action=full',
  42.             'Liste' => '/list'
  43.         ];
  44.         $content '';
  45.         $entitiesToSync $this
  46.             ->connection
  47.             ->executeQuery('
  48.                 SELECT COUNT(*) AS `count`
  49.                  FROM `object_1`
  50.                 WHERE o_modificationDate > shopwareSync
  51.                    OR shopwareSync IS NULL;
  52.             ')
  53.             ->fetchAllAssociative()
  54.         ;
  55.         $count $entitiesToSync[0]['count'] ?? 0;
  56.         if ($count 0) {
  57.             $content .= '<p>' sprintf('Zu Synchronisieren: %d'$count) . '</p>';
  58.         }
  59.         $content .= '<div style="margin: 25px">';
  60.         foreach ($links as $name => $link) {
  61.             $content .= sprintf(
  62.                 '<a style="margin: 10px; padding: 5px 10px; background: #EFEFEF; text-decoration: none;" href="%s">%s</a>',
  63.                 $link,
  64.                 $name
  65.             );
  66.         }
  67.         $content .= '</div><p>Synchronisation wird in den nächsten 5 Minuten gestartet.</p>';
  68.         return $content;
  69.     }
  70.     /**
  71.      * @Route("/list")
  72.      */
  73.     public function listAction(Request $requestConnection $connection)
  74.     {
  75.         $entitiesToSync $connection
  76.             ->executeQuery('
  77.                 SELECT oo_id AS id,
  78.                        customfield_asf_model AS model,
  79.                        customfield_asf_material AS material,
  80.                        customfield_asf_alloy AS alloy,
  81.                        customfield_asf_default_stone AS stone
  82.                  FROM `object_1`
  83.                 WHERE o_modificationDate > shopwareSync
  84.                    OR shopwareSync IS NULL;
  85.             ')
  86.             ->fetchAllAssociative()
  87.         ;
  88.         $content '<style>body { font-family: "Courier New"; } td,th { text-align: left; padding: 5px 10px; font-family: "Courier New"; }</style>';
  89.         $content .= '<h1>Zu aktualisierende Produkte: ' count($entitiesToSync) . '</h1>';
  90.         $content .= '<h4>Technische Daten direkt aus DB (nicht für Import / Export geeignet).</h4>';
  91.         $content .= '<table>';
  92.         $content .= '
  93.             <thead>
  94.                 <th>ID</th>
  95.                 <th>Modell</th>
  96.                 <th>Material</th>
  97.                 <th>Legierung</th>
  98.                 <th>Stein</th>
  99.                 <th>Einzelprodukt</th>
  100.                 <th>Modell</th>
  101.             </thead>
  102.         ';
  103.         foreach ($entitiesToSync as $entry) {
  104.             $content .= '<tr>';
  105.             $content .= sprintf('
  106.                         <td>%d</td>
  107.                         <td>%s</td>
  108.                         <td>%s</td>
  109.                         <td>%s</td>
  110.                         <td>%s</td>
  111.                         <td><pre style="color: gray;">bin/console shopware:sync --ring=%d</pre></td>
  112.                         <td><pre style="color: gray;">bin/console shopware:sync --model=%s</pre></td>
  113.                     ',
  114.                     $entry['id'],
  115.                     $entry['model'],
  116.                     $entry['material'],
  117.                     $entry['alloy'],
  118.                     $entry['stone'],
  119.                     $entry['id'],
  120.                     $entry['model']
  121.                 );
  122.             $content .= '</tr>';
  123.         }
  124.         $content .= '</table>';
  125.         return new Response($contentResponse::HTTP_OK);
  126.     }
  127. }