[icinga-checkins] icinga.org: icinga-web/mhein/default: * prepared generator for static content for repeating template definitions

git at icinga.org git at icinga.org
Fri Jan 22 13:10:02 CET 2010


Module: icinga-web
Branch: mhein/default
Commit: 35bc0f5571a3e99cbceccfb02e794af3f9921ec6
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=35bc0f5571a3e99cbceccfb02e794af3f9921ec6

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Thu Jan 21 14:25:13 2010 +0100

* prepared generator for static content for repeating template definitions

---

 .../models/System/StaticContentModel.class.php     |  105 ++++++++++----------
 .../System/StaticContentSuccessView.class.php      |    9 ++
 res/xml/icinga-tactical-overview-template.xml      |   17 ++--
 3 files changed, 71 insertions(+), 60 deletions(-)

diff --git a/app/modules/Cronks/models/System/StaticContentModel.class.php b/app/modules/Cronks/models/System/StaticContentModel.class.php
index 3da11af..b839dac 100644
--- a/app/modules/Cronks/models/System/StaticContentModel.class.php
+++ b/app/modules/Cronks/models/System/StaticContentModel.class.php
@@ -28,7 +28,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	}	
 
 	/**
-	 * main function to generate HTML content
+	 * main function to generate content
 	 * @param	string			$xmlFile			absolute filename of XML template
 	 * @return	unknown_type
 	 * @author	Christian Doebler <christian.doebler at netways.de>
@@ -36,9 +36,9 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	public function getContent ($xmlFile) {
 		$this->getTemplateData($xmlFile);
 		$this->fetchData();
-		$html = $this->getHtmlContent();
+		$content = $this->processTemplate();
 
-		return $html;
+		return $content;
 	}
 
 	/*
@@ -221,13 +221,6 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 				if ($success) {
 					$apiRes = $apiSearch->fetch();
 
-					// set output type
-					if (array_key_exists('output_type', $dataSource)) {
-						$outputType = $dataSource['output_type'];
-					} else {
-						$outputType = 'single';
-					}
-
 					// set function
 					if (array_key_exists('function', $dataSource)) {
 						$function = $dataSource['function'];
@@ -238,7 +231,6 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 					// set result data
 					$this->templateData[$dataSource['id']] = array(
 						'data'			=> $apiRes,
-						'output_type'	=> $outputType,
 						'function'		=> $function,
 					);
 				}
@@ -255,62 +247,71 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	 */
 
 	/**
-	 * generates HTML content from template and fetched data
+	 * generates content from template and fetched data
 	 * @param	void
-	 * @return	string								HTML content
+	 * @return	string								processed content
 	 * @author	Christian Doebler <christian.doebler at netways.de>
 	 */
-	private function getHtmlContent () {
-		$html = false;
+	private function processTemplate () {
+		$content = false;
 
 		if (array_key_exists('template_code', $this->xmlData)) {
-			$html = $this->xmlData['template_code'];
+			$content = $this->xmlData['template_code'];
 
-			// fetch variables from template
+			// fetch variables from template and call substitution routine
 			$variablePattern = '/\${([A-Za-z0-9_\-]+):([A-Z_]+)}/m';
-			preg_match_all($variablePattern, $html, $templateVariables);
-			// determine number of found template variables
-			$numMatches = count($templateVariables[0]);
-			
-			// replace template variables by found values
-			for ($x = 0; $x < $numMatches; $x++) {
-				$id = $templateVariables[1][$x];
-				$column = $templateVariables[2][$x];
-
-				// determine template values and set them
-				$substitution = null;
-				if (array_key_exists($id, $this->templateData)) {
-					$templateData = $this->templateData[$id];
-
-					switch ($templateData['output_type']) {
-						case 'single':
-						default:
-							if ($templateData['data']->getResultCount() == 1) {
-								$substitution = $templateData['data']->current()->$column;
-							}
-							break;
-					}
-				}
+			preg_match_all($variablePattern, $content, $templateVariables);
+			$content = $this->substituteTemplateVariables($content, $templateVariables);
 
-				// apply function
-				if ($templateData['function'] !== false) {
-					$substitution = $this->applyFunction(
-						$substitution,
-						$templateData['function']
-					);
+		} else {
+			throw new Cronks_System_StaticContentModelException('processTemplate(): no template_code defined!');
+		}
+
+		return $content;
+	}
+
+	/**
+	 * substitutes template variables by fetched data
+	 * @param	string			$content			template
+	 * @param	array			$templateVariables	template variables extracted via preg_match_all
+	 * @return	string								processed template
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	private function substituteTemplateVariables ($content, $templateVariables) {
+		// determine number of found template variables
+		$numMatches = count($templateVariables[0]);
+
+		// replace template variables by found values
+		for ($x = 0; $x < $numMatches; $x++) {
+			$id = $templateVariables[1][$x];
+			$column = $templateVariables[2][$x];
+
+			// determine template values and set them
+			$substitution = null;
+			if (array_key_exists($id, $this->templateData)) {
+				$templateData = $this->templateData[$id];
+
+				if ($templateData['data']->getResultCount() == 1) {
+					$substitution = $templateData['data']->current()->$column;
 				}
+			}
 
-				$html = str_replace(
-					$templateVariables[0][$x],
+			// apply function
+			if ($templateData['function'] !== false) {
+				$substitution = $this->applyFunction(
 					$substitution,
-					$html
+					$templateData['function']
 				);
 			}
-		} else {
-			throw new Cronks_System_StaticContentModelException('getHtmlContent(): no template_code defined!');
+
+			$content = str_replace(
+				$templateVariables[0][$x],
+				$substitution,
+				$content
+			);
 		}
 
-		return $html;
+		return $content;
 	}
 
 	/**
diff --git a/app/modules/Cronks/views/System/StaticContentSuccessView.class.php b/app/modules/Cronks/views/System/StaticContentSuccessView.class.php
index d8d4f5e..54dc3de 100644
--- a/app/modules/Cronks/views/System/StaticContentSuccessView.class.php
+++ b/app/modules/Cronks/views/System/StaticContentSuccessView.class.php
@@ -1,7 +1,16 @@
 <?php
 
+/**
+ * @author Christian Doebler <christian.doebler at netways.de>
+ */
 class Cronks_System_StaticContentSuccessView extends ICINGACronksBaseView
 {
+	/**
+	 * retrieves content via model and returns it
+	 * @param	AgaviRequestDataHolder		$rd				required by Agavi but not used here
+	 * @return	string						$content		generated content
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
 	public function executeHtml(AgaviRequestDataHolder $rd)
 	{
 		$templateFile = sprintf(
diff --git a/res/xml/icinga-tactical-overview-template.xml b/res/xml/icinga-tactical-overview-template.xml
index 370462d..101ca49 100644
--- a/res/xml/icinga-tactical-overview-template.xml
+++ b/res/xml/icinga-tactical-overview-template.xml
@@ -17,7 +17,6 @@
 	<datasources>
 		<datasource>
 			<id>SERVICE_EXECUTION_TIME</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_SERVICE</target>
 			<columns>SERVICE_EXECUTION_TIME_MIN,SERVICE_EXECUTION_TIME_MAX,SERVICE_EXECUTION_TIME_AVG</columns>
@@ -29,7 +28,6 @@
 
 		<datasource>
 			<id>SERVICE_LATENCY</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_SERVICE</target>
 			<columns>SERVICE_LATENCY_MIN,SERVICE_LATENCY_MAX,SERVICE_LATENCY_AVG</columns>
@@ -41,7 +39,6 @@
 
 		<datasource>
 			<id>HOST_EXECUTION_TIME</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_HOST</target>
 			<columns>HOST_EXECUTION_TIME_MIN,HOST_EXECUTION_TIME_MAX,HOST_EXECUTION_TIME_AVG</columns>
@@ -53,7 +50,6 @@
 
 		<datasource>
 			<id>HOST_LATENCY</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_HOST</target>
 			<columns>HOST_LATENCY_MIN,HOST_LATENCY_MAX,HOST_LATENCY_AVG</columns>
@@ -65,7 +61,6 @@
 
 		<datasource>
 			<id>NUM_ACTIVE_HOST_CHECKS</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_HOST</target>
 			<columns>HOST_OBJECT_ID</columns>
@@ -80,7 +75,6 @@
 
 		<datasource>
 			<id>NUM_ACTIVE_SERVICE_CHECKS</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_SERVICE</target>
 			<columns>SERVICE_OBJECT_ID</columns>
@@ -95,7 +89,6 @@
 
 		<datasource>
 			<id>NUM_PASSIVE_HOST_CHECKS</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_HOST</target>
 			<columns>HOST_OBJECT_ID</columns>
@@ -110,7 +103,6 @@
 
 		<datasource>
 			<id>NUM_PASSIVE_SERVICE_CHECKS</id>
-			<output_type>single</output_type>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_SERVICE</target>
 			<columns>SERVICE_OBJECT_ID</columns>
@@ -122,6 +114,15 @@
 				</columns>
 			</filter>
 		</datasource>
+
+		<!--
+		<datasource>
+			<id>HOST_NAMES</id>
+			<source_type>IcingaApi</source_type>
+			<target>IcingaApi::TARGET_HOST</target>
+			<columns>HOST_NAME</columns>
+		</datasource>
+		-->
 	</datasources>
 
     <!--





More information about the icinga-checkins mailing list