File: ProjectGroup.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (683 lines) | stat: -rw-r--r-- 17,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
<?php
/**
 * GForge Project Management Facility
 *
 * Copyright 2002 GForge, LLC
 * http://gforge.org/
 *
 * @version   $Id: ProjectGroup.class 5200 2006-01-03 11:29:52Z danper $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */
/*
	Project/Task Manager
	By Tim Perdue, Sourceforge, 11/99
	Heavy rewrite by Tim Perdue April 2000

	Total rewrite in OO and GForge coding guidelines 12/2002 by Tim Perdue
*/


require_once('common/include/Error.class');

	/**
	*	Fetches a ProjectGroup object from the database
	*
	* @param group_project_id	the projectgroup id  
	*	@param data	whether or not the db result handle is passed in
	*	@return	the ProjectGroup object
	*/
	function &projectgroup_get_object($group_project_id,$data=false) {
		global $PROJECTGROUP_OBJ;
		if (!isset($PROJECTGROUP_OBJ["_".$group_project_id."_"])) {
			if ($data) {
				//the db result handle was passed in
			} else {
				$res=db_query("SELECT * FROM project_group_list_vw
				WHERE group_project_id='$group_project_id'");
				if (db_numrows($res) <1 ) {
					$PROJECTGROUP_OBJ["_".$group_project_id."_"]=false;
					return false;
				}
				$data =& db_fetch_array($res);
			}
			$Group =& group_get_object($data["group_id"]);
			$PROJECTGROUP_OBJ["_".$group_project_id."_"]= new ProjectGroup($Group,$group_project_id,$data);
		}
		return $PROJECTGROUP_OBJ["_".$group_project_id."_"];
	}


class ProjectGroup extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var	 array   $data_array.
	 */
	var $data_array;

	/**
	 * The Group object.
	 *
	 * @var	 object  $Group.
	 */
	var $Group;
	var $statuses;
	var $categories;
	var $technicians;
	var $current_user_perm;

	/**
	 *  Constructor.
	 *
	 *	@param	object	The Group object to which this forum is associated.
	 *  @param  int	 The group_project_id.
	 *  @param  array	The associative array of data.
	 *	@return	boolean	success.
	 */
	function ProjectGroup(&$Group, $group_project_id=false, $arr=false) {
		$this->Error();
		if (!$Group || !is_object($Group)) {
			$this->setError('ProjectGroup:: No Valid Group Object');
			return false;
		}
		if ($Group->isError()) {
			$this->setError('ProjectGroup:: '.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;

		if ($group_project_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($group_project_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				if ($this->data_array['group_id'] != $this->Group->getID()) {
					$this->setError('Group_id in db result does not match Group Object');
					return false;
				}
			}
			//
			//  Make sure they can even access this object
			//
			if (!$this->userCanView()) {
				$this->setPermissionDeniedError();
				$this->data_array = null;
				return false;
			}
		}
		return true;
	}

	/**
	 *	create - create a new ProjectGroup in the database.
	 *
	 *	@param	string	The project name.
	 *	@param	string	The project description.
	 *	@param	int	Whether it is (1) public or (0) private .
	 *	@param	string	The email address to send new notifications to.
	 *	@return boolean success.
	 */
	function create($project_name,$description,$is_public=1,$send_all_posts_to='') {

		global $Language;
		
		if (strlen($project_name) < 3) {
			$this->setError($Language->getText('pm_projectgroup','error_min_name_length'));
			return false;
		}
		if (strlen($description) < 10) {
			$this->setError($Language->getText('pm_projectgroup','error_min_desc_length'));
			return false;
		}
		if ($send_all_posts_to) {
			$invalid_mails = validate_emails($send_all_posts_to);
			if (count($invalid_mails) > 0) {
				$this->setInvalidEmailError();
				return false;
			}
		}
		
		$perm =& $this->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isPMAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		$sql="INSERT INTO project_group_list (group_id,project_name,is_public,
			description,send_all_posts_to)
			VALUES ('".$this->Group->getId()."','". htmlspecialchars($project_name) ."','$is_public',
			'". htmlspecialchars($description) ."','$send_all_posts_to')";

		db_begin();
		$result=db_query($sql);
		if (!$result) {
			db_rollback();
			$this->setError('Error Adding ProjectGroup: '.db_error());
			return false;
		}
		$this->group_project_id=db_insertid($result,'project_group_list','group_project_id');
		$this->fetchData($this->group_project_id);

		if (!$this->addAllUsers()) {
			db_rollback();
			return false;
		}

		db_commit();
		return true;
	}

	/**
	 *  fetchData - re-fetch the data for this ProjectGroup from the database.
	 *
	 *  @param  int	 The project group ID.
	 *  @return	boolean	success.
	 */
	function fetchData($group_project_id) {
		$res=db_query("SELECT * FROM project_group_list_vw
			WHERE group_project_id='$group_project_id'
			AND group_id='". $this->Group->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError('ProjectGroup:: Invalid group_project_id');
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	getGroup - get the Group object this ProjectGroup is associated with.
	 *
	 *	@return	object	The Group object.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *	getID - get this GroupProjectID.
	 *
	 *	@return	int	The group_project_id #.
	 */
	function getID() {
		return $this->data_array['group_project_id'];
	}

	/**
	 *	getOpenCount - get the count of open tracker items in this tracker type.
	 *
	 *	@return   int The count.
	 */
	function getOpenCount() {
		return $this->data_array['open_count'];
	}

	/**
	 *	getTotalCount - get the total number of tracker items in this tracker type.
	 *
	 *	@return   int The total count.
	 */
	function getTotalCount() {
		return $this->data_array['count'];
	}

	/**
	 *	isPublic - Is this projectGroup open to the general public.
	 *
	 *	@return boolean	allow.
	 */
	function isPublic() {
		return $this->data_array['is_public'];
	}

	/**
	 *	getName - get the name of this projectGroup.
	 *
	 *	@return string	The name of this projectGroup.
	 */
	function getName() {
		return $this->data_array['project_name'];
	}

	/**
	 *	getSendAllPostsTo - an optional email address to send all task updates to.
	 *
	 *	@return string	The email address.
	 */
	function getSendAllPostsTo() {
		return $this->data_array['send_all_posts_to'];
	}

	/**
	 *	getDescription - the description of this ProjectGroup.
	 *
	 *	@return string	The description.
	 */
	function getDescription() {
		return $this->data_array['description'];
	}

	/**
	 * getStatuses - Return result set of statuses.
	 *
	 * @returns Database result set.
	 */
	function getStatuses () {
		if (!$this->statuses) {
			$sql='SELECT * FROM project_status';
			$this->statuses=db_query($sql);
		}
		return $this->statuses;
	}

	/**
	 * getCategories - Return result set of categories.
	 *
	 * @returns Database result set.
	 */
	function getCategories () {
		if (!$this->categories) {
			$sql="SELECT category_id,category_name 
				FROM project_category 
				WHERE group_project_id='".$this->getID()."'";
			$this->categories=db_query($sql);
		}
		return $this->categories;
	}

	/**
	 *  getCategoryObjects - Array of ProjectCategory objects set up for this artifact type.
	 *
	 *  @return array   Of ProjectCategory objects.
	 */
	function &getCategoryObjects() {
		$res = $this->getCategories();
		$cats = array();
		while ($arr = db_fetch_array($res)) {
			$cats[] = new ProjectCategory($this,$arr);
		}
		return $cats;
	}

	/**
	 * getTechnicians - Return a result set of pm technicians in this group.
	 *
	 * @returns Datbase result set.
	 */
	function getTechnicians () {
		if (!$this->technicians) {
			$sql="SELECT users.user_id,users.realname 
				FROM users,project_perm 
				WHERE users.user_id=project_perm.user_id 
				AND project_perm.group_project_id='". $this->getID() ."' 
				AND project_perm.perm_level IN (1,2) 
				ORDER BY users.realname";
			$this->technicians=db_query($sql);
		}
		return $this->technicians;
	}

	/**
	 *  getTechnicianObjects - Array of User objects set up for this artifact type.
	 *
	 *  @return array   Of User objects.
	 */
	function &getTechnicianObjects() {
		$res = $this->getTechnicians();
		$arr =& util_result_column_to_array($res,0);
		return user_get_objects($arr);
	}

	/**
	 *	update - update a ProjectGroup in the database.
	 *
	 *	@param	string	The project name.
	 *	@param	string	The project description.
	 *	@param	string	The email address to send new notifications to.
	 *	@return boolean success.
	 */
	function update($project_name,$description,$send_all_posts_to='') {

		global $Language;
		if (strlen($project_name) < 3) {
			$this->setError($Language->getText('pm_projectgroup','error_min_name_length'));
			return false;
		}
		if (strlen($description) < 10) {
			$this->setError($Language->getText('pm_projectgroup','error_min_desc_length'));
			return false;
		}

		if ($send_all_posts_to) {
			$invalid_mails = validate_emails($send_all_posts_to);
			if (count($invalid_mails) > 0) {
				$this->setInvalidEmailError();
				return false;
			}
		}


		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		$sql="UPDATE project_group_list SET
			project_name='". htmlspecialchars($project_name) ."',
			description='". htmlspecialchars($description) ."',
			send_all_posts_to='$send_all_posts_to'
			WHERE group_id='".$this->Group->getID()."'
			AND group_project_id='".$this->getID()."'";
		$res=db_query($sql);

		if (!$res || db_affected_rows($res) < 1) {
			$this->setError('Error On Update: '.db_error().$sql);
			return false;
		}
		return true;
	}

	/**
	 *	delete - delete this subproject and all its related data.
	 *
	 *	@param  bool	I'm Sure.
	 *	@param  bool	I'm REALLY sure.
	 *	@return   bool true/false;
	 */
	function delete($sure, $really_sure) {
		if (!$sure || !$really_sure) {
			$this->setMissingParamsError();
			return false;
		}
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		db_begin();
		db_query("DELETE FROM project_category
			WHERE group_project_id='".$this->getID()."'");
//echo '1'.db_error();
		db_query("DELETE FROM project_perm
			WHERE group_project_id='".$this->getID()."'");
//echo '2'.db_error();
		db_query("DELETE FROM project_assigned_to
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=project_assigned_to.project_task_id)");
//echo '4'.db_error();
		db_query("DELETE FROM project_dependencies
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=project_dependencies.project_task_id)");
//echo '5'.db_error();
		db_query("DELETE FROM project_history
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=project_history.project_task_id)");
//echo '6'.db_error();
		db_query("DELETE FROM project_messages
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=project_messages.project_task_id)");
//echo '7'.db_error();
		db_query("DELETE FROM project_task_artifact
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=project_task_artifact.project_task_id)");
//echo '8'.db_error();
		db_query("DELETE FROM rep_time_tracking
			WHERE EXISTS (SELECT project_task_id FROM project_task
			WHERE group_project_id='".$this->getID()."'
			AND project_task.project_task_id=rep_time_tracking.project_task_id)");
//echo '9'.db_error();
		db_query("DELETE FROM project_task
			WHERE group_project_id='".$this->getID()."'");
//echo '10'.db_error();
		db_query("DELETE FROM project_group_list
			WHERE group_project_id='".$this->getID()."'");
//echo '11'.db_error();
		db_query("DELETE FROM project_counts_agg
			WHERE group_project_id='".$this->getID()."'");
//echo '12'.db_error();
		db_commit();
		return true;
	}

	/**
	 *  addAllUsers - add all users to this project.
	 *
	 *  @return boolean success.
	 */
	function addAllUsers() {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		$sql="INSERT INTO project_perm (group_project_id,user_id,perm_level)
			SELECT '".$this->getID()."',user_id,project_flags
			FROM user_group
			WHERE
			group_id='".$this->Group->getID()."'
			AND NOT EXISTS (SELECT user_id FROM project_perm
			WHERE group_project_id='".$this->getID()."'
			AND user_id=user_group.user_id);";
		$res= db_query($sql);
		if (!$res) {
			$this->setError(db_error());
			return false;
		} else {
			return true;
		}
	}

	/**
	 *  addUser - add a user to this subproject.
	 *
	 *  @param  int	 user_id of the new user.
	 *  @return boolean success.
	 */
	function addUser($id) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		$sql="SELECT * FROM project_perm
			WHERE group_project_id='".$this->getID()."'
			AND user_id='$id'";
		$result=db_query($sql);
		if (db_numrows($result) > 0) {
			return true;
		} else {
			$sql="INSERT INTO project_perm (group_project_id,user_id,perm_level)
				VALUES ('".$this->getID()."','$id',0)";
			$result=db_query($sql);
			if ($result && db_affected_rows($result) > 0) {
				return true;
			} else {
				$this->setError(db_error());
				return false;
			}
		}
	}

	/**
	 *  updateUser - update a user's permissions.
	 *
	 *  @param  int	 user_id of the user to update.
	 *  @param  int	 (0) read only, (1) tech only, (2) admin & tech (3) admin only.
	 *  @return boolean success.
	 */
	function updateUser($id,$perm_level) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		//
		//  Update and test if it already exists
		//
		$sql="UPDATE project_perm SET perm_level='$perm_level'
			WHERE user_id='$id' AND group_project_id='".$this->getID()."'";
		$result=db_query($sql);
		if (db_affected_rows($result) < 1) {
			//
			//  If not, insert it.
			//
			$sql="INSERT INTO project_perm (group_project_id,user_id,perm_level) VALUES
				('".$this->getID()."','$id','$perm_level')";
			$result=db_query($sql);
			if (!$result) {
				$this->setError(db_error());
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	/**
	 *  deleteUser - delete a user's permissions.
	 *
	 *  @param  int	 user_id of the user who's permissions to delete.
	 *  @return boolean success.
	 */
	function deleteUser($id) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		$sql="DELETE FROM project_perm
			WHERE user_id='$id' AND group_project_id='".$this->getID()."'";
		$result=db_query($sql);
		if ($result) {
			return true;
		} else {
			$this->setError(db_error());
			return false;
		}
	}

	/*

		USER PERMISSION FUNCTIONS

	*/

	/**
	 *	userCanView - determine if the user can view this subproject.
	 *
	 *	@return boolean   user_can_view.
	 */
	function userCanView() {
		if ($this->isPublic()) {
			return true;
		} else {
			if (!session_loggedin()) {
				return false;
			} else {
				//
				//  You must have an entry in project_perm if this subproject is not public
				//
				if ($this->getCurrentUserPerm() >= 0) {
					return true;
				} else {
					return false;
				}
			}
		}
	}

	/**
	 *  userIsAdmin - see if the logged-in user's perms are >= 2 or Group PMAdmin.
	 *
	 *  @return boolean user_is_admin.
	 */
	function userIsAdmin() {
		if (!session_loggedin()) {
				return false;
		} else {
			$perm =& $this->Group->getPermission( session_get_user() );

			if (($this->getCurrentUserPerm() >= 2) || ($perm->isPMAdmin())) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 *  userIsTechnician - see if the logged-in user's perms are >= 1 or Group PMAdmin.
	 *
	 *  @return boolean user_is_technician.
	 */
	function userIsTechnician() {
		if (!session_loggedin()) {
				return false;
		} else {
			$perm =& $this->Group->getPermission( session_get_user() );

			if (($this->getCurrentUserPerm() >= 1) || ($perm->isPMAdmin())) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 *  getCurrentUserPerm - get the logged-in user's perms from project_perm.
	 *
	 *  @return int perm level for the logged-in user.
	 */
	function getCurrentUserPerm() {
		if (!session_loggedin()) {
			return -1;
		} else {
			if (!isset($this->current_user_perm)) {
				$sql="select perm_level
				FROM project_perm
				WHERE group_project_id='". $this->getID() ."'
				AND user_id='".user_getid()."'";
				$this->current_user_perm=db_result(db_query($sql),0,0);
			}
			return $this->current_user_perm;
		}
	}

}

?>