<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

function eventAdd($obj_type,$event_type,$obj){

  global $DEBUG;

  if (!(is_int($obj_type) && is_int($event_type)) ){
    if ($DEBUG){
      echo "obj_type: $obj_type, event_type: $event_type<br>\n";
    }
    
    return 0;
  }
  
  $sql="select id from events where object_type=$obj_type and event_type=$event_type";
  if (is_int($obj)){
    $obj_id=$obj;
    $sql.=" and object_id=$obj_id";
  }else{
    $obj_prop=$obj;
    if ($obj_prop){
      $sql.=" and object_prop='$obj_prop'";
    }  
  }
  if($DEBUG){
    echo "sql: $sql<br>\n";
  }
  $id = db_query($sql);
  if (db_num_rows($id) > 0){ 
    $row = db_fetch_array($id);
    $event_id = $row["id"];
    if ($event_id>0){
      $sql="update events set count=count+1 where id=$event_id";
    }else{
      $sql='';
    }
  }else{
    $sql="insert into events(object_type,event_type";
    if($obj_id>0){
      $sql.=",object_id";
    }else{
      if ($obj_prop){
	$sql.=",object_prop";
      }
    }
    $sql.=",count) values ($obj_type,$event_type";
    if($obj_id>0){
      $sql.=",$obj_id";
    }else{
      if ($obj_prop){
	$sql.=",'$obj_prop'";
      }
    }
    $sql.=",1)";
  }
  if($DEBUG){
    echo "sql: $sql<br>\n";
  }
  if ($sql){
    if (db_query($sql)) return 1;
  }
  return 0;
}

?>
