<?php
namespace AclStylesAndLooksBundle\EventListener;
use AclStylesAndLooksBundle\Service\StylesAndLooksService;
use AppBundle\EventListener\DataObjectEventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\DataObject\StylesAndLooksGallery;
use Pimcore\Model\DataObject\StylesAndLooksOnline;
class StylesAndLooksGalleryEventListener extends DataObjectEventListener
{
const PIMCORE_DATA_OBJECT_CLASS_PATH = StylesAndLooksGallery::class;
protected $object;
/**
* @param ElementEventInterface $event
* @return void
* @throws \Doctrine\DBAL\DBALException
*/
public function onObjectPreAdd(ElementEventInterface $event): void {
// Not StylesAndLooksGallery?
if(!$this->eventBelongsToThisClass($event)){
return;
}
$object = $event->getObject();
$object->setName($object->getKey());
$object->setShop(1);
}
public function onObjectPreUpdate(ElementEventInterface $event): void {
if(!$this->eventBelongsToThisClass($event)){
return;
}
$object = $event->getObject();
$gallery = $object->getGallery();
$styleName = $object->getName();
$stylesService = new StylesAndLooksService();
$imageIndex = 0;
$faultyIds = [];
//If trendname is changed, delete all old children first
$oldObject = StylesAndLooksGallery::getById($object->getId(), true);
if ($styleName != $oldObject->getName()) {
$oldStylesonline = $object->getChildren();
foreach ($oldStylesonline as $child) {
if ($child instanceof StylesAndLooksOnline) {
$child->delete();
}
}
}
//Generate StylesAndLooksOnline[76] objects as children based on the data from image names
foreach ($gallery as $image) {
$imageIndex++;
$imageName = $image->getImage();
$imageData = $stylesService->parseImageName($imageName);
$slug = 'styles-' . $stylesService->createSlug($styleName) . '-' . $imageIndex;
$stylesonline = StylesAndLooksOnline::getBySeoUrl($slug, 1);
if(!$stylesonline) {
$stylesonline = new StylesAndLooksOnline();
}
$stylesonline->setKey(\Pimcore\Model\Element\Service::getValidKey($slug, 'object'));
$stylesonline->setTrendname($styleName);
$stylesonline->setSeoUrl($slug);
$stylesonline->setParentId($object->getId());
$stylesonline->setBild($image->getImage());
$articleRelationData = $stylesService->getArticlesForRelation($imageData, $imageName);
$faultyIds = array_merge($faultyIds, $articleRelationData['faultyIDs']);
$stylesonline->setArtikel($articleRelationData['relatedProducts']);
$stylesonline->save();
}
//If image is removed from parent object, remove excess children
$childrenCount = StylesAndLooksOnline::getByTrendname($object->getName())->getCount();
if (count($gallery->getItems()) < $childrenCount) {
$excessChildren = new StylesAndLooksOnline();
while ($excessChildren) {
$imageIndex++;
$slug = 'styles-' . $stylesService->createSlug($styleName) . '-' . $imageIndex;
$excessChildren = StylesAndLooksOnline::getBySeoUrl($slug, 1);
if ($excessChildren) {
$excessChildren->delete();
}
}
}
// if (!empty($faultyIds)) {
// throw new \Error('Some articles cannot be found based on the following IDs: ' . implode(',', $faultyIds) . '. Please review the images uploaded and update the styles parent object.');
// }
}
}