From dd8a7129205a2fb39c7cb21a7d71fa9335f466c6 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Mon, 22 Dec 2014 21:14:07 +0100
Subject: [PATCH] blocks -> panel, database

---
 ajax/project/getBlocks.php |  33 +++++++++
 bin/Manager.js             | 148 ++++++++++++++++++++++++++++++++++++-
 database.xml               |  15 ++++
 lib/QUI/Blocks/Block.php   |  61 +++++++++++++++
 lib/QUI/Blocks/Manager.php |  98 +++++++++++++++++++++++-
 lib/QUI/Blocks/Utils.php   |  79 ++++++++++++++++++++
 6 files changed, 428 insertions(+), 6 deletions(-)
 create mode 100644 ajax/project/getBlocks.php
 create mode 100644 database.xml
 create mode 100644 lib/QUI/Blocks/Block.php
 create mode 100644 lib/QUI/Blocks/Utils.php

diff --git a/ajax/project/getBlocks.php b/ajax/project/getBlocks.php
new file mode 100644
index 0000000..925a295
--- /dev/null
+++ b/ajax/project/getBlocks.php
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * This file contains package_quiqqer_feed_ajax_getList
+ */
+
+/**
+ * Returns the feed list
+ *
+ * @author www.pcsg.de (Henning Leutz)
+ * @param string $project - json array, Project Data
+ * @return array
+ */
+function package_quiqqer_blocks_ajax_project_getBlocks($project)
+{
+    $Project      = QUI::getProjectManager()->decode( $project );
+    $BlockManager = new \QUI\Blocks\Manager();
+
+    $blocks = $BlockManager->getBlocksFromProject( $Project );
+    $result = array();
+
+    foreach ( $blocks as $Block ) {
+        $result = $Block->getAttributes();
+    }
+
+    return $result;
+}
+
+\QUI::$Ajax->register(
+    'package_quiqqer_blocks_ajax_project_getBlocks',
+    array( 'project' ),
+    'Permission::checkAdminUser'
+);
diff --git a/bin/Manager.js b/bin/Manager.js
index 6c59048..109a075 100644
--- a/bin/Manager.js
+++ b/bin/Manager.js
@@ -10,9 +10,15 @@ define('package/quiqqer/blocks/bin/Manager', [
 
     'qui/QUI',
     'qui/controls/desktop/Panel',
-    'Locale'
+    'qui/controls/buttons/Select',
+    'qui/controls/buttons/Button',
+    'qui/controls/buttons/Seperator',
+    'controls/grid/Grid',
+    'Locale',
+    'Projects',
+    'Ajax'
 
-], function(QUI, QUIPanel, QUILocale)
+], function(QUI, QUIPanel, QUISelect, QUIButton, QUISeperator, Grid, QUILocale, Projects, Ajax)
 {
     "use strict";
 
@@ -24,7 +30,10 @@ define('package/quiqqer/blocks/bin/Manager', [
         Type    : 'package/quiqqer/blocks/bin/Manager',
 
         Binds : [
-            '$onCreate'
+            'loadBlocksFromProject',
+            'refresh',
+            '$onCreate',
+            '$onResize'
         ],
 
         options : {
@@ -35,17 +44,148 @@ define('package/quiqqer/blocks/bin/Manager', [
         {
             this.parent( options );
 
+            this.$Grid = false;
+
             this.addEvents({
-                onCreate : this.$onCreate
+                onCreate : this.$onCreate,
+                onResize : this.$onResize
             });
         },
 
+        /**
+         * Refresh the panel data
+         */
+        refresh : function()
+        {
+            if ( !this.$Elm ) {
+                return;
+            }
+
+            var self = this;
+
+            this.Loader.show();
+
+            this.getBlocksFromProject(this.$ProjectSelect.getValue(), function(result)
+            {
+
+                console.log( result );
+
+                self.Loader.hide();
+
+            });
+        },
+
+
         /**
          * event : on create
          */
         $onCreate : function()
         {
+            var self = this;
+
+            // Buttons
+            this.$ProjectSelect = new QUISelect({
+                name   : 'projects',
+                events : {
+                    onChange : this.refresh
+                }
+            });
+
+            this.addButton( this.$ProjectSelect );
+            this.addButton( new QUISeperator() );
+
+            this.addButton(
+                new QUIButton({
+                    text : 'Block hinzufügen'
+                })
+            );
+
+            // Grid
+            var Container = new Element('div').inject(
+                this.getContent()
+            );
+
+            this.$Grid = new Grid( Container, {
+                columnModel : [{
+                    header    : QUILocale.get( 'quiqqer/system', 'id' ),
+                    dataIndex : 'id',
+                    dataType  : 'integer',
+                    width     : 40
+                }, {
+                    header    : QUILocale.get( 'quiqqer/system', 'title' ),
+                    dataIndex : 'title',
+                    dataType  : 'string',
+                    width     : 140
+                }, {
+                    header    : QUILocale.get( 'quiqqer/system', 'description' ),
+                    dataIndex : 'description',
+                    dataType  : 'string',
+                    width     : 300
+                }, {
+                    header    : QUILocale.get( lg, 'block.type' ),
+                    dataIndex : 'type',
+                    dataType  : 'string',
+                    width     : 200
+                }]
+            });
 
+            this.Loader.show();
+
+            Projects.getList(function(projects)
+            {
+                for ( var project in projects )
+                {
+                    if ( !projects.hasOwnProperty( project ) ) {
+                        continue;
+                    }
+
+                    self.$ProjectSelect.appendChild(
+                        project, project, 'icon-home'
+                    );
+                }
+
+                self.$ProjectSelect.setValue(
+                    self.$ProjectSelect.firstChild().getAttribute( 'value' )
+                );
+            });
+        },
+
+        /**
+         * event : on resize
+         */
+        $onResize : function()
+        {
+            if ( !this.$Grid ) {
+                return;
+            }
+
+            var Body = this.getContent();
+
+            if ( !Body ) {
+                return;
+            }
+
+
+            var size = Body.getSize();
+
+            this.$Grid.setHeight( size.y - 40 );
+            this.$Grid.setWidth( size.x - 40 );
+        },
+
+        /**
+         * Return the blocksf from a project
+         *
+         * @param {String} project - name of the project
+         * @param {Function} callback - callback function
+         */
+        getBlocksFromProject : function(project, callback)
+        {
+            Ajax.get('package_quiqqer_blocks_ajax_project_getBlocks', callback, {
+                'package' : 'quiqqer/blocks',
+                project   : JSON.encode({
+                    name : project
+                })
+            });
         }
     });
 });
diff --git a/database.xml b/database.xml
new file mode 100644
index 0000000..ea38a7f
--- /dev/null
+++ b/database.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<database>
+
+    <global>
+        <table name="blocks">
+            <field type="INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY">id</field>
+            <field type="VARCHAR( 255 )">project</field>
+            <field type="VARCHAR( 255 ) NOT NULL">title</field>
+            <field type="TEXT NOT NULL">description</field>
+            <field type="TEXT">settings</field>
+            <field type="VARCHAR( 255 )">type</field>
+        </table>
+    </global>
+
+</database>
diff --git a/lib/QUI/Blocks/Block.php b/lib/QUI/Blocks/Block.php
new file mode 100644
index 0000000..f8c622b
--- /dev/null
+++ b/lib/QUI/Blocks/Block.php
@@ -0,0 +1,61 @@
+<?php
+
+/**
+ * This file contains \QUI\Blocks\Block
+ */
+
+namespace QUI\BLocks;
+
+use QUI;
+
+/**
+ * Class Block
+ * A Block from the Blockmanager
+ *
+ * @author www.pcsg.de (Henning Leutz)
+ * @package quiqqer/blocks
+ */
+class Block extends QUI\QDOM
+{
+    /**
+     * Constructor
+     * @param array $params - block params
+     */
+    public function __construct($params=array())
+    {
+        // default
+        $this->setAttributes(array(
+            'type'    => 'content',
+            'content' => '',
+            'control' => false
+        ));
+    }
+
+    /**
+     * Return the HTML of the Block
+     */
+    public function create()
+    {
+        if ( $this->getAttribute( 'control' ) )
+        {
+            $Ctrl = $this->getAttribute( 'control' );
+
+            if ( !is_callable( $Ctrl ) ) {
+                throw new QUI\Exception( 'Control not found. Block could not be create' );
+            }
+
+            /* @var $Control \QUI\Control */
+            $Control = new $Ctrl();
+
+            if ( !($Control instanceof QUI\Control) ) {
+                throw new QUI\Exception( 'Control not found. Block could not be create' );
+            }
+
+            $Control->setAttributes( $this->getAttributes() );
+
+            return $Control->create();
+        }
+
+        return $this->getAttribute( 'content' );
+    }
+}
\ No newline at end of file
diff --git a/lib/QUI/Blocks/Manager.php b/lib/QUI/Blocks/Manager.php
index bc1b2e5..00a9b8a 100644
--- a/lib/QUI/Blocks/Manager.php
+++ b/lib/QUI/Blocks/Manager.php
@@ -7,6 +7,8 @@
 namespace QUI\Blocks;
 
 use QUI;
+use QUI\Projects\Project;
+use QUI\Projects\Site;
 
 /**
  * Block Manager
@@ -15,17 +17,109 @@
  */
 class Manager
 {
+    /**
+     * Blocks table name
+     */
+    const TABLE = 'blocks';
 
+    /**
+     * Returns the available blocks
+     *
+     * @return array
+     */
     public function getAvailableBlocks()
     {
+        $cache = 'quiqqer/blocks/availableBlocks';
 
-    }
+        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;
+            }
 
+            $result = array_merge( $result, Utils::getBlocksFromXML( $blocksXML ) );
+        }
 
-    public function getBlocks($blockName='')
+        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)
     {
+        if ( empty( $blockArea ) ) {
+            return array();
+        }
+
+        $blockAreas = $Site->getAttribute( 'quiqqer.blocks.areas' );
+
+        QUI\System\Log::writeRecursive( $blockAreas );
 
         return array();
     }
 
+    /**
+     * 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 );
+    }
 }
diff --git a/lib/QUI/Blocks/Utils.php b/lib/QUI/Blocks/Utils.php
new file mode 100644
index 0000000..4bc599c
--- /dev/null
+++ b/lib/QUI/Blocks/Utils.php
@@ -0,0 +1,79 @@
+<?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;
+        }
+
+        foreach ( $blocks as $Block )
+        {
+            /* @var $Block \DOMElement */
+            $control     = $Block->getAttribute( 'control' );
+            $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' )
+                );
+            }
+
+            if ( $descLocale->length )
+            {
+                $description = array(
+                    'group' => $descLocale->item( 0 )->getAttribute( 'group' ),
+                    'var'   => $descLocale->item( 0 )->getAttribute( 'var' )
+                );
+            }
+
+            $list[] = array(
+                'control'     => $control,
+                'title'       => $title,
+                'description' => $description
+            );
+        }
+
+        return $list;
+    }
+}
-- 
GitLab