vendor/pimcore/output-data-config-toolkit-bundle/src/Service.php line 53

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;
  15. use OutputDataConfigToolkitBundle\ConfigElement\IConfigElement;
  16. use Pimcore\Model\DataObject\ClassDefinition;
  17. class Service
  18. {
  19.     /**
  20.      * @static
  21.      *
  22.      * @return IConfigElement[]
  23.      */
  24.     public static function getOutputDataConfig($object$channel$class null$context null)
  25.     {
  26.         if ($class) {
  27.             if (is_string($class)) {
  28.                 $objectClass ClassDefinition::getByName($class);
  29.                 if (empty($objectClass)) {
  30.                     throw new \Exception("Class $class not found.");
  31.                 }
  32.             } elseif ($class instanceof ClassDefinition) {
  33.                 $objectClass $class;
  34.             } else {
  35.                 throw new \Exception('Invalid Parameter class - needs to be string or ClassDefinition');
  36.             }
  37.         } else {
  38.             $objectClass $object->getClass();
  39.         }
  40.         $outputDataConfig OutputDefinition::getByO_IdClassIdChannel($object->getId(), $objectClass->getId(), $channel);
  41.         if (empty($outputDataConfig)) {
  42.             while (empty($outputDataConfig) && !empty($object)) {
  43.                 $object $object->getParent();
  44.                 $outputDataConfig OutputDefinition::getByO_IdClassIdChannel($object->getId(), $objectClass->getId(), $channel);
  45.             }
  46.         }
  47.         return self::buildOutputDataConfig($outputDataConfig$context);
  48.     }
  49.     /**
  50.      * @param $outputDataConfig
  51.      *
  52.      * @return IConfigElement[]
  53.      */
  54.     public static function buildOutputDataConfig($outputDataConfig$context null)
  55.     {
  56.         $config = [];
  57.         $jsonConfig json_decode($outputDataConfig->getConfiguration());
  58.         $config self::doBuildConfig($jsonConfig$config$context);
  59.         return $config;
  60.     }
  61.     private static function locateOperatorConfigClass($configElement): string
  62.     {
  63.         $namespaces = [
  64.             '\\OutputDataConfigToolkitBundle\\ConfigElement\\Operator\\',
  65.             '\\App\\OutputDataConfigToolkit\\ConfigElement\\Operator\\',
  66.             '\\AppBundle\\OutputDataConfigToolkit\\ConfigElement\\Operator\\'
  67.         ];
  68.         foreach ($namespaces as $namespace) {
  69.             $name $namespace.ucfirst($configElement->class);
  70.             if (class_exists($name)) {
  71.                 return $name;
  72.             }
  73.         }
  74.         return '';
  75.     }
  76.     private static function doBuildConfig($jsonConfig$config$context null)
  77.     {
  78.         if (!empty($jsonConfig)) {
  79.             foreach ($jsonConfig as $configElement) {
  80.                 if ($configElement->type == 'value') {
  81.                     $name '\\OutputDataConfigToolkitBundle\\ConfigElement\\Value\\' ucfirst($configElement->class);
  82.                     if (class_exists($name)) {
  83.                         $config[] = new $name($configElement$context);
  84.                     }
  85.                 } elseif ($configElement->type == 'operator') {
  86.                     $className self::locateOperatorConfigClass($configElement);
  87.                     if (!empty($configElement->childs)) {
  88.                         $configElement->childs self::doBuildConfig($configElement->childs, [], $context);
  89.                     }
  90.                     if (!empty($className)) {
  91.                         $config[] = new $className($configElement$context);
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.         return $config;
  97.     }
  98.     /**
  99.      * inits channels for root object
  100.      */
  101.     public static function initChannelsForRootobject()
  102.     {
  103.         $channels self::getChannels();
  104.         $classList = new ClassDefinition\Listing();
  105.         $classList $classList->load();
  106.         foreach ($classList as $class) {
  107.             foreach ($channels as $channel) {
  108.                 $def OutputDefinition::getByO_IdClassIdChannel(1$class->getId(), $channel);
  109.                 if (empty($def)) {
  110.                     $def = new OutputDefinition();
  111.                     $def->setO_Id(1);
  112.                     $def->setO_ClassId($class->getId());
  113.                     $def->setChannel($channel);
  114.                     $def->save();
  115.                 }
  116.             }
  117.         }
  118.     }
  119.     public static function getChannels()
  120.     {
  121.         $config self::getConfig();
  122.         $channels = [];
  123.         $channelsConfig $config['channels'];
  124.         if ($channelsConfig) {
  125.             foreach ($channelsConfig as $c) {
  126.                 $channels[] = (string)$c;
  127.             }
  128.         }
  129.         return $channels;
  130.     }
  131.     public static function getConfig()
  132.     {
  133.         $file \Pimcore\Config::locateConfigFile('outputdataconfig/config.php');
  134.         if (file_exists($file)) {
  135.             $config = include($file);
  136.         } else {
  137.             \Pimcore\File::put($fileto_php_data_file_format([]));
  138.             $config = [];
  139.         }
  140.         return $config;
  141.     }
  142. }