vendor/pimcore/output-data-config-toolkit-bundle/src/Controller/AdminController.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace OutputDataConfigToolkitBundle\Controller;
  15. use OutputDataConfigToolkitBundle\Event\InitializeEvent;
  16. use OutputDataConfigToolkitBundle\Event\OutputDataConfigToolkitEvents;
  17. use OutputDataConfigToolkitBundle\Event\SaveConfigEvent;
  18. use OutputDataConfigToolkitBundle\OutputDefinition;
  19. use OutputDataConfigToolkitBundle\Service;
  20. use Pimcore\Logger;
  21. use Pimcore\Model\DataObject\AbstractObject;
  22. use Pimcore\Model\DataObject\ClassDefinition;
  23. use Pimcore\Model\DataObject\Classificationstore\KeyConfig;
  24. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. /**
  28.  * Class AdminController
  29.  *
  30.  * @Route("/admin")
  31.  */
  32. class AdminController extends \Pimcore\Bundle\AdminBundle\Controller\AdminController
  33. {
  34.     /* @var string[] $defaultGridClasses */
  35.     private $defaultGridClasses = [];
  36.     /* @var bool $orderByName */
  37.     private $orderByName false;
  38.     /**
  39.      * @param Request $request
  40.      *
  41.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  42.      *
  43.      * @Route("/initialize")
  44.      */
  45.     public function initializeAction(Request $requestEventDispatcherInterface $eventDispatcher)
  46.     {
  47.         $objectId $request->get('id');
  48.         $object AbstractObject::getById($objectId);
  49.         if (!$object) {
  50.             $this->adminJson(['error' => true'object' => (object)[]]);
  51.         }
  52.         $event = new InitializeEvent($object);
  53.         $eventDispatcher->dispatch($eventOutputDataConfigToolkitEvents::INITIALIZE);
  54.         if ($event->getHideConfigTab() || !$event->getObject()) {
  55.             // do not show output config tab
  56.             return $this->adminJson(['success' => true'object' => false]);
  57.         }
  58.         $data = ['id' => $event->getObject()->getId()];
  59.         return $this->adminJson(['success' => true'object' => $data]);
  60.     }
  61.     /**
  62.      * @param Request $request
  63.      *
  64.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  65.      *
  66.      * @Route("/get-output-configs")
  67.      */
  68.     public function getOutputConfigsAction(Request $request)
  69.     {
  70.         Service::initChannelsForRootobject();
  71.         $channels Service::getChannels();
  72.         $objectId $request->get('object_id');
  73.         $object AbstractObject::getById($objectId);
  74.         $classList $this->getFilteredClassDefinitionList($request);
  75.         if ($this->getOrderByName()) {
  76.             $classList->setOrderKey('name');
  77.             $classList->setOrder('ASC');
  78.         }
  79.         $classList $classList->load();
  80.         $translator $this->get('translator');
  81.         $outputDefinitions = [];
  82.         foreach ($classList as $class) {
  83.             foreach ($channels as $channel) {
  84.                 $def $this->getOutputDefinitionForObjectAndChannel($object$class->getId(), $channel);
  85.                 $outputDefinitions[] = [
  86.                     'id' => $def->getId(),
  87.                     'classname' => $translator->trans($class->getName(), [], 'admin'),
  88.                     'channel' => $translator->trans($channel, [], 'admin'),
  89.                     'object_id' => $def->getO_Id(),
  90.                     'is_inherited' => $def->getO_Id() != $objectId
  91.                 ];
  92.             }
  93.         }
  94.         return $this->adminJson(['success' => true'data' => $outputDefinitions]);
  95.     }
  96.     /**
  97.      * @param $object
  98.      * @param $classId
  99.      * @param $channel
  100.      *
  101.      * @return OutputDefinition
  102.      */
  103.     private function getOutputDefinitionForObjectAndChannel($object$classId$channel)
  104.     {
  105.         $outputDefinition OutputDefinition::getByO_IdClassIdChannel($object->getId(), $classId$channel);
  106.         if (empty($outputDefinition)) {
  107.             $parent $object->getParent();
  108.             if (!empty($parent)) {
  109.                 return $this->getOutputDefinitionForObjectAndChannel($parent$classId$channel);
  110.             }
  111.         }
  112.         return $outputDefinition;
  113.     }
  114.     /**
  115.      * @param Request $request
  116.      *
  117.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  118.      *
  119.      * @Route("/reset-output-config")
  120.      */
  121.     public function resetOutputConfigAction(Request $request)
  122.     {
  123.         try {
  124.             $config OutputDefinition::getByID($request->get('config_id'));
  125.             $config->delete();
  126.             return $this->adminJson(['success' => true]);
  127.         } catch (\Exception $e) {
  128.             Logger::err($e->getMessage(), $e);
  129.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  130.         }
  131.     }
  132.     /**
  133.      * @param Request $request
  134.      *
  135.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  136.      *
  137.      * @Route("/get-output-config")
  138.      */
  139.     public function getOutputConfigAction(Request $request)
  140.     {
  141.         try {
  142.             $config OutputDefinition::getByID($request->get('config_id'));
  143.             $objectClass ClassDefinition::getById($config->getO_ClassId());
  144.             $configuration json_decode($config->getConfiguration());
  145.             $configuration $this->doGetAttributeLabels($configuration$objectClass);
  146.             $config->setConfiguration($configuration);
  147.             return $this->adminJson(['success' => true'outputConfig' => $config]);
  148.         } catch (\Exception $e) {
  149.             Logger::err($e);
  150.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  151.         }
  152.     }
  153.     /**
  154.      * @param Request $request
  155.      *
  156.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  157.      *
  158.      * @Route("/get-or-create-output-config")
  159.      */
  160.     public function getOrCreateOutputConfigAction(Request $request)
  161.     {
  162.         try {
  163.             $config OutputDefinition::getByID($request->get('config_id'));
  164.             if (!$config) {
  165.                 if (is_numeric($request->get('class_id'))) {
  166.                     $class ClassDefinition::getById($request->get('class_id'));
  167.                 } else {
  168.                     $class ClassDefinition::getByName($request->get('class_id'));
  169.                 }
  170.                 if (!$class) {
  171.                     throw new \Exception('Class ' $request->get('class_id') . ' not found.');
  172.                 }
  173.                 $config OutputDefinition::getByO_IdClassIdChannel($request->get('o_id'), $class->getId(), $request->get('channel'));
  174.             }
  175.             if ($config) {
  176.                 $objectClass ClassDefinition::getById($config->getO_ClassId());
  177.                 $configuration json_decode($config->getConfiguration());
  178.                 $configuration $this->doGetAttributeLabels($configuration$objectClass);
  179.                 $config->setConfiguration($configuration);
  180.                 return $this->adminJson(['success' => true'outputConfig' => $config]);
  181.             } else {
  182.                 $config = new OutputDefinition();
  183.                 $config->setChannel($request->get('channel'));
  184.                 $config->setO_ClassId($class->getId());
  185.                 $config->setO_Id($request->get('o_id'));
  186.                 $config->save();
  187.                 return $this->adminJson(['success' => true'outputConfig' => $config]);
  188.             }
  189.         } catch (\Exception $e) {
  190.             Logger::err($e->getMessage(), $e);
  191.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  192.         }
  193.     }
  194.     /**
  195.      * @param $configuration
  196.      * @param $objectClass
  197.      *
  198.      * @return array
  199.      */
  200.     private function doGetAttributeLabels($configuration$objectClassbool $sort false)
  201.     {
  202.         $newConfiguration = [];
  203.         if (!empty($configuration)) {
  204.             foreach ($configuration as $c) {
  205.                 $newConfig $c;
  206.                 if (!empty($newConfig->label)) {
  207.                     $newConfig->text $newConfig->label;
  208.                 } else {
  209.                     $def null;
  210.                     if (isset($newConfig->attribute)) {
  211.                         $def $this->getFieldDefinition($newConfig->attribute$objectClass);
  212.                     }
  213.                     if ($def) {
  214.                         $translator $this->get('translator');
  215.                         $newConfig->text $translator->trans($def->getTitle(), [], 'admin');
  216.                     }
  217.                     if (isset($newConfig->dataType) && $newConfig->dataType == 'system' && isset($newConfig->attribute)) {
  218.                         $newConfig->text $newConfig->attribute;
  219.                     }
  220.                 }
  221.                 $children $this->doGetAttributeLabels($c->childs$objectClass$sort);
  222.                 if ($sort) {
  223.                     $this->sortAttributes($children);
  224.                 }
  225.                 $newConfig->childs $children;
  226.                 $newConfiguration[] = $newConfig;
  227.             }
  228.         }
  229.         if ($sort) {
  230.             $this->sortAttributes($newConfiguration);
  231.         }
  232.         return $newConfiguration;
  233.     }
  234.     private function sortAttributes(array &$attributes)
  235.     {
  236.         //@todo only sort if enabled in config...
  237.         usort($attributes, function ($a1$a2) {
  238.             return strcmp($a1->text$a2->text);
  239.         });
  240.     }
  241.     /**
  242.      * @param Request $request
  243.      *
  244.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  245.      *
  246.      * @Route("/get-attribute-labels")
  247.      */
  248.     public function getAttributeLabelsAction(Request $request)
  249.     {
  250.         $configration json_decode($request->get('configuration'));
  251.         $class ClassDefinition::getById($request->get('classId'));
  252.         $configration $this->doGetAttributeLabels($configration$class);
  253.         return $this->adminJson(['configuration' => $configration]);
  254.     }
  255.     /**
  256.      * @param $attributeName
  257.      * @param $objectClass
  258.      *
  259.      * @return mixed|ClassDefinition\Data|null
  260.      */
  261.     private function getFieldDefinition($attributeName$objectClass)
  262.     {
  263.         $label null;
  264.         $brickKey null;
  265.         $attributeParts explode('~'$attributeName);
  266.         if (substr($attributeName01) == '~') {
  267.             // key value, ignore for now
  268.         } elseif (count($attributeParts) > 1) {
  269.             $brickType $attributeParts[0];
  270.             $brickKey $attributeParts[1];
  271.         }
  272.         $def null;
  273.         $brickInfos null;
  274.         $classificationPrefix '#cs#';
  275.         if (substr($attributeName0strlen($classificationPrefix)) == $classificationPrefix) {
  276.             $attributeName substr($attributeNamestrlen($classificationPrefix));
  277.             $classificationKeyParts explode('#'$attributeName);
  278.             $classificationKeyId $classificationKeyParts[0];
  279.             $classificationKeyName $classificationKeyParts[1];
  280.             if (!empty($classificationKeyId)) { // for localized classification store attributes, such as #cs##Ba the key will be empty.
  281.                 if ($keyConfig KeyConfig::getById($classificationKeyId)) {
  282.                     $def \Pimcore\Model\DataObject\Classificationstore\Service::getFieldDefinitionFromKeyConfig($keyConfig);
  283.                 }
  284.             }
  285.         } elseif ($brickKey && strpos($brickType'?') === 0) {
  286.             $definitionJson substr($brickType1);
  287.             $brickInfos json_decode($definitionJson);
  288.             $containerKey $brickInfos->containerKey;
  289.             $fieldName $brickInfos->fieldname;
  290.             $brickfield $brickInfos->brickfield;
  291.             try {
  292.                 $brickDef \Pimcore\Model\DataObject\Objectbrick\Definition::getByKey($containerKey);
  293.                 $def $brickDef->getFieldDefinition($brickKey);
  294.                 if (empty($def) && $brickDef->getFieldDefinition('localizedfields')) {
  295.                     $def $brickDef->getFieldDefinition('localizedfields')->getFieldDefinition($brickfield);
  296.                 }
  297.             } catch (\Exception $e) {
  298.                 Logger::err($e);
  299.             }
  300.         } else {
  301.             $def $objectClass->getFieldDefinition($attributeName);
  302.         }
  303.         if (!$def && !empty($brickType)) {
  304.             try {
  305.                 $def \Pimcore\Model\DataObject\Objectbrick\Definition::getByKey($brickType);
  306.                 $def $def->getFieldDefinition($brickKey);
  307.             } catch (\Exception $e) {
  308.                 Logger::err($e);
  309.             }
  310.         }
  311.         if (empty($def) && $objectClass->getFieldDefinition('localizedfields')) {
  312.             $def $objectClass->getFieldDefinition('localizedfields')->getFieldDefinition($attributeName);
  313.         }
  314.         return $def;
  315.     }
  316.     /**
  317.      * @param Request $request
  318.      * @Route("/get-field-definition")
  319.      *
  320.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  321.      */
  322.     public function getFieldDefinitionAction(Request $request)
  323.     {
  324.         try {
  325.             $objectClass ClassDefinition::getById($request->get('class_id'));
  326.             $def $this->getFieldDefinition($request->get('key'), $objectClass);
  327.             return $this->adminJson(['success' => true'fieldDefinition' => $def]);
  328.         } catch (\Exception $e) {
  329.             Logger::err($e->getMessage(), $e);
  330.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  331.         }
  332.     }
  333.     /**
  334.      * @param Request $request
  335.      * @Route("/save-output-config")
  336.      *
  337.      * @return \Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse
  338.      */
  339.     public function saveOutputConfigAction(Request $requestEventDispatcherInterface $eventDispatcher)
  340.     {
  341.         try {
  342.             $config OutputDefinition::getByID($request->get('config_id'));
  343.             $object AbstractObject::getById($request->get('object_id'));
  344.             if (empty($object)) {
  345.                 throw new \Exception('Data Object with ID' $request->get('object_id') . ' not found.');
  346.             }
  347.             if ($config->getO_Id() == $request->get('object_id')) {
  348.             } else {
  349.                 $newConfig = new OutputDefinition();
  350.                 $newConfig->setChannel($config->getChannel());
  351.                 $newConfig->setO_ClassId($config->getO_ClassId());
  352.                 $newConfig->setO_Id($object->getId());
  353.                 $config $newConfig;
  354.             }
  355.             $configJson $request->get('config');
  356.             $config->setConfiguration($configJson);
  357.             $event = new SaveConfigEvent($config);
  358.             $eventDispatcher->dispatch($eventOutputDataConfigToolkitEvents::SAVE_CONFIG_EVENT);
  359.             if ($event->doSortAttributes()) {
  360.                 $objectClass ClassDefinition::getById($config->getO_ClassId());
  361.                 $configuration json_decode($configJson);
  362.                 $configuration $this->doGetAttributeLabels($configuration$objectClasstrue);
  363.                 $configJson json_encode($configuration);
  364.                 $config->setConfiguration($configJson);
  365.             }
  366.             $config->save();
  367.             return $this->adminJson(['success' => true]);
  368.         } catch (\Exception $e) {
  369.             Logger::err($e->getMessage(), $e);
  370.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  371.         }
  372.     }
  373.     /**
  374.      * @param Request $request
  375.      *
  376.      * @return ClassDefinition\Listing
  377.      */
  378.     private function getFilteredClassDefinitionList(Request $request): ClassDefinition\Listing
  379.     {
  380.         $classList = new ClassDefinition\Listing();
  381.         if ($request->get('class_id')) {
  382.             $classList->setCondition('id = ?'$request->get('class_id'));
  383.         } elseif (!empty($this->defaultGridClasses)) {
  384.             $allowedClassIds = [];
  385.             foreach ($this->defaultGridClasses as $allowedClass) {
  386.                 $classNamespace 'Pimcore\\Model\\DataObject\\';
  387.                 $allowedClassArr explode('\\'$allowedClass);
  388.                 $allowedClassFull $classNamespace array_pop($allowedClassArr);
  389.                 if (class_exists($allowedClassFull)) {
  390.                     $allowedClassIds[] = call_user_func([$allowedClassFull'classId']);
  391.                 } else {
  392.                     $allowedClassIds[] = $allowedClass;
  393.                 }
  394.             }
  395.             $classList->addConditionParam("id IN ('" implode("','"$allowedClassIds) . "')");
  396.         }
  397.         return $classList;
  398.     }
  399.     /**
  400.      * @param string[] $defaultGridClasses
  401.      *
  402.      * @return AdminController
  403.      */
  404.     public function setDefaultGridClasses(array $defaultGridClasses): self
  405.     {
  406.         $this->defaultGridClasses $defaultGridClasses;
  407.         return $this;
  408.     }
  409.     /**
  410.      * @return string[]
  411.      */
  412.     public function getDefaultGridClasses(): array
  413.     {
  414.         return $this->defaultGridClasses;
  415.     }
  416.     /**
  417.      * @return bool
  418.      */
  419.     public function getOrderByName(): bool
  420.     {
  421.         return $this->orderByName;
  422.     }
  423.     /**
  424.      * @param bool $orderByName
  425.      *
  426.      * @return AdminController
  427.      */
  428.     public function setOrderByName(bool $orderByName): self
  429.     {
  430.         $this->orderByName $orderByName;
  431.         return $this;
  432.     }
  433. }