db/db_itemperson.inc.php

00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002 require_once ("db/db.inc.php");
00003 
00004 
00005 
00006 
00007 global $g_itemperson_fields;
00008 $g_itemperson_fields=array();
00009 foreach(array(
00010             ### internal fields ###
00011             new FieldInternal(array('name'=>'id',
00012                 'default'=>0,
00013             )),
00014             new FieldInternal(array('name'=>'person',
00015                 'default'=>0,
00016             )),
00017             new FieldInternal(array('name'=>'item',
00018                 'default'=>0,
00019             )),
00020             new FieldInternal(array('name'=>'viewed',
00021                 'view_in_forms'=>false,
00022             )),
00023             new FieldDatetime(array('name'=>'viewed_last',
00024                 'default'=>FINIT_NEVER,
00025                 'view_in_forms'=>false,
00026             )),
00027             new FieldInternal(array('name'=>'view_count',
00028                 'view_in_forms'=>false,
00029                 'default'       => 1,
00030             )),
00031             new FieldInternal(array('name'=>'is_bookmark',
00032                 'default'=>0,
00033                 'view_in_forms'=>false,
00034             )),
00035             new FieldInternal(array('name'=>'notify_on_change',
00036                 'default'=>0,
00037                 'view_in_forms'=>false,
00038             )),
00039             new FieldInternal(array('name'=>'notify_if_unchanged',
00040                 'default'=>0,
00041                 'view_in_forms'=>false,
00042             )),
00043             new FieldText(array(
00044                 'name'=>'comment',
00045                 'title'=>__('Comment','form label for items'),
00046                 'tooltip'=>__('Optional'),
00047                 'log_changes'=>true,
00048             )),
00049              new FieldDatetime(array('name'=>'notify_date',
00050                 'default'=>FINIT_NEVER,
00051                 'view_in_forms'=>false,
00052             )),
00053             new FieldDatetime(array('name'=>'created',
00054                 'default'=>FINIT_NEVER,
00055                 'view_in_forms'=>false,
00056             )),
00057        ) as $f) {
00058             $g_itemperson_fields[$f->name]=$f;
00059        }
00060 
00061 
00062 
00063 
00064 class ItemPerson extends DbItem
00065 {
00066 
00067     public $fields_itemperson;
00068     private $_values_org = array();
00069     public $children = array();
00070 
00071     public function __construct($id_or_array=NULL)
00072     {
00073         global $g_itemperson_fields;
00074         $this->fields = &$g_itemperson_fields;
00080         $this->_type = strtolower(get_class($this));
00081 
00085         if(!$this->fields) {
00086             global $g_itemperson_fields;
00087             $this->fields = &$g_itemperson_fields;
00088         }
00089 
00093         parent::__construct();
00094 
00095         if(is_array($id_or_array)) {
00096             parent::__construct();
00097             foreach($id_or_array as $key => $value) {
00098                 is_null($this->$key); ### cause E_NOTICE on undefined properties
00099                 $this->$key=$value;
00100             }
00101         }
00102 
00108         else if(is_int($id_or_array)) {
00109             parent::__construct($id_or_array);
00110         }
00111         #--- just empty ----
00112         else {
00113             trigger_error("can't construct zero-id item",E_USER_WARNING);
00114             parent::__construct();
00115             return NULL;
00116         }
00117     }
00118 
00124     static function getById($id)
00125     {
00126         $i = new ItemPerson($id);
00127         if($i->id) {
00128             return $i;
00129         }
00130         return NULL;
00131     }
00132 
00133     function getItemModified(){
00134         if($i = DbProjectItem::getById($this->item)){
00135             $mod_date = $i->modified;
00136             return $mod_date;
00137         }
00138         else{
00139             return NULL;
00140         }
00141     }
00142 
00143     static function &getAll($args=NULL){
00144 
00145         global $auth;
00146 
00147         $prefix = confGet('DB_TABLE_PREFIX');
00148         $dbh = new DB_Mysql;
00149 
00150         ## default parameter ##
00151         $id               = NULL;
00152         $person           = $auth->cur_user->id;
00153         $item             = NULL;
00154         $viewed           = NULL;
00155         $viewed_last      = NULL;
00156         $is_bookmark      = NULL;
00157         $notify_on_change = NULL;
00158         $notify_if_unchanged_min = NULL;
00159         $notify_if_unchanged_max = NULL;
00160         $order_by         = "created DESC";
00161         
00162         ### filter params ###
00163         if($args) {
00164             foreach($args as $key=>$value) {
00165                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00166                     trigger_error("unknown parameter",E_USER_NOTICE);
00167                 }
00168                 else {
00169                     $$key = $value;
00170                 }
00171             }
00172         }
00173 
00174         $str_id = $id
00175             ? "AND id = " . $id .""
00176             : "";
00177 
00178         $str_item = $item
00179             ? "AND item = " . $item .""
00180             : "";
00181 
00182        if(is_null($is_bookmark)){
00183             $str_bookmark = "";
00184        }
00185        else{
00186            if($is_bookmark){
00187                $str_bookmark = "AND is_bookmark = 1";
00188            }
00189            else{
00190                $str_bookmark = "AND is_bookmark = 0";
00191            }
00192        }
00193         
00194         if(!is_null($notify_on_change)){
00195             $str_notify_on_change = 'AND notify_on_change = ' . $notify_on_change;
00196         }
00197         else{
00198             $str_notify_on_change = '';
00199         }
00200         
00201         if(!is_null($notify_if_unchanged_min)){
00202             $str_notify_if_unchanged_min = 'AND notify_if_unchanged >= ' . $notify_if_unchanged_min;
00203         }
00204         else{
00205             $str_notify_if_unchanged_min = '';
00206         }
00207         
00208         if(!is_null($notify_if_unchanged_max)){
00209             $str_notify_if_unchanged_max = 'AND notify_if_unchanged <= ' . $notify_if_unchanged_max;
00210         }
00211         else{
00212             $str_notify_if_unchanged_max = '';
00213         }
00214         
00215         if(!is_null($person)){
00216             $str_query = "SELECT * FROM {$prefix}itemperson
00217                           WHERE person = " . $person . "
00218                           $str_item
00219                           $str_bookmark
00220                           $str_id
00221                           $str_notify_on_change
00222                           $str_notify_if_unchanged_min
00223                           $str_notify_if_unchanged_max"
00224                           . getOrderByString($order_by);
00225                           
00226             $sth = $dbh->prepare($str_query);
00227             $sth->execute("",1);
00228             $tmp = $sth->fetchall_assoc();
00229             #echo "%%query: " .$str_query. "<br>";
00230             $itempersons = array();
00231             foreach($tmp as $t){
00232                 $itemperson = new ItemPerson($t);
00233                 $itempersons[] = $itemperson;
00234             }
00235 
00236             return $itempersons;
00237         }
00238         else{
00239             return NULL;
00240         }
00241     }
00242     
00243     static function checkChangedItem($args=NULL){
00244         global $auth;
00245         
00246         $prefix = confGet('DB_TABLE_PREFIX');
00247         $dbh = new DB_Mysql;
00248 
00249         ## default parameter ##
00250         $item             = NULL;
00251         $notify_on_change = NULL;
00252 
00253         ### filter params ###
00254         if($args) {
00255             foreach($args as $key=>$value) {
00256                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00257                     trigger_error("unknown parameter",E_USER_NOTICE);
00258                 }
00259                 else {
00260                     $$key = $value;
00261                 }
00262             }
00263         }
00264         
00265         $str_query = "SELECT * FROM {$prefix}itemperson
00266                       WHERE item = " . $item . "
00267                       AND notify_on_change = " . $notify_on_change . "
00268                       AND person != " . $auth->cur_user->id . ";";
00269 
00270         $sth = $dbh->prepare($str_query);
00271         $sth->execute("",1);
00272         #echo "%%query: " .$str_query. "<br>";
00273         $tmp = $sth->fetchall_assoc();
00274         
00275         if($tmp){
00276             return $tmp;
00277         }
00278         else{
00279             return NULL;
00280         }
00281         
00282         return NULL;
00283     }
00284     
00285     static function getPersons($item=NULL,$notify_on_change=NULL)
00286     {
00287         global $auth;
00288 
00289         $prefix = confGet('DB_TABLE_PREFIX');
00290         $dbh = new DB_Mysql;
00291         
00292         if(!is_null($item) && !is_null($notify_on_change)){
00293             $str_query = "SELECT ip.person, p.office_email, p.personal_email 
00294                           FROM {$prefix}itemperson ip, {$prefix}person p
00295                           WHERE ip.item = " . $item . "
00296                           AND ip.notify_on_change = " . $notify_on_change . "
00297                           AND ip.person = p.id;";
00298             $sth = $dbh->prepare($str_query);
00299             $sth->execute("",1);
00300             $tmp = $sth->fetchall_assoc();
00301             if($tmp){
00302                 return $tmp;
00303             }
00304         }
00305         else{
00306             return NULL;
00307         }
00308         
00309         return NULL;
00310     }
00311     
00312     public function update($args=NULL, $update_modifier=true)
00313     {
00314         global $auth;
00315         $dbh = new DB_Mysql;
00316 
00317         $prefix= confGet('DB_TABLE_PREFIX');
00318 
00319         $update_fields = NULL;
00320 
00321         ### build hash to fast access ##
00322         if($args) {
00323             $update_fields = array();
00324             foreach($args as $a) {
00325                 $update_fields[$a] = true;
00326             }
00327         }
00328 
00329         if(!$this->id) {
00330           trigger_error("User object without id can't be updated", E_USER_WARNING);
00331         }
00332         if(!sizeof($this->field_states)) {
00333             trigger_error("need members to update to database. e.g. 'firstname,lastname,data'", E_USER_WARNING);
00334         }
00335 
00336         $t_pairs=array();          # the 'id' field is skipped later, because it's defined as project-item-field. so we have to add it here
00337         foreach($this->fields as $f) {
00338             $name= $f->name;
00339 
00340             ### selective updates ###
00341             if($update_fields && !isset($update_fields[$name])) {
00342                 continue;
00343             }
00344 
00345             ### skip project-item fields ###
00346             if((isset($this->fields[$name]) && isset($this->fields[$name]->in_db_object)) || !isset($g_item_fields[$name])){
00347 
00348                 if(!isset($this->$name) && $this->$name!=NULL) {
00349                     trigger_error("$name is not a member of $this and can't be passed to db", E_USER_WARNING);
00350                     continue;
00351                 }
00352                 if(isset($this->_values_org[$name])) {
00353                     if (  $this->_values_org[$name] == stripslashes($this->$name)) {
00354                         continue;
00355                     }
00356                     else if($this->fields[$name]->log_changes) {
00357                         $log_changed_fields[]=$name;
00358                     }
00359                 }
00360                 global $sql_obj;
00361                 $t_pairs[]= $name.'='."'". asSecureString($this->$name)."'";
00362             }
00363         }
00364         if(count($t_pairs)) {
00365             $str_query= 'UPDATE '
00366                         .$prefix.$this->_type
00367                         .' SET ' . join(', ', $t_pairs)
00368                         .' WHERE id='.$this->id ;
00369             $sth= $dbh->prepare($str_query);
00370             $sth->execute("",1);
00371         }
00372     }
00373 }
00374 
00375 
00376 ?>

Generated on Sun Mar 4 17:19:28 2007 for streber by  doxygen 1.5.1-p1