<?php
declare(strict_types=1);
namespace AppBundle\EventListener;
use Exception;
use Pimcore\Event\BundleManager\PathsEvent;
use Pimcore\Model\DataObject\Folder;
use Symfony\Component\EventDispatcher\GenericEvent;
class AdminListener
{
public const FOLDER_UPGRADE_TOOL = 'UpgradeTool';
public const FOLDER_SERIAL_NUMBERS = 'SerialNumbers';
public function onObjectListBeforeLoad(GenericEvent $e)
{
$this->createFolderStructure();
}
public function addJSFiles(PathsEvent $event)
{
$event->setPaths(
array_merge(
$event->getPaths(),
[
'/bundles/app/js/postmessage.js'
]
)
);
}
/**
* @throws Exception
*/
protected function createFolderStructure(): void
{
$upgradeToolFolder = Folder::getByPath('/' . self::FOLDER_UPGRADE_TOOL);
if ($upgradeToolFolder === null) {
$rootFolder = Folder::getByPath('/');
if ($rootFolder === null) {
throw new Exception('No Root DataObject Folder found!!');
}
$upgradeToolFolder = new Folder();
$upgradeToolFolder->setParentId($rootFolder->getId());
$upgradeToolFolder->setKey(self::FOLDER_UPGRADE_TOOL);
$upgradeToolFolder->setLocked('self');
try {
$upgradeToolFolder->save();
} catch (Exception $e) {
}
$upgradeToolFolder->setLocked('self');
}
if ($upgradeToolFolder === null || !$upgradeToolFolder instanceof Folder) {
throw new Exception('No Root DataObject Folder found!!');
}
$serialNumberFolder = Folder::getByPath('/' . self::FOLDER_UPGRADE_TOOL . '/' . self::FOLDER_SERIAL_NUMBERS);
if ($serialNumberFolder === null) {
$serialNumberFolder = new Folder();
$serialNumberFolder->setParentId($upgradeToolFolder->getId());
$serialNumberFolder->setKey(self::FOLDER_SERIAL_NUMBERS);
$serialNumberFolder->setLocked('propagate');
try {
$serialNumberFolder->save();
} catch (Exception $e) {
}
}
$serialNumberFolder->setLocked('propagate')->save();
if ($serialNumberFolder === null || !$serialNumberFolder instanceof Folder) {
throw new Exception('No SerialNumber DataObject Folder found!!');
}
}
}