[icinga-checkins] icinga.org: icinga-web/mfriedrich/fixes: * Added more providers and fields to builder

git at icinga.org git at icinga.org
Mon Jan 10 09:04:59 CET 2011


Module: icinga-web
Branch: mfriedrich/fixes
Commit: 55e1b2db0030d5a21a4e78853bf6338932f282ee
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=55e1b2db0030d5a21a4e78853bf6338932f282ee

Author: Marius Hein <marius.hein at netways.de>
Date:   Fri Dec 17 17:20:47 2010 +0100

* Added more providers and fields to builder

---

 .../DataProvider/GroupProviderAction.class.php     |    2 +-
 .../AppKit/lib/util/AppKitArrayUtil.class.php      |    9 +++-
 .../actions/Provider/CategoriesAction.class.php    |   31 +++++++++-
 app/modules/Cronks/config/module.xml               |    1 +
 app/modules/Cronks/lib/js/CronkBuilder.js          |   67 +++++++++++++++++++-
 .../models/Provider/CronksDataModel.class.php      |   43 ++++++++++---
 .../Cronks/validate/Provider/Categories.xml        |   24 +++++++
 7 files changed, 161 insertions(+), 16 deletions(-)

diff --git a/app/modules/AppKit/actions/DataProvider/GroupProviderAction.class.php b/app/modules/AppKit/actions/DataProvider/GroupProviderAction.class.php
index 6be517e..aa488e2 100755
--- a/app/modules/AppKit/actions/DataProvider/GroupProviderAction.class.php
+++ b/app/modules/AppKit/actions/DataProvider/GroupProviderAction.class.php
@@ -24,7 +24,7 @@ class AppKit_DataProvider_GroupProviderAction extends AppKitBaseAction
 	}
 	
 	public function getCredentials() {
-		return array ('appkit.admin', 'appkit.admin.groups');
+		return array ('appkit.admin', 'appkit.admin.groups', 'icinga_user');
 	}
 	
 	public function executeRead(AgaviRequestDataHolder $rd) {
diff --git a/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php b/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
index f539ce4..3fc293b 100755
--- a/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
+++ b/app/modules/AppKit/lib/util/AppKitArrayUtil.class.php
@@ -133,7 +133,14 @@ class AppKitArrayUtil {
 				}
 			}
 		}
-	} 
+	}
+	
+	public static function swapKeys(array &$array, array $map) {
+		foreach ($map as $src=>$target) {
+			$array[$target] = $array[$src];
+			unset($array[$src]);
+		}
+	}
 }
 
 class AppKitArrayUtilException extends AppKitException {}
diff --git a/app/modules/Cronks/actions/Provider/CategoriesAction.class.php b/app/modules/Cronks/actions/Provider/CategoriesAction.class.php
index d778a12..cc52bfb 100755
--- a/app/modules/Cronks/actions/Provider/CategoriesAction.class.php
+++ b/app/modules/Cronks/actions/Provider/CategoriesAction.class.php
@@ -38,7 +38,9 @@ class Cronks_Provider_CategoriesAction extends CronksBaseAction {
 	
 	public function executeRead(AgaviParameterHolder $rd) {
 		
-		$categories = $this->cronks->getCategories();
+		$all = (bool)$rd->getParameter('all', false);
+		
+		$categories = $this->cronks->getCategories($all);
 		
 		$this->setAttributeByRef('categories', $categories);
 		
@@ -46,7 +48,32 @@ class Cronks_Provider_CategoriesAction extends CronksBaseAction {
 	}
 	
 	public function executeWrite(AgaviParameterHolder $rd) {
-		return $this->getDefaultViewName();
+		
+		if ($rd->getParameter('xaction', false) == 'create') {
+			
+			$rows = json_decode($rd->getParameter('rows', array()));
+			
+			if (!is_array($rows)) {
+				$rows = array($rows);
+			}
+			
+			$c = array ();
+			
+			foreach ($rows as $category) {
+				try {
+					$this->cronks->createCategory((array)$category);
+					$c[] = (array)$category;
+				}
+				catch (Doctrine_Exception $e) {}
+			}
+			
+			$this->setAttributeByRef('categories', $c);
+			
+			return $this->getDefaultViewName();
+			
+		}
+		
+		return $this->executeRead($rd);
 	}
 	
 	public function isSecure() {
diff --git a/app/modules/Cronks/config/module.xml b/app/modules/Cronks/config/module.xml
index 7f4811f..19716cf 100755
--- a/app/modules/Cronks/config/module.xml
+++ b/app/modules/Cronks/config/module.xml
@@ -21,6 +21,7 @@
 					<ae:parameter>%core.module_dir%/Cronks/lib/js/Portlet.js</ae:parameter>
 					<ae:parameter>%core.root_dir%/lib/ext3/examples/ux/GroupTabPanel.js</ae:parameter>
 					<ae:parameter>%core.root_dir%/lib/ext3/examples/ux/GroupTab.js</ae:parameter>
+					<ae:parameter>%core.root_dir%/lib/ext3/examples/ux/MultiSelect.js</ae:parameter>
 
 					<ae:parameter>%core.module_dir%/Cronks/lib/js/Cronk.js</ae:parameter>
 					<ae:parameter>%core.module_dir%/Cronks/lib/js/CronkUtil.js</ae:parameter>
diff --git a/app/modules/Cronks/lib/js/CronkBuilder.js b/app/modules/Cronks/lib/js/CronkBuilder.js
index b7eaa38..c23f9f6 100644
--- a/app/modules/Cronks/lib/js/CronkBuilder.js
+++ b/app/modules/Cronks/lib/js/CronkBuilder.js
@@ -86,7 +86,7 @@ Ext.extend(Cronk.util.CronkBuilder, Ext.Window, {
         		text: _('Add'),
         		handler: function(b, e) {
         			Ext.MessageBox.prompt(_('Add'), _('Add new parameter to properties'), function(btn, text) {
-        				if (!Ext.isEmpty(text)) {
+	    				if (!Ext.isEmpty(text)) {
 							var rec = new Ext.grid.PropertyRecord({
 							    name: text,
 							    value: null
@@ -113,6 +113,29 @@ Ext.extend(Cronk.util.CronkBuilder, Ext.Window, {
 	},
 	
 	_buildForm: function() {
+		
+		this.categories = new Ext.data.JsonStore({
+			autoDestroy: true,
+			url: AppKit.c.path + '/cronks/provider/categories',
+			baseParams: { all : 1 },
+			writer: new Ext.data.JsonWriter({
+			    encode: true,
+			    writeAllFields: false
+			})
+		});
+		
+		this.categories.load();
+		
+		this.groups = new Ext.data.JsonStore({
+			autoDestroy: true,
+			url: AppKit.c.path + '/appkit/provider/groups',
+			baseParams: { all : 1 },
+			writer: new Ext.data.JsonWriter({
+			    encode: true,
+			    writeAllFields: false
+			})
+		});
+		
 		return new Ext.form.FormPanel({
 			layout: 'border',
 			height: 400,
@@ -127,7 +150,7 @@ Ext.extend(Cronk.util.CronkBuilder, Ext.Window, {
 		        layout: 'form',
 		        region: 'center',
 		        height: 400,
-		        items: {
+		        items: [{
 		        	xtype: 'fieldset',
 		        	title: _('Meta'),
 		        	
@@ -149,8 +172,46 @@ Ext.extend(Cronk.util.CronkBuilder, Ext.Window, {
 			        	name: 'cid',
 			        	fieldLabel: _('Cronk Id'),
 			        	readOnly: true
+			        }, {
+			        	xtype: 'checkbox',
+			        	name: 'hide',
+			        	fieldLabel: _('Hidden')
 			        }]
-		        }
+		        }, {
+		        	xtype: 'fieldset',
+		        	title: _('Categories'),
+		        	height: 180,
+		        	items: [{
+		        		xtype: 'multiselect',
+		        		name: 'categories',
+		        		fieldLabel: _('Available'),
+		        		width: 200,
+		        		store: this.categories,
+		        		valueField: 'title',
+		        		displayField: 'title',
+		        		tbar: [{
+		        			text: _('Add'),
+		        			iconCls: 'icinga-icon-add',
+		        			handler: function(b, e) {
+		        				var c = this.categories;
+		        				
+		        				Ext.MessageBox.prompt(_('Add'), _('Add new category'), function(btn, text) {
+				    				if (!Ext.isEmpty(text)) {
+										var r = new c.recordType({
+											title: text,
+											visible: true,
+											position: 0,
+											active: false
+										});
+										
+										c.add(r);
+									}
+			        			}, this);
+		        			},
+		        			scope: this
+		        		}]
+		        	}]
+		        }]
 		    }, {
 		    	region: 'east',
 		    	width: 350,
diff --git a/app/modules/Cronks/models/Provider/CronksDataModel.class.php b/app/modules/Cronks/models/Provider/CronksDataModel.class.php
index d28403e..9049427 100755
--- a/app/modules/Cronks/models/Provider/CronksDataModel.class.php
+++ b/app/modules/Cronks/models/Provider/CronksDataModel.class.php
@@ -2,6 +2,12 @@
 
 class Cronks_Provider_CronksDataModel extends CronksBaseModel {
 
+	private static $cat_map = array (
+		'title'		=> 'cc_name',
+		'visible'	=> 'cc_visible',
+		'position'	=> 'cc_position'
+	); 
+	
 	/**
 	 * @var array
 	 */
@@ -60,18 +66,26 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel {
 		return $out;
 	}
 	
-	private function getDbCategories() {
+	private function getDbCategories($get_all=false) {
 		$collection = Doctrine_Query::create()
 		->select('cat.*')
-		->from('CronkCategory cat')
-		->innerJoin('cat.Cronk c')
-		->innerJoin('c.NsmPrincipal p')
-		->andWhereIn('p.principal_id', $this->principals)
-		->execute();
+		->from('CronkCategory cat');
+		
+		if ($get_all !== true) {
+			
+			$p = $this->principals;
+			$p[] = $this->user->user_id;
+			
+			$collection->innerJoin('cat.Cronk c')
+			->innerJoin('c.NsmPrincipal p')
+			->andWhereIn('p.principal_id', $u);
+		}
+		
+		$res = $collection->execute();
 		
 		$out = array ();
 		
-		foreach ($collection as $category) {
+		foreach ($res as $category) {
 			$out[$category->cc_name] = array (
 				'title'		=> $category->cc_name,
 				'visible'	=> (bool)$category->cc_visible,
@@ -83,9 +97,10 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel {
 		return $out;
 	}
 	
-	public function getCategories() {
+	public function getCategories($get_all=false) {
+		
 		$categories = $this->getXMLCategories();
-		$categories = (array)$this->getDbCategories() + $categories;
+		$categories = (array)$this->getDbCategories($get_all) + $categories;
 		
 		AppKitArrayUtil::subSort($categories, 'title');
 		AppKitArrayUtil::subSort($categories, 'position');		
@@ -99,6 +114,16 @@ class Cronks_Provider_CronksDataModel extends CronksBaseModel {
 		return $categories;
 	}
 	
+	public function createCategory(array $cat) {
+		AppKitArrayUtil::swapKeys($cat, self::$cat_map);
+
+		$category = new CronkCategory();
+		$category->fromArray($cat);
+		$category->save();
+		
+		return $category;
+	}
+	
 	private function checkGroups($listofnames) {
 		$groups = AppKitArrayUtil::trimSplit($listofnames, ',');
 		if (is_array($groups) && count($groups)) {
diff --git a/app/modules/Cronks/validate/Provider/Categories.xml b/app/modules/Cronks/validate/Provider/Categories.xml
index bfa516d..921382f 100755
--- a/app/modules/Cronks/validate/Provider/Categories.xml
+++ b/app/modules/Cronks/validate/Provider/Categories.xml
@@ -7,6 +7,30 @@
 	<ae:configuration>
 		
 		<validators>
+			
+			<validator class="number" name="all" required="false">
+				<argument>all</argument>
+				<ae:parameter name="min">0</ae:parameter>
+				<ae:parameter name="max">1</ae:parameter>
+				<errors>
+					<error>Argument all validation failed!</error>
+				</errors>
+			</validator>
+			
+			<validator class="string" name="xaction" required="false">
+				<argument>xaction</argument>
+				<errors>
+					<error>Argument xaction validation failed!</error>
+				</errors>
+			</validator>
+			
+			<validator class="string" name="rows" required="false">
+				<argument>rows</argument>
+				<errors>
+					<error>Argument rows validation failed!</error>
+				</errors>
+			</validator>
+			
 		</validators>
 		
 	</ae:configuration>





More information about the icinga-checkins mailing list