vendor/pimcore/pimcore/models/Document/Tag/Block.php line 27

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.  * @category   Pimcore
  12.  * @package    Document
  13.  *
  14.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  15.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  16.  */
  17. namespace Pimcore\Model\Document\Tag;
  18. use Pimcore\Document\Tag\Block\BlockName;
  19. use Pimcore\Model;
  20. use Pimcore\Tool\HtmlUtils;
  21. /**
  22.  * @method \Pimcore\Model\Document\Tag\Dao getDao()
  23.  */
  24. class Block extends Model\Document\Tag implements BlockInterface
  25. {
  26.     /**
  27.      * Contains an array of indices, which represent the order of the elements in the block
  28.      *
  29.      * @var array
  30.      */
  31.     public $indices = [];
  32.     /**
  33.      * Current step of the block while iteration
  34.      *
  35.      * @var int
  36.      */
  37.     public $current 0;
  38.     /**
  39.      * @var string[]
  40.      */
  41.     public $suffixes = [];
  42.     /**
  43.      * @see TagInterface::getType
  44.      *
  45.      * @return string
  46.      */
  47.     public function getType()
  48.     {
  49.         return 'block';
  50.     }
  51.     /**
  52.      * @see TagInterface::getData
  53.      *
  54.      * @return mixed
  55.      */
  56.     public function getData()
  57.     {
  58.         return $this->indices;
  59.     }
  60.     /**
  61.      * @see TagInterface::admin
  62.      */
  63.     public function admin()
  64.     {
  65.         // nothing to do
  66.     }
  67.     /**
  68.      * @see TagInterface::frontend
  69.      */
  70.     public function frontend()
  71.     {
  72.         // nothing to do
  73.         return null;
  74.     }
  75.     /**
  76.      * @see TagInterface::setDataFromResource
  77.      *
  78.      * @param mixed $data
  79.      *
  80.      * @return $this
  81.      */
  82.     public function setDataFromResource($data)
  83.     {
  84.         $this->indices = \Pimcore\Tool\Serialize::unserialize($data);
  85.         return $this;
  86.     }
  87.     /**
  88.      * @see TagInterface::setDataFromEditmode
  89.      *
  90.      * @param mixed $data
  91.      *
  92.      * @return $this
  93.      */
  94.     public function setDataFromEditmode($data)
  95.     {
  96.         $this->indices $data;
  97.         return $this;
  98.     }
  99.     /**
  100.      * @return $this
  101.      */
  102.     public function setDefault()
  103.     {
  104.         if (empty($this->indices) && isset($this->options['default']) && $this->options['default']) {
  105.             for ($i 0$i intval($this->options['default']); $i++) {
  106.                 $this->indices[$i] = $i 1;
  107.             }
  108.         }
  109.         return $this;
  110.     }
  111.     /**
  112.      * Loops through the block
  113.      *
  114.      * @return bool
  115.      */
  116.     public function loop()
  117.     {
  118.         $manual false;
  119.         if (array_key_exists('manual'$this->options) && $this->options['manual'] == true) {
  120.             $manual true;
  121.         }
  122.         $this->setDefault();
  123.         if ($this->current 0) {
  124.             if (!$manual) {
  125.                 $this->blockDestruct();
  126.                 $this->blockEnd();
  127.             }
  128.         } else {
  129.             if (!$manual) {
  130.                 $this->start();
  131.             }
  132.         }
  133.         if ($this->current count($this->indices) && $this->current $this->options['limit']) {
  134.             if (!$manual) {
  135.                 $this->blockConstruct();
  136.                 $this->blockStart();
  137.             }
  138.             return true;
  139.         } else {
  140.             if (!$manual) {
  141.                 $this->end();
  142.             }
  143.             return false;
  144.         }
  145.     }
  146.     /**
  147.      * @inheritDoc
  148.      */
  149.     protected function getEditmodeElementAttributes(array $options): array
  150.     {
  151.         $attributes parent::getEditmodeElementAttributes($options);
  152.         $attributes array_merge($attributes, [
  153.             'name' => $this->getName(),
  154.             'type' => $this->getType()
  155.         ]);
  156.         return $attributes;
  157.     }
  158.     /**
  159.      * Is executed at the beginning of the loop and setup some general settings
  160.      *
  161.      * @return $this
  162.      */
  163.     public function start()
  164.     {
  165.         $options $this->getEditmodeOptions();
  166.         $this->outputEditmodeOptions($options);
  167.         // set name suffix for the whole block element, this will be added to all child elements of the block
  168.         $this->getBlockState()->pushBlock(BlockName::createFromTag($this));
  169.         $attributes $this->getEditmodeElementAttributes($options);
  170.         $attributeString HtmlUtils::assembleAttributeString($attributes);
  171.         $this->outputEditmode('<div ' $attributeString '>');
  172.         return $this;
  173.     }
  174.     /**
  175.      * Is executed at the end of the loop and removes the settings set in start()
  176.      */
  177.     public function end()
  178.     {
  179.         $this->current 0;
  180.         // remove the current block which was set by $this->start()
  181.         $this->getBlockState()->popBlock();
  182.         $this->outputEditmode('</div>');
  183.     }
  184.     /**
  185.      * Called before the block is rendered
  186.      */
  187.     public function blockConstruct()
  188.     {
  189.         // set the current block suffix for the child elements (0, 1, 3, ...)
  190.         // this will be removed in blockDestruct
  191.         $this->getBlockState()->pushIndex($this->indices[$this->current]);
  192.     }
  193.     /**
  194.      * Called when the block was rendered
  195.      */
  196.     public function blockDestruct()
  197.     {
  198.         $this->getBlockState()->popIndex();
  199.     }
  200.     /**
  201.      * Is called evertime a new iteration starts (new entry of the block while looping)
  202.      *
  203.      * @param bool $showControls
  204.      */
  205.     public function blockStart($showControls true)
  206.     {
  207.         $attr $this->getBlockAttributes();
  208.         $outerAttributes = [
  209.             'key' => $this->indices[$this->current]
  210.         ];
  211.         $oAttr HtmlUtils::assembleAttributeString($outerAttributes);
  212.         // outer element
  213.         $this->outputEditmode('<div class="pimcore_block_entry" ' $oAttr ' ' $attr '>');
  214.         if ($showControls) {
  215.             $this->blockControls();
  216.         }
  217.     }
  218.     /**
  219.      * Custom position of button controls between blockStart -> blockEnd
  220.      */
  221.     public function blockControls()
  222.     {
  223.         $attr $this->getBlockAttributes();
  224.         $this->outputEditmode('<div class="pimcore_block_buttons" ' $attr '>');
  225.         $this->outputEditmode('<div class="pimcore_block_amount" ' $attr '></div>');
  226.         $this->outputEditmode('<div class="pimcore_block_plus" ' $attr '></div>');
  227.         $this->outputEditmode('<div class="pimcore_block_minus" ' $attr '></div>');
  228.         $this->outputEditmode('<div class="pimcore_block_up" ' $attr '></div>');
  229.         $this->outputEditmode('<div class="pimcore_block_down" ' $attr '></div>');
  230.         $this->outputEditmode('<div class="pimcore_block_clear" ' $attr '></div>');
  231.         $this->outputEditmode('</div>'); // .pimcore_block_buttons
  232.         $this->current++;
  233.     }
  234.     /**
  235.      * Is called evertime a new iteration ends (new entry of the block while looping)
  236.      */
  237.     public function blockEnd()
  238.     {
  239.         // close outer element
  240.         $this->outputEditmode('</div>');
  241.     }
  242.     /**
  243.      * @param array $options
  244.      *
  245.      * @return $this
  246.      */
  247.     public function setOptions($options)
  248.     {
  249.         if (empty($options['limit'])) {
  250.             $options['limit'] = 1000000;
  251.         }
  252.         $this->options $options;
  253.         return $this;
  254.     }
  255.     /**
  256.      * Return the amount of block elements
  257.      *
  258.      * @return int
  259.      */
  260.     public function getCount()
  261.     {
  262.         return count($this->indices);
  263.     }
  264.     /**
  265.      * Return current iteration step
  266.      *
  267.      * @return int
  268.      */
  269.     public function getCurrent()
  270.     {
  271.         return $this->current 1;
  272.     }
  273.     /**
  274.      * Return current index
  275.      *
  276.      * @return int
  277.      */
  278.     public function getCurrentIndex()
  279.     {
  280.         return $this->indices[$this->getCurrent()];
  281.     }
  282.     /**
  283.      * If object was serialized, set the counter back to 0
  284.      */
  285.     public function __wakeup()
  286.     {
  287.         $this->current 0;
  288.     }
  289.     /**
  290.      * @return bool
  291.      */
  292.     public function isEmpty()
  293.     {
  294.         return !(bool) count($this->indices);
  295.     }
  296.     /**
  297.      * @deprecated
  298.      *
  299.      * @param Model\Webservice\Data\Document\Element $wsElement
  300.      * @param Model\Document\PageSnippet $document
  301.      * @param array $params
  302.      * @param Model\Webservice\IdMapperInterface|null $idMapper
  303.      *
  304.      * @throws \Exception
  305.      */
  306.     public function getFromWebserviceImport($wsElement$document null$params = [], $idMapper null)
  307.     {
  308.         $data $this->sanitizeWebserviceData($wsElement->value);
  309.         if (($data->indices === null or is_array($data->indices)) and ($data->current == null or is_numeric($data->current))) {
  310.             $this->indices $data->indices;
  311.             $this->current $data->current;
  312.         } else {
  313.             throw new \Exception('cannot get  values from web service import - invalid data');
  314.         }
  315.     }
  316.     /**
  317.      * @return Block\Item[]
  318.      */
  319.     public function getElements()
  320.     {
  321.         $document $this->getDocument();
  322.         $parentBlockNames $this->getParentBlockNames();
  323.         $parentBlockNames[] = $this->getName();
  324.         $list = [];
  325.         foreach ($this->getData() as $index) {
  326.             $list[] = new Block\Item($document$parentBlockNames, (int)$index);
  327.         }
  328.         return $list;
  329.     }
  330.     /**
  331.      * @return string
  332.      */
  333.     private function getBlockAttributes(): string
  334.     {
  335.         $attributes = [
  336.             'data-name' => $this->getName(),
  337.             'data-real-name' => $this->getRealName(),
  338.         ];
  339.         return HtmlUtils::assembleAttributeString($attributes);
  340.     }
  341. }