Skip to content
Code-Schnipsel Gruppen Projekte
Manager.php 2,53 KiB
Newer Older
<?php

/**
 * This file contains \QUI\Blocks\Manager
 */

namespace QUI\Blocks;

use QUI;
Henning Leutz's avatar
Henning Leutz committed
use QUI\Projects\Project;
use QUI\Projects\Site;

/**
 * Block Manager
 *
 * @package quiqqer/blocks
 */
class Manager
{
Henning Leutz's avatar
Henning Leutz committed
    /**
     * Blocks table name
     */
    const TABLE = 'blocks';
Henning Leutz's avatar
Henning Leutz committed
    /**
     * Returns the available blocks
     *
     * @return array
     */
    public function getAvailableBlocks()
    {
Henning Leutz's avatar
Henning Leutz committed
        $cache = 'quiqqer/blocks/availableBlocks';
Henning Leutz's avatar
Henning Leutz committed
        try
        {
            return QUI\Cache\Manager::get( $cache );

        } catch ( QUI\Exception $Exception )
        {

        }

        $PKM      = QUI::getPackageManager();
        $packages = $PKM->getInstalled();
        $result   = array();

        foreach ( $packages as $package )
        {
            $blocksXML = OPT_DIR . $package['name'] .'/blocks.xml';

            if ( !file_exists( $blocksXML ) ) {
                continue;
            }
Henning Leutz's avatar
Henning Leutz committed
            $result = array_merge( $result, Utils::getBlocksFromXML( $blocksXML ) );
        }
Henning Leutz's avatar
Henning Leutz committed
        QUI\Cache\Manager::set( $cache, $result );


        return $result;
    }

    /**
     * Return the blocks from the area
     *
     * @param string $blockArea - Name of the area
     * @param Site $Site
     * @return array
     */
    public function getBlocksByArea($blockArea, Site $Site)
Henning Leutz's avatar
Henning Leutz committed
        if ( empty( $blockArea ) ) {
            return array();
        }

        $blockAreas = $Site->getAttribute( 'quiqqer.blocks.areas' );

        QUI\System\Log::writeRecursive( $blockAreas );
Henning Leutz's avatar
Henning Leutz committed
    /**
     * Return a list with \QUI\Blocks\Block which are assigned to a project
     *
     * @param Project $Project
     * @return array
     */
    public function getBlocksFromProject(Project $Project)
    {
        $result = array();

        $list = QUI::getDataBase()->fetch(array(
            'from'  => $this->_getTable(),
            'where' => array(
                'project' => $Project->getName()
            )
        ));

        foreach ( $list as $entry )
        {
            $Block = new Block();

            $Block->setAttribute( 'title', $entry['title'] );
            $Block->setAttribute( 'description', $entry['description'] );

            $settings = json_decode( $entry['settings'], true );
            $Block->setAttributes( $settings );

            $result[] = $Block;
        }

        return $result;
    }

    /**
     * Returns the blocks table name
     * @return String
     */
    protected function _getTable()
    {
        return QUI::getDBTableName( self::TABLE );
    }