<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace IndabaExtensionBundle\EventListener;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* Description of ClassEventListener
*
* @author PcCom
*/
class ClassEventListener
{
public function changeObjectbricksTree(GenericEvent $event)
{
if (\Pimcore::getContainer()->getParameter("indabaExtension.dataObject.onlyAllowedObjectbricks")) {
$list = $event->getArgument('list');
$objectId = $event->getArgument('objectId');
if ($objectId > 0) {
$object = \Pimcore\Model\DataObject\AbstractObject::getById($objectId);
$allowedString = $object->getProperty("allowedObjectbricks");
$allowedList = array();
if (!empty($allowedString)) {
$allowedList = explode(",", str_replace(" ", "", $allowedString));
}
if (is_array($allowedList) && count($allowedList) > 0) {
if ("none" === reset($allowedList)) {
$list = [];
} else {
foreach ($list as $key1 => $objectbrick_node1) {
if (array_key_exists("children", $objectbrick_node1)) {
foreach ($objectbrick_node1['children'] as $key2 => $objectbrick_node2) {
if (!in_array($objectbrick_node2['id'], $allowedList)) {
unset($list[$key1]["children"][$key2]);
}
}
if (0 == count($list[$key1]["children"])) {
unset($list[$key1]);
}
} else {
if (!in_array($objectbrick_node1['id'], $allowedList)) {
unset($list[$key1]);
}
}
}
sort($list);
}
}
}
$event->setArgument('list', $list);
}
}
}