vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/DataObject/VariantsController.php line 66

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 Enterprise License (PEL)
  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 PEL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject;
  15. use Pimcore\Bundle\AdminBundle\Controller\AdminController;
  16. use Pimcore\Bundle\AdminBundle\Helper\GridHelperService;
  17. use Pimcore\Model\DataObject;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. /**
  22.  * @Route("/variants")
  23.  */
  24. class VariantsController extends AdminController
  25. {
  26.     /**
  27.      * @Route("/update-key", name="pimcore_admin_dataobject_variants_updatekey", methods={"PUT"})
  28.      *
  29.      * @param Request $request
  30.      *
  31.      * @return JsonResponse
  32.      */
  33.     public function updateKeyAction(Request $request)
  34.     {
  35.         $id $request->get('id');
  36.         $key $request->get('key');
  37.         $object DataObject\Concrete::getById($id);
  38.         try {
  39.             if (!empty($object)) {
  40.                 $object->setKey($key);
  41.                 $object->save();
  42.                 return $this->adminJson(['success' => true]);
  43.             } else {
  44.                 throw new \Exception('No Object found for given id.');
  45.             }
  46.         } catch (\Exception $e) {
  47.             return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  48.         }
  49.     }
  50.     /**
  51.      * @Route("/get-variants", name="pimcore_admin_dataobject_variants_getvariants", methods={"GET", "POST"})
  52.      *
  53.      * @param Request $request
  54.      * @param GridHelperService $gridHelperService
  55.      *
  56.      * @return JsonResponse
  57.      *
  58.      * @throws \Exception
  59.      */
  60.     public function getVariantsAction(Request $requestGridHelperService $gridHelperService)
  61.     {
  62.         // get list of variants
  63.         if ($request->get('language')) {
  64.             $request->setLocale($request->get('language'));
  65.         }
  66.         if ($request->get('xaction') == 'update') {
  67.             $data $this->decodeJson($request->get('data'));
  68.             // save
  69.             $object DataObject\Concrete::getById($data['id']);
  70.             $class $object->getClass();
  71.             $requestedLanguage $allParams['language'] ?? null;
  72.             if ($requestedLanguage) {
  73.                 if ($requestedLanguage != 'default') {
  74.                     //                $this->get('translator')->setLocale($requestedLanguage);
  75.                     $request->setLocale($requestedLanguage);
  76.                 }
  77.             } else {
  78.                 $requestedLanguage $request->getLocale();
  79.             }
  80.             if ($object->isAllowed('publish')) {
  81.                 $objectData = [];
  82.                 foreach ($data as $key => $value) {
  83.                     $parts explode('~'$key);
  84.                     if (substr($key01) == '~') {
  85.                         $type $parts[1];
  86.                         $field $parts[2];
  87.                         $keyid $parts[3];
  88.                         if ($type == 'classificationstore') {
  89.                             $groupKeyId explode('-'$keyid);
  90.                             $groupId $groupKeyId[0];
  91.                             $keyid $groupKeyId[1];
  92.                             $getter 'get' ucfirst($field);
  93.                             if (method_exists($object$getter)) {
  94.                                 /** @var DataObject\ClassDefinition\Data\Classificationstore $csFieldDefinition */
  95.                                 $csFieldDefinition $object->getClass()->getFieldDefinition($field);
  96.                                 $csLanguage $requestedLanguage;
  97.                                 if (!$csFieldDefinition->isLocalized()) {
  98.                                     $csLanguage 'default';
  99.                                 }
  100.                                 /** @var DataObject\Classificationstore $classificationStoreData */
  101.                                 $classificationStoreData $object->$getter();
  102.                                 $keyConfig DataObject\Classificationstore\KeyConfig::getById($keyid);
  103.                                 if ($keyConfig) {
  104.                                     $fieldDefinition $keyDef DataObject\Classificationstore\Service::getFieldDefinitionFromJson(
  105.                                         json_decode($keyConfig->getDefinition()),
  106.                                         $keyConfig->getType()
  107.                                     );
  108.                                     if ($fieldDefinition && method_exists($fieldDefinition'getDataFromGridEditor')) {
  109.                                         $value $fieldDefinition->getDataFromGridEditor($value$object, []);
  110.                                     }
  111.                                 }
  112.                                 $activeGroups $classificationStoreData->getActiveGroups() ? $classificationStoreData->getActiveGroups() : [];
  113.                                 $activeGroups[$groupId] = true;
  114.                                 $classificationStoreData->setActiveGroups($activeGroups);
  115.                                 $classificationStoreData->setLocalizedKeyValue($groupId$keyid$value$csLanguage);
  116.                             }
  117.                         }
  118.                     } elseif (count($parts) > 1) {
  119.                         $brickType $parts[0];
  120.                         $brickDescriptor null;
  121.                         if (strpos($brickType'?') !== false) {
  122.                             $brickDescriptor substr($brickType1);
  123.                             $brickDescriptor json_decode($brickDescriptortrue);
  124.                             $brickType $brickDescriptor['containerKey'];
  125.                         }
  126.                         $brickKey $parts[1];
  127.                         $brickField DataObject\Service::getFieldForBrickType($object->getClass(), $brickType);
  128.                         $fieldGetter 'get' ucfirst($brickField);
  129.                         $brickGetter 'get' ucfirst($brickType);
  130.                         $valueSetter 'set' ucfirst($brickKey);
  131.                         $brick $object->$fieldGetter()->$brickGetter();
  132.                         if (empty($brick)) {
  133.                             $classname '\\Pimcore\\Model\\DataObject\\Objectbrick\\Data\\' ucfirst($brickType);
  134.                             $brickSetter 'set' ucfirst($brickType);
  135.                             $brick = new $classname($object);
  136.                             $object->$fieldGetter()->$brickSetter($brick);
  137.                         }
  138.                         if ($brickDescriptor) {
  139.                             $brickDefinition DataObject\Objectbrick\Definition::getByKey($brickType);
  140.                             /** @var DataObject\ClassDefinition\Data\Localizedfields $fieldDefinitionLocalizedFields */
  141.                             $fieldDefinitionLocalizedFields $brickDefinition->getFieldDefinition('localizedfields');
  142.                             $fieldDefinition $fieldDefinitionLocalizedFields->getFieldDefinition($brickKey);
  143.                         } else {
  144.                             $fieldDefinition $this->getFieldDefinitionFromBrick($brickType$brickKey);
  145.                         }
  146.                         if ($fieldDefinition && method_exists($fieldDefinition'getDataFromGridEditor')) {
  147.                             $value $fieldDefinition->getDataFromGridEditor($value$object, []);
  148.                         }
  149.                         if ($brickDescriptor) {
  150.                             /** @var DataObject\Localizedfield $localizedFields */
  151.                             $localizedFields $brick->getLocalizedfields();
  152.                             $localizedFields->setLocalizedValue($brickKey$value);
  153.                         } else {
  154.                             $brick->$valueSetter($value);
  155.                         }
  156.                     }
  157.                     $fieldDefinition $this->getFieldDefinition($class$key);
  158.                     if ($fieldDefinition && method_exists($fieldDefinition'getDataFromGridEditor')) {
  159.                         $value $fieldDefinition->getDataFromGridEditor($value$object, []);
  160.                     }
  161.                     $objectData[$key] = $value;
  162.                 }
  163.                 $object->setValues($objectData);
  164.                 try {
  165.                     $object->save();
  166.                     return $this->adminJson(['data' => DataObject\Service::gridObjectData($object$request->get('fields')), 'success' => true]);
  167.                 } catch (\Exception $e) {
  168.                     return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  169.                 }
  170.             } else {
  171.                 throw new \Exception('Permission denied');
  172.             }
  173.         } else {
  174.             $parentObject DataObject\Concrete::getById($request->get('objectId'));
  175.             if (empty($parentObject)) {
  176.                 throw new \Exception('No Object found with id ' $request->get('objectId'));
  177.             }
  178.             if ($parentObject->isAllowed('view')) {
  179.                 $allParams array_merge($request->request->all(), $request->query->all());
  180.                 //specify a few special params
  181.                 $allParams['folderId'] = $parentObject->getId();
  182.                 $allParams['only_direct_children'] = 'true';
  183.                 $allParams['classId'] = $parentObject->getClassId();
  184.                 $list $gridHelperService->prepareListingForGrid($allParams$request->getLocale(), $this->getAdminUser());
  185.                 $list->setObjectTypes([DataObject\AbstractObject::OBJECT_TYPE_VARIANT]);
  186.                 $list->load();
  187.                 $objects = [];
  188.                 foreach ($list->getObjects() as $object) {
  189.                     if ($object->isAllowed('view')) {
  190.                         $o DataObject\Service::gridObjectData($object$request->get('fields'));
  191.                         $objects[] = $o;
  192.                     }
  193.                 }
  194.                 return $this->adminJson(['data' => $objects'success' => true'total' => $list->getTotalCount()]);
  195.             } else {
  196.                 throw new \Exception('Permission denied');
  197.             }
  198.         }
  199.     }
  200.     /**
  201.      * @param DataObject\ClassDefinition $class
  202.      * @param string $key
  203.      *
  204.      * @return DataObject\ClassDefinition\Data|null
  205.      */
  206.     protected function getFieldDefinition($class$key)
  207.     {
  208.         $fieldDefinition $class->getFieldDefinition($key);
  209.         if ($fieldDefinition) {
  210.             return $fieldDefinition;
  211.         }
  212.         $localized $class->getFieldDefinition('localizedfields');
  213.         if ($localized instanceof DataObject\ClassDefinition\Data\Localizedfields) {
  214.             $fieldDefinition $localized->getFieldDefinition($key);
  215.         }
  216.         return $fieldDefinition;
  217.     }
  218.     /**
  219.      * @param string $brickType
  220.      * @param string $key
  221.      *
  222.      * @return DataObject\ClassDefinition\Data|null
  223.      */
  224.     protected function getFieldDefinitionFromBrick($brickType$key)
  225.     {
  226.         $brickDefinition DataObject\Objectbrick\Definition::getByKey($brickType);
  227.         $fieldDefinition null;
  228.         if ($brickDefinition) {
  229.             $fieldDefinition $brickDefinition->getFieldDefinition($key);
  230.         }
  231.         return $fieldDefinition;
  232.     }
  233. }