--- class.cdrule.php.orig	2011-04-12 16:33:37.000000000 +0400
+++ class.cdrule.php	2011-04-12 16:41:32.000000000 +0400
@@ -308,7 +308,7 @@
 		if(!is_null($options['selectDChecks'])){
 			$obj_params = array(
 				'nodeids' => $nodeids,
-				'dhostids' => $dhostids,
+				'druleids' => $druleids,
 				'preservekeys' => 1
 			);
 
@@ -318,17 +318,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];
 					}
 				}
 			}
@@ -431,18 +430,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;
 		}
 	}
@@ -536,79 +577,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);
+                        $check_inserts[$cnum] = $check;
+                }
 
-		if($drule['templateid'] != 0){
-			self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Cannot edit templated drule : '.$drule['name']);
-			return false;
-		}
+                $checkids = DB::insert('dchecks', $check_inserts);
 
-		// 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;
-		}
-
-		self::BeginTransaction(__METHOD__);
-		$result = self::addchecks_rec($druleid, $checks, $tpl_drule);
-		$result = self::EndTransaction($result, __METHOD__);
-
-		if($result){
-			return $result;
-		}
-		else{
-			self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error);//'Internal Zabbix error');
-			return false;
-		}
+                return true;
 	}
 
 // DEPRECATED
