Skip to content
Code-Schnipsel Gruppen Projekte
Utils.php 3,47 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    <?php
    
    /**
     * This file contains \QUI\Blocks\Utils
     */
    
    namespace QUI\Blocks;
    
    use QUI;
    use QUI\Utils\XML;
    
    /**
     * Class Utils
     * Blocks helper class
     *
     * @package quiqqer/blocks
     * @author www.pcsg.de (Henning Leutz)
     */
    
    class Utils
    {
        /**
         * Return the blocks from a xml file
         *
         * @param String $file
         * @return array
         */
        static function getBlocksFromXML($file)
        {
            if ( !file_exists( $file ) ) {
                return array();
            }
    
            $Dom  = XML::getDomFromXml( $file );
            $Path = new \DOMXPath( $Dom );
    
            $blocks = $Path->query( "//quiqqer/blocks/block" );
            $list   = array();
    
            if ( !$blocks->length ) {
                return $list;
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
            foreach ( $blocks as $Block ) {
                $list[] = self::parseBlockToArray( $Block, $Path );
            }
    
            return $list;
        }
    
        /**
         * Return the template blocks from a xml file
         *
         * @param string $file - path to xm file
    
         * @param string|bool $siteType - optional, return only the blocks for the specific site type
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return array
         */
    
        static function getTemplateAreasFromXML($file, $siteType=false)
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
            if ( !file_exists( $file ) ) {
                return array();
            }
    
            $Dom  = XML::getDomFromXml( $file );
            $Path = new \DOMXPath( $Dom );
    
            $globalBlocks = $Path->query( "//quiqqer/blocks/templateAreas/areas/area" );
    
    
            if ( $siteType )
            {
                $typeBlocks = $Path->query(
                    "//quiqqer/blocks/templateAreas/types/type[@type='{$siteType}']/area"
                );
    
            } else
            {
                $typeBlocks = $Path->query( "//quiqqer/blocks/templateAreas/types/type/area" );
            }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    
            $list = array();
    
            if ( $globalBlocks->length )
    
    Henning Leutz's avatar
    Henning Leutz committed
                foreach ( $globalBlocks as $Block ) {
                    $list[] = self::parseBlockToArray( $Block, $Path );
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
            if ( $typeBlocks->length )
            {
                foreach ( $typeBlocks as $Block ) {
                    $list[] = self::parseBlockToArray( $Block, $Path );
    
    Henning Leutz's avatar
    Henning Leutz committed
            }
    
    Henning Leutz's avatar
    Henning Leutz committed
            return $list;
        }
    
    
        static function getGlobalTemplateAreasFromXML()
        {
    
        }
    
        static function getTypeTemplateAreasFromXML($file, $siteType)
        {
    
        }
    
        /**
         * parse a <block> xml node to an array
         *
         * @param \DOMElement $Block
         * @param \DOMXPath $Path
         * @return array
         */
        static function parseBlockToArray(\DOMElement $Block, \DOMXPath $Path)
        {
            $control     = $Block->getAttribute( 'control' );
            $name        = $Block->getAttribute( 'name' );
            $title       = array();
            $description = array();
    
            $titleLocale = $Path->query( './title/locale', $Block );
            $descLocale  = $Path->query( './description/locale', $Block );
    
            if ( $titleLocale->length )
            {
                $title = array(
                    'group' => $titleLocale->item( 0 )->getAttribute( 'group' ),
                    'var'   => $titleLocale->item( 0 )->getAttribute( 'var' )
    
    Henning Leutz's avatar
    Henning Leutz committed
            if ( $descLocale->length )
            {
                $description = array(
                    'group' => $descLocale->item( 0 )->getAttribute( 'group' ),
                    'var'   => $descLocale->item( 0 )->getAttribute( 'var' )
                );
            }
    
            return array(
                'control'     => $control,
                'name'        => $name,
                'title'       => $title,
                'description' => $description
            );