<?php
namespace Jabber\GenericBundle\EventListener;
use \Pimcore\Bundle\AdminBundle\Controller\AdminController;
use Symfony\Component\EventDispatcher\GenericEvent;
class GridListener extends AdminController {
public function beforeListLoad(GenericEvent $list, $context) {
$test = "test";
$objects = $list->getArgument('list');
$listData = $objects['data'];
foreach ($listData as $key=>$object) {
if (isset($object['AssociatedProducts'])) {
$listData = $this->GetAssociatedProductValue($listData, $key, $object['AssociatedProducts']);
}
if (isset($object['ProductLevel'])) {
$listData = $this->GetProductLevel($listData, $key, $object['ProductLevel']);
}
}
$objects['data'] = $listData;
$list->setArgument('list', $objects);
}
private function GetAssociatedProductValue($listData, $key, $associatedProducts) {
foreach ($associatedProducts as $linkKey=>$linkValue) {
if (isset($linkValue['PmCode']) && !empty($linkValue['PmCode'])) {
$listData[$key]['AssociatedProducts'][$linkKey]['fullpath'] = $linkValue['PmCode'];
continue;
}
if (!empty($linkValue['Colour'])) {
$colour = array_filter($linkValue['Colour%options'], function($ar) use($linkValue) {
return ($ar['value'] == $linkValue['Colour']);
//return ($ar['name'] == 'cat 1' AND $ar['id'] == '3');// you can add multiple conditions
});
$colour = reset($colour)['key'] ?? '';
} else {
$colour = "";
}
$colour = !empty($colour) ? '-'.$colour : '';
$listData[$key]['AssociatedProducts'][$linkKey]['fullpath'] = "<span class=\"text-xs bg-pink-50 border border-1 rounded-md px-1\">" . $linkValue['SKU'].$colour . "</span>";
}
return $listData;
}
private function GetProductLevel($listData, $key, $productLevel) {
if ($productLevel == "PARENT") {
$value = "<div class='bg-purple-100 text-purple-700 text-center rounded-md'>PARENT</div>";
} else if ($productLevel == "STYLE") {
$value = "<div class='bg-blue-100 text-blue-700 text-center rounded-md'>STYLE</div>";
} else if ($productLevel == "CHILD") {
$value = "<div class='bg-yellow-100 text-yellow-700 text-center rounded-md'>CHILD</div>";
}
$listData[$key]['ProductLevel'] = $value;
return $listData;
}
}