Sets up the block managers.

When the blocks have not been given, it retrieves all the pages's contents saved on the slot

@param array $blocks

@api
331266
public function setUpBlockManagers(array $blocks)
    {
        foreach ($blocks as $block) {
            $blockManager = $this->blockManagerFactory->createBlockManager($block);
            if (null === $blockManager) {
                continue;
            }
            $this->blockManagersCollection->addBlockManager($blockManager);
        }
    }
438757
protected function declareListOfBlocks($blocks)
    {
        foreach ( $blocks as $block ) {
            try {
                $this->getSoapClient()->SetBlockFieldValues(array(
                    'blockName'        => $block->getName() ,
                    'blockFieldValues' => $this->getSoapClient()->convertArray($block->retrieveValues())
                ));
            } catch ( SoapException $ex ) {
                $s = '';
                if ( ! is_null($block->getName()) ) {
                    $s.= " (block: {$block->getName()})";
                }
                throw new DeclarationException("Error while sending blocks informations to Livedocx service" . $s , $ex);
            }
        }
        return $this;
    }
497704
public static function termsForBlock($terms, $block)
    {
        foreach ($terms as $term) {
            $title = $term->name;
            if (!empty($block->settings['show_counters'])) {
                $countCacheKey = "t{$term->id}";
                $count = (string)Cache::read($countCacheKey, 'terms_count');
                if ($count == '') {
                    $count = (int)TableRegistry::get('Taxonomy.EntitiesTerms')
                        ->find()
                        ->where(['EntitiesTerms.term_id' => $term->id])
                        ->count();
                    Cache::write($countCacheKey, $count, 'terms_count');
                }
                $title .= " ({$count})";
            }

            $term->set('title', $title);
            $term->set('url', "/find/term:{$term->slug}");

            if (!empty($term->children)) {
                $term->set('expanded', true);
                static::termsForBlock($term->children, $block);
            }
        }
    }
814956
protected function _setBlock($block)
    {
        if ($block !== null && !($block instanceof BlockInterface)) {
            throw $this->_createInvalidArgumentException(
                $this->__('Invalid block'),
                null,
                null,
                $block
            );
        }

        $this->block = $block;
    }
827724
protected function setUpSlotManagers()
    {
        if (null === $this->themeSlots || null === $this->template) {
            return;
        }

        $this->slotManagers = array();
        $templateSlots = $this->template->getSlots();
        $themeSlots = $this->themeSlots->getSlots();
        foreach ($themeSlots as $slotName => $slot) {

            // slots passes only when they are repeated or belongs the current template
            if ($slot->getRepeated() == 'page' && ! in_array($slotName, $templateSlots)) {
                continue;
            }

            $this->slotManagers[$slotName] = $this->createSlotManager($slot);
        }

        if (null === $this->pageBlocks) {
            return;
        }

        // Looks for included blocks' slots
        $includedSlots = array_diff(array_keys($this->pageBlocks->getBlocks()), array_keys($themeSlots));
        foreach ($includedSlots as $slotName) {
            if ($slotName != "") {
                $slot = new Slot($slotName);
                $this->slotManagers[$slotName] = $this->createSlotManager($slot);
            }
        }
    }
50548
public function publish($blocks)
    {
        if ($blocks instanceof PersistentCollection) {
            $blocks = $blocks->getValues();
        }

        if (!$blocks ||
            (is_array($blocks) && !count($blocks))) {
            return;
        }

        if (! is_array($blocks)) {
            $blocks = [$blocks];
        }

        $this->disableRevisionListener();

        $deletes = [];

        foreach ($blocks as $block) {
            if (null !== $revision = $this->revisionManager->getDraftRevision($block)) {
                try {
                    $this->revisionManager->revert($block, $revision);
                } catch (DeletedException $e) {
                    // $this->em->remove($block);
                    $deletes[] = $block;
                }
            }
        }

        $this->em->flush();

        // Cycle through all deleted blocks to perform cascades manually 
        foreach ($deletes as $block) {
            if ($block instanceof CompositeBlock) {
                $descendants = $this->findDescendants($block, false);
            } else {
                $descendants = [$block];
            }

            foreach ($descendants as $descendant) {
                $descendant->setDeletedAt(new \DateTime);
                $this->em->flush($descendant);
            }
        }

        $cacheDriver = $this->em->getConfiguration()->getResultCacheImpl();
        $cacheDriver->deleteAll();

        $this->enableRevisionListener();
    }
596067
private function loadBlocks(PageInterface $page)
    {
        $i = new \RecursiveIteratorIterator(new RecursiveBlockIterator($page->getBlocks()), \RecursiveIteratorIterator::SELF_FIRST);

        foreach ($i as $block) {
            $this->blocks[$block->getId()] = $block;
        }
    }
715321
protected function fetchBlocks()
    {
        if (null === $this->blocks)
        {
            $this->blocks = array();

            $allBlocks = ContentBlockQuery::create()
                ->find()
            ;

            foreach ($allBlocks as $block)
            {
                /* @var $block ContentBlock */
                $this->blocks[$block->getName()][$block->getLocale()] = $block;
            }
        }
    }
1180155
public function duplicate($blocks, $owner = null)
    {
        if ($blocks instanceof BlockInterface) {
            $blocks = array($blocks);
        }

        $iterator = new \RecursiveIteratorIterator(
            new RecursiveBlockIterator($blocks),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        $originalIdMap = array();
        $originalParentMap = array();

        $clones = array();

        // iterate over all owned blocks and disconnect parents keeping ids
        /** @var Block $block */
        foreach ($iterator as $block) {
            $blockId = $block->getId();
            $parentId = false;

            if (in_array($block->getId(), $originalIdMap)) {
                continue;
            }

            $clone = clone $block;
            $clone->setId(null);
            $clone->setParent(null);
            $clone->setOwner($owner);

            // if it has a parent we need to keep the id as reference for later
            if ($block->getParent()) {
                $parentId = $block->getParent()->getId();
            }

            if ($clone instanceof BlockContainerInterface) {
                $clone->setChildren(null);
            }

            $this->em->persist($clone);
            $this->em->flush($clone); // the block gets a new id

            $originalIdMap[$clone->getId()] = $blockId;
            if ($parentId) {
                $originalParentMap[$clone->getId()] = $parentId;
            }

            $clones[] = $clone;
        }

        // iterate over all new blocks and reset their parents
        foreach ($clones as $clone) {
            if (isset($originalParentMap[$clone->getId()])) {
                foreach ($clones as $parent) {
                    if (isset($originalParentMap[$clone->getId()]) &&
                        $originalParentMap[$clone->getId()] === $originalIdMap[$parent->getId()]) {
                        $clone->setParent($parent);
                        $parent->addChild($clone);

                        $this->em->flush($clone);
                        $this->em->flush($parent);
                    }
                }
            }
        }

        return $clones;
    }
88925
protected function loadBlocks()
    {
        if ($this->isLoaded)
            return;

        $blockOwners = $this->getBlockOwners();
        $blocks = array();

        foreach ($blockOwners as $blockOwner) {
            $version = $this->getVersion($blockOwner->getId());

            $owned = $this->blockManager->findByOwner($blockOwner, $version);

            $blocks = array_merge($blocks, $owned);
        }

        $blocks = $this->blockManager->sortBlocks(array_merge($blocks, $blockOwners));
        $cacheKey = $this->getCacheKey();

        $this->blockCache[$cacheKey] = array_filter($blocks, function($item) {
            return ($item instanceof DocumentBlock) ? false : true;
        });

        $this->isLoaded = true;

        return $this;
    }
1233653
protected function generateDocBlock(array $columns): void
  {
    $this->codeStore->append('/**');
    $this->codeStore->append(' * @todo describe routine', false);
    $this->codeStore->append(' * ', false);

    $padding = $this->getMaxColumnLength($columns);
    $format  = sprintf(' * @param p_%%-%ds @todo describe parameter', $padding);
    foreach ($columns as $column)
    {
      $this->codeStore->append(sprintf($format, $column['column_name']), false);
    }

    $this->codeStore->append(' */', false);
  }
510474
public function saveMessages($blockName, $messages) {
        $allMessages = $this->getMessages();
        $allMessages[$blockName] = array_merge(
            $this->loadMessages($blockName), $messages );
        $this->setMessages($allMessages);
    }
553545
function blockSet(BlockInterface $block, $templateFile) {
		$this->_blockStack[]= $block;
		$this->_templateFileStack[]= $templateFile;
	}
255200
public function setRules(array $rules)
    {
        $this->rules = [];

        foreach ($rules as $rule) {
            $controller  = strtolower($rule['controller']);
            $actions     = isset($rule['actions']) ? (array)$rule['actions'] : [];
            $permissions = (array)$rule['permissions'];

            if (empty($actions)) {
                $this->rules[$controller][0] = $permissions;
                continue;
            }

            foreach ($actions as $action) {
                $this->rules[$controller][strtolower($action)] = $permissions;
            }
        }
    }
125215
public function getMissingBlocks(Array $blocks = array())
    {
        $missing = array();
        $blocks  = $blocks ? $blocks : $this->getWroteBlocks();
        $last    = array_shift($blocks);

        if ($last['offset'] !== 0) {
            $missing[] = array('offset' => 0, 'size' => $last['offset']);
        }

        foreach ($blocks as $block) {
            if ($block['offset'] !== $last['offset'] + $last['size']) {
                $offset = $last['offset'] + $last['size'];
                $missing[] = array('offset' => $offset, 'size' => $block['offset'] - $offset);
            }
            $last = $block;
        }

        return $missing;
    }
1292721
protected function _prepareBlocks()
    {
        $cacheKey = "{$this->_View->theme}_{$this->_machineName}";
        $blocks = TableRegistry::get('Block.Blocks')
            ->find('all')
            ->cache($cacheKey, 'blocks')
            ->contain(['Roles', 'BlockRegions'])
            ->matching('BlockRegions', function ($q) {
                return $q->where([
                    'BlockRegions.theme' => $this->_View->theme(),
                    'BlockRegions.region' => $this->_machineName,
                ]);
            })
            ->where(['Blocks.status' => 1])
            ->order(['BlockRegions.ordering' => 'ASC']);

        $blocks->sortBy(function ($block) {
            return $block->region->ordering;
        }, SORT_ASC);

        // remove blocks that cannot be rendered based on current request.
        $blocks = $blocks->filter(function ($block) {
            return $this->_filterBlock($block) && $block->renderable();
        });

        $this->blocks($blocks);
    }
1131468
public static List<Block> removeEmptyBlocks(final Collection<Block> blocks) {
      List<Block> cleanBlocks = Lists.newArrayListWithExpectedSize(blocks.size());
      for(Block block : blocks) {
         if(block.content.isEmpty()) {
            continue;
         }

         if(isWhitespace(block.content)) {
            continue;
         }

         cleanBlocks.add(block);
      }
      return cleanBlocks;
   }