diff -Nur zabbix-1.8.6.orig/frontends/php/api/classes/class.cdrule.php zabbix-1.8.6/frontends/php/api/classes/class.cdrule.php
--- zabbix-1.8.6.orig/frontends/php/api/classes/class.cdrule.php	2011-07-26 01:00:16.000000000 +0400
+++ zabbix-1.8.6/frontends/php/api/classes/class.cdrule.php	2011-07-27 13:15:47.000000000 +0400
@@ -309,7 +309,7 @@
 		if(!is_null($options['selectDChecks'])){
 			$obj_params = array(
 				'nodeids' => $nodeids,
-				'dhostids' => $dhostids,
+				'druleids' => $druleids,
 				'preservekeys' => 1
 			);
 
@@ -319,17 +319,16 @@
 
 				if(!is_null($options['limitSelects'])) order_result($dchecks, 'name');
 				foreach($dchecks as $dcheckid => $dcheck){
-					unset($dchecks[$dcheckid]['dhosts']);
+					unset($dchecks[$dcheckid]['drules']);
 					$count = array();
-					foreach($dcheck['dhosts'] as $dnum => $dhost){
+					foreach($dcheck['drules'] as $dnum => $drule){
 						if(!is_null($options['limitSelects'])){
-							if(!isset($count[$dhost['dhostid']])) $count[$dhost['dhostid']] = 0;
-							$count[$dhost['dhostid']]++;
+							if(!isset($count[$drule['druleid']])) $count[$drule['druleid']] = 0;
+							$count[$drule['druleid']]++;
 
-							if($count[$dhost['dhostid']] > $options['limitSelects']) continue;
+							if($count[$drule['druleid']] > $options['limitSelects']) continue;
 						}
-
-						$result[$dhost['dhostid']]['dchecks'][] = &$dchecks[$dcheckid];
+						$result[$drule['druleid']]['dchecks'][] = &$dchecks[$dcheckid];
 					}
 				}
 			}
@@ -432,18 +431,60 @@
 	public static function create($drules){
 		$drules = zbx_toArray($drules);
 		$druleids = array();
+		$dchecks = array();
 
 		try{
 			self::BeginTransaction(__METHOD__);
-			self::EndTransaction(true, __METHOD__);
 
+// Check fields
+                        $drule_db_fields = array(
+                                'name' => null,
+                                'iprange' => null,
+                                'delay' => null,
+                        );
+                        $duplicates = array();
+                        foreach($drules as $dnum => $drule){
+                                if(!check_db_fields($drule_db_fields, $drule)){
+                                        self::exception(ZBX_API_ERROR_PARAMETERS, S_INCORRECT_PARAMETER_USED_FOR_DRULE.' [ '.$drule['name'].' ]');
+                                }
+
+                                if(isset($duplicates[$drule['name']]))
+                                        self::exception(ZBX_API_ERROR_PARAMETERS, S_DRULE.' [ '.$drule['name'].' ] '.S_ALREADY_EXISTS_SMALL);
+                                else
+                                        $duplicates[$drule['name']] = $drule['name'];
+                        }
+
+                        $options = array(
+                                'filter' => array('name' => $duplicates),
+                                'output' => API_OUTPUT_EXTEND,
+                                'editable' => 1,
+                                'nopermissions' => 1
+                        );
+                        $dbDRules = self::get($options);
+                        foreach($dbDRules as $dnum => $dbDRule){
+                                self::exception(ZBX_API_ERROR_PARAMETERS, S_DRULE.' [ '.$dbDRule['name'].' ] '.S_ALREADY_EXISTS_SMALL);
+                        }
+//------
+			$druleids = DB::insert('drules', $drules);
+
+                        foreach($drules as $dnum => $drule){
+                                if(isset($drule['dchecks']) && !empty($drule['dchecks'])){
+                                        foreach($drule['dchecks'] as $dcheck){
+                                                $dchecks[] = array_merge(array('druleid' => $druleids[$dnum]), $dcheck);
+                                        }
+                                }
+                        }
+
+                        self::addChecks($dchecks);
+
+			self::EndTransaction(true, __METHOD__);
 			return array('druleids' => $druleids);
 		}
 		catch(APIException $e){
 			self::EndTransaction(false, __METHOD__);
 			$error = $e->getErrors();
 			$error = reset($error);
-			self::setError(__METHOD__, ZBX_API_ERROR_PARAMETERS, $error);
+			self::setError(__METHOD__, $e->getCode(), $error);
 			return false;
 		}
 	}
@@ -457,11 +498,85 @@
  */
 	public static function update($drules){
 		$drules = zbx_toArray($drules);
-		$druleids = array();
+		$druleids = zbx_objectValues($drules, 'druleid');
 
+		$update = array();
+		$dchecks = array();
 		try{
 			self::BeginTransaction(__METHOD__);
 
+			$options = array(
+				'druleids' => $druleids,
+				'editable' => 1,
+				'output' => API_OUTPUT_SHORTEN,
+				'preservekeys' => 1
+			);
+			$upd_drules = self::get($options);
+			foreach($drules as $dnum => $drule){
+				if(!isset($upd_drules[$drule['druleid']])){
+					self::exception(ZBX_API_ERROR_PERMISSIONS, S_NO_PERMISSIONS);
+				}
+			}
+
+// Check fields
+			$drule_db_fields = array(
+				'druleid' => null
+			);
+
+			$duplicates = array();
+			foreach($drules as $dnum => $drule){
+				if(!check_db_fields($drule_db_fields, $drule)){
+					self::exception(ZBX_API_ERROR_PARAMETERS, S_INCORRECT_PARAMETER_USED_FOR_DRULE.' [ '.$drule['name'].' ]');
+				}
+
+				if(!isset($drule['name'])) continue;
+
+				if(isset($duplicates[$drule['name']]))
+					self::exception(ZBX_API_ERROR_PARAMETERS, S_DRULE.' [ '.$drule['name'].' ] '.S_ALREADY_EXISTS_SMALL);
+				else
+					$duplicates[$drule['name']] = $drule['name'];
+			}
+//------
+
+			foreach($drules as $dnum => $drule){
+// Existance
+				if (isset($drule['name'])) {
+					$options = array(
+						'filter' => array( 'name' => $drule['name'] ),
+						'output' => API_OUTPUT_SHORTEN,
+						'editable' => 1,
+						'nopermissions' => 1
+					);
+					$drule_exists = self::get($options);
+					if(($drule_exist = reset($drule_exists)) && ($drule_exist['druleid'] != $drule['druleid'])){
+						self::exception(ZBX_API_ERROR_PARAMETERS, S_DRULE.' [ '.$drule['name'].' ] '.S_ALREADY_EXISTS_SMALL);
+					}
+				}
+//----
+
+				if(isset($drule['dchecks']) && !empty($drule['dchecks'])){
+					foreach($drule['dchecks'] as $dcheck){
+						$dchecks[] = array_merge(array('druleid' => $drule['druleid']), $dcheck);
+					}
+				}
+
+				$druleid = $drule['druleid'];
+				unset($drule['druleid']);
+				if(!empty($drule)){
+					$update[] = array(
+						'values' => $drule,
+						'where' => array('druleid='.$druleid),
+					);
+				}
+			}
+
+			DB::update('drules', $update);
+
+			if (!empty($dchecks)) {
+				DB::delete('dchecks', DBcondition('druleid', $druleids));
+				self::addChecks($dchecks);
+			}
+
 			self::EndTransaction(true, __METHOD__);
 			return array('druleids' => $druleids);
 		}
@@ -469,7 +584,7 @@
 			self::EndTransaction(false, __METHOD__);
 			$error = $e->getErrors();
 			$error = reset($error);
-			self::setError(__METHOD__, ZBX_API_ERROR_PARAMETERS, $error);
+			self::setError(__METHOD__, $e->getCode(), $error);
 			return false;
 		}
 	}
@@ -537,79 +652,26 @@
 			return false;
 		}
 	}
-
-// DEPRECATED
+// INCOMPLETE
 	public static function addChecks($checks){
+		$checks = zbx_toArray($checks);
+                $check_inserts = array();
+// Check fields
+// TODO: More validation checks should be performed here!
+                foreach($checks as $cnum => $check){
+                        $check_db_fields = array(
+                                'type' => null,
+                        );
+                        if(!check_db_fields($check_db_fields, $check)){
+                                self::exception(ZBX_API_ERROR_PARAMETERS, S_INCORRECT_PARAMETER_USED_FOR_DCHECK);
+                        }
 
-		$error = 'Unknown Zabbix internal error';
-		$result_ids = array();
-		$result = false;
-		$tpl_drule = false;
-
-		$druleid = $checks['druleid'];
-		$checks_tmp = $checks['checks'];
-		$checks = array();
-		$checkids = array();
-
-		foreach($checks_tmp as $check){
-
-			$drule_db_fields = array(
-				'checkid'	=> null,
-				'color'		=> '000000',
-				'drawtype'	=> 0,
-				'sortorder'	=> 0,
-				'yaxisside'	=> 1,
-				'calc_fnc'	=> 2,
-				'type'		=> 0,
-				'periods_cnt'	=> 5
-			);
-
-			if(!check_db_fields($drule_db_fields, $check)){
-				self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Wrong fields for check [ '.$check['checkid'].' ]');
-				return false;
-			}
-			$checks[$check['checkid']] = $check;
-			$checkids[$check['checkid']] = $check['checkid'];
-		}
-
-// check if drule is templated drule, then checks cannot be added
-		$drule = self::get(array('druleids' => $druleid,  'extendoutput' => 1));
-		$drule = reset($drule);
-
-		if($drule['templateid'] != 0){
-			self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Cannot edit templated drule : '.$drule['name']);
-			return false;
-		}
-
-		// check if drule belongs to template, if so, only checks from same template can be added
-		$tmp_hosts = get_hosts_by_druleid($druleid);
-		$host = DBfetch($tmp_hosts); // if drule belongs to template, only one host is possible
-
-		if($host["status"] == HOST_STATUS_TEMPLATE ){
-			$sql = 'SELECT DISTINCT count(i.hostid) as count
-					FROM checks i
-					WHERE i.hostid<>'.$host['hostid'].
-						' AND '.DBcondition('i.checkid', $checkids);
-
-			$host_count = DBfetch(DBselect($sql));
-			if ($host_count['count']){
-				self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'You must use checks only from host : '.$host['host'].' for template drule : '.$drule['name']);
-				return false;
-			}
-			$tpl_drule = true;
-		}
+                        $check_inserts[$cnum] = $check;
+                }
 
-		self::BeginTransaction(__METHOD__);
-		$result = self::addchecks_rec($druleid, $checks, $tpl_drule);
-		$result = self::EndTransaction($result, __METHOD__);
+                $checkids = DB::insert('dchecks', $check_inserts);
 
-		if($result){
-			return $result;
-		}
-		else{
-			self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error);//'Internal Zabbix error');
-			return false;
-		}
+                return true;
 	}
 
 // DEPRECATED
diff -Nur zabbix-1.8.6.orig/frontends/php/include/locales/en_gb.inc.php zabbix-1.8.6/frontends/php/include/locales/en_gb.inc.php
--- zabbix-1.8.6.orig/frontends/php/include/locales/en_gb.inc.php	2011-07-26 01:00:15.000000000 +0400
+++ zabbix-1.8.6/frontends/php/include/locales/en_gb.inc.php	2011-07-27 13:17:37.000000000 +0400
@@ -2483,5 +2483,10 @@
 	'S_INCORRECT_SYNTAX_NEAR' => 'Incorrect syntax near \'%1$s\'',
 	'S_INCORRECT_USAGE_OF_BRACKET_SYMBOLS' => 'Incorrect usage of bracket symbols. \'%s\' found after final bracket.',
 	'S_INVALID_KEY_FORMAT' => 'Invalid item key format.',
+
+// class.cdrule.php
+	'S_INCORRECT_PARAMETER_USED_FOR_DCHECK' => 'Incorrect parameter used for discovery check',
+	'S_INCORRECT_PARAMETER_USED_FOR_DRULE' => 'Incorrect parameter used for discovery rule',
+	'S_DRULE' => 'Discovery rule',
 );
 ?>
