[icinga-checkins] icinga.org: icinga-web/r1.5: * Added systemwide customvar filter in Doctrine query (fixes #1453)

git at icinga.org git at icinga.org
Thu Dec 1 10:49:16 CET 2011


Module: icinga-web
Branch: r1.5
Commit: 95383fb30dc5665fcf5b06f1cc7b0e10272fa7eb
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=95383fb30dc5665fcf5b06f1cc7b0e10272fa7eb

Author: Marius Hein <marius.hein at netways.de>
Date:   Wed Nov 23 14:54:46 2011 +0100

* Added systemwide customvar filter in Doctrine query (fixes #1453)

Conflicts:

	app/modules/Api/config/module.xml.in
	app/modules/Api/models/Relation/DataModelModel.class.php

---

 app/modules/Api/config/module.xml                  |    8 +++-
 .../lib/database/IcingaDoctrine_Query.class.php    |   43 +++++++++++++++++-
 .../to/icinga-tactical-overview-template-cv.xml    |   47 ++++++++------------
 3 files changed, 67 insertions(+), 31 deletions(-)

diff --git a/app/modules/Api/config/module.xml b/app/modules/Api/config/module.xml
index 96133f0..9b06e03 100755
--- a/app/modules/Api/config/module.xml
+++ b/app/modules/Api/config/module.xml
@@ -6,7 +6,13 @@
 			<settings>
 				<setting name="title">Api Module</setting>
 				<setting name="version">1.0</setting>
-				<setting name="access"></setting>		
+				<setting name="access"></setting>
+
+				<setting name="exclude_customvars">
+					<!-- <ae:parameter>PASSWORD</ae:parameter> -->
+					<!-- <ae:parameter>DN</ae:parameter> -->
+					<ae:parameter>EDB_FILTER</ae:parameter>
+				</setting>
 			</settings>
 	
 			
diff --git a/app/modules/Api/lib/database/IcingaDoctrine_Query.class.php b/app/modules/Api/lib/database/IcingaDoctrine_Query.class.php
index 55ea162..7057c7c 100644
--- a/app/modules/Api/lib/database/IcingaDoctrine_Query.class.php
+++ b/app/modules/Api/lib/database/IcingaDoctrine_Query.class.php
@@ -230,7 +230,48 @@ class IcingaDoctrine_Query extends Doctrine_Query {
                 $this->checkForAlias($dql,array($this->mainAlias));
             }
         }
-
+    }
+    
+    /**
+     * Tries to find the alias name used in the query
+     * @todo Search over joins too
+     * @param string $componentName
+     * @return string
+     */
+    protected function findAliasByComponent($componentName) {
+        $alias = null;
+        
+        foreach ($this->_dqlParts['from'] as $from) {
+            if (!(strstr($from, $componentName)!==false)) {
+                $alias = $from;
+            }
+        }
+        
+        if ($alias) {
+            $arry = explode(' ', $alias);
+            return $arry[1];
+        }
+        
+        return null;
+    }
+    
+    /**
+     * Appends a custom variable filter to doctrine query
+     * @param string $alias
+     * @return IcingaDoctrine_Query
+     */
+    public function appendCustomvarFilter($alias=null) {
+        if ($alias === null) {
+            $alias = $this->findAliasByComponent('IcingaCustomVars');
+        }
+        
+        if ($alias) {
+            $exclude = AgaviConfig::get('modules.api.exclude_customvars');
+            if (is_array($exclude) && count($exclude)) {
+                $this->andWhereNotIn($alias. '.varname', $exclude);
+            }
+        }
+        return $this;
     }
 
 
diff --git a/app/modules/Cronks/data/xml/to/icinga-tactical-overview-template-cv.xml b/app/modules/Cronks/data/xml/to/icinga-tactical-overview-template-cv.xml
index 7b3d038..8dd875d 100644
--- a/app/modules/Cronks/data/xml/to/icinga-tactical-overview-template-cv.xml
+++ b/app/modules/Cronks/data/xml/to/icinga-tactical-overview-template-cv.xml
@@ -15,17 +15,6 @@
 		data sources 
 	-->
 	<datasources>
-		<datasource id="cv_name">
-			<source_type>IcingaApi</source_type>
-			<target>IcingaApiConstants::TARGET_CUSTOMVARIABLE</target>
-			<columns>CUSTOMVARIABLE_NAME</columns>
-		</datasource>
-
-		<datasource id="cv_val">
-			<source_type>IcingaApi</source_type>
-			<target>IcingaApiConstants::TARGET_CUSTOMVARIABLE</target>
-			<columns>CUSTOMVARIABLE_VALUE</columns>
-		</datasource>
 	</datasources>
 	
 	
@@ -34,28 +23,28 @@
     	html template
     -->
     <template_code>
-		<cv_aggregate>
-			<![CDATA[
-				<?php
-					$data = $t->ds2Array('cv_val', array(array(
-						'CUSTOMVARIABLE_NAME', $CUSTOMVARIABLE_NAME
-					)));
-
-					foreach ($data as $row) {
-						echo $t->renderSub('icinga-tactical-overview-groupstat', 'customvariable', array(
-							'CUSTOMVARIABLE_NAME' => $CUSTOMVARIABLE_NAME,
-							'CUSTOMVARIABLE_VALUE' => $row['CUSTOMVARIABLE_VALUE']
-						));
-					}
-				?>
-			]]>
-		</cv_aggregate>
-
     	<MAIN>
 	    	<![CDATA[
 				<h3>TO: Howto organize by customvariables</h3>
 				<div style="padding: 5px 5px;">
-				<?php echo $t->ds2Template('cv_name', 'cv_aggregate', '__xcount')?>
+                <?php 
+                
+                    $records = IcingaDoctrine_Query::create()
+                    ->from('IcingaCustomvariables c')
+                    ->distinct(true)
+                    ->select('c.customvariable_id, c.varname, c.varvalue')
+                    ->orderBy('c.varname ASC')
+                    ->appendCustomvarFilter()
+                    ->execute();
+                    
+                    foreach ($records as $record) {
+                        echo $t->renderSub('icinga-tactical-overview-groupstat', 'customvariable', array(
+                            'CUSTOMVARIABLE_NAME' => $record->varname,
+                            'CUSTOMVARIABLE_VALUE' => $record->varvalue
+                        ));
+                    }
+                
+                ?>
 				</div>
 			]]>
 		</MAIN>





More information about the icinga-checkins mailing list