vendor/pimcore/pimcore/models/DataObject/Fieldcollection.php line 265

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 Pimcore\Model\DataObject;
  15. use Pimcore\Model;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\Element\DirtyIndicatorInterface;
  18. /**
  19.  * @method array delete(Concrete $object, $saveMode = false)
  20.  * @method Fieldcollection\Dao getDao()
  21.  * @method array load(Concrete $object)
  22.  */
  23. class Fieldcollection extends Model\AbstractModel implements \IteratorDirtyIndicatorInterface
  24. {
  25.     use Model\Element\Traits\DirtyIndicatorTrait;
  26.     /**
  27.      * @internal
  28.      *
  29.      * @var Model\DataObject\Fieldcollection\Data\AbstractData[]
  30.      */
  31.     protected $items = [];
  32.     /**
  33.      * @internal
  34.      *
  35.      * @var string
  36.      */
  37.     protected $fieldname;
  38.     /**
  39.      * @param Model\DataObject\Fieldcollection\Data\AbstractData[] $items
  40.      * @param string|null $fieldname
  41.      */
  42.     public function __construct($items = [], $fieldname null)
  43.     {
  44.         if (!empty($items)) {
  45.             $this->setItems($items);
  46.         }
  47.         if ($fieldname) {
  48.             $this->setFieldname($fieldname);
  49.         }
  50.         $this->markFieldDirty('_self'true);
  51.     }
  52.     /**
  53.      * @return Model\DataObject\Fieldcollection\Data\AbstractData[]
  54.      */
  55.     public function getItems()
  56.     {
  57.         return $this->items;
  58.     }
  59.     /**
  60.      * @param Model\DataObject\Fieldcollection\Data\AbstractData[] $items
  61.      *
  62.      * @return $this
  63.      */
  64.     public function setItems($items)
  65.     {
  66.         $this->items $items;
  67.         $this->markFieldDirty('_self'true);
  68.         return $this;
  69.     }
  70.     /**
  71.      * @return string
  72.      */
  73.     public function getFieldname()
  74.     {
  75.         return $this->fieldname;
  76.     }
  77.     /**
  78.      * @param string $fieldname
  79.      *
  80.      * @return $this
  81.      */
  82.     public function setFieldname($fieldname)
  83.     {
  84.         $this->fieldname $fieldname;
  85.         return $this;
  86.     }
  87.     /**
  88.      * @internal
  89.      *
  90.      * @return Fieldcollection\Definition[]
  91.      */
  92.     public function getItemDefinitions()
  93.     {
  94.         $definitions = [];
  95.         foreach ($this->getItems() as $item) {
  96.             $definitions[$item->getType()] = $item->getDefinition();
  97.         }
  98.         return $definitions;
  99.     }
  100.     /**
  101.      * @param Concrete $object
  102.      * @param array $params
  103.      *
  104.      * @throws \Exception
  105.      */
  106.     public function save($object$params = [])
  107.     {
  108.         $saveRelationalData $this->getDao()->save($object$params);
  109.         /** @var Model\DataObject\ClassDefinition\Data\Fieldcollections $fieldDef */
  110.         $fieldDef $object->getClass()->getFieldDefinition($this->getFieldname());
  111.         $allowedTypes $fieldDef->getAllowedTypes();
  112.         $collectionItems $this->getItems();
  113.         if (is_array($collectionItems)) {
  114.             $index 0;
  115.             foreach ($collectionItems as $collection) {
  116.                 if ($collection instanceof Fieldcollection\Data\AbstractData) {
  117.                     if (in_array($collection->getType(), $allowedTypes)) {
  118.                         $collection->setFieldname($this->getFieldname());
  119.                         $collection->setIndex($index++);
  120.                         $params['owner'] = $collection;
  121.                         // set the current object again, this is necessary because the related object in $this->object can change (eg. clone & copy & paste, etc.)
  122.                         $collection->setObject($object);
  123.                         $collection->save($object$params$saveRelationalData);
  124.                     } else {
  125.                         throw new \Exception('Fieldcollection of type ' $collection->getType() . ' is not allowed in field: ' $this->getFieldname());
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
  131.     /**
  132.      * @return bool
  133.      */
  134.     public function isEmpty()
  135.     {
  136.         return count($this->getItems()) < 1;
  137.     }
  138.     /**
  139.      * @param Model\DataObject\Fieldcollection\Data\AbstractData $item
  140.      */
  141.     public function add($item)
  142.     {
  143.         $this->items[] = $item;
  144.         $this->markFieldDirty('_self'true);
  145.     }
  146.     /**
  147.      * @param int $index
  148.      */
  149.     public function remove($index)
  150.     {
  151.         if (isset($this->items[$index])) {
  152.             array_splice($this->items$index1);
  153.             $this->markFieldDirty('_self'true);
  154.         }
  155.     }
  156.     /**
  157.      * @param int $index
  158.      *
  159.      * @return Fieldcollection\Data\AbstractData|null
  160.      */
  161.     public function get($index)
  162.     {
  163.         return $this->items[$index] ?? null;
  164.     }
  165.     /**
  166.      * @param int|null $index
  167.      *
  168.      * @return Fieldcollection\Data\AbstractData|null
  169.      */
  170.     private function getByOriginalIndex($index)
  171.     {
  172.         if ($index === null) {
  173.             return null;
  174.         }
  175.         if (is_array($this->items)) {
  176.             foreach ($this->items as $item) {
  177.                 if ($item->getIndex() === $index) {
  178.                     return $item;
  179.                 }
  180.             }
  181.         }
  182.         return null;
  183.     }
  184.     /**
  185.      * @return int
  186.      */
  187.     public function getCount()
  188.     {
  189.         return count($this->getItems());
  190.     }
  191.     /**
  192.      * Methods for Iterator
  193.      */
  194.     /**
  195.      * {@inheritdoc}
  196.      */
  197.     public function rewind()
  198.     {
  199.         reset($this->items);
  200.     }
  201.     /**
  202.      * {@inheritdoc}
  203.      */
  204.     public function current()
  205.     {
  206.         $var current($this->items);
  207.         return $var;
  208.     }
  209.     /**
  210.      * {@inheritdoc}
  211.      */
  212.     public function key()
  213.     {
  214.         $var key($this->items);
  215.         return $var;
  216.     }
  217.     /**
  218.      * {@inheritdoc}
  219.      */
  220.     public function next()
  221.     {
  222.         next($this->items);
  223.     }
  224.     /**
  225.      * {@inheritdoc}
  226.      */
  227.     public function valid()
  228.     {
  229.         $var $this->current() !== false;
  230.         return $var;
  231.     }
  232.     /**
  233.      * @param Concrete $object
  234.      * @param string $type
  235.      * @param string $fcField
  236.      * @param int $index
  237.      * @param string $field
  238.      *
  239.      * @throws \Exception
  240.      *
  241.      * @internal
  242.      */
  243.     public function loadLazyField(Concrete $object$type$fcField$index$field)
  244.     {
  245.         // lazy loading existing can be data if the item already had an index
  246.         $item $this->getByOriginalIndex($index);
  247.         if ($item && !$item->isLazyKeyLoaded($field)) {
  248.             if ($type == $item->getType()) {
  249.                 $fcDef Model\DataObject\Fieldcollection\Definition::getByKey($type);
  250.                 /** @var Model\DataObject\ClassDefinition\Data\CustomResourcePersistingInterface $fieldDef */
  251.                 $fieldDef $fcDef->getFieldDefinition($field);
  252.                 $params = [
  253.                     'context' => [
  254.                         'object' => $object,
  255.                         'containerType' => 'fieldcollection',
  256.                         'containerKey' => $type,
  257.                         'fieldname' => $fcField,
  258.                         'index' => $index,
  259.                     ], ];
  260.                 $isDirtyDetectionDisabled DataObject::isDirtyDetectionDisabled();
  261.                 DataObject::disableDirtyDetection();
  262.                 $data $fieldDef->load($item$params);
  263.                 DataObject::setDisableDirtyDetection($isDirtyDetectionDisabled);
  264.                 $item->setObjectVar($field$data);
  265.             }
  266.             $item->markLazyKeyAsLoaded($field);
  267.         }
  268.     }
  269.     /**
  270.      * @return Concrete|null
  271.      */
  272.     protected function getObject(): ?Concrete
  273.     {
  274.         $this->rewind();
  275.         $item $this->current();
  276.         if ($item instanceof Model\DataObject\Fieldcollection\Data\AbstractData) {
  277.             return $item->getObject();
  278.         }
  279.         return null;
  280.     }
  281.     /**
  282.      * @internal
  283.      */
  284.     public function loadLazyData()
  285.     {
  286.         $items $this->getItems();
  287.         if (is_array($items)) {
  288.             /** @var Model\DataObject\Fieldcollection\Data\AbstractData $item */
  289.             foreach ($items as $item) {
  290.                 $fcType $item->getType();
  291.                 $fieldcolDef Model\DataObject\Fieldcollection\Definition::getByKey($fcType);
  292.                 $fds $fieldcolDef->getFieldDefinitions();
  293.                 foreach ($fds as $fd) {
  294.                     $fieldGetter 'get' ucfirst($fd->getName());
  295.                     $fieldValue $item->$fieldGetter();
  296.                     if ($fieldValue instanceof Localizedfield) {
  297.                         $fieldValue->loadLazyData();
  298.                     }
  299.                 }
  300.             }
  301.         }
  302.     }
  303. }