<?php
namespace ZweiPunkt\ShopwareBundle\Controller;
use DateInterval;
use DateTime;
use Doctrine\DBAL\Connection;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use ZweiPunkt\ShopwareBundle\Command\SyncCommand;
class DefaultController extends FrontendController
{
private Connection $connection;
/**
* @Route("/sync")
*/
public function indexAction(Request $request, Connection $connection)
{
$this->connection = $connection;
if (file_exists(SyncCommand::SYNC_FILE)) {
unlink(SyncCommand::SYNC_FILE);
}
$action = $request->query->get('action', 'none');
if ('full' == $action) {
$query = '
UPDATE object_1
SET shopwareSync = 0
WHERE shopwareSync IS NOT NULL;
';
$connection->executeStatement($query);
return new RedirectResponse('/sync');
}
return new Response($this->getContent(), Response::HTTP_OK);
}
private function getContent(): string
{
$links = [
'Update starten' => '/sync',
'Vollsynchronisation' => '/sync?action=full',
'Liste' => '/list'
];
$content = '';
$entitiesToSync = $this
->connection
->executeQuery('
SELECT COUNT(*) AS `count`
FROM `object_1`
WHERE o_modificationDate > shopwareSync
OR shopwareSync IS NULL;
')
->fetchAllAssociative()
;
$count = $entitiesToSync[0]['count'] ?? 0;
if ($count > 0) {
$content .= '<p>' . sprintf('Zu Synchronisieren: %d', $count) . '</p>';
}
$content .= '<div style="margin: 25px">';
foreach ($links as $name => $link) {
$content .= sprintf(
'<a style="margin: 10px; padding: 5px 10px; background: #EFEFEF; text-decoration: none;" href="%s">%s</a>',
$link,
$name
);
}
$content .= '</div><p>Synchronisation wird in den nächsten 5 Minuten gestartet.</p>';
return $content;
}
/**
* @Route("/list")
*/
public function listAction(Request $request, Connection $connection)
{
$entitiesToSync = $connection
->executeQuery('
SELECT oo_id AS id,
customfield_asf_model AS model,
customfield_asf_material AS material,
customfield_asf_alloy AS alloy,
customfield_asf_default_stone AS stone
FROM `object_1`
WHERE o_modificationDate > shopwareSync
OR shopwareSync IS NULL;
')
->fetchAllAssociative()
;
$content = '<style>body { font-family: "Courier New"; } td,th { text-align: left; padding: 5px 10px; font-family: "Courier New"; }</style>';
$content .= '<h1>Zu aktualisierende Produkte: ' . count($entitiesToSync) . '</h1>';
$content .= '<h4>Technische Daten direkt aus DB (nicht für Import / Export geeignet).</h4>';
$content .= '<table>';
$content .= '
<thead>
<th>ID</th>
<th>Modell</th>
<th>Material</th>
<th>Legierung</th>
<th>Stein</th>
<th>Einzelprodukt</th>
<th>Modell</th>
</thead>
';
foreach ($entitiesToSync as $entry) {
$content .= '<tr>';
$content .= sprintf('
<td>%d</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td><pre style="color: gray;">bin/console shopware:sync --ring=%d</pre></td>
<td><pre style="color: gray;">bin/console shopware:sync --model=%s</pre></td>
',
$entry['id'],
$entry['model'],
$entry['material'],
$entry['alloy'],
$entry['stone'],
$entry['id'],
$entry['model']
);
$content .= '</tr>';
}
$content .= '</table>';
return new Response($content, Response::HTTP_OK);
}
}