[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/addressbook/inc/ -> class.socontacts.inc.php (source)

   1  <?php
   2  /**************************************************************************\
   3  * eGroupWare - Adressbook - General storage object                         *
   4  * http://www.egroupware.org                                                *
   5  * Written and (c) 2005 by Cornelius_weiss <egw@von-und-zu-weiss.de>        *
   6  * --------------------------------------------                             *
   7  *  This program is free software; you can redistribute it and/or modify it *
   8  *  under the terms of the GNU General Public License as published by the   *
   9  *  Free Software Foundation; either version 2 of the License, or (at your  *
  10  *  option) any later version.                                              *
  11  \**************************************************************************/
  12  
  13  /* $Id: class.socontacts.inc.php 20923 2006-04-05 13:44:56Z nelius_weiss $ */
  14  
  15  /**
  16   * General storage object of the adressbook
  17   *
  18   * @package addressbook
  19   * @author Cornelius Weiss <egw@von-und-zu-weiss.de>
  20   * @copyright (c) 2005 by Cornelius Weiss <egw@von-und-zu-weiss.de>
  21   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  22   */
  23  
  24  class socontacts
  25  {
  26      /**
  27       * @var string $links_table table name 'egw_links'
  28       */
  29      var $links_table = 'egw_links';
  30      
  31      /**
  32       * @var string $extra_table name of customefields table
  33       */
  34      var $extra_table = 'egw_addressbook_extra';
  35      
  36      /**
  37      * @var string $extra_id
  38      */
  39      var $extra_id = 'contact_id';
  40      
  41      /**
  42      * @var string $extra_owner
  43      */
  44      var $extra_owner = 'contact_owner';
  45  
  46      /**
  47      * @var string $extra_key
  48      */
  49      var $extra_key = 'contact_name';
  50      
  51      /**
  52      * @var string $extra_value
  53      */
  54      var $extra_value = 'contact_value';
  55      
  56  	function socontacts($contact_app='addressbook')
  57      {
  58          if($GLOBALS['egw_info']['server']['contact_repository'] == 'sql' || !isset($GLOBALS['egw_info']['server']['contact_repository']))
  59          {
  60              $this->somain =& CreateObject('etemplate.so_sql','phpgwapi','egw_addressbook');
  61          }
  62          else
  63          {
  64              $this->somain =& CreateObject('addressbook.so_'.$GLOBALS['egw_info']['server']['contact_repository']);
  65          }
  66          $this->somain->contacts_id = 'id';
  67          $this->soextra =& CreateObject('etemplate.so_sql');
  68          $this->soextra->so_sql('phpgwapi',$this->extra_table);
  69              
  70          $custom =& CreateObject('admin.customfields',$contact_app);
  71          $this->customfields = $custom->get_customfields();
  72          if ($this->customfields && !is_array($this->customfields))
  73          {
  74              $this->customfields = unserialize($this->customfields);
  75          }
  76          if (!$this->customfields) $this->customfields = array();
  77      }
  78      
  79      /**
  80       * changes the data from the db-format to your work-format
  81       *
  82       * it gets called everytime when data is read from the db
  83       * This function needs to be reimplemented in the derived class
  84       *
  85       * @param array $data 
  86       */
  87  	function db2data($data)
  88      {
  89          // do the necessare changes here
  90  
  91          return $data;
  92      }
  93  
  94      /**
  95       * changes the data from your work-format to the db-format
  96       *
  97       * It gets called everytime when data gets writen into db or on keys for db-searches
  98       * this needs to be reimplemented in the derived class
  99       *
 100       * @param array $data
 101       */
 102  	function data2db($data)
 103      {
 104          // do the necessary changes here
 105  
 106          return $data;
 107      }
 108  
 109      /**
 110      * deletes contact entry including custom fields
 111      *
 112      * @param array &$contact contact data from etemplate::exec
 113      * @return bool false if all went right
 114      */
 115  	function delete($contact)
 116      {
 117          // delete mainfields
 118          if ($this->somain->delete(array('id' => $contact['id'])))
 119          {        
 120              // delete customfields, can return 0 if there are no customfields
 121              $this->soextra->delete(array($this->extra_id => $contact['id']));
 122  
 123              return false;
 124          }
 125          return true;
 126      }
 127      
 128      /**
 129      * saves contact data including custiom felds
 130      *
 131      * @param array &$contact contact data from etemplate::exec
 132      * @return bool false if all went wrong, errornumber on failure
 133      */
 134  	function save(&$contact)
 135      {
 136          // save mainfields
 137          $this->somain->data = $this->data2db($contact);
 138          $error_nr = $this->somain->save();
 139          $contact['id'] = $this->somain->data['id'];
 140          if($error_nr) return $error_nr_main;
 141  
 142          // save customfields
 143          foreach ((array)$this->customfields + array('ophone' => '', 'address2' => '' , 'address3' => '') as $field => $options)
 144          {
 145              $value = $contact['#'.$field];
 146              $data = array(
 147                  $this->extra_id => $contact['id'],
 148                  $this->extra_owner => $contact['owner'],
 149                  $this->extra_key => $field,
 150                  $this->extra_value => $value,
 151              );
 152              $this->soextra->data = $data;
 153              $error_nr = $this->soextra->save();
 154              if($error_nr) return $error_nr;
 155          }
 156          return;
 157      }
 158      
 159      /**
 160       * reads contact data including custom fields
 161       *
 162       * @param interger $contact_id contact_id
 163       * @return array/boolean data if row could be retrived else False
 164      */
 165  	function read($contact_id)
 166      {
 167          // read main data
 168          if (!($contact = $this->somain->read($contact_id)))
 169          {
 170              return $contact;
 171          }
 172          
 173          // read customfilds
 174          $keys = array(
 175              $this->extra_id => $contact['id'],
 176              $this->extra_owner => $contact['owner'],
 177          );
 178          $customfields = $this->soextra->search($keys,false);
 179          foreach ((array)$customfields as $field)
 180          {
 181              $contact['#'.$field[$this->extra_key]] = $field[$this->extra_value];
 182          }
 183          return $this->db2data($contact);
 184      }
 185      
 186      /**
 187       * searches db for rows matching searchcriteria
 188       *
 189       * '*' and '?' are replaced with sql-wildcards '%' and '_'
 190       *
 191       * @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
 192       * @param boolean/string $only_keys=true True returns only keys, False returns all cols. comma seperated list of keys to return
 193       * @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
 194       * @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
 195       * @param string $wildcard='' appended befor and after each criteria
 196       * @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
 197       * @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
 198       * @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num)
 199       * @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
 200       * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or 
 201       *    "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
 202       * @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
 203       * @return array of matching rows (the row is an array of the cols) or False
 204       */
 205  	function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false)
 206      {
 207           //echo 'socontacts::search->criteria:'; _debug_array($criteria);
 208           // we can only deal with one category atm.
 209           $criteria['cat_id'] = $criteria['cat_id'][0];
 210           if (empty($criteria['cat_id'])) unset($criteria['cat_id']);
 211           
 212          // We just want to deal with generalized vars, to simpyfie porting of this code to so_sql later...
 213          $this->main_id = $this->somain->contacts_id;
 214  
 215          // seperate custom fields from main fields
 216          foreach ($criteria as $crit_key => $crit_val)
 217          {
 218              if(!(isset($this->somain->db_data_cols [$crit_key]) || isset($this->somain->db_key_cols [$crit_key])))
 219              {
 220                  if(strpos($crit_key,'#') !== false && $crit_key{0} != '!' )
 221                  {
 222                      $extra_crit_key = substr($crit_key,1);
 223                      $criteria_extra[$extra_crit_key][$this->extra_key] = $extra_crit_key;
 224                      $criteria_extra[$extra_crit_key][$this->extra_value] = $crit_val;
 225                  }
 226                  unset($criteria[$crit_key]);
 227              }
 228          }
 229          //_debug_array($criteria);
 230          //_debug_array($criteria_extra);
 231  
 232          // search in custom fields
 233          $resultextra = array();
 234          if (count($criteria_extra) >= 1)
 235          {
 236              $firstrun = true;
 237              foreach ((array)$criteria_extra as $extra_crit)
 238              {
 239                  if($extra_crit[$this->extra_value]{0} == '!')
 240                  {
 241                      if(!isset($all_main_ids)) $all_main_ids = $this->somain->search(array($this->main_id => '*'));
 242                      $extra_crit[$this->extra_value] = substr($extra_crit[$this->extra_value],1);
 243                      $not_result = $this->soextra->search($extra_crit,true,'','',$wildcard);
 244                      if(is_array($not_result))
 245                      {
 246                          $expr = '$not_result[0]';
 247                          for($i=1; $i<count($not_result); $i++)
 248                          {
 249                              $expr .= ',$not_result['.$i.']';
 250                          }
 251                          @eval('$not_result = array_merge_recursive('.$expr.');');
 252                      }
 253                      foreach($all_main_ids as $entry)
 254                      {
 255                          if(array_search($entry[$this->main_id],(array)$not_result[$this->extra_id]) === false)
 256                          {
 257                              $result[] = array(
 258                                  $this->extra_id => $entry[$this->main_id],
 259                                  $this->extra_key => $extra_crit[$this->extra_key],
 260                              );
 261                          }
 262                      }
 263                  }
 264                  else
 265                  {
 266                      $result = $this->soextra->search($extra_crit,true,'','',$wildcard);
 267                  }
 268  
 269                  if ($op == 'OR' && $result)
 270                  {
 271                      $resultextra = array_merge_recursive((array)$result,(array)$resultextra);
 272                  }
 273                  elseif ($op == 'AND')
 274                  {
 275                      if (!$result)
 276                      {
 277                          return false;
 278                          //$resultextra = array();
 279                          //break;
 280                      }
 281                      $expr = '$result[0]';
 282                      for($i=1; $i<count($result); $i++)
 283                      {
 284                          $expr .= ',$result['.$i.']';
 285                      }
 286                      @eval('$merge = array_merge_recursive('.$expr.');');
 287                      if(!is_array($merge[$this->extra_id]))
 288                      {
 289                          $merge[$this->extra_id] = (array)$merge[$this->extra_id];
 290                      }
 291                      if($firstrun)
 292                      {
 293                          $resultextra = $merge[$this->extra_id];
 294                          $firstrun = false;
 295                      }
 296                      else
 297                      {
 298                          $resultextra = array_intersect((array)$resultextra,$merge[$this->extra_id]);
 299                      }
 300                  }
 301              }
 302              if($op == 'OR' && $resultextra)
 303              {
 304                  $expr = '$resultextra[0]';
 305                  for($i=1; $i<count($resultextra); $i++)
 306                  {
 307                      $expr .= ',$resultextra['.$i.']';
 308                  }
 309                  @eval('$merge = array_merge_recursive('.$expr.');');
 310                  $resultextra = array_unique((array)$merge[$this->extra_id]);
 311              }
 312          }
 313          //echo 'socontacts::search->resultextra:'; _debug_array($resultextra);
 314          
 315          // search in main fields
 316          $result = array();
 317          // include results from extrafieldsearch
 318          if(!empty($resultextra))
 319          {
 320              $criteria[$this->main_id] = $resultextra;
 321          }
 322          if (count($criteria) >= 1)
 323          {
 324              // We do have to apply wildcard by hand, as the result-ids of extrasearch are included in this search
 325              if($wildcard)
 326              {
 327                  foreach ($criteria as $field => $value)
 328                  {
 329                      if ($field == $this->main_id) continue;
 330                      $criteria[$field] = '*'.$value.'*';
 331                  }
 332              }
 333              $result = $this->somain->search($criteria,true,$order_by,$extra_cols,false,$empty,$op,false,$filter);
 334              if(!is_array($result)) return false;
 335              $expr = '$result[0]';
 336              for($i=1; $i<count($result); $i++)
 337              {
 338                  $expr .= ',$result['.$i.']';
 339              }
 340              @eval('$merge = array_merge_recursive('.$expr.');');
 341              $result = ($merge[$this->main_id]);
 342          }
 343          //echo 'socontacts::search->result:'; _debug_array($result);
 344  
 345          if(count($result) == 0) return false;
 346          if(!is_bool($only_keys_main = $only_keys))
 347          {
 348              $keys_wanted = explode(',',$only_keys);
 349              foreach ($keys_wanted as $num => $key_wanted)
 350              {
 351                  if(!(isset($this->somain->db_data_cols [$key_wanted]) || isset($this->somain->db_key_cols [$key_wanted])))
 352                  {
 353                      unset($keys_wanted[$num]);
 354                      $keys_wanted_custom[] = $key_wanted;
 355                  }
 356              }
 357              $only_keys_main = implode(',',$keys_wanted);
 358          }
 359          $result = $this->somain->search(array($this->main_id => $result),$only_keys_main,$order_by,$extra_cols,'','','OR',$start,$filter,$join,$need_full_no_count);
 360          
 361          // append custom fields for each row
 362          if($only_keys === false || is_array($keys_wanted_custom))
 363          {
 364              foreach ($result as $num => $contact)
 365              {
 366                  $extras = $this->soextra->search(array($this->extra_id => $contact[$this->main_id]),false);
 367                  foreach ((array)$extras as $extra)
 368                  {
 369                      if ($only_keys === false || in_array($extra[$this->extra_key],$keys_wanted_custom))
 370                      {
 371                          $result[$num][$extra[$this->extra_key]] = $extra[$this->extra_value];
 372                      }
 373                  }
 374              }
 375          }
 376          foreach($result as $num => $contact)
 377          {
 378              $result[$num] = $this->db2data($contact);
 379          }
 380          return $need_full_no_count ? count($result) : $result;
 381      }
 382      
 383      /**
 384       * gets all contact fields from database
 385       */
 386  	function get_contact_conlumns()
 387      {
 388          $fields = $this->somain->db_data_cols;
 389          foreach ((array)$this->customfields as $cfield => $coptions)
 390          {
 391              $fields['#'.$cfield] = '#'.$cfield;
 392          }
 393          return $fields;
 394      }
 395  }


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