diff --git a/ajax/getBrick.php b/ajax/getBrick.php index 75732f744d18f1704fa547a5a7113c187e5ad355..3931c5d905467553c2a59d81892d30a8a975b70b 100644 --- a/ajax/getBrick.php +++ b/ajax/getBrick.php @@ -17,7 +17,8 @@ function package_quiqqer_bricks_ajax_getBrick($brickId) return array( 'attributes' => $Brick->getAttributes(), - 'settings' => $BrickManager->getAvailableBrickSettingsByBrickType( + 'settings' => $Brick->getSettings(), + 'availableSettings' => $BrickManager->getAvailableBrickSettingsByBrickType( $Brick->getAttribute( 'type' ) ) ); diff --git a/ajax/project/getBricks.php b/ajax/project/getBricks.php index fdacc8ce75d05e39d02d22e9724c6fbb4db85a5d..881a2cf7d1a073dd78086100b7b480512543f210 100644 --- a/ajax/project/getBricks.php +++ b/ajax/project/getBricks.php @@ -5,12 +5,13 @@ */ /** - * Returns the feed list + * Returns the bricks of the project area * * @param string $project - json array, Project Data + * @param string|bool $area - (optional), Area name * @return array */ -function package_quiqqer_bricks_ajax_project_getBricks($project) +function package_quiqqer_bricks_ajax_project_getBricks($project, $area=false) { $Project = QUI::getProjectManager()->decode( $project ); $BrickManager = new QUI\Bricks\Manager(); @@ -18,8 +19,20 @@ function package_quiqqer_bricks_ajax_project_getBricks($project) $bricks = $BrickManager->getBricksFromProject( $Project ); $result = array(); - foreach ( $bricks as $Brick ) { - $result[] = $Brick->getAttributes(); + foreach ( $bricks as $Brick ) + { + /* @var $Brick QUI\Bricks\Brick */ + if ( !$area ) + { + $result[] = $Brick->getAttributes(); + continue; + } + + $areas = $Brick->getAttribute('areas'); + + if ( strpos( $areas, ','.$area.',' ) !== false ) { + $result[] = $Brick->getAttributes(); + } } return $result; @@ -27,6 +40,6 @@ function package_quiqqer_bricks_ajax_project_getBricks($project) QUI::$Ajax->register( 'package_quiqqer_bricks_ajax_project_getBricks', - array( 'project' ), + array( 'project', 'area' ), 'Permission::checkAdminUser' ); diff --git a/bin/BrickEdit.js b/bin/BrickEdit.js index 30fb1e5fb03324ac4caf56fe94030a655e28ac28..913dcd98073ad3a5b40ad5d40bc6ffff420c9181 100644 --- a/bin/BrickEdit.js +++ b/bin/BrickEdit.js @@ -13,13 +13,15 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ 'qui/QUI', 'qui/controls/Control', + 'qui/utils/Form', 'package/quiqqer/bricks/bin/BrickAreas', 'Ajax', 'Locale', + 'utils/Controls', 'css!package/quiqqer/bricks/bin/BrickEdit.css' -], function(QUI, QUIControl, BrickAreas, Ajax, QUILocale) +], function(QUI, QUIControl, QUIFormUtils, BrickAreas, Ajax, QUILocale, ControlUtils) { "use strict"; @@ -81,10 +83,17 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ 'package_quiqqer_bricks_ajax_getAvailableBricks' ], function(data, bricks) { + /** + * @param {{availableSettings:object}} data + * @param {{attributes:object}} data + * @param {{settings:object}} data + */ self.$availableBricks = bricks; - self.$availableSettings = data.settings; + self.$availableSettings = data.availableSettings; self.setAttributes( data.attributes ); + self.setAttribute( 'settings', data.settings ); + self.$createData(function() { self.fireEvent( 'loaded', [ self ] ); }); @@ -134,21 +143,68 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ '</label>' }).inject( this.$Elm ); + var i, len, title, group, val; if ( this.$availableSettings ) { - console.log( this.$availableSettings ); + var setting, dataQui; + var SettingsElement = new Element('form', { + 'class' : 'quiqqer-bricks-brickedit-settings' + }); + + for ( i = 0, len = this.$availableSettings.length; i < len; i++ ) + { + setting = this.$availableSettings[ i ]; + dataQui = ''; + + if ( setting['data-qui'] !== '' ) { + dataQui = ' data-qui="'+ setting['data-qui'] +'" '; + } + + new Element('div', { + html : '<label class="quiqqer-bricks-areas">' + + ' <span class="quiqqer-bricks-brickedit-label-text">' + + setting.text + + ' </span>' + + '</label>' + + '<input type="'+ setting.type +'" ' + + ' name="'+ setting.name +'" ' + + ' class="'+ setting.class +'" ' + + dataQui + + '/>' + }).inject( SettingsElement ); + } + SettingsElement.inject( Container ); - var Setting; + // set data + QUIFormUtils.setDataToForm( + this.getAttribute( 'settings' ), + SettingsElement + ); - for ( i = 0, len = this.$availableSettings.length; i < len; i++ ) + // parse controls + ControlUtils.parse( SettingsElement ); + + QUI.parse(SettingsElement, function() { - Setting = this.$availableSettings[ i ]; + // set project to the controls + SettingsElement.getElements( '[data-quiid]' ).each(function(Elm) + { + var Control = QUI.Controls.getById( + Elm.get('data-quiid') + ); - console.log( Setting ); - } + if ( 'setProject' in Control ) + { + Control.setProject( + self.getAttribute( 'projectName' ), + self.getAttribute( 'projectLang' ) + ); + } + }); + }); } var Type = this.$Elm.getElement( '[name="type"]'), @@ -190,7 +246,7 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ // areas this.$Areas = new BrickAreas({ - brickId : this.getAttribute( 'id' ), + brickId : this.getAttribute( 'id' ), projectName : this.getAttribute( 'projectName' ), projectLang : this.getAttribute( 'projectLang' ), areas : areas, @@ -254,7 +310,7 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ */ save : function(callback) { - var Type = this.$Elm.getElement( '[name="type"]'), + var Type = this.$Elm.getElement( '[name="type"]' ), Title = this.$Elm.getElement( '[name="title"]' ); var data = { @@ -268,6 +324,16 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ data.content = this.$Editor.getContent(); } + // settings + var SettingsForm = this.$Elm.getElement( + '.quiqqer-bricks-brickedit-settings' + ); + + if ( SettingsForm ) { + data.settings = QUIFormUtils.getFormData( SettingsForm ); + } + + Ajax.post('package_quiqqer_bricks_ajax_brick_save', function() { if ( typeof callback === 'function' ) { diff --git a/bin/Manager.js b/bin/Manager.js index 4ec810cead68c064334fea1bbc750cb4a5c31de5..fb2b059f2d746ad731430af199066d0f53a6dd83 100644 --- a/bin/Manager.js +++ b/bin/Manager.js @@ -306,7 +306,7 @@ define('package/quiqqer/bricks/bin/Manager', [ }, /** - * + * Opens the brick creation dialog */ $openCreateDialog : function() { @@ -482,9 +482,10 @@ define('package/quiqqer/bricks/bin/Manager', [ require(['package/quiqqer/bricks/bin/BrickEdit'], function(BrickEdit) { Brick = new BrickEdit({ - id : brickId, - project : self.$ProjectSelect.getValue(), - events : + id : brickId, + projectName : self.$ProjectSelect.getValue(), + projectLang : self.$ProjectLangs.getValue(), + events : { onLoaded : function() { self.Loader.hide(); diff --git a/bin/Site/Area.js b/bin/Site/Area.js index b5b7844ae04b8f5491579d31ba40e79851c28203..8886cc2a736f0183d57e4113d3d39c2d4761e801 100644 --- a/bin/Site/Area.js +++ b/bin/Site/Area.js @@ -105,7 +105,8 @@ define('package/quiqqer/bricks/bin/Site/Area', [ }, { 'package' : 'quiqqer/bricks', - project : Project.encode() + project : Project.encode(), + area : this.getAttribute( 'name' ) }); }, diff --git a/bin/Site/Category.js b/bin/Site/Category.js index 20bbc2cf64d8b84aa9743b1253a9be0efd2ef329..7a7682dadd9bdf04f8475217d59cd5c141a97053 100644 --- a/bin/Site/Category.js +++ b/bin/Site/Category.js @@ -35,7 +35,7 @@ define('package/quiqqer/bricks/bin/Site/Category', [ initialize: function (options) { - this.parent(options); + this.parent( options ); this.Loader = new QUILoader(); this.areas = []; @@ -75,11 +75,11 @@ define('package/quiqqer/bricks/bin/Site/Category', [ { var i, len, data, AC; - var Site = self.getAttribute( 'Site'), + var Site = self.getAttribute( 'Site' ), areas = Site.getAttribute( 'quiqqer.bricks.areas' ); if ( areas ) { - areas = JSON.decode(areas); + areas = JSON.decode( areas ); } for ( i = 0, len = bricks.length; i < len; i++ ) @@ -109,7 +109,7 @@ define('package/quiqqer/bricks/bin/Site/Category', [ { var i, len, AC; - var Site = this.getAttribute( 'Site'), + var Site = this.getAttribute( 'Site' ), areas = {}; for ( i = 0, len = this.areas.length; i < len; i++ ) @@ -128,7 +128,7 @@ define('package/quiqqer/bricks/bin/Site/Category', [ */ getBrickAreas : function(callback) { - var Site = this.getAttribute( 'Site'), + var Site = this.getAttribute( 'Site' ), Project = Site.getProject(); QUIAjax.get('package_quiqqer_bricks_ajax_project_getAreas', callback, { @@ -146,7 +146,7 @@ define('package/quiqqer/bricks/bin/Site/Category', [ */ $insertBrickAreaEdit : function(area) { - var Site = this.getAttribute( 'Site'), + var Site = this.getAttribute( 'Site' ), Project = Site.getProject(), Control = new Area(); @@ -161,5 +161,4 @@ define('package/quiqqer/bricks/bin/Site/Category', [ return Control; } }); - -}); \ No newline at end of file +}); diff --git a/lib/QUI/Bricks/Brick.php b/lib/QUI/Bricks/Brick.php index 60ae818e739be4d4107b555ccecd242031e02121..5f2b6fba2b472bce0e6dbdb2d35a519399c8cb5a 100644 --- a/lib/QUI/Bricks/Brick.php +++ b/lib/QUI/Bricks/Brick.php @@ -41,6 +41,40 @@ public function __construct($params=array()) $this->setAttributes( $default ); + foreach ( $default as $key => $value ) + { + if ( isset( $params[ $key ] ) ) { + $this->setAttribute( $key, $params[ $key ] ); + } + } + + + // default settings from control + $Control = $this->_getControl(); + $Manager = new Manager(); + + $availableSettings = $Manager->getAvailableBrickSettingsByBrickType( + $this->getAttribute( 'type' ) + ); + + foreach ( $availableSettings as $entry ) { + $this->_settings[ $entry['name'] ] = false; + } + + // control default settings + if ( $Control ) + { + $controlSettings = $Control->getAttributes(); + + foreach ( $this->_settings as $key => $value ) + { + if ( isset( $controlSettings[ $key ] ) ) { + $this->_settings[ $key ] = $controlSettings[ $key ]; + } + } + } + + // settings from database if ( isset( $params['settings'] ) ) { $settings = $params['settings']; @@ -49,13 +83,15 @@ public function __construct($params=array()) $settings = json_decode( $settings, true ); } - $this->_settings = $settings; - } + if ( !is_array( $settings ) ) { + return; + } - foreach ( $default as $key => $value ) - { - if ( isset( $params[ $key ] ) ) { - $this->setAttribute( $key, $params[ $key ] ); + foreach ( $this->_settings as $key => $value ) + { + if ( isset( $settings[ $key ] ) ) { + $this->_settings[ $key ] = $settings[ $key ]; + } } } } @@ -71,31 +107,86 @@ public function create() return $this->getAttribute( 'content' ); } + $Control = $this->_getControl(); + + if ( !$Control ) { + throw new QUI\Exception( 'Control not found. Brick could not be create' ); + } + + $Control->setAttributes( $this->getSettings() ); + + return $Control->create(); + } + /** + * Return the internal control + * @return QUI\Control|Bool + */ + protected function _getControl() + { $Ctrl = $this->getAttribute( 'type' ); if ( !is_callable( $Ctrl ) && !class_exists( $Ctrl ) ) { - throw new QUI\Exception( 'Control not found. Brick could not be create' ); + return false; } /* @var $Control \QUI\Control */ $Control = new $Ctrl(); if ( !($Control instanceof QUI\Control) ) { - throw new QUI\Exception( 'Control not found. Brick could not be create' ); + return false; } - $Control->setAttributes( $this->getSettings() ); - - return $Control->create(); + return $Control; } /** * Return the brick settings - * @return array + * + * @return Array */ public function getSettings() { return $this->_settings; } -} \ No newline at end of file + + /** + * Set brick settings + * + * @param Array $settings + */ + public function setSettings($settings) + { + foreach ( $settings as $key => $value ) { + $this->setSetting( $key, $value ); + } + } + + /** + * Return the setting of the brick + * + * @param String $name - Name of the setting + * @return Bool|String + */ + public function getSetting($name) + { + if ( isset( $this->_settings[ $name ] ) ) { + return $this->_settings[ $name ]; + } + + return false; + } + + /** + * Set a brick setting + * + * @param String $name - name of the setting + * @param String $value - value of the setting + */ + public function setSetting($name, $value) + { + if ( isset( $this->_settings[ $name ] ) ) { + $this->_settings[ $name ] = $value; + } + } +} diff --git a/lib/QUI/Bricks/Manager.php b/lib/QUI/Bricks/Manager.php index e8f6bf4208a3c2843f8b518c21059e148310da08..040abdff8e79e344b21d2e2b99f4bad88f36a646 100644 --- a/lib/QUI/Bricks/Manager.php +++ b/lib/QUI/Bricks/Manager.php @@ -233,8 +233,13 @@ public function getAvailableBrickSettingsByBrickType($brickType) foreach ( $Settings as $Setting ) { + /* @var $Setting \DOMElement */ $settings[] = array( - 'name' => $Setting->getAttribute( 'name' ) + 'name' => $Setting->getAttribute( 'name' ), + 'text' => QUI\Utils\DOM::getTextFromNode( $Setting ), + 'type' => $Setting->getAttribute( 'type' ), + 'class' => $Setting->getAttribute( 'class' ), + 'data-qui' => $Setting->getAttribute( 'data-qui' ) ); } @@ -364,12 +369,16 @@ public function saveBrick($brickId, array $brickData) $Brick->setAttributes( $brickData ); + if ( isset( $brickData['settings'] ) ) { + $Brick->setSettings( $brickData['settings'] ); + } + QUI::getDataBase()->update($this->_getTable(), array( 'title' => $Brick->getAttribute( 'title' ), 'description' => $Brick->getAttribute( 'description' ), 'content' => $Brick->getAttribute( 'content' ), 'type' => $Brick->getAttribute( 'type' ), - 'settings' => json_encode( $Brick->getAttribute( 'settings' ) ), + 'settings' => json_encode( $Brick->getSettings() ), 'areas' => $areaString ), array( 'id' => (int)$brickId