<?php
namespace App\EventListener;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition\CustomLayout;
use Pimcore\Model\DataObject\ClassDefinition\Data\Classificationstore;
use Pimcore\Model\DataObject\Service;
use Symfony\Component\EventDispatcher\GenericEvent;
class LayoutSwitcher {
public function selectCustomLayout(GenericEvent $event) {
$object = $event->getArgument("object");
$data = $event->getArgument("data");
$currentLayout = $data['currentLayoutId'] ?? null;
try {
if (!empty($object->getClass()->getGroup()) && $object->getClass()->getGroup() == "Products") {
$classId = $object->getClassId();
$level = $object->getProductLevel();
// $this->GetClassificationStore($object);
$data = $event->getArgument("data");
switch ($level) {
case "CHILD":
$data = $this->doModifyCustomLayouts($data, $object, "{$classId}2", ["{$classId}0", "{$classId}1"], $currentLayout);
break;
case "STYLE":
$data = $this->doModifyCustomLayouts($data, $object, "{$classId}1", ["{$classId}0", "{$classId}2"], $currentLayout);
break;
default:
$data = $this->doModifyCustomLayouts($data, $object, "{$classId}0", ["{$classId}1", "{$classId}2"], $currentLayout);
break;
}
$event->setArgument("data", $data);
}
} catch (\Exception $e) {
}
}
// public function selectCustomLayout(GenericEvent $event) {
// $object = $event->getArgument("object");
// $data = $event->getArgument("data");
// $currentLayout = $data['currentLayoutId'] ?? null;
// try {
// if (!empty($object->getClass()->getGroup()) && $object->getClass()->getGroup() == "Products") {
// $classId = $object->getClassId();
// $level = $this->GetLevel($object);
// $this->GetClassificationStore($object);
// $data = $event->getArgument("data");
// switch ($level) {
// case "3":
// $data = $this->doModifyCustomLayouts($data, $object, "{$classId}2", ["{$classId}0", "{$classId}1"], $currentLayout);
// break;
// case "2":
// $data = $this->doModifyCustomLayouts($data, $object, "{$classId}1", ["{$classId}0", "{$classId}2"], $currentLayout);
// break;
// default:
// $data = $this->doModifyCustomLayouts($data, $object, "{$classId}0", ["{$classId}1", "{$classId}2"], $currentLayout);
// break;
// }
// $event->setArgument("data", $data);
// }
// } catch (\Exception $e) {
// }
// }
private function GetClassificationStore(DataObject $dataObject) {
$classification = [];
$dataObject->setGetInheritedValues(true);
$classification[] = $dataObject->getDepartment();
$classification[] = $dataObject->getMainGroup();
$classification[] = $dataObject->getSubGroup();
$groupConfig = new \Pimcore\Model\DataObject\Classificationstore\GroupConfig();
$activeGroup = [];
foreach ($classification as $path) {
if (!empty($path)) {
for ($i = 0; $collection = $groupConfig->getByName("{$path}_{$i}"); ++$i) {
if (empty($collection)) {
break;
}
$activeGroup[$collection->getId()] = true;
}
}
}
$dataObject->getClassificationData()->setActiveGroups($activeGroup);
return $dataObject;
}
private function doModifyCustomLayouts($data, $object, $customLayoutToSelect = null, $layoutsToRemove = [], $currentLayout = null) {
if($customLayoutToSelect != null) {
if ($currentLayout != $customLayoutToSelect."A") {
//set current layout to subcategory layout
$data['currentLayoutId'] = $customLayoutToSelect;
$customLayout = CustomLayout::getById($customLayoutToSelect);
if ($customLayout == null) {
return $data;
}
$data['layout'] = $customLayout->getLayoutDefinitions();
Service::enrichLayoutDefinition($data["layout"], $object);
}
}
if(!empty($layoutsToRemove)) {
//remove master layout from valid layouts
$validLayouts = $data["validLayouts"];
foreach($validLayouts as $key => $validLayout) {
if(in_array($validLayout['id'], $layoutsToRemove)) {
unset($validLayouts[$key]);
}
}
$data["validLayouts"] = array_values($validLayouts);
}
$data['layout'] = $this->modifyClassificationName($data['layout']);
return $data;
}
private function modifyClassificationName($layout) {
foreach ($layout->getChildren() as $childLayout) {
if ($childLayout->fieldtype == "classificationstore") {
foreach ($childLayout->activeGroupDefinitions as $activeKey=>$activeGroup) {
if (!empty($activeGroup['description'])) {
$childLayout->activeGroupDefinitions[$activeKey]['name'] = "<div class=\"bg-indigo-500 text-white rounded-md p-1\">" . $activeGroup['description'] . "</div>";
$childLayout->activeGroupDefinitions[$activeKey]['description'] = '';
}
}
} else if (method_exists($childLayout, 'getChildren')) {
$this->modifyClassificationName($childLayout);
}
}
return $layout;
}
private function GetLevel(DataObject $dataObject) {
$levelCount = 0;
do {
$levelCount++;
$dataObject = $dataObject->getParent();
if ($levelCount > 5) {
break;
}
} while ($dataObject->getType() != "folder");
return $levelCount;
}
}