[icinga-checkins] icinga.org: icinga-web/master: * static content: implemented if-statements for templates

git at icinga.org git at icinga.org
Fri Jan 29 15:07:28 CET 2010


Module: icinga-web
Branch: master
Commit: 63c01c392aa56b0781d16cdf8acd12cca0c0c046
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=63c01c392aa56b0781d16cdf8acd12cca0c0c046

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Thu Jan 28 13:54:11 2010 +0100

* static content: implemented if-statements for templates
* static content: added conditional display of rows to additional template

---

 .../models/System/StaticContentModel.class.php     |   58 ++++++++++++++++++--
 .../icinga-tactical-overview-template-custom.xml   |   28 +++++-----
 2 files changed, 68 insertions(+), 18 deletions(-)

diff --git a/app/modules/Cronks/models/System/StaticContentModel.class.php b/app/modules/Cronks/models/System/StaticContentModel.class.php
index 385ccb8..8882073 100644
--- a/app/modules/Cronks/models/System/StaticContentModel.class.php
+++ b/app/modules/Cronks/models/System/StaticContentModel.class.php
@@ -357,11 +357,14 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 
 		$content = $this->xmlData['template_code'][$tplId];
 
+		// process if-statements
+		$content = $this->processIfStatements($tplId, $content, $filter);
+
 		// fetch remaining variables from template and call substitution routine
 		// for variables
 		$variablePattern = '/\${([A-Z0-9_\-]+):([A-Z_]+)(:[^}]+)?}/s';
 		preg_match_all($variablePattern, $content, $templateVariables);
-		$content = $this->substituteTemplateVariables($tplId, $templateVariables, false, $filter);
+		$content = $this->substituteTemplateVariables($tplId, $templateVariables, $content, $filter);
 
 		// fetch remaining variables from template and call substitution routine
 		// for processed content
@@ -371,9 +374,6 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 		for ($x = 0; $x < $numMatches; $x++) {
 			if (!empty($templateVariables[2][$x])) {
 				$currentFilter = trim($templateVariables[2][$x]);
-				if ($tplId != 'MAIN') {
-					
-				}
 			} else {
 				$currentFilter = false;
 			}
@@ -396,6 +396,56 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	}
 
 	/**
+	 * generates content from template and fetched data
+	 * @param	string			$tplId				id of content template
+	 * @param	string			$content			content to process
+	 * @param	string			$filter				additional filter data for query
+	 * @return	string								processed content
+	 * @author	Christian Doebler <christian.doebler at netways.de>
+	 */
+	private function processIfStatements ($tplId, $content, $filter = false) {
+		while (($start = strpos($content, '${if:')) !== false) {
+			if (($end = strpos($content, '${/if}', $start)) !== false) {
+				$length = $end - $start;
+				$workStr = substr($content, $start, $length);
+				$replaceStr = $workStr . substr($content, $end, 6);
+
+				// fetch definition of if-statement
+				$openBraces = 1;
+				$pos = 5;
+				$replaceLength = strlen($workStr);
+				while ($openBraces) {
+					$nextOpen = strpos($workStr, '${', $pos);
+					$nextClose = strpos($workStr, '}', $pos);
+					if ($nextOpen < $nextClose) {
+						$openBraces++;
+						$pos = $nextOpen + 1;
+					} else {
+						$openBraces--;
+						$pos = $nextClose + 1;
+					}
+				}
+				$ifStmtStr = substr($workStr, 5, $pos - 6);
+
+				// substitute data of statement
+				$variablePattern = '/\${([A-Z0-9_\-]+):([A-Z_]+)(:[^}]+)?}/s';
+				preg_match_all($variablePattern, $content, $templateVariables);
+				$ifStmtStr = $this->substituteTemplateVariables($tplId, $templateVariables, $ifStmtStr, $filter);
+
+				// determine statement value
+				$stmtValue = false;
+				eval("\$stmtValue = ($ifStmtStr);");
+
+				// create new content
+				$newStr = ($stmtValue) ? substr($workStr, $pos) : null;
+				$content = str_replace($replaceStr, $newStr, $content);
+			}
+		}
+
+		return $content;
+	}
+
+	/**
 	 * adds filter data to global filter to provide inheritance of filters
 	 * @param	mixed			$filterRaw			filter to add to global filter as string or array
 	 * @return	void
diff --git a/res/xml/icinga-tactical-overview-template-custom.xml b/res/xml/icinga-tactical-overview-template-custom.xml
index 67054a7..2d9eb91 100644
--- a/res/xml/icinga-tactical-overview-template-custom.xml
+++ b/res/xml/icinga-tactical-overview-template-custom.xml
@@ -254,17 +254,17 @@
 					<tr>
 						<td>
 							<table border="0" cellpadding="0" cellspacing="0">
-								<tr><td style="background-color:#00cc00;">${HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} UP</td></tr>
-								<tr><td style="background-color:#cc0000;">${HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} DOWN</td></tr>
-								<tr><td style="background-color:#ff8000;">${HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} UNREACHABLE</td></tr>
+								${if:${HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#00cc00;">${HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} UP</td></tr>${/if}
+								${if:${HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#cc0000;">${HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} DOWN</td></tr>${/if}
+								${if:${HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#ff8000;">${HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} UNREACHABLE</td></tr>${/if}
 							</table>
 						</td>
 						<td>
 							<table border="0" cellpadding="0" cellspacing="0">
-								<tr><td style="background-color:#00cc00;">${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} OK</td></tr>
-								<tr><td style="background-color:#ffff00;">${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} WARNING</td></tr>
-								<tr><td style="background-color:#ff0000;">${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} CRITICAL</td></tr>
-								<tr><td style="background-color:#ff8000;">${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} UNKNOWN</td></tr>
+								${if:${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#00cc00;">${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} OK</td></tr>${/if}
+								${if:${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ffff00;">${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} WARNING</td></tr>${/if}
+								${if:${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ff0000;">${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} CRITICAL</td></tr>${/if}
+								${if:${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ff8000;">${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} UNKNOWN</td></tr>${/if}
 							</table>
 						</td>
 					</tr>
@@ -278,17 +278,17 @@
     				<tr>
     					<td>
 							<table border="0" cellpadding="0" cellspacing="0">
-								<tr><td style="background-color:#00cc00;">${SERVICE_HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} UP</td></tr>
-								<tr><td style="background-color:#cc0000;">${SERVICE_HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} DOWN</td></tr>
-								<tr><td style="background-color:#ff8000;">${SERVICE_HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} UNREACHABLE</td></tr>
+								${if:${SERVICE_HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#00cc00;">${SERVICE_HOST_STATUS_UP:COUNT_HOST_OBJECT_ID} UP</td></tr>${/if}
+								${if:${SERVICE_HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#cc0000;">${SERVICE_HOST_STATUS_DOWN:COUNT_HOST_OBJECT_ID} DOWN</td></tr>${/if}
+								${if:${SERVICE_HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} > 0}<tr><td style="background-color:#ff8000;">${SERVICE_HOST_STATUS_UNREACHABLE:COUNT_HOST_OBJECT_ID} UNREACHABLE</td></tr>${/if}
 							</table>
 						</td>
     					<td>
 							<table border="0" cellpadding="0" cellspacing="0">
-								<tr><td style="background-color:#00cc00;">${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} OK</td></tr>
-								<tr><td style="background-color:#ffff00;">${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} WARNING</td></tr>
-								<tr><td style="background-color:#ff0000;">${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} CRITICAL</td></tr>
-								<tr><td style="background-color:#ff8000;">${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} UNKNOWN</td></tr>
+								${if:${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#00cc00;">${SERVICE_STATUS_OK:COUNT_SERVICE_OBJECT_ID} OK</td></tr>${/if}
+								${if:${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ffff00;">${SERVICE_STATUS_WARNING:COUNT_SERVICE_OBJECT_ID} WARNING</td></tr>${/if}
+								${if:${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ff0000;">${SERVICE_STATUS_CRITICAL:COUNT_SERVICE_OBJECT_ID} CRITICAL</td></tr>${/if}
+								${if:${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} > 0}<tr><td style="background-color:#ff8000;">${SERVICE_STATUS_UNKNOWN:COUNT_SERVICE_OBJECT_ID} UNKNOWN</td></tr>${/if}
 							</table>
 						</td>
 					</tr>





More information about the icinga-checkins mailing list