-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Franck Allimant
committed
Nov 14, 2024
1 parent
7acb206
commit c8b5c1e
Showing
7 changed files
with
272 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/*************************************************************************************/ | ||
/* Copyright (c) Open Studio */ | ||
/* web : https://open.studio */ | ||
/* */ | ||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
/*************************************************************************************/ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <fallimant@openstudio.fr> | ||
* Date: 14/11/2024 09:23 | ||
*/ | ||
namespace CsvImporter\Controller; | ||
|
||
use CsvImporter\Service\CsvProductImporterService; | ||
use Propel\Runtime\Propel; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Thelia\Controller\Admin\BaseAdminController; | ||
use Thelia\Log\Destination\TlogDestinationFile; | ||
use Thelia\Log\Destination\TlogDestinationRotatingFile; | ||
use Thelia\Log\Tlog; | ||
use Thelia\Tools\URL; | ||
|
||
/** | ||
* @Route("/admin/module/csvimporter") | ||
*/ | ||
class ConfigurationController extends BaseAdminController | ||
{ | ||
protected string $logFile = THELIA_LOG_DIR . 'catalog-import-log.txt'; | ||
|
||
/** | ||
* Import manuel du catalogue | ||
* | ||
* @Route("/import", methods="GET") | ||
*/ | ||
public function import(CsvProductImporterService $csvProductImporterService): Response | ||
{ | ||
@unlink($this->logFile); | ||
|
||
Tlog::getInstance() | ||
->setLevel(Tlog::INFO) | ||
->setDestinations(TlogDestinationFile::class) | ||
->setConfig(TlogDestinationFile::class, TlogDestinationFile::VAR_PATH_FILE, $this->logFile) | ||
|
||
->setFiles('*') | ||
->setPrefix('[#LEVEL] #DATE #HOUR:'); | ||
|
||
// Pas de log des requetes SQL | ||
Propel::getConnection('TheliaMain')->useDebug(false); | ||
|
||
$catalogDir = THELIA_LOCAL_DIR . 'Catalogue'; | ||
|
||
if (! is_dir($catalogDir)) { | ||
return $this->generateRedirect(URL::getInstance()?->absoluteUrl( | ||
'/admin/module/CsvImporter', | ||
[ 'error' => 'Répertoire ' . $catalogDir . ' non trouvé.' ]) | ||
); | ||
} | ||
|
||
$csvProductImporterService->importFromDirectory($catalogDir); | ||
|
||
return $this->generateRedirect(URL::getInstance()?->absoluteUrl('/admin/module/CsvImporter', [ 'done' => 1 ])); | ||
} | ||
|
||
/** | ||
* Import manuel du catalogue | ||
* | ||
* @Route("/log", methods="GET") | ||
*/ | ||
public function getLogFile(): Response | ||
{ | ||
return new Response( | ||
@file_get_contents($this->logFile), | ||
200, | ||
array( | ||
'Content-type' => "text/plain", | ||
'Content-Disposition' => 'Attachment;filename=csv-import-log.txt' | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/*************************************************************************************/ | ||
/* Copyright (c) Open Studio */ | ||
/* web : https://open.studio */ | ||
/* */ | ||
/* For the full copyright and license information, please view the LICENSE */ | ||
/* file that was distributed with this source code. */ | ||
/*************************************************************************************/ | ||
|
||
/** | ||
* Created by Franck Allimant, OpenStudio <fallimant@openstudio.fr> | ||
* Date: 14/11/2024 09:27 | ||
*/ | ||
namespace CsvImporter\Hook; | ||
|
||
use BestSellers\BestSellers; | ||
use Thelia\Core\Event\Hook\HookRenderBlockEvent; | ||
use Thelia\Core\Event\Hook\HookRenderEvent; | ||
use Thelia\Core\Hook\BaseHook; | ||
use Thelia\Tools\URL; | ||
|
||
/** | ||
* | ||
*/ | ||
class HookManager extends BaseHook | ||
{ | ||
protected const MAX_TRACE_SIZE_IN_BYTES = 40000; | ||
|
||
public function onModuleConfiguration(HookRenderEvent $event) | ||
{ | ||
$logFilePath = THELIA_LOG_DIR . 'catalog-import-log.txt'; | ||
|
||
$traces = @file_get_contents($logFilePath); | ||
|
||
// Limiter la taille des traces à 1MO | ||
if (strlen($traces) > self::MAX_TRACE_SIZE_IN_BYTES) { | ||
$traces = substr($traces, strlen($traces) - self::MAX_TRACE_SIZE_IN_BYTES); | ||
|
||
// Cut a first line break; | ||
if (false !== $lineBreakPos = strpos($traces, "\n")) { | ||
$traces = substr($traces, $lineBreakPos+1); | ||
} | ||
} | ||
|
||
$event->add( | ||
$this->render( | ||
'module-configuration.html', | ||
[ 'trace_content' => nl2br($traces) ] | ||
) | ||
); | ||
} | ||
|
||
public function onMainTopMenuTools(HookRenderBlockEvent $event) | ||
{ | ||
$event->add( | ||
[ | ||
'id' => 'csvimporter_menu', | ||
'class' => '', | ||
'url' => URL::getInstance()->absoluteUrl('/admin/module/CsvImporter'), | ||
'title' =>"Import catalogue CSV" | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.