From 0a47b872ac28f25e87d533a63d844d2dbf0449ac Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Fri, 9 Jan 2015 10:54:53 +0100
Subject: [PATCH] bricks -> project multilingual

---
 ajax/getBrick.php          |   7 ++-
 bin/AreaWindow.js          |   6 +-
 bin/BrickAreas.js          |   6 +-
 bin/BrickEdit.js           |  37 +++++++++---
 bin/Manager.js             |  82 ++++++++++++++++++++++----
 database.xml               |   1 +
 lib/QUI/Bricks/Manager.php | 116 +++++++++++++++++++++++++++++++++----
 7 files changed, 219 insertions(+), 36 deletions(-)

diff --git a/ajax/getBrick.php b/ajax/getBrick.php
index 294b6bb..75732f7 100644
--- a/ajax/getBrick.php
+++ b/ajax/getBrick.php
@@ -15,7 +15,12 @@ function package_quiqqer_bricks_ajax_getBrick($brickId)
     $BrickManager = new \QUI\Bricks\Manager();
     $Brick = $BrickManager->getBrickById( $brickId );
 
-    return $Brick->getAttributes();
+    return array(
+        'attributes' => $Brick->getAttributes(),
+        'settings'   => $BrickManager->getAvailableBrickSettingsByBrickType(
+            $Brick->getAttribute( 'type' )
+        )
+    );
 }
 
 \QUI::$Ajax->register(
diff --git a/bin/AreaWindow.js b/bin/AreaWindow.js
index a394e57..9e39edc 100644
--- a/bin/AreaWindow.js
+++ b/bin/AreaWindow.js
@@ -32,7 +32,8 @@ define('package/quiqqer/bricks/bin/AreaWindow', [
 
         options : {
             title     : 'Projekt Brickbereiche',
-            project   : false,
+            projectName : false,
+            projectLang : false,
             maxHeight : 500,
             maxWidth  : 400
         },
@@ -86,7 +87,8 @@ define('package/quiqqer/bricks/bin/AreaWindow', [
             Ajax.get('package_quiqqer_bricks_ajax_project_getAreas', callback, {
                 'package' : 'quiqqer/brick',
                 project   : JSON.encode({
-                    name : this.getAttribute( 'project' )
+                    name : this.getAttribute( 'projectName' ),
+                    lang : this.getAttribute( 'projectLang' )
                 })
             });
         },
diff --git a/bin/BrickAreas.js b/bin/BrickAreas.js
index f3572fd..ce76692 100644
--- a/bin/BrickAreas.js
+++ b/bin/BrickAreas.js
@@ -36,7 +36,8 @@ define('package/quiqqer/bricks/bin/BrickAreas', [
         options : {
             brickId : false, // brickId
             styles  : false,
-            project : false,
+            projectName : false,
+            projectLang : false,
             areas   : false
         },
 
@@ -79,7 +80,8 @@ define('package/quiqqer/bricks/bin/BrickAreas', [
                     onClick : function()
                     {
                         new AreaWindow({
-                            project : self.getAttribute( 'project' ),
+                            projectName : self.getAttribute( 'projectName' ),
+                            projectLang : self.getAttribute( 'projectLang' ),
                             events  :
                             {
                                 onSubmit : function(Win, areas)
diff --git a/bin/BrickEdit.js b/bin/BrickEdit.js
index 4027c6d..30fb1e5 100644
--- a/bin/BrickEdit.js
+++ b/bin/BrickEdit.js
@@ -34,15 +34,17 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
         ],
 
         options : {
-            id      : false,
-            project : false
+            id : false,
+            projectName : false,
+            projectLang : false
         },
 
         initialize : function(options)
         {
             this.parent( options );
 
-            this.$availableBricks = [];
+            this.$availableBricks   = [];
+            this.$availableSettings = [];
 
             this.$Editor = false;
             this.$Areas  = false;
@@ -79,9 +81,10 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
                 'package_quiqqer_bricks_ajax_getAvailableBricks'
             ], function(data, bricks)
             {
-                self.$availableBricks = bricks;
+                self.$availableBricks   = bricks;
+                self.$availableSettings = data.settings;
 
-                self.setAttributes( data );
+                self.setAttributes( data.attributes );
                 self.$createData(function() {
                     self.fireEvent( 'loaded', [ self ] );
                 });
@@ -111,7 +114,7 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
         {
             var self = this;
 
-            new Element('div', {
+            var Container = new Element('div', {
                 'html' : '<label>' +
                          '    <span class="quiqqer-bricks-brickedit-label-text">' +
                          '        Title' +
@@ -133,6 +136,21 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
 
             var i, len, title, group, val;
 
+            if ( this.$availableSettings )
+            {
+                console.log( this.$availableSettings );
+
+
+                var Setting;
+
+                for ( i = 0, len = this.$availableSettings.length; i < len; i++ )
+                {
+                    Setting = this.$availableSettings[ i ];
+
+                    console.log( Setting );
+                }
+            }
+
             var Type  = this.$Elm.getElement( '[name="type"]'),
                 Title = this.$Elm.getElement( '[name="title"]' );
 
@@ -173,9 +191,10 @@ define('package/quiqqer/bricks/bin/BrickEdit', [
             // areas
             this.$Areas = new BrickAreas({
                 brickId : this.getAttribute( 'id' ),
-                project : this.getAttribute( 'project' ),
-                areas   : areas,
-                styles  : {
+                projectName : this.getAttribute( 'projectName' ),
+                projectLang : this.getAttribute( 'projectLang' ),
+                areas  : areas,
+                styles : {
                     height : 120
                 }
             }).inject( this.$Elm.getElement( '.quiqqer-bricks-areas' ), 'after'  );
diff --git a/bin/Manager.js b/bin/Manager.js
index eb34aa0..4ec810c 100644
--- a/bin/Manager.js
+++ b/bin/Manager.js
@@ -35,6 +35,7 @@ define('package/quiqqer/bricks/bin/Manager', [
         Binds : [
             'loadBricksFromProject',
             'refresh',
+            '$refreshProjectLanguages',
             '$onCreate',
             '$onResize',
             '$openCreateDialog',
@@ -51,7 +52,9 @@ define('package/quiqqer/bricks/bin/Manager', [
         {
             this.parent( options );
 
-            this.$Grid = false;
+            this.$Grid          = false;
+            this.$ProjectSelect = false;
+            this.$ProjectLangs  = false;
 
             this.addEvents({
                 onCreate : this.$onCreate,
@@ -70,11 +73,13 @@ define('package/quiqqer/bricks/bin/Manager', [
                 return;
             }
 
-            var self = this;
+            var self    = this,
+                project = this.$ProjectSelect.getValue(),
+                lang    = this.$ProjectLangs.getValue();
 
             this.Loader.show();
 
-            this.getBricksFromProject(this.$ProjectSelect.getValue(), function(result)
+            this.getBricksFromProject( project, lang, function(result)
             {
                 if ( typeof callback === 'function' ) {
                     callback();
@@ -85,7 +90,6 @@ define('package/quiqqer/bricks/bin/Manager', [
                 });
 
                 self.refreshButtons();
-
                 self.Loader.hide();
             });
         },
@@ -123,13 +127,24 @@ define('package/quiqqer/bricks/bin/Manager', [
 
             // Buttons
             this.$ProjectSelect = new QUISelect({
-                name   : 'projects',
+                name   : 'projects-name',
+                events : {
+                    onChange : this.$refreshProjectLanguages
+                }
+            });
+
+            this.$ProjectLangs = new QUISelect({
+                name   : 'projects-langs',
                 events : {
                     onChange : this.refresh
+                },
+                styles : {
+                    width : 80
                 }
             });
 
             this.addButton( this.$ProjectSelect );
+            this.addButton( this.$ProjectLangs );
             this.addButton( new QUISeperator() );
 
             this.addButton(
@@ -251,6 +266,45 @@ define('package/quiqqer/bricks/bin/Manager', [
             this.refreshButtons();
         },
 
+        /**
+         * Refresh the project language dropdown
+         */
+        $refreshProjectLanguages : function()
+        {
+            var self          = this,
+                activeProject = this.$ProjectSelect.getValue();
+
+            Projects.getList(function(projects)
+            {
+                for ( var project in projects )
+                {
+                    if ( !projects.hasOwnProperty( project ) ) {
+                        continue;
+                    }
+
+                    if ( activeProject != project ) {
+                        continue;
+                    }
+
+                    var langs = projects[ project ].langs;
+                        langs = langs.split( ',' );
+
+                    self.$ProjectLangs.clear();
+
+                    for ( var i = 0, len = langs.length; i < len; i++ )
+                    {
+                        self.$ProjectLangs.appendChild(
+                            langs[ i ], langs[ i ], 'icon-home'
+                        );
+                    }
+
+                    self.$ProjectLangs.setValue(
+                        self.$ProjectLangs.firstChild().getAttribute( 'value' )
+                    );
+                }
+            });
+        },
+
         /**
          *
          */
@@ -338,14 +392,16 @@ define('package/quiqqer/bricks/bin/Manager', [
                             return;
                         }
 
-                        self.createBrick(self.$ProjectSelect.getValue(), {
+                        var project = self.$ProjectSelect.getValue(),
+                            lang    = self.$ProjectLangs.getValue();
+
+                        self.createBrick(project, lang, {
                             title : Title.value,
                             type  : Type.value
                         }, function(brickId)
                         {
                             Win.close();
 
-
                             self.refresh(function() {
                                 self.editBrick( brickId );
                             });
@@ -484,14 +540,16 @@ define('package/quiqqer/bricks/bin/Manager', [
          * Return the bricksf from a project
          *
          * @param {String} project - name of the project
+         * @param {String} lang - Language of the project
          * @param {Function} callback - callback function
          */
-        getBricksFromProject : function(project, callback)
+        getBricksFromProject : function(project, lang, callback)
         {
             Ajax.get('package_quiqqer_bricks_ajax_project_getBricks', callback, {
                 'package' : 'quiqqer/bricks',
                 project   : JSON.encode({
-                    name : project
+                    name : project,
+                    lang : lang
                 })
             });
         },
@@ -500,15 +558,17 @@ define('package/quiqqer/bricks/bin/Manager', [
          * Create a new brick
          *
          * @param {String} project
+         * @param {String} lang
          * @param {Object} data
          * @param {Function} callback
          */
-        createBrick : function(project, data, callback)
+        createBrick : function(project, lang, data, callback)
         {
             Ajax.post('package_quiqqer_bricks_ajax_project_createBrick', callback, {
                 'package' : 'quiqqer/bricks',
                 project : JSON.encode({
-                    name : project
+                    name : project,
+                    lang : lang
                 }),
                 data : JSON.encode( data )
             });
diff --git a/database.xml b/database.xml
index f5c89ba..3891259 100644
--- a/database.xml
+++ b/database.xml
@@ -5,6 +5,7 @@
         <table name="bricks">
             <field type="INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY">id</field>
             <field type="VARCHAR( 255 )">project</field>
+            <field type="VARCHAR( 2 )">lang</field>
             <field type="VARCHAR( 255 ) NOT NULL">title</field>
             <field type="TEXT NOT NULL">description</field>
             <field type="TEXT">settings</field>
diff --git a/lib/QUI/Bricks/Manager.php b/lib/QUI/Bricks/Manager.php
index 55ee3c4..e8f6bf4 100644
--- a/lib/QUI/Bricks/Manager.php
+++ b/lib/QUI/Bricks/Manager.php
@@ -41,6 +41,7 @@ public function createBrickForProject(Project $Project, Brick $Brick)
             $this->_getTable(),
             array(
                 'project'     => $Project->getName(),
+                'lang'        => $Project->getLang(),
                 'title'       => $Brick->getAttribute('title'),
                 'description' => $Brick->getAttribute('description'),
                 'type'        => $Brick->getAttribute('type')
@@ -52,6 +53,14 @@ public function createBrickForProject(Project $Project, Brick $Brick)
         return $lastId;
     }
 
+    /**
+     * CLears the bricks cache
+     */
+    public function clearCache()
+    {
+        QUI\Cache\Manager::clear( 'quiqqer/bricks' );
+    }
+
     /**
      * Delete the brick
      *
@@ -133,8 +142,7 @@ public function getAvailableBricks()
 
         }
 
-        $PKM      = QUI::getPackageManager();
-        $packages = $PKM->getInstalled();
+        $xmlFiles = $this->_getBricksXMLFiles();
         $result   = array();
 
         $result[] = array(
@@ -143,14 +151,7 @@ public function getAvailableBricks()
             'control'     => 'content'
         );
 
-        foreach ( $packages as $package )
-        {
-            $bricksXML = OPT_DIR . $package['name'] .'/bricks.xml';
-
-            if ( !file_exists( $bricksXML ) ) {
-                continue;
-            }
-
+        foreach ( $xmlFiles as $bricksXML ) {
             $result = array_merge( $result, Utils::getBricksFromXML( $bricksXML ) );
         }
 
@@ -190,6 +191,61 @@ public function getBrickById($id)
         return $this->_bricks[ $id ];
     }
 
+    /**
+     * Return the available brick settings by the brick type
+     *
+     * @param $brickType
+     * @return array
+     */
+    public function getAvailableBrickSettingsByBrickType($brickType)
+    {
+        if ( $brickType == 'content' ) {
+            return array();
+        }
+
+        $cache = 'quiqqer/bricks/brickType/'. md5($brickType);
+
+        try
+        {
+            return QUI\Cache\Manager::get( $cache );
+
+        } catch ( QUI\Exception $Exception )
+        {
+
+        }
+
+
+        $settings = array();
+        $xmlFiles = $this->_getBricksXMLFiles();
+
+        foreach ( $xmlFiles as $brickXML )
+        {
+            $Dom  = QUI\Utils\XML::getDomFromXml( $brickXML );
+            $Path = new \DOMXPath( $Dom );
+
+            $Settings = $Path->query(
+                "//quiqqer/bricks/brick[@control='{$brickType}']/settings/setting"
+            );
+
+            if ( !$Settings->length ) {
+                continue;
+            }
+
+            foreach ( $Settings as $Setting )
+            {
+                $settings[] = array(
+                    'name' => $Setting->getAttribute( 'name' )
+                );
+            }
+
+            break;
+        }
+
+        QUI\Cache\Manager::set( $cache, $settings );
+
+        return $settings;
+    }
+
     /**
      * Return the bricks from the area
      *
@@ -244,7 +300,8 @@ public function getBricksFromProject(Project $Project)
         $list = QUI::getDataBase()->fetch(array(
             'from'  => $this->_getTable(),
             'where' => array(
-                'project' => $Project->getName()
+                'project' => $Project->getName(),
+                'lang'    => $Project->getLang()
             )
         ));
 
@@ -327,4 +384,41 @@ protected function _getTable()
     {
         return QUI::getDBTableName( self::TABLE );
     }
+
+    /**
+     * List of available bricks.xml files
+     * @return array
+     */
+    protected function _getBricksXMLFiles()
+    {
+        $cache = 'quiqqer/bricks/availableBrickFiles';
+
+        try
+        {
+            return QUI\Cache\Manager::get( $cache );
+
+        } catch ( QUI\Exception $Exception )
+        {
+
+        }
+
+        $PKM      = QUI::getPackageManager();
+        $packages = $PKM->getInstalled();
+        $result   = array();
+
+        foreach ( $packages as $package )
+        {
+            $bricksXML = OPT_DIR . $package['name'] .'/bricks.xml';
+
+            if ( !file_exists( $bricksXML ) ) {
+                continue;
+            }
+
+            $result[] = $bricksXML;
+        }
+
+        QUI\Cache\Manager::set( $cache, $result );
+
+        return $result;
+    }
 }
-- 
GitLab