Determines whether a block should be allowed given certain parameters

@param   string          $blockId
@param   ModelInterface  $block
@return  bool
483719
private function isAllowed($blockId, ModelInterface $block)
    {
        $result = $this->getEventManager()->trigger(
            __FUNCTION__,
            $this,
            [
                'block_id' => $blockId,
                'block' => $block
            ]
        );
        if ($result->stopped()) {
            return $result->last();
        }
        return true;
    }
1280256
public function render(AbstractBlock $block, CliRenderer $renderer)
    {
        if (!($block instanceof ListBlock)) {
            throw new \InvalidArgumentException(sprintf('Incompatible block type: "%s"', get_class($block)));
        }

        return $renderer->renderBlocks($block->children());
    }
705670
protected function getUrl(BlockInterface $block)
    {
        if (substr($block->getValue(), 0, 4) == 'http') {
            return $block->getValue();
        }

        return $this->generateUrl($block);
    }
1288016
public function getViewParameters(BlockInterface $block)
    {
        $parameters = [
            'block_service' => $this,
            'block' => $block,
            'children' => $this->getEnvironment() ? $this->getEnvironment()->getBlockChildren($block) : [],
        ];

        return $parameters;
    }
142935
private function getAllowedBlocks(Zone $zone, LayoutTypeInterface $layoutType)
    {
        if ($layoutType->hasZone($zone->getIdentifier())) {
            $layoutTypeZone = $layoutType->getZone($zone->getIdentifier());
            $allowedBlockDefinitions = $layoutTypeZone->getAllowedBlockDefinitions();

            if (count($allowedBlockDefinitions) > 0) {
                return $allowedBlockDefinitions;
            }
        }

        return true;
    }
1096903
public function collectBlockView(BlockViewInterface $blockView): void
    {
        $block = $blockView->getBlock();
        $blockDefinition = $block->getDefinition();

        $blockData = [
            'id' => $block->getId(),
            'layout_id' => $block->getLayoutId(),
            'definition' => $blockDefinition->getName(),
            'view_type' => $blockDefinition->hasViewType($block->getViewType()) ?
                $blockDefinition->getViewType($block->getViewType())->getName() :
                'Invalid view type',
            'locale' => $block->getLocale(),
            'template' => null,
            'template_path' => null,
        ];

        if ($blockView->getTemplate() !== null) {
            $templateSource = $this->getTemplateSource($blockView->getTemplate());

            $blockData['template'] = $templateSource->getName();
            $blockData['template_path'] = $templateSource->getPath();
        }

        $this->data['blocks'][] = $blockData;
    }
364020
private function getBlockModule(Tree $tree, int $block_id): ModuleBlockInterface
    {
        $active_blocks = $this->module_service->findByComponent(ModuleBlockInterface::class, $tree, Auth::user());

        $module_name = DB::table('block')
            ->where('block_id', '=', $block_id)
            ->value('module_name');

        $block = $active_blocks->first(static function (ModuleInterface $module) use ($module_name): bool {
            return $module->name() === $module_name;
        });

        if ($block === null) {
            throw new NotFoundHttpException('Block not found');
        }

        return $block;
    }
151688
protected function loadCollection(BlockInterface $block)
    {
        $properties = $block->getProperties();

        $conditions = (isset($properties['conditions'])) ? $properties['conditions'] : '[]';
        $conditions = $this->expressionEngine->deserialize($conditions);

        if (empty($conditions)) {
            return;
        }

        $site = $this->siteManager->getSite();

        $qb = $this->expressionEngine->toQueryBuilder($conditions, $this->contentManager->getClass());
        $qb->andWhere('a.publishAt < :now OR a.publishAt IS NULL')
            ->andWhere('a.active = :active')
            ->andWhere('a.layout = :layout');

        if ($site == null) {
            $qb->andWhere('a.site IS NULL');
        } else {
            $qb->andWhere('a.site = :site')
               ->setParameter('site', $site);
        }

        $qb->setParameter('active', true)
            ->setParameter('layout', false)
            ->setParameter('now', new \DateTime());

        if (isset($properties['order_by'])) {
            $direction = (isset($properties['order_direction'])) ? $properties['order_direction'] : 'ASC';

            $qb->orderBy('a.'.$properties['order_by'], $direction);
        }

        $limit = (isset($properties['limit'])) ? $properties['limit'] : 10;
        $qb->setMaxResults($limit);

        $collection = $qb->getQuery()->getResult();

        if ($collection) {
            $block->setCollection($collection);
        }
    }
567011
public function updateBlock(Block $block): void
    {
        $query = $this->connection->createQueryBuilder();
        $query
            ->update('ngbm_block')
            ->set('layout_id', ':layout_id')
            ->set('depth', ':depth')
            ->set('path', ':path')
            ->set('parent_id', ':parent_id')
            ->set('placeholder', ':placeholder')
            ->set('position', ':position')
            ->set('definition_identifier', ':definition_identifier')
            ->set('view_type', ':view_type')
            ->set('item_view_type', ':item_view_type')
            ->set('name', ':name')
            ->set('translatable', ':translatable')
            ->set('main_locale', ':main_locale')
            ->set('always_available', ':always_available')
            ->set('config', ':config')
            ->where(
                $query->expr()->eq('id', ':id')
            )
            ->setParameter('id', $block->id, Type::INTEGER)
            ->setParameter('layout_id', $block->layoutId, Type::INTEGER)
            ->setParameter('depth', $block->depth, Type::STRING)
            ->setParameter('path', $block->path, Type::STRING)
            ->setParameter('parent_id', $block->parentId, Type::INTEGER)
            ->setParameter('placeholder', $block->placeholder, Type::STRING)
            ->setParameter('position', $block->position, Type::INTEGER)
            ->setParameter('definition_identifier', $block->definitionIdentifier, Type::STRING)
            ->setParameter('view_type', $block->viewType, Type::STRING)
            ->setParameter('item_view_type', $block->itemViewType, Type::STRING)
            ->setParameter('name', $block->name, Type::STRING)
            ->setParameter('translatable', $block->isTranslatable, Type::BOOLEAN)
            ->setParameter('main_locale', $block->mainLocale, Type::STRING)
            ->setParameter('always_available', $block->alwaysAvailable, Type::BOOLEAN)
            ->setParameter('config', $block->config, Type::JSON_ARRAY);

        $this->applyStatusCondition($query, $block->status);

        $query->execute();
    }
1344979
public function moveRight(Model\BlockInterface $block)
    {
        $sibling = $this->editor->getRepository()->findSiblingBlock($block, true);
        if (null === $sibling) {
            throw new InvalidOperationException(
                "The block can't be moved to the right as no sibling block has been found."
            );
        }

        $block->setPosition($block->getPosition() + 1);
        $sibling->setPosition($sibling->getPosition() - 1);

        $this->sortChildrenByPosition($block->getRow(), 'blocks');

        return $sibling;
    }
649654
public function revert(BlockInterface $block, $version)
    {
        $block->accept(new RevertVisitor($this, $version));

        if ($block instanceof BlockContainerInterface) {
            self::treeAddMissingChildren($block);
            self::treeRemoveInvalidChildren($block);
            $block->accept(new SortVisitor());
        }
    }
1222173
public function revertSingle(BlockInterface $block, $version)
    {
        // check for LogEntries
        /** @var BlockLogEntryRepository $repo */
        $repo = $this->em->getRepository('OpiferContentBundle:BlockLogEntry');

        $logs = $repo->getLogEntriesRoot($block, $version);

        if (count($logs)) {
            $latest = current($logs)->getVersion();
            $repo->revert($block, $latest);  // We "revert" to the current working draft, as the Block itself is a published one
        } else {
            if ($block->getVersion() > $version) {
                // no logs where found, assume block was created in a later version
                $block->getParent()->removeChild($block);
                unset($block);
            }
        }
    }
1119379
protected function getDisplayLogicPrototypes(BlockInterface $block)
    {
        $collection = new PrototypeCollection([
            new OrXPrototype(),
            new AndXPrototype(),
            new EventPrototype('click_event', 'Click Event', 'event.type.click'),
            new TextPrototype('dom_node_id', 'DOM Node Id', 'node.id')
        ]);

        $owner = $block->getOwner();
        if ($owner) {
            // Avoid trying to add display logic for blocks when the current block has no owner (e.g. shared blocks)
            $blockChoices = [];
            foreach ($owner->getBlocks() as $member) {
                try {
                    $properties = $member->getProperties();

                    if ($member instanceof ChoiceFieldBlock) {
                        if (empty($member->getName())) {
                            continue;
                        }
                        if (!isset($properties['options'])) {
                            continue;
                        }
                        $choices = [];
                        foreach ($properties['options'] as $option) {
                            if (empty($option['key'])) {
                                continue;
                            }
                            $choices[] = new Choice($option['key'], $option['value']);
                        }
                        $collection->add(new SelectPrototype($member->getName(), $properties['label'], $member->getName(), $choices));
                    } elseif ($member instanceof NumberFieldBlock || $member instanceof RangeFieldBlock) {
                        if (empty($member->getName())) {
                            continue;
                        }

                        $collection->add(new NumberPrototype($member->getName(), $properties['label'], $member->getName()));
                    }

                    if (!empty($member->getName())) {
                        $blockChoices[] = new Choice($member->getName(), $member->getName());
                    }
                } catch (\Exception $e) {
                    // Avoid throwing exceptions here for now, since e.g. duplicate namesthis will cause all blocks to be uneditable.
                }
            }

            $collection->add(new SelectPrototype('block_name', 'Block Name', 'block.name', $blockChoices));
        }

        return $collection->all();
    }
673766
public function validateBlocks(ElementInterface $element)
    {
        /** @var Element $element */
        /** @var SuperTableBlockQuery $value */
        $value = $element->getFieldValue($this->handle);

        foreach ($value->all() as $i => $block) {
            /** @var SuperTableBlock $block */
            if ($element->getScenario() === Element::SCENARIO_LIVE) {
                $block->setScenario(Element::SCENARIO_LIVE);
            }

            if (!$block->validate()) {
                foreach ($block->getErrors() as $attribute => $errors) {
                    $element->addErrors([
                        "{$this->handle}[{$i}].{$attribute}" => $errors,
                    ]);
                }
            }
        }
    }
172774
public function changeType(Model\BlockInterface $block, $type, array $data = [])
    {
        if ($type === $block->getType()) {
            return;
        }

        if ($block->isNamed()) {
            throw new InvalidOperationException(
                "The type of this block can't be changed."
            );
        }

        // Plugin removal
        $this->editor
            ->getBlockPlugin($block->getType())
            ->remove($block);

        // Sets the new type
        $block->setType($type);

        // Plugin creation
        $this->editor
            ->getBlockPlugin($block->getType())
            ->create($block, $data);
    }
547026
final void freeBlock(long blockId) throws IOException {
        if (blockId == 0) {
            // Can't free block 0.
        }
        else {
            try {
                mFreeBlocksBitlist.lock().acquireWriteLock();
                mFreeBlocksBitlist.set(blockId);
                if (mFirstFreeBlock == 0 || blockId < mFirstFreeBlock) {
                    setFirstFreeBlock(blockId);
                }
            }
            catch (InterruptedException e) {
                throw new InterruptedIOException();
            }
            finally {
                mFreeBlocksBitlist.lock().releaseLock();
            }
        }
    }
10159
public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
    {
        $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER));
        $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE));
    }