[icinga-checkins] icinga.org: icinga-web/cdoebler/default: * added base for repeating sub templates to static content

git at icinga.org git at icinga.org
Thu Jan 21 16:06:40 CET 2010


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

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Thu Jan 21 16:06:05 2010 +0100

* added base for repeating sub templates to static content

---

 .../models/System/StaticContentModel.class.php     |   64 ++++++++++++++++---
 res/xml/icinga-tactical-overview-template.xml      |    7 ++-
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/app/modules/Cronks/models/System/StaticContentModel.class.php b/app/modules/Cronks/models/System/StaticContentModel.class.php
index b839dac..26dd7d7 100644
--- a/app/modules/Cronks/models/System/StaticContentModel.class.php
+++ b/app/modules/Cronks/models/System/StaticContentModel.class.php
@@ -166,7 +166,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 
 		} else {
 
-			$apiSearch = $this->api->API()->createSearch();
+			$apiSearch = $this->api->API()->createSearch()->setResultType(IcingaApi::RESULT_ARRAY);
 			if (!array_key_exists('target', $dataSource)) {
 
 				throw new Cronks_System_StaticContentModelException('fetchTemplateData(): no target in datasource!');
@@ -219,7 +219,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 
 				// execute query and fetch result
 				if ($success) {
-					$apiRes = $apiSearch->fetch();
+					$apiRes = $apiSearch->fetch()->getAll();
 
 					// set function
 					if (array_key_exists('function', $dataSource)) {
@@ -253,13 +253,18 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	 * @author	Christian Doebler <christian.doebler at netways.de>
 	 */
 	private function processTemplate () {
-		$content = false;
+		$content = null;
 
 		if (array_key_exists('template_code', $this->xmlData)) {
 			$content = $this->xmlData['template_code'];
 
-			// fetch variables from template and call substitution routine
-			$variablePattern = '/\${([A-Za-z0-9_\-]+):([A-Z_]+)}/m';
+			// fetch repeating variables from template and call substitution routine
+			$variablePattern = '/\${([A-Za-z0-9_\-]+):repeat}/s';
+			preg_match_all($variablePattern, $content, $templateVariables);
+			$content = $this->substituteRepeatingTemplateVariables($content, $templateVariables);
+
+			// fetch remaining variables from template and call substitution routine
+			$variablePattern = '/\${([A-Za-z0-9_\-]+):([A-Z_]+)}/s';
 			preg_match_all($variablePattern, $content, $templateVariables);
 			$content = $this->substituteTemplateVariables($content, $templateVariables);
 
@@ -271,13 +276,55 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	}
 
 	/**
+	 * substitutes repeating 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 substituteRepeatingTemplateVariables ($content, $templateVariables) {
+		// determine number of repeating sub templates
+		$numRepeatDefinitions = count($templateVariables[0]);
+
+		for ($x = 0; $x < $numRepeatDefinitions; $x++) {
+			$currentVariable = $templateVariables[1][$x];
+			$variablePattern = '/\${' . $currentVariable . ':repeat}(.*)\${' . $currentVariable . ':repeat_end}/s';
+			preg_match_all($variablePattern, $content, $templateSubVariables);
+			$numSubTemplates = count($templateSubVariables[0]);
+
+			// loop through sub templates and create sub content
+			$subContent = null;
+			for ($y = 0; $y < $numSubTemplates; $y++) {
+				$subContentTemplate = $templateSubVariables[1][$y];
+
+				$variablePattern = '/\${([A-Za-z0-9_\-]+):([A-Z_]+)}/s';
+				preg_match_all($variablePattern, $subContentTemplate, $subTemplateSubVariables);
+				$numSubTemplateSubVariables = count($subTemplateSubVariables);
+				for ($z = 0; $z < $numSubTemplateSubVariables; $z++) {
+					$subContent .= $this->substituteTemplateVariables($subContentTemplate, $subTemplateSubVariables, $z);
+				}
+
+				// substitute template variable by generated sub content
+				$content = $content = str_replace(
+					$templateSubVariables[0][$y],
+					$subContent,
+					$content
+				);
+			}
+
+		}
+
+		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) {
+	private function substituteTemplateVariables ($content, $templateVariables, $offset = 0) {
 		// determine number of found template variables
 		$numMatches = count($templateVariables[0]);
 
@@ -290,10 +337,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 			$substitution = null;
 			if (array_key_exists($id, $this->templateData)) {
 				$templateData = $this->templateData[$id];
-
-				if ($templateData['data']->getResultCount() == 1) {
-					$substitution = $templateData['data']->current()->$column;
-				}
+				$substitution = $templateData['data'][$offset][$column];
 			}
 
 			// apply function
diff --git a/res/xml/icinga-tactical-overview-template.xml b/res/xml/icinga-tactical-overview-template.xml
index 101ca49..b9d4be1 100644
--- a/res/xml/icinga-tactical-overview-template.xml
+++ b/res/xml/icinga-tactical-overview-template.xml
@@ -115,14 +115,12 @@
 			</filter>
 		</datasource>
 
-		<!--
 		<datasource>
 			<id>HOST_NAMES</id>
 			<source_type>IcingaApi</source_type>
 			<target>IcingaApi::TARGET_HOST</target>
 			<columns>HOST_NAME</columns>
 		</datasource>
-		-->
 	</datasources>
 
     <!--
@@ -165,6 +163,11 @@
 					</td>
 				</tr>
 			</table>
+			${HOST_NAMES:repeat}
+				<br/>
+				${HOST_NAMES:HOST_NAME} |
+				<br/> 
+			${HOST_NAMES:repeat_end}
 		]]>
     </template_code>
 </template>
\ No newline at end of file





More information about the icinga-checkins mailing list