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

   1  <?php
   2  /**************************************************************************\
   3  * eGroupWare - ProjectManager - Constraints 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.soconstraints.inc.php 19000 2005-08-17 11:06:25Z ralfbecker $ */
  14  
  15  include_once (EGW_INCLUDE_ROOT.'/etemplate/inc/class.so_sql.inc.php');
  16  
  17  /**
  18   * Constraints storage object of the projectmanager
  19   *
  20   * There are 3 constraint-types:
  21   * - start:     PE has to start after an other PE (own pe_id is in pe_id_start)
  22   * - end:       PE has to end before the start an other PE (own pe_id is in pe_id_end)
  23   * - milestone: PE has to end before a milestone (own pe_id is in pe_id_end)
  24   *
  25   * Tables: egw_pm_constraints
  26   *
  27   * @package projectmanager
  28   * @author RalfBecker-AT-outdoor-training.de
  29   * @copyright (c) 2005 by RalfBecker-AT-outdoor-training.de
  30   * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
  31   */
  32  class soconstraints extends so_sql
  33  {
  34      /**
  35       * Constructor, calls the constructor of the extended class
  36       * 
  37       * @param int $pm_id pm_id of the project to use, default null
  38       */
  39  	function soconstraints($pm_id=null)
  40      {
  41          $this->so_sql('projectmanager','egw_pm_constraints');
  42  
  43          if ((int) $pm_id) 
  44          {
  45              $this->pm_id = (int) $pm_id;
  46          }
  47      }
  48  
  49      /**
  50       * searches db for rows matching searchcriteria, reimplemented to automatic add $this->pm_id
  51       *
  52       * '*' and '?' are replaced with sql-wildcards '%' and '_'
  53       *
  54       * @param array/string $criteria array of key and data cols, OR a SQL query (content for WHERE), fully quoted (!)
  55       * @param boolean $only_keys=true True returns only keys, False returns all cols
  56       * @param string $order_by='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
  57       * @param string/array $extra_cols='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
  58       * @param string $wildcard='' appended befor and after each criteria
  59       * @param boolean $empty=false False=empty criteria are ignored in query, True=empty have to be empty in row
  60       * @param string $op='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
  61       * @param mixed $start=false if != false, return only maxmatch rows begining with start, or array($start,$num)
  62       * @param array $filter=null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
  63       * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or 
  64       *    "LEFT JOIN table2 ON (x=y)", Note: there's no quoting done on $join!
  65       * @return array of matching rows (the row is an array of the cols) or False
  66       */
  67      function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='')
  68      {
  69          if (!$this->pm_id && !isset($criteria['pm_id']) && !isset($filter['pm_id']))
  70          {
  71              $filter['pm_id'] = $this->pm_id;
  72          }
  73          if (isset($criteria['pe_id']) && (int)$criteria['pe_id'])
  74          {
  75              $pe_id = (int) $criteria['pe_id'];
  76              unset($criteria['pe_id']);
  77          }
  78          if (isset($filter['pe_id']) && (int)$filter['pe_id'])
  79          {
  80              $pe_id = (int) $filter['pe_id'];
  81              unset($filter['pe_id']);
  82          }
  83          if ($pe_id)
  84          {
  85              $filter[] = "(pe_id_end=$pe_id OR pe_id_start=$pe_id)";
  86  
  87              if ($extra_cols && !is_array($extra_cols)) $extra_cols = explode(',',$extra_cols);
  88              // defines 3 constrain-types: milestone, start and end
  89              $extra_cols[] = "CASE WHEN ms_id != 0 THEN 'milestone' WHEN pe_id_start=$pe_id THEN 'start' ELSE 'end' END AS constraint_type";
  90              if (!$order_by) $order_by = 'constraint_type';
  91          }
  92          return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join);
  93      }
  94      
  95      /**
  96       * reads all constraints of a milestone (ms_id given), an element (pe_id given) or a project (pm_id given)
  97       *
  98       * It calls allways search to retrive the data. The form of the data returned depends on the given keys!
  99       *
 100       * @param array $keys array with keys in form internalName => value, may be a scalar value if only one key
 101       * @param string/array $extra_cols string or array of strings to be added to the SELECT, eg. "count(*) as num"
 102       * @param string $join='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or 
 103       * @return array/boolean milestones: array with pe_id's, element: array with subarrays for start, end, milestone, 
 104       *    or same as search($keys) would return
 105      */
 106  	function read($keys,$extra_cols='',$join='')
 107      {
 108          if (!$search =& $this->search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$keys))
 109          {
 110              return false;
 111          }
 112          $ret = array();
 113  
 114          if ((int) $keys['ms_id'])
 115          {
 116              foreach($search as $row)
 117              {
 118                  $ret[] = $row['pe_id_end'];
 119              }
 120          }
 121          elseif ((int) $keys['pe_id'])
 122          {
 123              foreach($search as $row)
 124              {
 125                  switch($row['constraint_type'])
 126                  {
 127                      case 'milestone':
 128                          $ret['milestone'][] = $row['ms_id'];
 129                          break;
 130                      case 'start':
 131                          $ret['start'][] = $row['pe_id_end'];
 132                          break;
 133                      case 'end':
 134                          $ret['end'][] = $row['pe_id_start'];
 135                          break;
 136                  }
 137              }
 138          }
 139          else
 140          {
 141              $ret =& $search;
 142          }
 143          if ($this->debug)
 144          {
 145              echo "<p>soconstraints::read(".print_r($keys,true).",'$extra_cols','$join')</p>\n";
 146              _debug_array($ret);
 147          }
 148          return $ret;
 149      }
 150  
 151      /**
 152       * saves the given data to the db
 153       *
 154       * @param array $data with either data for one row or null, or 
 155       *    for the constraints of an elements the keys pe_id, start, end, milestone, or 
 156       *    for the constraints of a milestone the keys ms_id, pe_id (pm_id can be given or is taken from $this->pm_id)
 157       * @return int 0 on success and errno != 0 else
 158       */
 159  	function save($data=null)
 160      {
 161          if ($this->debug) { echo "<p>soconstraints::save:"; _debug_array($data); }
 162  
 163          // constraints of an element?
 164          if ($data['pe_id'])
 165          {
 166              $pm_id = $data['pm_id'] ? $data['pm_id'] : $this->pm_id;
 167              unset($data['pm_id']);
 168              $pe_id = $data['pe_id'];
 169              unset($data['pe_id']);
 170  
 171              $this->delete(array(
 172                  'pm_id' => $pm_id,
 173                  'pe_id' => $pe_id,
 174              ));
 175              foreach($data as $type => $ids)
 176              {
 177                  foreach(is_array($ids) ? $ids : explode(',',$ids) as $id)
 178                  {
 179                      if (!$id) continue;
 180  
 181                      switch($type)
 182                      {
 183                          case 'milestone':
 184                              $row = array(
 185                                  'pe_id_end'   => $pe_id,
 186                                  'pe_id_start' => 0,
 187                                  'ms_id'       => $id,
 188                              );
 189                              break;
 190                          case 'start':
 191                              $row = array(
 192                                  'pe_id_end'   => $id,
 193                                  'pe_id_start' => $pe_id,
 194                                  'ms_id'       => 0,
 195                              );
 196                              break;
 197                          case 'end':
 198                              $row = array(
 199                                  'pe_id_end'   => $pe_id,
 200                                  'pe_id_start' => $id,
 201                                  'ms_id'       => 0,
 202                              );
 203                              break;
 204                      }
 205                      $row['pm_id'] = $pm_id;
 206  
 207                      if (($err = parent::save($row)))
 208                      {
 209                          return $err;
 210                      }
 211                  }
 212              }
 213              return 0;
 214          }
 215          // constraints of a milestone
 216          if ($data['ms_id'] && is_array($data['pe_id']))
 217          {
 218              $keys = array(
 219                  'pm_id'       => $data['pm_id'] ? $data['pm_id'] : $this->pm_id,
 220                  'pe_id_start' => 0,
 221                  'ms_id'       => $data['ms_id'],
 222              );
 223              $this->delete($keys);
 224  
 225              foreach($data['pe_id'] as $pe_id);
 226              {
 227                  $keys['pe_id_end'] = $pe_id;
 228                  
 229                  if (($err = parent::save($keys)))
 230                  {
 231                      return $err;
 232                  }
 233              }
 234              return 0;
 235          }
 236          return parent::save($data);
 237      }        
 238          
 239      /**
 240       * reimplented to delete all constraints from a project-element if a pe_id is given
 241       *
 242       * @param array/int $keys if given array with col => value pairs to characterise the rows to delete or pe_id
 243       * @return int affected rows, should be 1 if ok, 0 if an error
 244       */
 245  	function delete($keys=null)
 246      {
 247          if ($this->debug) echo "<p>soconstraints::delete(".print_r($keys,true).")</p>\n";
 248  
 249          if (is_numeric($keys) || is_array($keys) && (int) $keys['pe_id'])
 250          {
 251              if (is_array($keys))
 252              {
 253                  $pe_id = (int) $keys['pe_id'];
 254                  unset($keys['pe_id']);
 255              }
 256              else
 257              {
 258                  $pe_id = (int) $keys;
 259                  $keys = array();
 260              }
 261              $keys[] = "(pe_id_end=$pe_id OR pe_id_start=$pe_id)";        
 262              return $this->db->delete($this->table_name,$keys,__LINE__,__FILE__);
 263          }
 264          return parent::delete($keys);
 265      }
 266  }


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