Skip to content
Code-Schnipsel Gruppen Projekte
Manager.php 6,94 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?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
        /**
         * Block temp collector
         * @var array
         */
        protected $_blocks = array();
    
        /**
         * Creates a new block for the project
         *
         * @param Project $Project
         * @param Block $Block
         * @return integer - Block-ID
         */
        public function createBlockForProject(Project $Project, Block $Block)
        {
            QUI::getDataBase()->insert(
                $this->_getTable(),
                array(
                    'project'     => $Project->getName(),
                    'title'       => $Block->getAttribute('title'),
                    'description' => $Block->getAttribute('description'),
                    'type'        => $Block->getAttribute('type')
                )
            );
    
            $lastId = QUI::getPDO()->lastInsertId();
    
            return $lastId;
        }
    
        /**
         * Return the areas which are available in the project
         *
         * @param Project $Project
         * @return array
         */
        public function getAreasByProject(Project $Project)
        {
            $templates = array();
            $blocks    = array();
    
            $projectName = $Project->getName();
    
            // get all vhosts, and the used templates of the project
            $vhosts = QUI::getRewrite()->getVHosts();
    
            foreach ( $vhosts as $vhost )
            {
                if ( !isset( $vhost['template'] ) ) {
                    continue;
                }
    
                if ( $vhost['project'] != $projectName ) {
                    continue;
                }
    
                $templates[] = $vhost['template'];
            }
    
            // get blocks
            foreach ( $templates as $template )
            {
                $blockXML = realpath( OPT_DIR . $template .'/blocks.xml' );
    
                if ( !$blockXML ) {
                    continue;
                }
    
                $blocks = array_merge( $blocks, Utils::getTemplateAreasFromXML( $blockXML ) );
            }
    
            return $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();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            $result[] = array(
                'title'       => array( 'quiqqer/blocks', 'block.content.title' ),
                'description' => array( 'quiqqer/blocks', 'block.content.description' ),
                'control'     => 'content'
            );
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            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;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Get a Block by its Block-ID
         *
         * @param Integer $id
         * @return Block
         * @throws QUI\Exception
         */
        public function getBlockById($id)
        {
            if ( isset( $this->_blocks[ $id ] ) ) {
                return $this->_blocks[ $id ];
            }
    
            $data = QUI::getDataBase()->fetch(array(
                'from'  => $this->_getTable(),
                'where' => array(
                    'id' => (int)$id
                ),
                'limit' => 1
            ));
    
            if ( !isset( $data[0] ) ) {
                throw new QUI\Exception( 'Block not found' );
            }
    
            $this->_blocks[ $id ] = new Block( $data[0] );
    
            return $this->_blocks[ $id ];
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * 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' );
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    
    
    
    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();
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                $Block->setAttribute( 'id', $entry['id'] );
    
    Henning Leutz's avatar
    Henning Leutz committed
                $Block->setAttribute( 'title', $entry['title'] );
                $Block->setAttribute( 'description', $entry['description'] );
    
                $settings = json_decode( $entry['settings'], true );
                $Block->setAttributes( $settings );
    
                $result[] = $Block;
            }
    
            return $result;
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * @param string|integer $blockId - Block-ID
         * @param array $blockData - Block data
         */
        public function saveBlock($blockId, array $blockData)
        {
            $Block      = $this->getBlockById( $blockId );
            $areas      = array();
            $areaString = '';
    
            if ( isset( $blockData[ 'id' ] ) ) {
                unset( $blockData[ 'id' ] );
            }
    
            // check areas
            $Project = QUI::getProjectManager()->getProject(
                $Block->getAttribute( 'project' )
            );
    
            $availableAreas = array_map(function($data)
            {
                if ( isset( $data[ 'name' ] ) ) {
                    return $data[ 'name' ];
                }
    
                return '';
            }, $this->getAreasByProject( $Project ));
    
    
            if ( isset( $blockData[ 'areas' ] ) )
            {
                $parts = explode( ',', $blockData[ 'areas' ] );
    
                foreach ( $parts as $area )
                {
                    if ( in_array( $area, $availableAreas ) ) {
                        $areas[] = $area;
                    }
                }
            }
    
            if ( !empty( $areas ) ) {
                $areaString = ','. implode( ',', $areas ) .',';
            }
    
    
            $Block->setAttributes( $blockData );
    
            QUI::getDataBase()->update($this->_getTable(), array(
                'title'       => $Block->getAttribute( 'title' ),
                'description' => $Block->getAttribute( 'description' ),
                'content'     => $Block->getAttribute( 'content' ),
                'type'        => $Block->getAttribute( 'type' ),
                'settings'    => json_encode( $Block->getAttribute( 'settings' ) ),
                'areas'       => $areaString
            ), array(
                'id' => (int)$blockId
            ));
        }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Returns the blocks table name
         * @return String
         */
        protected function _getTable()
        {
            return QUI::getDBTableName( self::TABLE );
        }