<?php
namespace Metallit\Storefront\Controller;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class MetallitController extends AbstractController
{
/**
* @Route("/getitemfile", name="getitemfile", methods={"GET"})
* @RouteScope(scopes={"storefront"})
*/
public function getitemfile(Request $request): Response
{
$filename = base64_decode($request->get('filename'));
if (!is_file($filename)) {
die('Die gewünschte Datei wurde nicht gefunden');
}
$info = pathinfo($filename);
$response = new Response(file_get_contents($filename));
$response->headers->set('Content-Type', 'application/pdf');
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
urlencode($info['basename'])
));
return $response;
}
/**
* @Route("/getdocuments", name="getdocuments", methods={"GET"})
* @RouteScope(scopes={"storefront"})
*/
public function getdocuments(Request $request, Context $context): JsonResponse
{
$item_no = $request->get('productNumber');
$criteria = new Criteria([$request->get('master')]);
$master = $this->container->get('product.repository')->search($criteria, $context)->first();
$master_nr = substr($master->getProductNumber(), 1);
// Flyer und Sicherheitsdatenblätter
$SD_FilesPath = '/mnt/Storagebox/Produktinformationen/';
$FL_FilesPath = '/mnt/Storagebox/Marketing_Kommunikationsmittel/';
$folders = array(
'0100_Sicherheitsdatenblätter' => array($SD_FilesPath, scandir($SD_FilesPath . '0100_Sicherheitsdatenblätter')),
'0150_Betriebsanweisungen' => array($SD_FilesPath, scandir($SD_FilesPath . '0150_Betriebsanweisungen')),
'0300_Produktflyer' => array($FL_FilesPath, scandir($FL_FilesPath . '0300_Produktflyer')),
'0300_Bedienungsanleitungen' => array($SD_FilesPath, scandir($SD_FilesPath . '0300_Bedienungsanleitungen')),
'0200_Technische Datenblätter' => array($SD_FilesPath, scandir($SD_FilesPath . '0200_Technische Datenblätter')),
'0400_Zertifikate' => array($SD_FilesPath, scandir($SD_FilesPath . '0400_Zertifikate')),
// '0600_Bildarchiv' => array($this->SD_FilesPath, scandir($this->SD_FilesPath . '0600_Bildarchiv'))
);
$pdf_arr = [];
$div_template = '<div class="card product-box box-standard">
<div class="card-body">
<div class="product-badges">
</div>
</div>';
foreach($folders as $name => $files) {
$found = false;
$itemno = trim($item_no);
StartSearch:
foreach($files[1] as $file) {
if ($file == "." || $file == ".." || $file == "001_Archiv" || $file[0] == "@") continue;
// $erg .= "<p>$file</p>";
if (is_dir($files[0] . $name . '/' . $file)) {
// Bei 0400_Zertifikate die Unterverzeichnisse durchsuchen
$subdir = scandir($files[0] . $name . '/' . $file);
foreach($subdir as $subfile) {
if ($subfile == "." || $subfile == ".." || is_dir($subfile)) continue;
$cleanfile = str_replace(' ', '', $subfile); // zur Sicherheit noch mal alle Leerzeichen raus
$tmp = explode('_', $cleanfile);
if (count($tmp) < 3) continue;
$lang = strtoupper($tmp[2]);
if (strstr($cleanfile, "_" . $itemno . "_") !== false) {
$pdf_arr[$name][] = array(($files[0] . $name . '/' . $file.'/'.$subfile), $lang);
$found = true;
}
}
} else {
$cleanfile = str_replace(' ', '', $file); // zur Sicherheit noch mal alle Leerzeichen raus
$tmp = explode('_', $cleanfile);
if (count($tmp) < 3) continue;
$lang = strtoupper($tmp[2]);
$tmp = explode('_', $name);
if (strstr($cleanfile, "_" . $itemno . "_") !== false) {
$pdf_arr[$name][] = array(($files[0] . $name . '/' . $file), $lang);
$found = true;
}
}
}
if (!$found) {
if ($itemno !== $master_nr) {
$itemno = $master_nr;
goto StartSearch;
}
}
}
if ($pdf_arr) {
$divcontent = '<div class="row">';
foreach($pdf_arr as $folder => $pdf_files) {
$divcontent .= '<div class="metallit-download-documents col-sm-4" style="text-align:center;"><h4>'.explode("_", $folder)[1].'</h4><div class="metallit-download-documents-body">';
foreach($pdf_files as $pdf_file) {
$divcontent .= '<div class="metallit-download-documents-entry">';
$divcontent .= '<a target="_blank" href="/getitemfile?filename='.base64_encode($pdf_file[0]).'" title="'.basename($pdf_file[0]).'">';
$divcontent .= '<i class="fa fa-file-pdf" style="font-size:64px!important"></i><div>' . /*basename($pdf_file[0]).' ('.$pdf_file[1].')'*/ $pdf_file[1].'</div>';
$divcontent .= '</a>';
$divcontent .= '</div>';
}
$divcontent .= '</div></div>';
}
$erg = $divcontent.'</div>';
} else {
$erg = 0;
}
$responseData = $erg;
return $this->json($responseData);
}
/**
* @Route("/getcustomerhistory", name="getcustomerhistory", methods={"GET"})
* @RouteScope(scopes={"storefront"})
*/
public function getcustomerhistory(Request $request): Response
{
// holt alle Käufe aus Navision von Produkten, die auch im Shop aktiv sind
try {
$mysqli = new \mysqli("88.99.105.137", "db_aistest", "M3tAll!T", "db_ais");
$mysqli->set_charset("utf8");
if ($mysqli->connect_errno) {
return new Response('Verbindung zur Datenbank fehlgeschlagen: ' . $mysqli->connect_error);
}
$kdnr = $request->get('kdnr');//
//
$query = "SELECT * FROM (SELECT a.A_NO, a.A_RNR, TRIM(CONCAT(i.Description, ' ', i.Description_2)) AS A_BEZ, MAX(A_AD) AS Last_Order_Date
, A_MENGE, TRIM(g.groupname) AS Item_Group, u.seo_path_info AS A_BEZ_URL,
i.shop_image_path
FROM articles a
LEFT JOIN items i ON i.No_ = a.A_NO
LEFT JOIN db_pim.maingroups g ON g.code = MID(i.Item_Group, 4, 2)
LEFT JOIN db_shop.product p ON a.A_NO = p.product_number
LEFT JOIN db_shop.product_translation t ON t.product_id = p.id AND t.product_version_id = p.version_id
LEFT JOIN db_shop.seo_url u ON u.foreign_key = p.id
WHERE (DBNr = ? OR BillTo = ?) AND A_NO IN (SELECT product_number FROM db_shop.product WHERE active = 1)
AND A_AD > SUBDATE(NOW(), INTERVAL 5 YEAR)
GROUP BY i.Item_Group, a.A_NO) t
ORDER BY Item_Group, Last_Order_Date DESC";
$stmt = $mysqli->prepare($query);
if (!$stmt) {
return new Response('Fehler beim Vorbereiten der Abfrage: ' . $mysqli->error);
}
$stmt->bind_param("ss", $kdnr, $kdnr);
$stmt->execute();
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$mysqli->close();
return $this->json($result);
} catch (Exception $ex) {
return $ex->getMessage();
}
}
}