[ 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.bocontacts.inc.php (source)

   1  <?php
   2  /**************************************************************************\
   3  * eGroupWare - Adressbook - General business 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.bocontacts.inc.php 22523 2006-09-28 04:31:05Z nelius_weiss $ */
  14  
  15  require_once (EGW_INCLUDE_ROOT.'/addressbook/inc/class.socontacts.inc.php');
  16  
  17  /**
  18  * General business object of the adressbook
  19  *
  20  * @package addressbook
  21  * @author Cornelius Weiss <egw@von-und-zu-weiss.de>
  22  * @copyright (c) 2005 by Cornelius Weiss <egw@von-und-zu-weiss.de>
  23  * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  24  */
  25  class bocontacts extends socontacts
  26  {
  27      /**
  28      * @var $grants array with grants
  29      */
  30      var $grants;
  31      
  32      /**
  33      * @var $user userid of current user
  34      */
  35      var $user;
  36      
  37      /**
  38       * @var int $tz_offset_s offset in secconds between user and server-time,
  39       *    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
  40       */
  41      var $tz_offset_s;
  42  
  43      /**
  44       * @var int $now_su actual user (!) time
  45       */
  46      var $now_su;
  47      
  48      /**
  49       * @var array $timestamps timestamps
  50       */
  51      var $timestamps = array('last_mod');
  52      
  53  	function bocontacts($contact_app='addressbook')
  54      {
  55          $this->socontacts($contact_app);
  56          $this->grants = $GLOBALS['egw']->acl->get_grants($contact_app);
  57          $this->user = $GLOBALS['egw_info']['user']['account_id'];
  58          $this->tz_offset_s = 3600 * $GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'];
  59          $this->now_su = time() + $this->tz_offset_s;
  60  
  61  /*        foreach(array(
  62              'so'    => $appname. 'soadb',
  63          ) as $my => $app_class)
  64          {
  65              list(,$class) = explode('.',$app_class);
  66  
  67              if (!is_object($GLOBALS['egw']->$class))
  68              {
  69                  $GLOBALS['egw']->$class =& CreateObject($app_class);
  70              }
  71              $this->$my = &$GLOBALS['egw']->$class;
  72          }*/
  73          
  74      }
  75      
  76      /**
  77       * changes the data from the db-format to your work-format
  78       *
  79       * it gets called everytime when data is read from the db
  80       * This function needs to be reimplemented in the derived class
  81       *
  82       * @param array $data 
  83       */
  84  	function db2data($data)
  85      {
  86          // convert timestamps from server-time in the db to user-time
  87          foreach($this->timestamps as $name)
  88          {
  89              if(isset($data[$name]))
  90              {
  91                  $data[$name] += $this->tz_offset_s;
  92              }
  93          }
  94          return $data;
  95      }
  96  
  97      /**
  98       * changes the data from your work-format to the db-format
  99       *
 100       * It gets called everytime when data gets writen into db or on keys for db-searches
 101       * this needs to be reimplemented in the derived class
 102       *
 103       * @param array $data
 104       */
 105  	function data2db($data)
 106      {
 107          // convert timestamps from user-time to server-time in the db
 108          foreach($this->timestamps as $name)
 109          {
 110              if(isset($data[$name]))
 111              {
 112                  $data[$name] -= $this->tz_offset_s;
 113              }
 114          }
 115          return $data;
 116      }
 117  
 118      /**
 119      * deletes contact in db
 120      *
 121      * @param mixed &contact contact array from etemplate::exec or id
 122      * @return bool false if all went right
 123      */
 124  	function delete(&$contact)
 125      {
 126          // multiple delete from advanced search
 127          if(isset($contact[0]))
 128          {
 129              foreach($contact as $single)
 130              {
 131                  if($this->check_perms(EGW_ACL_DELETE,$single['id']))
 132                  {
 133                      if(parent::delete($single))
 134                      {
 135                          $msg .= lang('Something went wrong by deleting %1', $single['n_given'].$single['n_family']);
 136                      }
 137                      else
 138                      {
 139                          $GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $single['id'], 'delete', time());
 140                      }
 141                  }
 142                  else
 143                  {
 144                      $msg .= lang('You are not permitted to delete contact %1', $single['n_given'].$single['n_family']);
 145                  }
 146              }
 147              return $msg;
 148          }
 149          if((isset($contact['id']) && !$this->check_perms(EGW_ACL_DELETE,$contact['id'])) ||
 150              (is_numeric($contact) && !$this->check_perms(EGW_ACL_DELETE,$contact)))
 151          {
 152              $contact['msg'] = lang('You are not permittet to delete this contact');
 153              return 1;
 154          }
 155          if(is_numeric($contact)) $contact = array('id' => $contact);
 156          if(parent::delete($contact))
 157          {
 158              $contact['msg'] = lang('Something went wrong by deleting this contact');
 159              return 1;
 160          }
 161          $GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $contact['id'], 'delete', time());
 162  
 163          return;
 164      }
 165      
 166      /**
 167      * saves contact to db
 168      *
 169      * @param array &contact contact array from etemplate::exec
 170      * @return array $contact
 171      * TODO make fullname format choosable at best with selectbox and javascript in edit dialoge
 172      */
 173  	function save(&$contact)
 174      {
 175          // stores if we add or update a entry
 176          $isUpdate = true;
 177          
 178          if($contact['id'] && !$this->check_perms(EGW_ACL_EDIT,$contact['id']))
 179          {
 180              $contact['msg'] = lang('You are not permittet to edit this contact');
 181              return $contact;
 182          }
 183          
 184          // new contact
 185          if($contact['id'] == 0)
 186          {
 187              $contact['owner'] = $this->user;
 188              // we create a normal contact
 189              $contact['tid'] = 'n';
 190              
 191              $isUpdate = false;
 192          }
 193          
 194          // convert categories
 195          $contact['cat_id'] = $contact['cat_id'] ? (is_array($contact['cat_id']) ? implode(',',$contact['cat_id']) : $contact['cat_id']) : '';
 196          // last modified
 197          $contact['last_mod'] = $this->now_su;
 198          // only owner can set access status
 199          $contact['access'] = $contact['owner'] == $this->user ? ($contact['private'] ? 'private': 'public') : $contact['access'];
 200          // create fullname
 201          $contact['fn'] = array();
 202          foreach(array('n_prefix','n_given','n_middle','n_family','n_suffix') as $n)
 203          {
 204              if ($contact[$n]) $contact['fn'][] = $contact[$n];
 205          }
 206          $contact['fn'] = implode(' ',$contact['fn']);
 207          // for some bad historical reasons we mainfileds saved in cf :-(((
 208          $contact['#ophone'] = $contact['ophone']; unset($contact['ophone']);
 209          $contact['#address2'] = $contact['address2']; unset($contact['address2']);
 210          $contact['#address3'] = $contact['address3']; unset($contact['address3']);
 211      
 212          $error_nr = parent::save($contact);
 213  
 214          if(!$error_nr)
 215          {
 216              if($isUpdate) {
 217                  $GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $contact['id'], 'modify', time());
 218              } else {
 219                  $GLOBALS['egw']->contenthistory->updateTimeStamp('contacts', $contact['id'], 'add', time());
 220              }
 221          }
 222          
 223          // for some bad historical reasons we mainfileds saved in cf :-(((
 224          $contact['ophone'] = $contact['#ophone']; unset($contact['#ophone']);
 225          $contact['address2'] = $contact['#address2']; unset($contact['#address2']);
 226          $contact['address3'] = $contact['#address3']; unset($contact['#address3']);
 227          
 228          $contact['msg'] = $error_nr ?
 229              lang('Something went wrong by saving this contact. Errorcode %1',$error_nr) :
 230              lang('Contact saved');
 231          
 232          return $contact;
 233      }
 234      
 235      /**
 236      * reads contacts matched by key and puts all cols in the data array
 237      *
 238      * @param array $keys array with keys in form internalName => value, may be a scalar value if only one key
 239      * @param string/array $extra_cols string or array of strings to be added to the SELECT, eg. "count(*) as num"
 240      * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or 
 241      * @return array with data or errormessage
 242      */
 243  	function read($keys,$extra_cols='',$join='')
 244      {
 245          $data = parent::read($keys,$extra_cols,$join);
 246          if (!$data)
 247          {
 248              return lang('something went wrong by reading this contact');
 249          }
 250          if(!$this->check_perms(EGW_ACL_READ,$data))
 251          {
 252              return lang('you are not permittet to view this contact');
 253          }
 254          
 255          // convert access into private for historical reasons
 256          $data['private'] = $data['access'] == 'private' ? 1 : 0;
 257          // for some bad historical reasons we mainfileds saved in cf :-(((
 258          $data['ophone'] = $data['#ophone']; unset($data['#ophone']);
 259          $data['address2'] = $data['#address2']; unset($data['#address2']);
 260          $data['address3'] = $data['#address3']; unset($data['#address3']);
 261          
 262          return $data;
 263      }
 264      /**
 265      * searches contacts for rows matching searchcriteria
 266      *
 267      * @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
 268      * @param boolean/string $only_keys=true True returns only keys, False returns all cols. comma seperated list of keys to return
 269      * @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
 270      * @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
 271      * @param string $wildcard='' appended befor and after each criteria
 272      * @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
 273      * @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
 274      * @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num)
 275      * @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
 276      * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or 
 277      *    "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
 278      * @param boolean $need_full_no_count=false If true an unlimited query is run to determine the total number of rows, default false
 279      * @return array of matching rows (the row is an array of the cols) or False
 280      */
 281  	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)
 282      {
 283          $filter = array(
 284              'owner' => array_keys($this->grants),
 285              'access' => 'public',
 286          );
 287          return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);
 288      }
 289      
 290      /**
 291      * Checks if the current user has the necessary ACL rights
 292      *
 293      * @param int $needed necessary ACL right: EGW_ACL_{READ|EDIT|DELETE}
 294      * @param mixed $contact contact as array or the contact-id
 295      * @return boolean true permission granted or false for permission denied
 296      */
 297  	function check_perms($needed,&$contact)
 298      {
 299          if (is_array($contact))
 300          {
 301              $owner = $contact['owner'];
 302              $access = $contact['access'];
 303          }
 304          elseif (is_numeric($contact))
 305          {
 306              $read_contact = parent::read($contact);
 307              $owner = $read_contact['owner'];
 308              $access = $read_contact['access'];
 309          }
 310          if($owner == $this->user)
 311          {
 312              return true;
 313          }
 314          $access = $access ? $access : 'public';
 315          return $access == 'private' ? false : $this->grants[$owner] & $needed;
 316      }
 317  }


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