[3594] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * ProjectManager - General business object |
---|
| 4 | * |
---|
| 5 | * @link http://www.egroupware.org |
---|
| 6 | * @author Ralf Becker <RalfBecker-AT-outdoor-training.de> |
---|
| 7 | * @package projectmanager |
---|
| 8 | * @copyright (c) 2005/6 by Ralf Becker <RalfBecker-AT-outdoor-training.de> |
---|
| 9 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
---|
| 10 | * @version $Id: class.boprojectmanager.inc.php 24677 2007-11-22 14:48:38Z ralfbecker $ |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | include_once(PHPGW_INCLUDE_ROOT.'/projectmanager/inc/class.soprojectmanager.inc.php'); |
---|
| 14 | |
---|
| 15 | define('PHPGW_ACL_BUDGET',PHPGW_ACL_CUSTOM_1); |
---|
| 16 | define('PHPGW_ACL_EDIT_BUDGET',PHPGW_ACL_CUSTOM_2); |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * General business object of the projectmanager |
---|
| 20 | * |
---|
| 21 | * This class does all the timezone-conversation: All function expect user-time and convert them to server-time |
---|
| 22 | * before calling the storage object. |
---|
| 23 | */ |
---|
| 24 | class boprojectmanager extends soprojectmanager |
---|
| 25 | { |
---|
| 26 | /** |
---|
| 27 | * Debuglevel: 0 = no debug-messages, 1 = main, 2 = more, 3 = all, 4 = all incl. so_sql, or string with function-name to debug |
---|
| 28 | * |
---|
| 29 | * @var int/string |
---|
| 30 | */ |
---|
| 31 | var $debug=false; |
---|
| 32 | /** |
---|
| 33 | * File to log debug-messages, ''=echo them |
---|
| 34 | * |
---|
| 35 | * @var string |
---|
| 36 | */ |
---|
| 37 | var $logfile='/tmp/pm_log'; |
---|
| 38 | /** |
---|
| 39 | * Instance of the link-class |
---|
| 40 | * |
---|
| 41 | * @var bolink |
---|
| 42 | */ |
---|
| 43 | var $link; |
---|
| 44 | /** |
---|
| 45 | * Timestaps that need to be adjusted to user-time on reading or saving |
---|
| 46 | * |
---|
| 47 | * @var array |
---|
| 48 | */ |
---|
| 49 | var $timestamps = array( |
---|
| 50 | 'pm_created','pm_modified','pm_planned_start','pm_planned_end','pm_real_start','pm_real_end', |
---|
| 51 | ); |
---|
| 52 | /** |
---|
| 53 | * Offset in secconds between user and server-time, it need to be add to a server-time to get the user-time |
---|
| 54 | * or substracted from a user-time to get the server-time |
---|
| 55 | * |
---|
| 56 | * @var int |
---|
| 57 | */ |
---|
| 58 | var $tz_offset_s; |
---|
| 59 | /** |
---|
| 60 | * Current time as timestamp in user-time |
---|
| 61 | * |
---|
| 62 | * @var int |
---|
| 63 | */ |
---|
| 64 | var $now_su; |
---|
| 65 | /** |
---|
| 66 | * Instance of the soconstraints-class |
---|
| 67 | * |
---|
| 68 | * @var soconstraints |
---|
| 69 | */ |
---|
| 70 | var $constraints; |
---|
| 71 | /** |
---|
| 72 | * Instance of the somilestones-class |
---|
| 73 | * |
---|
| 74 | * @var somilestones |
---|
| 75 | */ |
---|
| 76 | var $milestones; |
---|
| 77 | /** |
---|
| 78 | * Instance of the soroles-class, not instanciated automatic! |
---|
| 79 | * |
---|
| 80 | * @var soroles |
---|
| 81 | */ |
---|
| 82 | var $roles; |
---|
| 83 | /** |
---|
| 84 | * Atm. projectmanager-admins are identical to eGW admins, this might change in the future |
---|
| 85 | * |
---|
| 86 | * @var boolean |
---|
| 87 | */ |
---|
| 88 | var $is_admin; |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Constructor, calls the constructor of the extended class |
---|
| 92 | * |
---|
| 93 | * @param int $pm_id id of the project to load, default null |
---|
| 94 | * @param string $instanciate='' comma-separated: constraints,milestones,roles |
---|
| 95 | * @return boprojectmanager |
---|
| 96 | */ |
---|
| 97 | function boprojectmanager($pm_id=null,$instanciate='') |
---|
| 98 | { |
---|
| 99 | if ((int) $this->debug >= 3 || $this->debug == 'projectmanager') $this->debug_message(function_backtrace()."\nboprojectmanager::boprojectmanager($pm_id) started"); |
---|
| 100 | |
---|
| 101 | if (!is_object($GLOBALS['phpgw']->datetime)) |
---|
| 102 | { |
---|
| 103 | $GLOBALS['phpgw']->datetime =& CreateObject('phpgwapi.datetime'); |
---|
| 104 | } |
---|
| 105 | $this->tz_offset_s = $GLOBALS['phpgw']->datetime->tz_offset; |
---|
| 106 | $this->now_su = time() + $this->tz_offset_s; |
---|
| 107 | |
---|
| 108 | $this->soprojectmanager($pm_id); |
---|
| 109 | |
---|
| 110 | // save us in $GLOBALS['boprojectselements'] for ExecMethod used in hooks |
---|
| 111 | if (!is_object($GLOBALS['boprojectmanager'])) |
---|
| 112 | { |
---|
| 113 | $GLOBALS['boprojectmanager'] =& $this; |
---|
| 114 | } |
---|
| 115 | // instanciation of link-class has to be after making us globaly availible, as it calls us to get the search_link |
---|
| 116 | if (!is_object($GLOBALS['phpgw']->link)) |
---|
| 117 | { |
---|
| 118 | $GLOBALS['phpgw']->link =& CreateObject('phpgwapi.bolink'); |
---|
| 119 | } |
---|
| 120 | $this->link =& $GLOBALS['phpgw']->link; |
---|
| 121 | $this->links_table = $this->link->link_table; |
---|
| 122 | |
---|
| 123 | // atm. projectmanager-admins are identical to eGW admins, this might change in the future |
---|
| 124 | $this->is_admin = isset($GLOBALS['phpgw_info']['user']['apps']['admin']); |
---|
| 125 | |
---|
| 126 | if ($instanciate) $this->instanciate($instanciate); |
---|
| 127 | |
---|
| 128 | if ((int) $this->debug >= 3 || $this->debug == 'projectmanager') $this->debug_message("boprojectmanager::boprojectmanager($pm_id) finished"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Instanciates some classes which dont get instanciated by default |
---|
| 133 | * |
---|
| 134 | * @param string $instanciate comma-separated: constraints,milestones,roles |
---|
| 135 | * @param string $pre='so' class prefix to use, default so |
---|
| 136 | */ |
---|
| 137 | function instanciate($instanciate,$pre='so') |
---|
| 138 | { |
---|
| 139 | foreach(explode(',',$instanciate) as $class) |
---|
| 140 | { |
---|
| 141 | if (!is_object($this->$class)) |
---|
| 142 | { |
---|
| 143 | $this->$class =& CreateObject('projectmanager.'.$pre.$class); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Summarize the information of all elements of a project: min(start-time), sum(time), avg(completion), ... |
---|
| 150 | * |
---|
| 151 | * This is implemented in the projectelements class, we call it via ExecMethod |
---|
| 152 | * |
---|
| 153 | * @param int/array $pm_id=null int project-id, array of project-id's or null to use $this->pm_id |
---|
| 154 | * @return array/boolean with summary information (keys as for a single project-element), false on error |
---|
| 155 | */ |
---|
| 156 | function pe_summary($pm_id=null) |
---|
| 157 | { |
---|
| 158 | if (is_null($pm_id)) $pm_id = $this->data['pm_id']; |
---|
| 159 | |
---|
| 160 | if (!$pm_id) return array(); |
---|
| 161 | |
---|
| 162 | return ExecMethod('projectmanager.boprojectelements.summary',$pm_id); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * update a project after a change in one of it's project-elements |
---|
| 167 | * |
---|
| 168 | * If the data and the exact changes gets supplied (see params), |
---|
| 169 | * an whole update or even the update itself might be avoided. |
---|
| 170 | * Not used at the moment! |
---|
| 171 | * |
---|
| 172 | * @param int $pm_id=null project-id or null to use $this->data['pm_id'] |
---|
| 173 | * @param int $update_necessary=-1 which fields need updating, or'ed PM_ constants from the datasource class |
---|
| 174 | * @param array $data=null data of the project-element if availible |
---|
| 175 | */ |
---|
| 176 | function update($pm_id=null,$update_necessary=-1,$data=null) |
---|
| 177 | { |
---|
| 178 | if (!$pm_id) |
---|
| 179 | { |
---|
| 180 | $pm_id = $this->data['pm_id']; |
---|
| 181 | } |
---|
| 182 | elseif ($pm_id != $this->data['pm_id']) |
---|
| 183 | { |
---|
| 184 | // we need to restore it later |
---|
| 185 | $save_data = $this->data; |
---|
| 186 | |
---|
| 187 | $this->read(array('pm_id' => $pm_id)); |
---|
| 188 | } |
---|
| 189 | $pe_summary = $this->pe_summary($pm_id); |
---|
| 190 | |
---|
| 191 | if ((int) $this->debug >= 2 || $this->debug == 'update') $this->debug_message("boprojectmanager::update($pm_id) pe_summary=".print_r($pe_summary,true)); |
---|
| 192 | |
---|
| 193 | if (!$this->pe_name2id) |
---|
| 194 | { |
---|
| 195 | // we need the PM_ id's |
---|
| 196 | include_once(PHPGW_INCLUDE_ROOT.'/projectmanager/inc/class.datasource.inc.php'); |
---|
| 197 | |
---|
| 198 | $ds =& new datasource(); |
---|
| 199 | $this->pe_name2id = $ds->name2id; |
---|
| 200 | unset($ds); |
---|
| 201 | } |
---|
| 202 | $save_necessary = false; |
---|
| 203 | foreach($this->pe_name2id as $name => $id) |
---|
| 204 | { |
---|
| 205 | $pm_name = str_replace('pe_','pm_',$name); |
---|
| 206 | if (!($this->data['pm_overwrite'] & $id) && $this->data[$pm_name] != $pe_summary[$name]) |
---|
| 207 | { |
---|
| 208 | $this->data[$pm_name] = $pe_summary[$name]; |
---|
| 209 | $save_necessary = true; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | if ($save_necessary) |
---|
| 213 | { |
---|
| 214 | $this->save(null,false); // dont touch modification date |
---|
| 215 | } |
---|
| 216 | // restore $this->data |
---|
| 217 | if (is_array($save_data) && $save_data['pm_id']) |
---|
| 218 | { |
---|
| 219 | $this->data = $save_data; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * saves a project |
---|
| 225 | * |
---|
| 226 | * reimplemented to automatic create a project-ID / pm_number, if empty |
---|
| 227 | * |
---|
| 228 | * @param array $keys if given $keys are copied to data before saveing => allows a save as |
---|
| 229 | * @param boolean $touch_modified=true should modification date+user be set, default yes |
---|
| 230 | * @param boolean $do_notify=true should link::notify be called, default yes |
---|
| 231 | * @return int 0 on success and errno != 0 else |
---|
| 232 | */ |
---|
| 233 | function save($keys=null,$touch_modified=true,$do_notify=true) |
---|
| 234 | { |
---|
| 235 | if ($keys) $this->data_merge($keys); |
---|
| 236 | |
---|
| 237 | // check if we have a project-ID and generate one if not |
---|
| 238 | if (empty($this->data['pm_number'])) |
---|
| 239 | { |
---|
| 240 | $this->generate_pm_number(); |
---|
| 241 | } |
---|
| 242 | // set creation and modification data |
---|
| 243 | if (!$this->data['pm_id']) |
---|
| 244 | { |
---|
| 245 | $this->data['pm_creator'] = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
| 246 | $this->data['pm_created'] = $this->now_su; |
---|
| 247 | } |
---|
| 248 | if ($touch_modified) |
---|
| 249 | { |
---|
| 250 | $this->data['pm_modifier'] = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
| 251 | $this->data['pm_modified'] = $this->now_su; |
---|
| 252 | } |
---|
| 253 | if ((int) $this->debug >= 1 || $this->debug == 'save') $this->debug_message("boprojectmanager::save(".print_r($keys,true).",".(int)$touch_modified.") data=".print_r($this->data,true)); |
---|
| 254 | |
---|
| 255 | if (!($err = parent::save()) && $do_notify) |
---|
| 256 | { |
---|
| 257 | // notify the link-class about the update, as other apps may be subscribt to it |
---|
| 258 | $this->link->notify_update('projectmanager',$this->data['pm_id'],$this->data); |
---|
| 259 | } |
---|
| 260 | return $err; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * deletes a project identified by $keys or the loaded one, reimplemented to remove the project-elements too |
---|
| 265 | * |
---|
| 266 | * @param array $keys if given array with col => value pairs to characterise the rows to delete |
---|
| 267 | * @return int affected rows, should be 1 if ok, 0 if an error |
---|
| 268 | */ |
---|
| 269 | function delete($keys=null) |
---|
| 270 | { |
---|
| 271 | if ((int) $this->debug >= 1 || $this->debug == 'delete') $this->debug_message("boprojectmanager::delete(".print_r($keys,true).") this->data[pm_id] = ".$this->data['pm_id']); |
---|
| 272 | |
---|
| 273 | $pm_id = is_null($keys) ? $this->data['pm_id'] : (is_array($keys) ? $keys['pm_id'] : $keys); |
---|
| 274 | |
---|
| 275 | if (($ret = parent::delete($keys)) && $pm_id) |
---|
| 276 | { |
---|
| 277 | ExecMethod('projectmanager.boprojectelements.delete',array('pm_id' => $pm_id)); |
---|
| 278 | |
---|
| 279 | // the following is not really necessary, as it's already one in boprojectelements::delete |
---|
| 280 | // delete all links to project $pm_id |
---|
| 281 | $this->link->unlink(0,'projectmanager',$pm_id); |
---|
| 282 | |
---|
| 283 | $this->instanciate('constraints,milestones,pricelist,roles'); |
---|
| 284 | |
---|
| 285 | // delete all constraints of the project |
---|
| 286 | $this->constraints->delete(array('pm_id' => $pm_id)); |
---|
| 287 | |
---|
| 288 | // delete all milestones of the project |
---|
| 289 | $this->milestones->delete(array('pm_id' => $pm_id)); |
---|
| 290 | |
---|
| 291 | // delete all pricelist items of the project |
---|
| 292 | $this->pricelist->delete(array('pm_id' => $pm_id)); |
---|
| 293 | |
---|
| 294 | // delete all project specific roles |
---|
| 295 | $this->roles->delete(array('pm_id' => $pm_id)); |
---|
| 296 | } |
---|
| 297 | return $ret; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | /** |
---|
| 301 | * changes the data from the db-format to your work-format |
---|
| 302 | * |
---|
| 303 | * reimplemented to adjust the timezone of the timestamps (adding $this->tz_offset_s to get user-time) |
---|
| 304 | * Please note, we do NOT call the method of the parent or so_sql !!! |
---|
| 305 | * |
---|
| 306 | * @param array $data if given works on that array and returns result, else works on internal data-array |
---|
| 307 | * @return array with changed data |
---|
| 308 | */ |
---|
| 309 | function db2data($data=null) |
---|
| 310 | { |
---|
| 311 | if (!is_array($data)) |
---|
| 312 | { |
---|
| 313 | $data = &$this->data; |
---|
| 314 | } |
---|
| 315 | foreach($this->timestamps as $name) |
---|
| 316 | { |
---|
| 317 | if (isset($data[$name]) && $data[$name]) $data[$name] += $this->tz_offset_s; |
---|
| 318 | } |
---|
| 319 | if (is_numeric($data['pm_completion'])) $data['pm_completion'] .= '%'; |
---|
| 320 | |
---|
| 321 | return $data; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | * changes the data from your work-format to the db-format |
---|
| 326 | * |
---|
| 327 | * reimplemented to adjust the timezone of the timestamps (subtraction $this->tz_offset_s to get server-time) |
---|
| 328 | * Please note, we do NOT call the method of the parent or so_sql !!! |
---|
| 329 | * |
---|
| 330 | * @param array $data if given works on that array and returns result, else works on internal data-array |
---|
| 331 | * @return array with changed data |
---|
| 332 | */ |
---|
| 333 | function data2db($data=null) |
---|
| 334 | { |
---|
| 335 | if ($intern = !is_array($data)) |
---|
| 336 | { |
---|
| 337 | $data = &$this->data; |
---|
| 338 | } |
---|
| 339 | foreach($this->timestamps as $name) |
---|
| 340 | { |
---|
| 341 | if (isset($data[$name]) && $data[$name]) $data[$name] -= $this->tz_offset_s; |
---|
| 342 | } |
---|
| 343 | if (substr($data['pm_completion'],-1) == '%') $data['pm_completion'] = (int) round(substr($data['pm_completion'],0,-1)); |
---|
| 344 | |
---|
| 345 | return $data; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * generate a project-ID / pm_number in the form P-YYYY-nnnn (YYYY=year, nnnn=incrementing number) |
---|
| 350 | * |
---|
| 351 | * @param boolean $set_data=true set generated number in $this->data, default true |
---|
| 352 | * @param string $parent='' pm_number of parent, if given a /nnnn is added |
---|
| 353 | * @return string the new pm_number |
---|
| 354 | */ |
---|
| 355 | function generate_pm_number($set_data=true,$parent='') |
---|
| 356 | { |
---|
| 357 | $n = 1; |
---|
| 358 | do |
---|
| 359 | { |
---|
| 360 | if ($parent) |
---|
| 361 | { |
---|
| 362 | $pm_number = sprintf('%s/%04d',$parent,$n++); |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | { |
---|
| 366 | $pm_number = sprintf('P-%04d-%04d',date('Y'),$n++); |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | while ($this->not_unique(array('pm_number' => $pm_number))); |
---|
| 370 | |
---|
| 371 | if ($set_data) $this->data['pm_number'] = $pm_number; |
---|
| 372 | |
---|
| 373 | return $pm_number; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * checks if the user has enough rights for a certain operation |
---|
| 378 | * |
---|
| 379 | * Rights are given via owner grants or role based acl |
---|
| 380 | * |
---|
| 381 | * @param int $required PHPGW_ACL_READ, PHPGW_ACL_WRITE, PHPGW_ACL_ADD, PHPGW_ACL_DELETE, PHPGW_ACL_BUDGET, PHPGW_ACL_EDIT_BUDGET |
---|
| 382 | * @param array/int $data=null project or project-id to use, default the project in $this->data |
---|
| 383 | * @param boolean $no_cache=false should a cached value be used, if availible, or not |
---|
| 384 | * @return boolean true if the rights are ok, false if not or null if entry not found |
---|
| 385 | */ |
---|
| 386 | function check_acl($required,$data=0,$no_cache=false) |
---|
| 387 | { |
---|
| 388 | static $rights = array(); |
---|
| 389 | $pm_id = (!$data ? $this->data['pm_id'] : (is_array($data) ? $data['pm_id'] : $data)); |
---|
| 390 | |
---|
| 391 | if (!$pm_id) // new entry, everything allowed, but delete |
---|
| 392 | { |
---|
| 393 | return $required != PHPGW_ACL_DELETE; |
---|
| 394 | } |
---|
| 395 | if (!isset($rights[$pm_id]) || $no_cache) // check if we have a cache entry for $pm_id |
---|
| 396 | { |
---|
| 397 | if ($data) |
---|
| 398 | { |
---|
| 399 | if (!is_array($data)) |
---|
| 400 | { |
---|
| 401 | $data_backup =& $this->data; unset($this->data); |
---|
| 402 | $data =& $this->read($data); |
---|
| 403 | $this->data =& $data_backup; unset($data_backup); |
---|
| 404 | |
---|
| 405 | if (!$data) return null; // $pm_id not found ==> no rights |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | else |
---|
| 409 | { |
---|
| 410 | $data =& $this->data; |
---|
| 411 | } |
---|
| 412 | // rights come from owner grants or role based acl |
---|
| 413 | $rights[$pm_id] = (int) $this->grants[$data['pm_creator']] | (int) $data['role_acl']; |
---|
| 414 | |
---|
| 415 | // for status or times accounting-type (no accounting) remove the budget-rights from everyone |
---|
| 416 | if ($data['pm_accounting_type'] == 'status' || $data['pm_accounting_type'] == 'times') |
---|
| 417 | { |
---|
| 418 | $rights[$pm_id] &= ~(PHPGW_ACL_BUDGET | PHPGW_ACL_EDIT_BUDGET); |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | if ((int) $this->debug >= 2 || $this->debug == 'check_acl') $this->debug_message("boprojectmanager::check_acl($required,pm_id=$pm_id) rights[$pm_id]=".$rights[$pm_id]); |
---|
| 422 | |
---|
| 423 | if ($required == PHPGW_ACL_READ) // read-rights are implied by all other rights |
---|
| 424 | { |
---|
| 425 | return (boolean) $rights[$pm_id]; |
---|
| 426 | } |
---|
| 427 | if ($required == PHPGW_ACL_BUDGET) $required |= PHPGW_ACL_EDIT_BUDGET; // EDIT_BUDGET implies BUDGET |
---|
| 428 | |
---|
| 429 | return (boolean) ($rights[$pm_id] & $required); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | * Read a project |
---|
| 434 | * |
---|
| 435 | * reimplemented to add an acl check |
---|
| 436 | * |
---|
| 437 | * @param array $keys |
---|
| 438 | * @return array/boolean array with project, null if project not found or false if no perms to view it |
---|
| 439 | */ |
---|
| 440 | function read($keys) |
---|
| 441 | { |
---|
| 442 | if (!parent::read($keys)) |
---|
| 443 | { |
---|
| 444 | return null; |
---|
| 445 | } |
---|
| 446 | if (!$this->check_acl(PHPGW_ACL_READ)) |
---|
| 447 | { |
---|
| 448 | return false; |
---|
| 449 | } |
---|
| 450 | return $this->data; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | /** |
---|
| 454 | * get title for an project identified by $entry |
---|
| 455 | * |
---|
| 456 | * Is called as hook to participate in the linking |
---|
| 457 | * |
---|
| 458 | * @param int/array $entry int pm_id or array with project entry |
---|
| 459 | * @param string/boolean string with title, null if project not found or false if no perms to view it |
---|
| 460 | */ |
---|
| 461 | function link_title( $entry ) |
---|
| 462 | { |
---|
| 463 | if (!is_array($entry)) |
---|
| 464 | { |
---|
| 465 | $entry = $this->read( $entry ); |
---|
| 466 | } |
---|
| 467 | if (!$entry) |
---|
| 468 | { |
---|
| 469 | return $entry; |
---|
| 470 | } |
---|
| 471 | return $entry['pm_number'].': '.$entry['pm_title']; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | /** |
---|
| 475 | * query projectmanager for entries matching $pattern |
---|
| 476 | * |
---|
| 477 | * Is called as hook to participate in the linking |
---|
| 478 | * |
---|
| 479 | * @param string $pattern pattern to search |
---|
| 480 | * @return array with pm_id - title pairs of the matching entries |
---|
| 481 | */ |
---|
| 482 | function link_query( $pattern ) |
---|
| 483 | { |
---|
| 484 | $criteria = array(); |
---|
| 485 | foreach(array('pm_number','pm_title','pm_description') as $col) |
---|
| 486 | { |
---|
| 487 | $criteria[$col] = $pattern; |
---|
| 488 | } |
---|
| 489 | $result = array(); |
---|
| 490 | foreach((array) $this->search($criteria,false,'pm_number','','%',false,'OR',false,array('pm_status'=>'active')) as $prj ) |
---|
| 491 | { |
---|
| 492 | if ($prj['pm_id']) $result[$prj['pm_id']] = $this->link_title($prj); |
---|
| 493 | } |
---|
| 494 | return $result; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | /** |
---|
| 498 | * Hook called by link-class to include projectmanager in the appregistry of the linkage |
---|
| 499 | * |
---|
| 500 | * @param array/string $location location and other parameters (not used) |
---|
| 501 | * @return array with method-names |
---|
| 502 | */ |
---|
| 503 | function search_link($location) |
---|
| 504 | { |
---|
| 505 | return array( |
---|
| 506 | 'query' => 'projectmanager.boprojectmanager.link_query', |
---|
| 507 | 'title' => 'projectmanager.boprojectmanager.link_title', |
---|
| 508 | 'view' => array( |
---|
| 509 | 'menuaction' => 'projectmanager.uiprojectelements.index', |
---|
| 510 | ), |
---|
| 511 | 'view_id' => 'pm_id', |
---|
| 512 | 'notify' => 'projectmanager.boprojectelements.notify', |
---|
| 513 | 'add' => array( |
---|
| 514 | 'menuaction' => 'projectmanager.uiprojectmanager.edit', |
---|
| 515 | ), |
---|
| 516 | 'add_app' => 'link_app', |
---|
| 517 | 'add_id' => 'link_id', |
---|
| 518 | ); |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | /** |
---|
| 522 | * gets all ancestors of a given project (calls itself recursivly) |
---|
| 523 | * |
---|
| 524 | * A project P is the parent of an other project C, if link_id1=P.pm_id and link_id2=C.pm_id ! |
---|
| 525 | * To get all parents of a project C, we use all links to the project, which link_id2=C.pm_id. |
---|
| 526 | * |
---|
| 527 | * @param int $pm_id=0 id or 0 to use $this->pm_id |
---|
| 528 | * @param array $ancestors=array() already identified ancestors, default none |
---|
| 529 | * @return array with ancestors |
---|
| 530 | */ |
---|
| 531 | function &ancestors($pm_id=0,$ancestors=array()) |
---|
| 532 | { |
---|
| 533 | static $ancestors_cache = array(); // some caching |
---|
| 534 | |
---|
| 535 | if (!$pm_id && !($pm_id = $this->pm_id)) return false; |
---|
| 536 | |
---|
| 537 | if (!isset($ancestors_cache[$pm_id])) |
---|
| 538 | { |
---|
| 539 | $ancestors_cache[$pm_id] = array(); |
---|
| 540 | |
---|
| 541 | // read all projectmanager entries attached to this one |
---|
| 542 | foreach($this->link->get_links('projectmanager',$pm_id,'projectmanager') as $link_id => $data) |
---|
| 543 | { |
---|
| 544 | // we need to read the complete link, to know if the entry is a child (link_id1 == pm_id) |
---|
| 545 | $link = $this->link->get_link($link_id); |
---|
| 546 | if ($link['link_id1'] == $pm_id) |
---|
| 547 | { |
---|
| 548 | continue; // we are the parent in this link ==> ignore it |
---|
| 549 | } |
---|
| 550 | $parent = (int) $link['link_id1']; |
---|
| 551 | if (!in_array($parent,$ancestors_cache[$pm_id])) |
---|
| 552 | { |
---|
| 553 | $ancestors_cache[$pm_id][] = $parent; |
---|
| 554 | // now we call ourself recursivly to get all parents of the parents |
---|
| 555 | $ancestors_cache[$pm_id] =& $this->ancestors($parent,$ancestors_cache[$pm_id]); |
---|
| 556 | } |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | //echo "<p>ancestors($pm_id)=".print_r($ancestors_cache[$pm_id],true)."</p>\n"; |
---|
| 560 | return array_merge($ancestors,$ancestors_cache[$pm_id]); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | /** |
---|
| 564 | * gets recursive all children (only projects) of a given project (calls itself recursivly) |
---|
| 565 | * |
---|
| 566 | * A project P is the parent of an other project C, if link_id1=P.pm_id and link_id2=C.pm_id ! |
---|
| 567 | * To get all children of a project C, we use all links to the project, which link_id1=C.pm_id. |
---|
| 568 | * |
---|
| 569 | * @param int $pm_id=0 id or 0 to use $this->pm_id |
---|
| 570 | * @param array $children=array() already identified ancestors, default none |
---|
| 571 | * @return array with children |
---|
| 572 | */ |
---|
| 573 | function &children($pm_id=0,$children=array()) |
---|
| 574 | { |
---|
| 575 | static $children_cache = array(); // some caching |
---|
| 576 | |
---|
| 577 | if (!$pm_id && !($pm_id = $this->pm_id)) return false; |
---|
| 578 | |
---|
| 579 | if (!isset($children_cache[$pm_id])) |
---|
| 580 | { |
---|
| 581 | $children_cache[$pm_id] = array(); |
---|
| 582 | |
---|
| 583 | // read all projectmanager entries attached to this one |
---|
| 584 | foreach($this->link->get_links('projectmanager',$pm_id,'projectmanager') as $link_id => $data) |
---|
| 585 | { |
---|
| 586 | // we need to read the complete link, to know if the entry is a child (link_id1 == pm_id) |
---|
| 587 | $link = $this->link->get_link($link_id); |
---|
| 588 | if ($link['link_id1'] != $pm_id) |
---|
| 589 | { |
---|
| 590 | continue; // we are NOT the parent in this link ==> ignore it |
---|
| 591 | } |
---|
| 592 | $child = (int) $link['link_id2']; |
---|
| 593 | if (!in_array($child,$children_cache[$pm_id])) |
---|
| 594 | { |
---|
| 595 | $children_cache[$pm_id][] = $child; |
---|
| 596 | // now we call ourself recursivly to get all parents of the parents |
---|
| 597 | $children_cache[$pm_id] =& $this->children($child,$children_cache[$pm_id]); |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | } |
---|
| 601 | //echo "<p>children($pm_id)=".print_r($children_cache[$pm_id],true)."</p>\n"; |
---|
| 602 | return array_merge($children,$children_cache[$pm_id]); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | /** |
---|
| 606 | * Query the project-tree from the DB, project tree is indexed by a path consisting of pm_id's delimited by slashes (/) |
---|
| 607 | * |
---|
| 608 | * @param array $filter=array('pm_status' => 'active') filter for the search, default active projects |
---|
| 609 | * @param string $filter_op='AND' AND or OR filters together, default AND |
---|
| 610 | * @return array with path => array(pm_id,pm_number,pm_title,pm_parent) pairs |
---|
| 611 | */ |
---|
| 612 | function get_project_tree($filter = array('pm_status' => 'active'),$filter_op='AND') |
---|
| 613 | { |
---|
| 614 | $projects = array(); |
---|
| 615 | $parents = 'mains'; |
---|
| 616 | // get the children |
---|
| 617 | while (($children = $this->search($filter,$GLOBALS['boprojectmanager']->table_name.'.pm_id AS pm_id,pm_number,pm_title,link_id1 AS pm_parent', |
---|
| 618 | 'pm_status,pm_number','','',false,$filter_op,false,array('subs_or_mains' => $parents)))) |
---|
| 619 | { |
---|
| 620 | //echo $parents == 'mains' ? "Mains" : "Children of ".implode(',',$parents); _debug_array($children); |
---|
| 621 | |
---|
| 622 | // sort the children behind the parents |
---|
| 623 | $parents = $both = array(); |
---|
| 624 | foreach ($projects as $parent) |
---|
| 625 | { |
---|
| 626 | $both[$parent['path']] = $parent; |
---|
| 627 | |
---|
| 628 | foreach($children as $key => $child) |
---|
| 629 | { |
---|
| 630 | if ($child['pm_parent'] == $parent['pm_id']) |
---|
| 631 | { |
---|
| 632 | $child['path'] = $parent['path'] . '/' . $child['pm_id']; |
---|
| 633 | $both[$child['path']] = $child; |
---|
| 634 | $parents[] = $child['pm_id']; |
---|
| 635 | unset($children[$key]); |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | } |
---|
| 639 | // mains or orphans |
---|
| 640 | foreach ($children as $child) |
---|
| 641 | { |
---|
| 642 | $child['path'] = '/' . $child['pm_id']; |
---|
| 643 | $both[$child['path']] = $child; |
---|
| 644 | $parents[] = $child['pm_id']; |
---|
| 645 | |
---|
| 646 | } |
---|
| 647 | $projects = $both; |
---|
| 648 | } |
---|
| 649 | //echo "tree"; _debug_array($projects); |
---|
| 650 | return $projects; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | /** |
---|
| 654 | * write a debug-message to the log-file $this->logfile (if set) |
---|
| 655 | * |
---|
| 656 | * @param string $msg |
---|
| 657 | */ |
---|
| 658 | function log2file($msg) |
---|
| 659 | { |
---|
| 660 | if ($this->logfile && ($f = @fopen($this->logfile,'a+'))) |
---|
| 661 | { |
---|
| 662 | fwrite($f,date('Y-m-d H:i:s: ').$GLOBALS['phpgw']->common->grab_owner_name($GLOBALS['phpgw_info']['user']['account_id'])."\n"); |
---|
| 663 | fwrite($f,$msg."\n\n"); |
---|
| 664 | fclose($f); |
---|
| 665 | } |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | /** |
---|
| 669 | * EITHER echos a (preformatted / no-html) debug-message OR logs it to a file |
---|
| 670 | * |
---|
| 671 | * @param string $msg |
---|
| 672 | */ |
---|
| 673 | function debug_message($msg) |
---|
| 674 | { |
---|
| 675 | $msg = 'Backtrace: '.function_backtrace(2)."\n".$msg; |
---|
| 676 | |
---|
| 677 | if (!$this->logfile) |
---|
| 678 | { |
---|
| 679 | echo '<pre>'.$msg."</pre>\n"; |
---|
| 680 | } |
---|
| 681 | else |
---|
| 682 | { |
---|
| 683 | $this->log2file($msg); |
---|
| 684 | } |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | /** |
---|
| 688 | * Add a timespan to a given datetime, taking into account the availibility and worktimes of the user |
---|
| 689 | * |
---|
| 690 | * ToDo: take exclusivly blocked times (calendar) into account |
---|
| 691 | * |
---|
| 692 | * @param int $start start timestamp (usertime) |
---|
| 693 | * @param int $time working time in minutes to add, 0 advances to the next working time |
---|
| 694 | * @param int $uid user-id |
---|
| 695 | * @return int/boolean end-time or false if it cant be calculated because user has no availibility or worktime |
---|
| 696 | */ |
---|
| 697 | function date_add($start,$time,$uid) |
---|
| 698 | { |
---|
| 699 | // we cache the user-prefs with the working times globaly, as they are expensive to read |
---|
| 700 | $user_prefs =& $GLOBALS['phpgw_info']['projectmanager']['user_prefs'][$uid]; |
---|
| 701 | if (!is_array($user_prefs)) |
---|
| 702 | { |
---|
| 703 | if ($uid == $GLOBALS['phpgw_info']['user']['account_id']) |
---|
| 704 | { |
---|
| 705 | $user_prefs = $GLOBALS['phpgw_info']['user']['preferences']['projectmanager']; |
---|
| 706 | } |
---|
| 707 | else |
---|
| 708 | { |
---|
| 709 | $prefs =& CreateObject('phpgwapi.preferences',$uid); |
---|
| 710 | $prefs->read_repository(); |
---|
| 711 | $user_prefs =& $prefs->data['projectmanager']; |
---|
| 712 | unset($prefs); |
---|
| 713 | } |
---|
| 714 | // calculate total weekly worktime |
---|
| 715 | for($day=$user_prefs['duration']=0; $day <= 6; ++$day) |
---|
| 716 | { |
---|
| 717 | $user_prefs['duration'] += $user_prefs['duration_'.$day]; |
---|
| 718 | } |
---|
| 719 | } |
---|
| 720 | $availibility = 1.0; |
---|
| 721 | if (isset($this->data['pm_members'][$uid])) |
---|
| 722 | { |
---|
| 723 | $availibility = $this->data['pm_members'][$uid]['member_availibility'] / 100.0; |
---|
| 724 | } |
---|
| 725 | $general = $this->get_availibility($uid); |
---|
| 726 | if (isset($general[$uid])) |
---|
| 727 | { |
---|
| 728 | $availibility *= $general[$uid] / 100.0; |
---|
| 729 | } |
---|
| 730 | // if user has no availibility or no working duration ==> fail |
---|
| 731 | if (!$availibility || !$user_prefs['duration']) |
---|
| 732 | { |
---|
| 733 | return false; |
---|
| 734 | } |
---|
| 735 | $time_s = $time * 60 / $availibility; |
---|
| 736 | |
---|
| 737 | if (!is_object($this->bocal)) |
---|
| 738 | { |
---|
| 739 | $this->bocal =& CreateObject('calendar.bocal'); |
---|
| 740 | } |
---|
| 741 | $events =& $this->bocal->search(array( |
---|
| 742 | 'start' => $start, |
---|
| 743 | 'end' => $start+max(10*$time,30*24*60*60), |
---|
| 744 | 'users' => $uid, |
---|
| 745 | 'show_rejected' => false, |
---|
| 746 | 'ignore_acl' => true, |
---|
| 747 | )); |
---|
| 748 | if ($events) $event = array_shift($events); |
---|
| 749 | |
---|
| 750 | $end_s = $start; |
---|
| 751 | // we use do-while to allow with time=0 to advance to the next working time |
---|
| 752 | do { |
---|
| 753 | // ignore non-blocking events or events already over |
---|
| 754 | while ($event && ($event['non_blocking'] || $event['end'] <= $end_s)) |
---|
| 755 | { |
---|
| 756 | //echo "<p>ignoring event $event[title]: ".date('Y-m-d H:i',$event['start'])."</p>\n"; |
---|
| 757 | $event = array_shift($events); |
---|
| 758 | } |
---|
| 759 | $day = date('w',$end_s); // 0=Sun, 1=Mon, ... |
---|
| 760 | $work_start_s = $user_prefs['start_'.$day] * 60; |
---|
| 761 | $max_add_s = 60 * $user_prefs['duration_'.$day]; |
---|
| 762 | $time_of_day_s = $end_s - mktime(0,0,0,date('m',$end_s),date('d',$end_s),date('Y',$end_s)); |
---|
| 763 | |
---|
| 764 | // befor workday starts ==> go to start of workday |
---|
| 765 | if ($max_add_s && $time_of_day_s < $work_start_s) |
---|
| 766 | { |
---|
| 767 | $end_s += $work_start_s - $time_of_day_s; |
---|
| 768 | } |
---|
| 769 | // after workday ends or non-working day ==> go to start of NEXT workday |
---|
| 770 | elseif (!$max_add_s || $time_of_day_s >= $work_start_s+$max_add_s) // after workday ends |
---|
| 771 | { |
---|
| 772 | //echo date('D Y-m-d H:i',$end_s)." ==> go to next day: work_start_s=$work_start_s, time_of_day_s=$time_of_day_s, max_add_s=$max_add_s<br>\n"; |
---|
| 773 | do { |
---|
| 774 | $day = ($day+1) % 7; |
---|
| 775 | $end_s = mktime($user_prefs['start_'.$day]/60,$user_prefs['start_'.$day]%60,0,date('m',$end_s),date('d',$end_s)+1,date('Y',$end_s)); |
---|
| 776 | } while (!($max_add_s = 60 * $user_prefs['duration_'.$day])); |
---|
| 777 | } |
---|
| 778 | // in the working period ==> adjust max_add_s accordingly |
---|
| 779 | else |
---|
| 780 | { |
---|
| 781 | $max_add_s -= $time_of_day_s - $work_start_s; |
---|
| 782 | } |
---|
| 783 | $add_s = min($max_add_s,$time_s); |
---|
| 784 | |
---|
| 785 | //echo date('D Y-m-d H:i',$end_s)." + ".($add_s/60/60)."h / ".($time_s/60/60)."h<br>\n"; |
---|
| 786 | |
---|
| 787 | if ($event) |
---|
| 788 | { |
---|
| 789 | //echo "<p>checking event $event[title] (".date('Y-m-d H:i',$event['start']).") against end_s=$end_s=".date('Y-m-d H:i',$end_s)." + add_s=$add_s</p>\n"; |
---|
| 790 | if ($end_s+$add_s > $event['start']) // event overlaps added period |
---|
| 791 | { |
---|
| 792 | $time_s -= max(0,$event['start'] - $end_s); // add only time til events starts (if any) |
---|
| 793 | $end_s = $event['end']; // set time for further calculation to event end |
---|
| 794 | //echo "<p>==> event overlaps: time_s=$time_s, end_s=$end_s now</p>\n"; |
---|
| 795 | $event = array_shift($events); // advance to next event |
---|
| 796 | continue; |
---|
| 797 | } |
---|
| 798 | } |
---|
| 799 | $end_s += $add_s; |
---|
| 800 | $time_s -= $add_s; |
---|
| 801 | } while ($time_s > 0); |
---|
| 802 | |
---|
| 803 | if ((int) $this->debug >= 3 || $this->debug == 'date_add') $this->debug_message("boprojectmanager::date_add($start=".date('D Y-m-d H:i',$start).", $time=".($time/60.0)."h, $uid)=".date('D Y-m-d H:i',$end_s)); |
---|
| 804 | |
---|
| 805 | return $end_s; |
---|
| 806 | } |
---|
| 807 | |
---|
| 808 | /** |
---|
| 809 | * Copies a project |
---|
| 810 | * |
---|
| 811 | * @param int $source id of project to copy |
---|
| 812 | * @param int $only_stage=0 0=both stages plus saving the project, 1=copy of the project, 2=copying the element tree |
---|
| 813 | * @param string $parent_number='' number of the parent project, to create a sub-project-number |
---|
| 814 | * @return int/boolean successful copy new pm_id or true if $only_stage==1, false otherwise (eg. permission denied) |
---|
| 815 | */ |
---|
| 816 | function copy($source,$only_stage=0,$parent_number='') |
---|
| 817 | { |
---|
| 818 | if ((int) $this->debug >= 1 || $this->debug == 'copy') $this->debug_message("boprojectmanager::copy($source,$only_stage)"); |
---|
| 819 | |
---|
| 820 | if ($only_stage == 2) |
---|
| 821 | { |
---|
| 822 | if (!(int)$this->data['pm_id']) return false; |
---|
| 823 | |
---|
| 824 | $data_backup = $this->data; |
---|
| 825 | } |
---|
| 826 | if (!$this->read((int) $source) || !$this->check_acl(PHPGW_ACL_READ)) |
---|
| 827 | { |
---|
| 828 | if ((int) $this->debug >= 1 || $this->debug == 'copy') $this->debug_message("boprojectmanager::copy($source,$only_stage) returning false (not found or no perms), data=".print_r($this->data,true)); |
---|
| 829 | return false; |
---|
| 830 | } |
---|
| 831 | if ($only_stage == 2) |
---|
| 832 | { |
---|
| 833 | $this->data = $data_backup; |
---|
| 834 | unset($data_backup); |
---|
| 835 | } |
---|
| 836 | else |
---|
| 837 | { |
---|
| 838 | // if user has no budget rights on the source, we need to unset the budget fields |
---|
| 839 | if ($this->check_acl(PHPGW_ACL_BUDGET)) |
---|
| 840 | { |
---|
| 841 | include_once(PHPGW_INCLUDE_ROOT.'/projectmanager/inc/class.datasource.inc.php'); |
---|
| 842 | foreach(array(PM_PLANNED_BUDGET => 'pm_planned_budget',PM_USED_BUDGET => 'pm_used_budget') as $id => $key) |
---|
| 843 | { |
---|
| 844 | unset($this->data[$key]); |
---|
| 845 | $this->data['pm_overwrite'] &= ~$id; |
---|
| 846 | } |
---|
| 847 | } |
---|
| 848 | // we unset a view things, as this should be a new project |
---|
| 849 | foreach(array('pm_id','pm_number','pm_creator','pm_created','pm_modified','pm_modifier') as $key) |
---|
| 850 | { |
---|
| 851 | unset($this->data[$key]); |
---|
| 852 | } |
---|
| 853 | $this->data['pm_status'] = 'active'; |
---|
| 854 | |
---|
| 855 | if ($parent_number) $this->generate_pm_number(true,$parent_number); |
---|
| 856 | |
---|
| 857 | if ($only_stage == 1) |
---|
| 858 | { |
---|
| 859 | return true; |
---|
| 860 | } |
---|
| 861 | if ($this->save() != 0) return false; |
---|
| 862 | } |
---|
| 863 | // copying the element tree |
---|
| 864 | $elements =& CreateObject('projectmanager.boprojectelements',$this->data['pm_id']); |
---|
| 865 | |
---|
| 866 | return $elements->copytree((int) $source) ? $elements->pm_id : false; |
---|
| 867 | } |
---|
| 868 | } |
---|