--- /usr/share/zabbix/include/classes/class.cpagefilter.php.orig	2013-10-12 15:51:50.000000000 +0200
+++ /usr/share/zabbix/include/classes/class.cpagefilter.php	2014-01-05 14:27:15.000000000 +0100
@@ -393,7 +393,7 @@
 	 * @param $options
 	 */
 	private function _initHosts($hostid, $options) {
-		$this->data['hosts'] = array();
+		$this->data['hosts'] = $this->data['templates'] = array();
 
 		if (!$this->groupsSelected) {
 			$hostid = 0;
@@ -405,7 +405,23 @@
 				'groupids' => ($this->groupid > 0) ? $this->groupid : null
 			);
 			$options = zbx_array_merge($def_options, $options);
-			$hosts = API::Host()->get($options);
+
+			if (isset($options['templated_hosts']) && !is_null($options['templated_hosts']) && $options['templated_hosts'] != false) {
+				unset($options['templated_hosts']);
+				$hosts = API::Host()->get($options);
+
+				$options['output'] = array('templateid', 'name');
+				$templates = API::Template()->get($options);
+				order_result($templates, 'name');
+
+				foreach ($templates as $template) {
+					$this->data['templates'][$template['templateid']] = $template['name'];
+				}
+			}
+			else {
+				$hosts = API::Host()->get($options);
+			}
+
 			order_result($hosts, 'name');
 
 			foreach ($hosts as $host) {
@@ -418,7 +434,7 @@
 			}
 
 			// nonexisting or unset $hostid
-			if ((!isset($this->data['hosts'][$hostid]) && $hostid > 0) || is_null($hostid)) {
+			if (!isset($this->data['hosts'][$hostid]) && !isset($this->data['templates'][$hostid]) && $hostid > 0 || is_null($hostid)) {
 				// for popup select first host in the list
 				if ($this->config['popupDD'] && !empty($this->data['hosts'])) {
 					reset($this->data['hosts']);
@@ -435,8 +451,10 @@
 			CProfile::update($this->_profileIdx['hosts'], $hostid, PROFILE_TYPE_ID);
 			CProfile::update(self::HOST_LATEST_IDX, $hostid, PROFILE_TYPE_ID);
 		}
-		$this->isSelected['hostsSelected'] = ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL && !empty($this->data['hosts'])) || $hostid > 0;
-		$this->isSelected['hostsAll'] = $this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL && !empty($this->data['hosts']) && $hostid == 0;
+		$this->isSelected['hostsSelected'] = ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL
+			&& (!empty($this->data['hosts']) || !empty($this->data['templates']))) || $hostid > 0;
+		$this->isSelected['hostsAll'] = $this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL
+			&& (!empty($this->data['hosts']) || !empty($this->data['templates'])) && $hostid == 0;
 		$this->ids['hostid'] = $hostid;
 	}
 
@@ -586,7 +604,7 @@
 	}
 
 	public function getHostsCB($withNode = false) {
-		return $this->_getCB('hostid', $this->hostid, $this->hosts, $withNode);
+		return $this->_getCB('hostid', $this->hostid, $this->hosts, $withNode, $this->templates);
 	}
 
 	public function getGroupsCB($withNode = false) {
@@ -616,7 +634,7 @@
 		return $this->_getCB('druleid', $this->druleid, $this->drules, $withNode);
 	}
 
-	private function _getCB($cbname, $selectedid, $items, $withNode) {
+	private function _getCB($cbname, $selectedid, $items, $withNode, $items2 = array()) {
 		$cmb = new CComboBox($cbname, $selectedid, 'javascript: submit();');
 
 		if ($withNode) {
@@ -627,12 +645,46 @@
 
 		natcasesort($items);
 
-		if (!$this->config['popupDD']) {
-			$items = array(0 => ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) ? _('not selected') : _('all')) + $items;
+		if (empty($items2)) {
+			if (!$this->config['popupDD']) {
+				$items = array(0 => ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) ? _('not selected') : _('all')) + $items;
+			}
+
+			foreach ($items as $id => $name) {
+				$cmb->addItem($id, $name);
+			}
 		}
+		else {
+			if ($withNode) {
+				foreach ($items2 as $id => $item) {
+					$items2[$id] = get_node_name_by_elid($id, null, ': ').$item;
+				}
+			}
 
-		foreach ($items as $id => $name) {
-			$cmb->addItem($id, $name);
+			if (!$this->config['popupDD']) {
+				if ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONEALL) $cmb->addItem(-1,  _('all'));
+				$cmb->addItem(0, ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_ALL) ? _('all') : _('not selected'));
+			}
+
+			if (!empty($items)) {
+				$hostGroups['hosts'] = new COptGroup(_('Hosts'));
+				foreach ($items as $id => $name) {
+					$option = new CComboItem($id, $name);
+					if ($selectedid == $id) $option->setAttribute('selected', 'selected');
+					$hostGroups['hosts']->addItem($option);
+				}
+				$cmb->addItem($hostGroups['hosts']);
+			}
+
+			natcasesort($items2);
+
+			$hostGroups['templates'] = new COptGroup(_('Templates'));
+			foreach ($items2 as $id => $name) {
+				$option = new CComboItem($id, $name);
+				if ($selectedid == $id) $option->setAttribute('selected', 'selected');
+				$hostGroups['templates']->addItem($option);
+			}
+			$cmb->addItem($hostGroups['templates']);
 		}
 
 		return $cmb;
