--- ./include/classes/class.cpagefilter.php.orig	2014-07-17 15:49:47.000000000 +0200
+++ ./include/classes/class.cpagefilter.php	2014-08-23 19:54:57.000000000 +0200
@@ -476,7 +476,7 @@
 	 * @param string $options['DDFirstLabel']
 	 */
 	private function _initHosts($hostId, array $options) {
-		$this->data['hosts'] = array();
+		$this->data['hosts'] = $this->data['templates'] = array();
 
 		if (isset($options['DDFirstLabel'])) {
 			$this->config['DDFirstLabels']['hosts'] = $options['DDFirstLabel'];
@@ -493,7 +493,22 @@
 				'output' => array('hostid', 'name', 'status'),
 				'groupids' => ($this->groupid > 0) ? $this->groupid : null
 			);
-			$hosts = API::Host()->get(zbx_array_merge($defaultOptions, $options));
+
+			if (isset($options['templated_hosts']) && !is_null($options['templated_hosts']) && $options['templated_hosts'] != false) {
+				unset($options['templated_hosts']);
+				$hosts = API::Host()->get(zbx_array_merge($defaultOptions, $options));
+
+				$options['output'] = array('templateid', 'name');
+				$templates = API::Template()->get(zbx_array_merge($defaultOptions, $options));
+				order_result($templates, 'name');
+
+				foreach ($templates as $template) {
+					$this->data['templates'][$template['templateid']] = $template['name'];
+				}
+			}
+			else {
+				$hosts = API::Host()->get(zbx_array_merge($defaultOptions, $options));
+			}
 
 			if ($hosts) {
 				order_result($hosts, 'name');
@@ -509,7 +524,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']);
@@ -527,8 +542,10 @@
 			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;
 	}
 
@@ -770,7 +787,7 @@
 		}
 		$options = array('objectName' => 'hosts', 'classes' => $classes);
 
-		return $this->_getCB('hostid', $this->hostid, $items, $withNode, $options);
+		return $this->_getCB('hostid', $this->hostid, $items, $withNode, $options, $this->templates);
 	}
 
 	/**
@@ -870,7 +887,7 @@
 	 *
 	 * @return CComboBox
 	 */
-	private function _getCB($name, $selectedId, $items, $withNode, array $options = array()) {
+	private function _getCB($name, $selectedId, $items, $withNode, array $options = array(), $items2 = array()) {
 		$comboBox = new CComboBox($name, $selectedId, 'javascript: submit();');
 
 		if ($withNode) {
@@ -882,24 +899,70 @@
 		natcasesort($items);
 
 		// add drop down first item
-		if (!$this->config['popupDD']) {
-			if (isset($this->config['DDFirstLabels'][$options['objectName']])) {
-				$firstLabel = $this->config['DDFirstLabels'][$options['objectName']];
+		if (empty($items2)) {
+			if (!$this->config['popupDD']) {
+				if (isset($this->config['DDFirstLabels'][$options['objectName']])) {
+					$firstLabel = $this->config['DDFirstLabels'][$options['objectName']];
+				}
+				else {
+					$firstLabel = ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) ? _('not selected') : _('all');
+				}
+
+				if ($name == 'application') {
+					$items = array('' => $firstLabel) + $items;
+				}
+				else {
+					$items = array($firstLabel) + $items;
+				}
 			}
-			else {
-				$firstLabel = ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) ? _('not selected') : _('all');
+
+			foreach ($items as $id => $name) {
+				$comboBox->addItem($id, $name, null, 'yes', isset($options['classes'][$id]) ? $options['classes'][$id] : null);
+			}
+		}
+		else {
+			if ($withNode) {
+				foreach ($items2 as $id => $item) {
+					$items2[$id] = get_node_name_by_elid($id, null, NAME_DELIMITER).$item;
+				}
+			}
+
+			if (!$this->config['popupDD']) {
+				if (isset($this->config['DDFirstLabels'][$options['objectName']])) {
+					$firstLabel = $this->config['DDFirstLabels'][$options['objectName']];
+				}
+				else {
+					$firstLabel = ($this->config['DDFirst'] == ZBX_DROPDOWN_FIRST_NONE) ? _('not selected') : _('all');
+				}
+
+				if ($name == 'application') {
+					$comboBox->addItem('', $firstLabel);
+				}
+				else {
+					$comboBox->addItem(0, $firstLabel);
+				}
 			}
 
-			if ($name == 'application') {
-				$items = array('' => $firstLabel) + $items;
+			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);
+				}
+				$comboBox->addItem($hostGroups['hosts']);
 			}
-			else {
-				$items = array($firstLabel) + $items;
+
+			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);
 			}
-		}
+			$comboBox->addItem($hostGroups['templates']);
 
-		foreach ($items as $id => $name) {
-			$comboBox->addItem($id, $name, null, 'yes', isset($options['classes'][$id]) ? $options['classes'][$id] : null);
 		}
 
 		return $comboBox;
