<?php
namespace App\EventListener;
use Pimcore\Event\BundleManager\PathsEvent;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\AveryProdBase;
use Pimcore\Model\DataObject\AverySalesChannel;
use Pimcore\Model\DataObject\ExportProfile;
use Pimcore\Model\DataObject\AveryExportProfile;
// use Pimcore\Model\Notification\Service\NotificationService;
// use Symfony\Contracts\Translation\TranslatorInterface;
use App\Service\AverySettingsService;
use Pimcore\Tool\Session;
use Symfony\Component\HttpFoundation\RequestStack;
class AveryEventListener
{
private $logger;
private $settingsService;
private RequestStack $requestStack;
public function __construct(
ApplicationLogger $logger,
AverySettingsService $settingsService,
RequestStack $requestStack
)
{
$this->logger = $logger;
$this->settingsService = $settingsService;
$this->requestStack = $requestStack;
}
public function addCSSFiles(PathsEvent $event): void
{
$event->setPaths(
array_merge(
$event->getPaths(),
[
'/admin-static/css/admin-style.css'
]
)
);
$leerFeatureEnabled = false;
$user = \Pimcore\Tool\Admin::getCurrentUser();
if ($user == null && $this->requestStack->getCurrentRequest()) {
try {
$user = Session::getSessionBag($this->requestStack->getSession())->get("user");
} catch (\Exception $e) {
$this->logger->error("AVERY EVENT LISTENER | Could not get user session from session bag!", [$e]);
}
}
if ($user != null) {
$roles = $user->getRoles();
foreach ($roles as $role) {
$role = \Pimcore\Model\User\Role::getById($role);
if (strpos(strtolower($role->getName()), "meto") !== false) {
$leerFeatureEnabled = true;
break;
}
}
}
if ($leerFeatureEnabled) {
$event->setPaths(
array_merge(
$event->getPaths(),
[
'/admin-static/css/leerFeature.css',
]
)
);
}
}
public function addJSFiles(PathsEvent $event): void
{
$event->setPaths(
array_merge(
$event->getPaths(),
[
'/admin-static/js/startup.js',
'/admin-static/js/metostartup.js',
'/admin-static/js/transitions.js',
]
)
);
$leerFeatureEnabled = false;
$user = \Pimcore\Tool\Admin::getCurrentUser();
if ($user == null) {
try {
$user = Session::getSessionBag($this->requestStack->getSession())->get("user");
} catch (\Exception $e) {
$this->logger->error("AVERY EVENT LISTENER | Could not get user session from session bag!", [$e]);
}
}
if ($user != null) {
if (strpos(strtolower($user->getEmail()), "@meto") !== false) {
$leerFeatureEnabled = true;
} else {
$roles = $user->getRoles();
foreach ($roles as $role) {
$role = \Pimcore\Model\User\Role::getById($role);
if (strpos(strtolower($role->getName()), "meto") !== false) {
$leerFeatureEnabled = true;
break;
}
}
}
}
if ($leerFeatureEnabled) {
$event->setPaths(
array_merge(
$event->getPaths(),
[
'/admin-static/js/leerFeature.js',
]
)
);
}
}
public function onPreAdd(ElementEventInterface $e): void
{
if ($e instanceof DataObjectEvent) {
$newExportProfileObject = $e->getObject();
// TODO: Abfrage für Meto Profile Klasse
if ($newExportProfileObject instanceof ExportProfile || $newExportProfileObject instanceof AveryExportProfile) {
// TODO: An neue Ordnerstruktur anpassen
// CR: done! (11 months later...)
$defaultMappingSetting = null;
$defaultAttributesMapping = null;
if ($newExportProfileObject instanceof ExportProfile) {
$averyAssDefaultMappingSetting = $this->settingsService->getDataFromWebsiteSetting('averyAssDefaultMapping', 'de');
if (!empty($averyAssDefaultMappingSetting) && !empty($averyAssDefaultMappingSetting->getData())) {
$defaultMappingSetting = $averyAssDefaultMappingSetting->getData();
}
} elseif ($newExportProfileObject instanceof AveryExportProfile) {
$averyAssPhase2DefaultMapSetting = $this->settingsService->getDataFromWebsiteSetting('averyAssPhase2DefaultMap', 'de');
if (!empty($averyAssPhase2DefaultMapSetting) && !empty($averyAssPhase2DefaultMapSetting->getData())) {
$defaultMappingSetting = $averyAssPhase2DefaultMapSetting->getData();
}
}
if (!empty($defaultMappingSetting)) {
$defaultAttributesMapping = Asset::getByPath($defaultMappingSetting);
}
$defaultLanguage = "de";
if ($defaultAttributesMapping) {
$newExportProfileObject->setExportAttributesConfig($defaultAttributesMapping);
}
if ($defaultLanguage) {
$newExportProfileObject->setExportLanguage($defaultLanguage);
}
// Set the default export assets, if any
if (!empty($this->settingsService->getDataFromWebsiteSetting('averyTxtExportDefaultAssets', 'de'))) {
$tmpSetting = $this->settingsService->getDataFromWebsiteSetting('averyTxtExportDefaultAssets', 'de')->getData();
}
if (isset($tmpSetting)) {
(array)$exportDefaultAssetsSetting = explode(',', $tmpSetting);
if (isset($exportDefaultAssetsSetting)) {
$newExportProfileObject->setExportAssetTypes($exportDefaultAssetsSetting);
}
}
}
}
}
public function onObjectPreUpdate(ElementEventInterface $e): void
{
$object = $e->getObject();
if (!$e->hasArgument("saveVersionOnly")) {
if ($object instanceof AverySalesChannel) {
// Check for and remove offline products
$salesChannelProducts = $object->getSalesChannelProducts();
// Fixing problem with changing count by unsetting products in temporarily duplicated array instead of actual iterated array
$tempProds = $salesChannelProducts;
$changed = false;
if (!empty($salesChannelProducts)) {
for ($i = 0; $i <= count($salesChannelProducts) - 1; $i++) {
if (!$this->checkIsOnline($salesChannelProducts[$i])) {
unset($tempProds[$i]);
$changed = true;
}
}
}
if ($changed) {
$salesChannelProducts = $tempProds;
$object->setSalesChannelProducts($salesChannelProducts);
}
}
}
}
public function checkIsOnline(AveryProdBase $prod): bool
{
$result = false;
if ($prod->getWorkflowState() == "online") {
$result = true;
}
/*
if($prod->isPublished()){
$result = true;
}
*/
return $result;
}
}