[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.contacts_sql.inc.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare API - Contacts manager for SQL                                *
   4      * Reworked with new DB-functions by RalfBecker-AT-outdoor-training.de      *
   5      * This file written by Joseph Engo <jengo@phpgroupware.org>                *
   6      *   and Miles Lott <milosch@groupwhere.org>                                *
   7      * View and manipulate contact records using SQL                            *
   8      * Copyright (C) 2001 Joseph Engo                                           *
   9      * ------------------------------------------------------------------------ *
  10      * This library is part of the eGroupWare API                               *
  11      * http://www.egroupware.org/api                                            *
  12      * ------------------------------------------------------------------------ *
  13      * This library is free software; you can redistribute it and/or modify it  *
  14      * under the terms of the GNU Lesser General Public License as published by *
  15      * the Free Software Foundation; either version 2.1 of the License,         *
  16      * or any later version.                                                    *
  17      * This library is distributed in the hope that it will be useful, but      *
  18      * WITHOUT ANY WARRANTY; without even the implied warranty of               *
  19      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
  20      * See the GNU Lesser General Public License for more details.              *
  21      * You should have received a copy of the GNU Lesser General Public License *
  22      * along with this library; if not, write to the Free Software Foundation,  *
  23      * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
  24      \**************************************************************************/
  25  
  26      /**
  27      * This class provides a contact database scheme.
  28      * It attempts to be based on the vcard 2.1 standard, with mods as needed
  29      * to make for more reasonable sql storage.
  30      * Note that changes here must also work in the LDAP version.
  31      * Syntax: CreateObject('phpgwapi.contacts');
  32      * Example1: $contacts = CreateObject('phpgwapi.contacts');
  33      *
  34      * The contacts class now uses user-time in all param- and return-values.
  35      * The time stored in the DB is in server-time, as in all other eGW apps.
  36      *
  37      * Last Editor: $Author: ralfbecker $
  38      * @class contacts_
  39      * @abstract Contact Management System
  40      * @author jengo/Milosch
  41      * @version $Revision: 22506 $
  42      * @license LGPL
  43      */
  44  
  45      /* $Id: class.contacts_sql.inc.php 22506 2006-09-27 04:16:09Z ralfbecker $ */
  46  
  47      class contacts_
  48      {
  49          var $db = '';
  50          var $std_table='egw_addressbook';
  51          var $ext_table='egw_addressbook_extra';
  52  
  53          /**
  54           * @var int $tz_offset_s offset in secconds between user and server-time,
  55           *    it need to be add to a server-time to get the user-time or substracted from a user-time to get the server-time
  56           */
  57          var $tz_offset_s;
  58  
  59          var $account_id = 0;
  60          var $total_records = 0;
  61          var $grants = '';
  62  
  63          /* The left side are the array elements used throughout phpgw, right side are the db field names. */
  64          var $stock_contact_fields = array(
  65              'fn'                  => 'fn',
  66              'n_given'             => 'n_given',
  67              'n_family'            => 'n_family',
  68              'n_middle'            => 'n_middle',
  69              'n_prefix'            => 'n_prefix',
  70              'n_suffix'            => 'n_suffix',
  71              'sound'               => 'sound',
  72              'bday'                => 'bday',
  73              'note'                => 'note',
  74              'tz'                  => 'tz',
  75              'geo'                 => 'geo',
  76              'url'                 => 'url',
  77              'pubkey'              => 'pubkey',
  78              'org_name'            => 'org_name',
  79              'org_unit'            => 'org_unit',
  80              'title'               => 'title',
  81              'adr_one_street'      => 'adr_one_street',
  82              'adr_one_locality'    => 'adr_one_locality',
  83              'adr_one_region'      => 'adr_one_region',
  84              'adr_one_postalcode'  => 'adr_one_postalcode',
  85              'adr_one_countryname' => 'adr_one_countryname',
  86              'adr_one_type'        => 'adr_one_type',
  87              'label'               => 'label',
  88              'adr_two_street'      => 'adr_two_street',
  89              'adr_two_locality'    => 'adr_two_locality',
  90              'adr_two_region'      => 'adr_two_region',
  91              'adr_two_postalcode'  => 'adr_two_postalcode',
  92              'adr_two_countryname' => 'adr_two_countryname',
  93              'adr_two_type'        => 'adr_two_type',
  94              'tel_work'            => 'tel_work',
  95              'tel_home'            => 'tel_home',
  96              'tel_voice'           => 'tel_voice',
  97              'tel_fax'             => 'tel_fax',
  98              'tel_msg'             => 'tel_msg',
  99              'tel_cell'            => 'tel_cell',
 100              'tel_pager'           => 'tel_pager',
 101              'tel_bbs'             => 'tel_bbs',
 102              'tel_modem'           => 'tel_modem',
 103              'tel_car'             => 'tel_car',
 104              'tel_isdn'            => 'tel_isdn',
 105              'tel_video'           => 'tel_video',
 106              'tel_prefer'          => 'tel_prefer',
 107              'email'               => 'email',
 108              'email_type'          => 'email_type',
 109              'email_home'          => 'email_home',
 110              'email_home_type'     => 'email_home_type'
 111          );
 112  
 113          var $non_contact_fields = array(
 114              'id'     => 'id',
 115              'lid'    => 'lid',
 116              'tid'    => 'tid',
 117              'cat_id' => 'cat_id',
 118              'access' => 'access',
 119              'owner'  => 'owner'
 120          );
 121  
 122          var $adr_types = array();
 123  
 124          /* Used to set preferred number field */
 125          var $tel_types = array(
 126              'work'  => 'work',
 127              'home'  => 'home',
 128              'voice' => 'voice',
 129              'fax'   => 'fax',
 130              'msg'   => 'msg',
 131              'cell'  => 'cell',
 132              'pager' => 'pager',
 133              'bbs'   => 'bbs',
 134              'modem' => 'modem',
 135              'car'   => 'car',
 136              'isdn'  => 'isdn',
 137              'video' => 'video'
 138          );
 139  
 140          /* Used to set email_type fields */
 141          var $email_types = array(
 142              'INTERNET'   => 'INTERNET',
 143              'CompuServe' => 'CompuServe',
 144              'AOL'        => 'AOL',
 145              'Prodigy'    => 'Prodigy',
 146              'eWorld'     => 'eWorld',
 147              'AppleLink'  => 'AppleLink',
 148              'AppleTalk'  => 'AppleTalk',
 149              'PowerShare' => 'PowerShare',
 150              'IBMMail'    => 'IBMMail',
 151              'ATTMail'    => 'ATTMail',
 152              'MCIMail'    => 'MCIMail',
 153              'X.400'      => 'X.400',
 154              'TLX'        => 'TLX'
 155          );
 156  
 157  		function contacts_($useacl=True)
 158          {
 159              $this->db = clone($GLOBALS['egw']->db);
 160              $this->db->set_app('phpgwapi');
 161  
 162              if($useacl)
 163              {
 164                  $this->grants = $GLOBALS['egw']->acl->get_grants('addressbook');
 165              }
 166              $this->account_id = $GLOBALS['egw_info']['user']['account_id'];
 167  
 168              /* Used to flag an address as being:
 169                 domestic AND/OR international(default)
 170                 parcel(default)
 171                 postal(default)
 172              */
 173              $this->adr_types = array(
 174                  'dom'    => lang('Domestic'),
 175                  'intl'   => lang('International'),
 176                  'parcel' => lang('Parcel'),
 177                  'postal' => lang('Postal')
 178              );
 179              if (!is_object($GLOBALS['egw']->datetime))
 180              {
 181                  $GLOBALS['egw']->datetime =& CreateObject('phpgwapi.datetime');
 182              }
 183              $this->tz_offset_s = $GLOBALS['egw']->datetime->tz_offset;
 184          }
 185  
 186          /* send this the id and whatever fields you want to see */
 187  		function read_single_entry($id,$fields='')
 188          {
 189              if (!$fields || empty($fields))
 190              {
 191                  $fields = $this->stock_contact_fields;
 192              }
 193              list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
 194  
 195              if (count($stock_fieldnames))
 196              {
 197                  $t_fields = ',' . implode(',',$stock_fieldnames);
 198                  if ($t_fields == ',')
 199                  {
 200                      unset($t_fields);
 201                  }
 202              }
 203  
 204              $this->db->select($this->std_table,'id,lid,tid,owner,access,cat_id'.$t_fields,array('id' => $id),__LINE__,__FILE__);
 205              $this->db->next_record();
 206  
 207              $return_fields[0]['id']     = $this->db->f('id');
 208              $return_fields[0]['lid']    = $this->db->f('lid');
 209              $return_fields[0]['tid']    = $this->db->f('tid');
 210              $return_fields[0]['owner']  = $this->db->f('owner');
 211              $return_fields[0]['access'] = $this->db->f('access');
 212              $return_fields[0]['cat_id'] = $this->db->f('cat_id');
 213              $return_fields[0]['rights'] = (int)$this->grants[$this->db->f('owner')];
 214  
 215              if(@is_array($stock_fieldnames))
 216              {
 217                  foreach($stock_fieldnames as $f_name)
 218                  {
 219                      $return_fields[0][$f_name] = $this->db->f($f_name);
 220                  }
 221              }
 222  
 223              /* Setup address type fields for ui forms display */
 224              if ($this->db->f('adr_one_type'))
 225              {
 226                  $one_type = $this->db->f('adr_one_type');
 227                  foreach($this->adr_types as $name => $val)
 228                  {
 229                      if (strstr($one_type,$name))
 230                      {
 231                          $return_fields[0]['one_'.$name] = 'on';
 232                      }
 233                  }
 234              }
 235              if ($this->db->f('adr_two_type'))
 236              {
 237                  $two_type = $this->db->f('adr_two_type');
 238                  foreach($this->adr_types as $name => $val)
 239                  {
 240                      if (strstr($two_type,$name))
 241                      {
 242                          $return_fields[0]['two_'.$name] = 'on';
 243                      }
 244                  }
 245              }
 246  
 247              $this->db->select($this->ext_table,'contact_name,contact_value',array('contact_id'=>$id),__LINE__,__FILE__);
 248              while ($this->db->next_record())
 249              {
 250                  if ($extra_fields[$this->db->f('contact_name')])
 251                  {
 252                      $return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value');
 253                  }
 254              }
 255              return $return_fields;
 256          }
 257  
 258          // better use get_last_insert_id !!!
 259  		function read_last_entry($fields='')
 260          {
 261              $this->db->select($this->std_table,'max(id)',False,__LINE__,__FILE__);
 262              $this->db->next_record();
 263  
 264              return $this->read_single_entry($this->db->f(0),$fields);
 265          }
 266  
 267          /**
 268           * Searches for contacts meating certain criteria and evtl. return only a range of them
 269           *
 270           * @param int $start=0 starting number of the range, if $limit != 0
 271           * @param int $limit=0 max. number of entries to return, 0=all
 272           * @param array $fields=null fields to return or null for all stock fields
 273           * @param string $query='' search pattern or '' for none
 274           * @param string $filter='' filters with syntax like <name>=<value>,<name2>=<value2> OR <name3>=<value3>,<name4>=!'' for not empty
 275           * @param string $sort='' sorting: ASC or DESC
 276           * @param string $order='' column to order, default ('') n_family,n_given,email ASC
 277           * @param int $lastmod=-1 return only values modified after given timestamp, default (-1) return all
 278           * @param string $cquery='' return only entries starting with given character, default ('') all
 279           * @return array of contacts
 280           */
 281  		function read($start=0,$limit=0,$fields=null,$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='')
 282          {
 283              if (!$limit)
 284              {
 285                  $limit = -1;    // db::query uses -1 for all
 286              }
 287              elseif (!$start)
 288              {
 289                  $start  = 0;
 290              }
 291              if(!$filter) { $filter = 'tid=n'; }
 292  
 293              if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; }
 294              $DEBUG = 0;
 295  
 296              list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
 297              if (count($stock_fieldnames))
 298              {
 299                  $t_fields = ',' . implode(',',$stock_fieldnames);
 300                  if ($t_fields == ',')
 301                  {
 302                      unset($t_fields);
 303                  }
 304              }
 305  
 306              /* turn filter's a=b,c=d OR a=b into an array */
 307              if ($filter)
 308              {
 309                  $check_stock = $this->stock_contact_fields + $this->non_contact_fields;
 310  
 311                  if ($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; }
 312  
 313                  $filterlist = array();
 314                  foreach(explode(',',$filter) as $pair)
 315                  {
 316                      list($name,$value) = explode('=',$pair,2);
 317                      if (!$name || !isset($check_stock[$name]))    // only use valid column-names
 318                      {
 319                          continue;
 320                      }
 321                      if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 2: #'.$name.'# => #'.$value.'#'; }
 322  
 323                      if (empty($value))
 324                      {
 325                          if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is empty (NULL)'; }
 326  
 327                          $filterlist[] = $name.' is NULL';
 328                      }
 329                      else
 330                      {
 331                          if($name == 'cat_id')
 332                          {
 333                              if (!(int)$value) continue;    // nothing to filter
 334  
 335                              //$filterlist[] = "(" . $name . " LIKE '%," . (int)$value . ",%' OR " . $name."='".(int)$value."')";
 336                              if (!is_object($GLOBALS['egw']->categories))
 337                              {
 338                                  $GLOBALS['egw']->categories = CreateObject('phpgwapi.categories');
 339                              }
 340                              $cats = $GLOBALS['egw']->categories->return_all_children((int)$value);
 341                              foreach($cats as $cat)
 342                              {
 343                                  $cat_filter[] = $this->db->concat("','",cat_id,"','")." LIKE '%,$cat,%'";
 344                              }
 345                              $filterlist[] = '('.implode(' OR ',$cat_filter).')';
 346                          }
 347                          elseif(@is_int($value))
 348                          {
 349                              $filterlist[] = $name . '=' . $value;
 350                          }
 351                          elseif ($value == "!''")    // check for not empty
 352                          {
 353                              $filterlist[] = $name . "!=''";
 354                          }
 355                          else
 356                          {
 357                              $filterlist[] = $name . "='" . $this->db->db_addslashes($value) . "'";
 358                          }
 359                      }
 360                  }
 361                  $filterlist = implode(' AND ',$filterlist);
 362  
 363                  if ($DEBUG)
 364                  {
 365                      echo '<br>DEBUG - Filter output string: #'.$filterlist.'#';
 366                  }
 367  
 368                  if ($filterlist)
 369                  {
 370                      $filtermethod = '('.$filterlist.') ';
 371                      $fwhere = ' WHERE '; $fand = ' AND ';
 372                  }
 373              }
 374              else
 375              {
 376                  $filtermethod = " AND (tid='n' OR tid is null)";
 377              }
 378  
 379              if (!$filtermethod)
 380              {
 381                  if($this->account_id)
 382                  {
 383                      $fwhere .= ' (owner=' . $this->account_id;
 384                      $fand   .= ' (owner=' . $this->account_id;
 385                  }
 386              }
 387              else
 388              {
 389                  if($this->account_id)
 390                  {
 391                      $fwhere .= $filtermethod . ' AND (owner=' . $this->account_id;
 392                      $fand   .= $filtermethod . ' AND (owner=' . $this->account_id;
 393                  }
 394                  else
 395                  {
 396                      $filtermethod = substr($filtermethod,0,-2);
 397                      $fwhere .= $filtermethod;
 398                      $fand   .= $filtermethod;
 399                  }
 400              }
 401  
 402              if(@is_array($this->grants))
 403              {
 404                  $grants = $this->grants;
 405                  foreach($grants as $user => $_right)
 406                  {
 407                      $public_user_list[] = $user;
 408                  }
 409                  $fwhere .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
 410                  $fand   .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) ";
 411              }
 412              else
 413              {
 414                  $fwhere .= ') '; $fand .= ') ';
 415              }
 416  
 417              if ($DEBUG && $filtermethod)
 418              {
 419                  echo '<br>DEBUG - Filtering with: #' . $filtermethod . '#';
 420              }
 421  
 422              if (!$sort) { $sort = 'ASC'; }
 423  
 424              if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort)))
 425              {
 426                  $ordermethod = "ORDER BY $order $sort ";
 427              }
 428              else
 429              {
 430                  $ordermethod = "ORDER BY n_family,n_given,org_name ASC";
 431              }
 432  
 433              if ($DEBUG && $ordermethod)
 434              {
 435                  echo "<br>DEBUG - $ordermethod";
 436              }
 437  
 438              if($lastmod >= 0)
 439              {
 440                  if (!$fwhere) $fwhere = ' WHERE ';
 441                  $fwhere .= " AND last_mod > ".(int)($lastmod - $this->tz_offset_s).' ';
 442  
 443                  if ($DEBUG)
 444                  {
 445                      echo "<br>DEBUG - last_mod_filter added to fwhere: $fwhere";
 446                  }
 447              }
 448  
 449              $filtermethod = '';
 450  
 451              if($cquery)
 452              {
 453                  $sql = 'SELECT * FROM ' . $this->std_table . ' WHERE (';
 454                  $sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table  . ' WHERE (';
 455                  foreach(array(
 456                      'fn'       => 'cn',
 457                      'n_family' => 'sn',
 458                      'n_given'  => 'givenname',
 459                      'org_name' => 'o'
 460                  ) as $f => $x)
 461                  {
 462                      $cquery = strtoupper($this->db->db_addslashes($cquery));
 463                      $sql .= " UPPER($f) LIKE '$cquery%' OR ";
 464                      $sqlcount .= " UPPER($f) LIKE '$cquery%' OR ";
 465                  }
 466                  $sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod;
 467                  $sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod;
 468                  unset($f); unset($x);
 469              }
 470              elseif($query)
 471              {
 472                  if(is_array($query))
 473                  {
 474                      $sql = "SELECT * FROM $this->std_table WHERE (";
 475                      $sqlcount = "SELECT COUNT(id) FROM $this->std_table WHERE (";
 476                      foreach($query as $queryKey => $queryValue)
 477                      {
 478                          if (!preg_match('/^[a-zA-Z0-9_]+$/',$queryKey))
 479                          {
 480                              continue;    // this can be something nasty
 481                          }
 482                          // special handling of text columns for certain db's;
 483                          if (in_array($f,array('note','pubkey','label')))
 484                          {
 485                              switch($this->db->Type)
 486                              {
 487                                  case 'sapdb': case 'maxdb':
 488                                      $queryKey = false;    // sapdb cant use LIKE on text/LONG columns
 489                                      break;
 490                                  case 'mssql':
 491                                      $queryKey = "CAST($queryKey AS varchar)";    // mssql cant use UPPER on text columns
 492                                      break;
 493                              }
 494                              if (!$queryKey) continue;
 495                          }
 496                          $queryValue  = strtoupper($this->db->db_addslashes($queryValue));
 497                          if(empty($queryValue)) {
 498                              $sql .= " ($queryKey IS NULL OR $queryKey = '') AND ";
 499                              $sqlcount .= " ($queryKey IS NULL OR $queryKey = '') AND ";
 500                          } else {
 501                              $sql .= " UPPER($queryKey) LIKE '$queryValue' AND ";
 502                              $sqlcount .= " UPPER($queryKey) LIKE '$queryValue' AND ";
 503                          }
 504                      }
 505                      $sql = substr($sql,0,-5) . ') ' . $fand . $filtermethod . $ordermethod;
 506                      $sqlcount = substr($sqlcount,0,-5) . ') ' . $fand . $filtermethod;
 507                      unset($queryKey); unset($queryValue);
 508                  }
 509                  else
 510                  {
 511                      $query = strtoupper($this->db->db_addslashes($query));
 512  
 513                      $sql = "SELECT * FROM $this->std_table WHERE (";
 514                      $sqlcount = "SELECT COUNT(*) FROM $this->std_table WHERE (";
 515                      foreach($this->stock_contact_fields as $f => $x)
 516                      {
 517                          // special handling of text columns for certain db's;
 518                          if (in_array($f,array('note','pubkey','label')))
 519                          {
 520                              switch($this->db->Type)
 521                              {
 522                                  case 'sapdb': case 'maxdb':
 523                                      $f = false;    // sapdb cant use LIKE on text/LONG columns
 524                                      break;
 525                                  case 'mssql':
 526                                      $f = "CAST($f AS varchar)";    // mssql cant use UPPER on text columns
 527                                      break;
 528                              }
 529                              if (!$f) continue;
 530                          }
 531                          $sql .= " UPPER($f) LIKE '%$query%' OR ";
 532                          $sqlcount .= " UPPER($f) LIKE '%$query%' OR ";
 533                      }
 534                      $sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod;
 535                      $sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod;
 536                      unset($f); unset($x);
 537                  }
 538              }
 539              else
 540              {
 541                  $sql = "SELECT id,lid,tid,owner,access,cat_id,last_mod $t_fields FROM $this->std_table " . $fwhere
 542                      . $filtermethod . ' ' . $ordermethod;
 543                  $sqlcount = "SELECT COUNT(*) FROM $this->std_table " . $fwhere . $filtermethod;
 544              }
 545              if($DEBUG)
 546              {
 547                  echo '<br>COUNT QUERY' . $sqlcount;
 548                  echo '<br>FULL  QUERY' . $sql;
 549              }
 550  
 551  //            $db2 = $this->db;
 552              copyobj($this->db,$db2);
 553  
 554              /* Perhaps it is more efficient to count records for this query, which is all we need here */
 555              $this->db->query($sqlcount,__LINE__,__FILE__);
 556              $this->db->next_record();
 557              unset($sqlcount);
 558              $this->total_records = $this->db->f(0);
 559  
 560              //echo "<p align=right>search() start=$start, limit=$limit, total=$this->total_records</p>\n";
 561              if ($this->total_records < $start) $start = 0;
 562  
 563              $this->db->query($sql,__LINE__,__FILE__,$start,$limit);
 564  
 565              $i = 0;
 566              while($this->db->next_record())
 567              {
 568                  $return_fields[$i]['id']       = $this->db->f('id');
 569                  $return_fields[$i]['lid']      = $this->db->f('lid');
 570                  $return_fields[$i]['tid']      = $this->db->f('tid');
 571                  $return_fields[$i]['owner']    = $this->db->f('owner');
 572                  $return_fields[$i]['access']   = $this->db->f('access');
 573                  $return_fields[$i]['cat_id']   = $this->db->f('cat_id');
 574                  $return_fields[$i]['last_mod'] = $this->db->f('last_mod')+$this->tz_offset_s;
 575                  $return_fields[$i]['rights']   = (int)$this->grants[$this->db->f('owner')];
 576  
 577                  if(@is_array($stock_fieldnames))
 578                  {
 579                      foreach($stock_fieldnames as $f_name)
 580                      {
 581                          $return_fields[$i][$f_name] = $this->db->f($f_name);
 582                      }
 583                      reset($stock_fieldnames);
 584                  }
 585                  $db2->select($this->ext_table,'contact_name,contact_value',array('contact_id'=>$this->db->f('id')),__LINE__,__FILE__);
 586                  while($db2->next_record())
 587                  {
 588                      if($extra_fields[$db2->f('contact_name')])
 589                      {
 590                          $return_fields[$i][$db2->f('contact_name')] = $db2->f('contact_value');
 591                      }
 592                  }
 593                  $i++;
 594              }
 595              return $return_fields;
 596          }
 597  
 598  		function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
 599          {
 600              $owner = (int)$owner;
 601              // access, cat_id and tid can be in $fields now or as extra params
 602              foreach(array('access','cat_id','tid') as $extra)
 603              {
 604                  if (!is_null($$extra))
 605                  {
 606                      $fields[$extra] = $$extra;
 607                  }
 608              }
 609              if(empty($fields['tid']))
 610              {
 611                  $fields['tid'] = 'n';
 612              }
 613              // setting the telephone numbers to empty if unset, as otherwise the db-default adds '+1 (000) 000-0000' 
 614              // I dont want to change the default in the stable release
 615              foreach($this->stock_contact_fields as $name)
 616              {
 617                  if (substr($name,0,4) == 'tel_' && !isset($fields[$name])) $fields[$name] = '';
 618              }
 619              list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);
 620  
 621              //this is added here so it is never tainted
 622              $this->stock_contact_fields['last_mod'] = 'last_mod';
 623              $stock_fields['last_mod'] = time();
 624  
 625              $data = array(
 626                  'owner'        => $owner,
 627                  'access'    => $fields['access'],
 628                  'cat_id'    => $fields['cat_id'],
 629                  'tid'        => $fields['tid'],
 630              );
 631              if (isset($fields['lid'])) $data['lid'] = $fields['lid'];
 632              
 633  
 634              $this->db->insert($this->std_table,array_merge($data,$stock_fields),False,__LINE__,__FILE__);
 635  
 636              $id = $this->db->get_last_insert_id($this->std_table, 'id');
 637  
 638              if($id && count($extra_fields))
 639              {
 640                  foreach($extra_fields as $name => $value)
 641                  {
 642                      $this->db->insert($this->ext_table,array(
 643                              'contact_id'    => $id,
 644                              'contact_owner'    => $owner,
 645                              'contact_name'    => $name,
 646                              'contact_value'    => $value,
 647                          ),False,__LINE__,__FILE__);
 648                  }
 649              }
 650              return ($id ? $id : False);
 651          }
 652  
 653  		function field_exists($id,$field_name)
 654          {
 655              $this->db->select($this->ext_table,'COUNT(*)',array(
 656                      'contact_id'    => $id,
 657                      'contact_name'    => $field_name,
 658                  ),__LINE__,__FILE__);
 659              $this->db->next_record();
 660              return $this->db->f(0);
 661          }
 662  
 663  		function add_single_extra_field($id,$owner,$field_name,$field_value)
 664          {
 665              $this->db->insert($this->ext_table,array(
 666                      'contact_id'    => $id,
 667                      'contact_owner'    => $owner,
 668                      'contact_name'    => $field_name,
 669                      'contact_value'    => $field_value,
 670                  ),False,__LINE__,__FILE__);
 671          }
 672  
 673  		function delete_single_extra_field($id,$field_name)
 674          {
 675              $this->db->delete($this->ext_table,array(
 676                      'contact_id'    => $id,
 677                      'contact_name'    => $field_name,
 678                  ),__LINE__,__FILE__);
 679          }
 680  
 681  		function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL)
 682          {
 683              /* First make sure that id number exists */
 684              $this->db->select($this->std_table,'COUNT(*)',array('id'=>$id),__LINE__,__FILE__);
 685              $this->db->next_record();
 686              if (!$this->db->f(0))
 687              {
 688                  return False;
 689              }
 690  
 691              list($stock_fields,,$extra_fields) = $this->split_stock_and_extras($fields);
 692              // access, cat_id and tid can be in $fields now or as extra params
 693              foreach(array('access','cat_id','tid','owner') as $extra)
 694              {
 695                  if (!is_null($$extra))
 696                  {
 697                      $fields[$extra] = $$extra;
 698                  }
 699                  if (isset($fields[$extra]))
 700                  {
 701                      $stock_fields[$extra] = $fields[$extra];
 702                  }
 703              }
 704  
 705              if (count($stock_fields))
 706              {
 707                  $stock_fields['last_mod'] = time();
 708                  $this->db->update($this->std_table,$stock_fields,array('id'=>$id),__LINE__,__FILE__);
 709              }
 710              if (is_array($extra_fields))
 711              {
 712                  foreach($extra_fields as $x_name => $x_value)
 713                  {
 714                      if ($this->field_exists($id,$x_name))
 715                      {
 716                          if (!$x_value)
 717                          {
 718                              $this->delete_single_extra_field($id,$x_name);
 719                          }
 720                          else
 721                          {
 722                              $this->db->update($this->ext_table,array(
 723                                      'contact_value'    => $x_value,
 724                                      'contact_owner'    => $owner,
 725                                  ),array(
 726                                      'contact_name'    => $x_name,
 727                                      'contact_id'    => $id,
 728                                  ),__LINE__,__FILE__);
 729                          }
 730                      }
 731                      elseif($x_value)    // dont write emtpy extra-fields
 732                      {
 733                          $this->add_single_extra_field($id,$owner,$x_name,$x_value);
 734                      }
 735                  }
 736              }
 737              return True;
 738          }
 739  
 740          /* Used by admin to change ownership on account delete */
 741  		function change_owner($old_owner,$new_owner)
 742          {
 743              if (!(int)$new_owner || !(int)$old_owner)
 744              {
 745                  return False;
 746              }
 747              $this->db->update($this->std_table,array('owner'=>$new_owner),array('owner'=>$old_owner),__LINE__,__FILE__);
 748              $this->db->update($this->ext_table,array('contact_owner'=>$new_owner),array('contact_owner'=>$old_owner),__LINE__,__FILE__);
 749          }
 750  
 751          /* This is where the real work of delete() is done, shared class file contains calling function */
 752  		function delete_($id)
 753          {
 754              $this->db->delete($this->std_table,array('id'=>$id),__LINE__,__FILE__);
 755              $this->db->delete($this->ext_table,array('contact_id'=>$id),__LINE__,__FILE__);
 756          }
 757  
 758          /* This is for the admin script deleteaccount.php */
 759  		function delete_all($owner=0)
 760          {
 761              if ($owner)
 762              {
 763                  $this->db->delete($this->std_table,array('owner'=>$owner),__LINE__,__FILE__);
 764                  $this->db->delete($this->ext_table,array('contact_owner'=>$owner),__LINE__,__FILE__);
 765              }
 766          }
 767      }
 768  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7