[ Index ]
 

Code source de eGroupWare 1.2.106-2

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

title

Body

[fermer]

/projectmanager/inc/ -> class.soprojectmanager.inc.php (source)

   1  <?php
   2  /**************************************************************************\
   3  * eGroupWare - ProjectManager - General storage object                     *
   4  * http://www.egroupware.org                                                *
   5  * Written and (c) 2005 by Ralf Becker <RalfBecker@outdoor-training.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.soprojectmanager.inc.php 21589 2006-05-17 10:37:03Z ralfbecker $ */
  14  
  15  include_once (EGW_INCLUDE_ROOT.'/etemplate/inc/class.so_sql.inc.php');
  16  
  17  /**
  18   * General storage object of the projectmanager: access the main project data
  19   *
  20   * Tables: egw_pm_projects, egw_pm_extra, egw_pm_roles, egw_pm_members
  21   *
  22   * A project P is the parent of an other project C, if link_id1=P.pm_id and link_id2=C.pm_id !
  23   *
  24   * @package projectmanager
  25   * @author RalfBecker-AT-outdoor-training.de
  26   * @copyright (c) 2005 by RalfBecker-AT-outdoor-training.de
  27   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  28   */
  29  class soprojectmanager extends so_sql
  30  {
  31      /**
  32       * @var string $links_table table name 'egw_links'
  33       */
  34      var $links_table = 'egw_links';
  35      /**
  36       * @var array $config configuration data
  37       */
  38      var $config = array(
  39          'customfields' => array(),
  40      );
  41      var $customfields;
  42      /**
  43       * @var string $extra_table name of customefields table
  44       */
  45      var $extra_table = 'egw_pm_extra';
  46      var $members_table = 'egw_pm_members';
  47      var $roles_table = 'egw_pm_roles';
  48      /**
  49       * @var string $acl_join join with the members and the roles table to get the role-ACL of the current user
  50       */
  51      var $acl_join;
  52      /**
  53       * @var array $acl_extracols extracolumns from the members table
  54       */
  55      var $acl_extracols='role_acl';
  56      /**
  57       * @var array $grants ACL grants from other users
  58       */
  59      var $grants;
  60      var $read_grants,$private_grants;
  61  
  62      /**
  63       * Constructor, calls the constructor of the extended class
  64       * 
  65       * @param int $pm_id id of the project to load, default null
  66       */
  67  	function soprojectmanager($pm_id=null)
  68      {
  69          $this->so_sql('projectmanager','egw_pm_projects');
  70          
  71          $config =& CreateObject('phpgwapi.config','projectmanager');
  72          $config->read_repository();
  73          $this->config =& $config->config_data;
  74          unset($config);
  75          $this->customfields =& $this->config['customfields'];
  76          $this->config['duration_format'] = str_replace(',','',$this->config['duration_units']).','.$this->config['hours_per_workday'];
  77  
  78          $this->grants = $GLOBALS['egw']->acl->get_grants('projectmanager');
  79          $this->user = (int) $GLOBALS['egw_info']['user']['account_id'];
  80  
  81          $this->read_grants = $this->private_grants = array();
  82          foreach($this->grants as $owner => $rights)
  83          {
  84              if ($rights) $this->read_grants[] = $owner;        // ANY ACL implies READ!
  85              
  86              if ($rights & EGW_ACL_PRIVATE) $this->private_grants[] = $owner;
  87          }
  88          $this->acl_join = "LEFT JOIN $this->members_table ON ($this->table_name.pm_id=$this->members_table.pm_id AND member_uid=$this->user) ".
  89              " LEFT JOIN $this->roles_table ON $this->members_table.role_id=$this->roles_table.role_id";
  90  
  91          if ($pm_id) $this->read($pm_id);
  92      }
  93      
  94      /**
  95       * reads a project
  96       *
  97       * reimplemented to handle custom fields
  98       */
  99  	function read($keys)
 100      {
 101          //echo "<p>soprojectmanager::read(".print_r($keys,true).")</p>\n";
 102  
 103          if ($keys && is_numeric($keys) && $this->data['pm_id'] == $keys ||
 104              $keys['pm_id'] &&  $this->data['pm_id'] == $keys['pm_id'])
 105          {
 106              return $this->data;
 107          }
 108          if (!parent::read($keys))
 109          {
 110              return false;
 111          }
 112          if ($this->customfields)
 113          {
 114              $this->db->select($this->extra_table,'*',array('pm_id' => $this->data['pm_id']),__LINE__,__FILE__);
 115  
 116              while (($row = $this->db->row(true)))
 117              {
 118                  $this->data['#'.$row['pm_extra_name']] = $row['pm_extra_value'];
 119              }
 120          }
 121          // query project_members and their roles
 122          $this->db->select($this->members_table,'*',$this->members_table.'.pm_id='.(int)$this->data['pm_id'],__LINE__,__FILE__,
 123              False,'',False,0,"LEFT JOIN $this->roles_table ON $this->members_table.role_id=$this->roles_table.role_id");
 124      
 125          while (($row = $this->db->row(true)))
 126          {
 127              $this->data['pm_members'][$row['member_uid']] = $row;
 128          }
 129          $this->data['role_acl'] = $this->data['pm_members'][$this->user]['role_acl'];
 130          
 131          return $this->data;
 132      }
 133      
 134      /**
 135       * saves a project
 136       *
 137       * reimplemented to handle custom fields and set modification and creation data
 138       *
 139       * @param array $keys if given $keys are copied to data before saveing => allows a save as
 140       * @return int 0 on success and errno != 0 else
 141       */
 142  	function save($keys=null)
 143      {
 144          //echo "soprojectmanager::save(".print_r($keys,true).") this->data="; _debug_array($this->data);
 145          
 146          if (is_array($keys) && count($keys))
 147          {
 148              $this->data_merge($keys);
 149              $keys = null;
 150          }
 151          if (parent::save($keys) == 0 && $this->data['pm_id'])
 152          {
 153              if ($this->customfields)
 154              {
 155                  // custome fields: first delete all, then save the ones with non-empty content
 156                  $this->db->delete($this->extra_table,array('pm_id' => $this->data['pm_id']),__LINE__,__FILE__);
 157                  foreach($this->customfields as $name => $data)
 158                  {
 159                      if ($name && isset($this->data['#'.$name]) && !empty($this->data['#'.$name]))
 160                      {
 161                          $this->db->insert($this->extra_table,array(
 162                              'pm_id'          => $this->data['pm_id'],
 163                              'pm_extra_name'  => $name,
 164                              'pm_extra_value' => $this->data['#'.$name],
 165                          ),false,__LINE__,__FILE__);
 166                      }
 167                  }
 168              }
 169              // project-members: first delete all, then save the (still) assigned ones
 170              $this->db->delete($this->members_table,array('pm_id' => $this->data['pm_id']),__LINE__,__FILE__);
 171              foreach((array)$this->data['pm_members'] as $uid => $data)
 172              {
 173                  $this->db->insert($this->members_table,array(
 174                      'pm_id'      => $this->data['pm_id'],
 175                      'member_uid' => $uid,
 176                      'role_id'    => $data['role_id'],
 177                      'member_availibility' => $data['member_availibility'], 
 178                  ),false,__LINE__,__FILE__);
 179              }
 180          }
 181          return $this->db->Errno;
 182      }
 183      
 184      /**
 185       * merges in new values from the given new data-array
 186       *
 187       * reimplemented to also merge the customfields
 188       *
 189       * @param $new array in form col => new_value with values to set
 190       */
 191  	function data_merge($new)
 192      {
 193          parent::data_merge($new);
 194          
 195          if (is_array($this->customfields))
 196          {
 197              foreach($this->customfields as $name => $data)
 198              {
 199                  if (isset($new['#'.$name]))
 200                  {
 201                      $this->data['#'.$name] = $new['#'.$name];
 202                  }
 203              }
 204          }
 205      }
 206  
 207      /**
 208       * search projects, re-implemented to include sub-cats and allow to filter for subs and mains
 209       *
 210       * @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
 211       * @param boolean $only_keys True returns only keys, False returns all cols
 212       * @param string $order_by fieldnames + {ASC|DESC} separated by colons ','
 213       * @param string/array $extra_cols string or array of strings to be added to the SELECT, eg. "count(*) as num"
 214       * @param string $wildcard appended befor and after each criteria
 215       * @param boolean $empty False=empty criteria are ignored in query, True=empty have to be empty in row
 216       * @param string $op defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
 217       * @param int/boolean $start if != false, return only maxmatch rows begining with start
 218       * @param array $filter if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
 219       * @param string/boolean $join=true sql to do a join, added as is after the table-name, default true=add join for acl
 220       * @return array of matching rows (the row is an array of the cols) or False
 221       */
 222  	function search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join=true,$need_full_no_count=false)
 223      {
 224          // include sub-categories in the search
 225          if ($filter['cat_id'])
 226          {
 227              if (!is_object($GLOBALS['egw']->categories))
 228              {
 229                  $GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories');
 230              }
 231              $filter['cat_id'] = $GLOBALS['egw']->categories->return_all_children($filter['cat_id']);
 232          }
 233          if ($join === true)    // add acl-join, to get role_acl of current user
 234          {
 235              $join = $this->acl_join;
 236              
 237              if (!is_array($extra_cols)) $extra_cols = $extra_cols ? explode(',',$extra_cols) : array();
 238              $extra_cols = array_merge($extra_cols,array(
 239                  $this->acl_extracols,
 240                  $this->table_name.'.pm_id AS pm_id',
 241              ));
 242              if ($only_keys === true) $only_keys='';    // otherwise we use ambigues pm_id
 243      
 244              if (isset($criteria['pm_id']))
 245              {
 246                  $criteria[$this->table_name.'.pm_id'] = $criteria['pm_id'];
 247                  unset($criteria['pm_id']);
 248              }
 249              if (isset($filter['pm_id']) && $filter['pm_id'])
 250              {
 251                  $filter[$this->table_name.'.pm_id'] = $filter['pm_id'];
 252                  unset($filter['pm_id']);
 253              }
 254              // include an ACL filter for read-access
 255              $filter[] = "(pm_access='anonym' OR pm_access='public' AND pm_creator IN (".implode(',',$this->read_grants).
 256                  ") OR pm_access='private' AND pm_creator IN (".implode(',',$this->private_grants).')'.
 257                  ($join == $this->acl_join ? ' OR '.$this->roles_table.'.role_acl!=0' : '').')';
 258          }
 259          if ($filter['subs_or_mains'])
 260          {
 261  /* old code using a sub-query
 262              $ids = "SELECT link_id2 FROM $this->links_table WHERE link_app2='projectmanager' AND link_app1='projectmanager'";
 263              
 264              if (is_array($filter['subs_or_mains']))        // sub-projects of given parent-projects
 265              {
 266                  $ids .= ' AND '.$this->db->expression($this->links_table,array('link_id1' => $filter['subs_or_mains']));
 267                  $filter['subs_or_mains'] = 'subs';
 268              }
 269              if (!$this->db->capabilities['sub_queries'])
 270              {
 271                  $this->db->query($ids,__LINE__,__FILE__);
 272                  $ids = array();
 273                  while($this->db->next_record())
 274                  {
 275                      $ids[] = $this->db->f(0);
 276                  }
 277                  $ids = count($ids) ? implode(',',$ids) : 0;
 278              }
 279              $filter[] = $this->table_name.'.pm_id '.($filter['subs_or_mains'] == 'mains' ? 'NOT ' : '').'IN ('.$ids.')';
 280  */
 281              // new code using a JOIN
 282              if ($filter['subs_or_mains'] == 'mains')
 283              {
 284                  $filter[] = 'link_id2 IS NULL';
 285                  $join .= ' LEFT';
 286              }
 287              $join .= " JOIN $this->links_table ON link_app2='projectmanager' AND link_app1='projectmanager' AND link_id2=$this->table_name.pm_id";
 288  
 289              if (is_array($filter['subs_or_mains']))    // sub-projects of given parent-projects
 290              {
 291                  $join .= ' AND '.$this->db->expression($this->links_table,array('link_id1' => $filter['subs_or_mains']));
 292              }
 293          }
 294          unset($filter['subs_or_mains']);
 295          
 296          return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count);
 297      }
 298      
 299      /**
 300       * reimplemented to set some defaults and cope with ambigues pm_id column
 301       *
 302       * @param string $value_col='pm_title' column-name for the values of the array, can also be an expression aliased with AS
 303       * @param string $key_col='pm_id' column-name for the keys, default '' = same as $value_col: returns a distinct list
 304       * @param array $filter=array() to filter the entries
 305       * @return array with key_col => value_col pairs, ordered by value_col
 306       */
 307  	function query_list($value_col='pm_title',$key_col='pm_id',$filter=array('pm_status'=>'active'))
 308      {
 309          if ($key_col == 'pm_id') $key_col = $this->table_name.'.pm_id AS pm_id';
 310          
 311          return parent::query_list($value_col,$key_col,$filter);
 312      }
 313      
 314      /**
 315       * read the general availebility of one or all users
 316       *
 317       * A not set availibility is by default 100%
 318       *
 319       * @param int $uid user-id or 0 to read all
 320       * @return array uid => availibility
 321       */
 322  	function get_availibility($uid=0)
 323      {
 324          $where = array('pm_id' => 0);
 325          
 326          if ($uid) $where['member_uid'] = $uid;
 327          
 328          $this->db->select($this->members_table,'member_uid,member_availibility',$where,__LINE__,__FILE__);
 329          $avails = array();
 330          while (($row = $this->db->row(true)))
 331          {
 332              $avails[$row['member_uid']] = empty($row['member_availibility']) ? 100.0 : $row['member_availibility'];
 333          }
 334          return $avails;
 335      }
 336      
 337      /**
 338       * set or delete the general availibility of a user
 339       *
 340       * A not set availibility is by default 100%
 341       *
 342       * @param int $uid user-id
 343       * @param float $availiblity=null percentage to set or nothing to delete the avail. for the user
 344       */
 345  	function set_availibility($uid,$availibility=null)
 346      {
 347          if (!is_numeric($uid)) return;
 348  
 349          if (!is_null($availibility) && !empty($availibility) && $availibility != 100.0)
 350          {
 351              $this->db->insert($this->members_table,array(
 352                  'member_availibility' => $availibility
 353              ),array(
 354                  'member_uid' => $uid,
 355                  'pm_id'      => 0,
 356              ),__LINE__,__FILE__);
 357          }
 358          else
 359          {
 360              $this->db->delete($this->members_table,array(
 361                  'pm_id' => 0,
 362                  'member_uid' => $uid,
 363              ),__LINE__,__FILE__);
 364          }
 365      }
 366  }


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