3 * GForge Project Management Facility
5 * Copyright 2002 GForge, LLC
10 * This file is part of GForge.
12 * GForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * GForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with GForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
28 By Tim Perdue, Sourceforge, 11/99
29 Heavy rewrite by Tim Perdue April 2000
31 Total rewrite in OO and GForge coding guidelines 12/2002 by Tim Perdue
35 require_once $gfcommon.'include/Error.class.php';
38 * Fetches a ProjectGroup object from the database
40 * @param group_project_id the projectgroup id
41 * @param data whether or not the db result handle is passed in
42 * @return the ProjectGroup object
44 function &projectgroup_get_object($group_project_id,$data=false) {
45 global $PROJECTGROUP_OBJ;
46 if (!isset($PROJECTGROUP_OBJ["_".$group_project_id."_"])) {
48 //the db result handle was passed in
50 $res=db_query("SELECT * FROM project_group_list_vw
51 WHERE group_project_id='$group_project_id'");
52 if (db_numrows($res) <1 ) {
53 $PROJECTGROUP_OBJ["_".$group_project_id."_"]=false;
56 $data =& db_fetch_array($res);
58 $Group =& group_get_object($data["group_id"]);
59 $PROJECTGROUP_OBJ["_".$group_project_id."_"]= new ProjectGroup($Group,$group_project_id,$data);
61 return $PROJECTGROUP_OBJ["_".$group_project_id."_"];
65 class ProjectGroup extends Error {
68 * Associative array of data from db.
70 * @var array $data_array.
83 var $current_user_perm;
88 * @param object The Group object to which this forum is associated.
89 * @param int The group_project_id.
90 * @param array The associative array of data.
91 * @return boolean success.
93 function ProjectGroup(&$Group, $group_project_id=false, $arr=false) {
95 if (!$Group || !is_object($Group)) {
96 $this->setError('ProjectGroup:: No Valid Group Object');
99 if ($Group->isError()) {
100 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
103 $this->Group =& $Group;
105 if ($group_project_id) {
106 if (!$arr || !is_array($arr)) {
107 if (!$this->fetchData($group_project_id)) {
111 $this->data_array =& $arr;
112 if ($this->data_array['group_id'] != $this->Group->getID()) {
113 $this->setError('Group_id in db result does not match Group Object');
118 // Make sure they can even access this object
120 if (!$this->userCanView()) {
121 $this->setPermissionDeniedError();
122 $this->data_array = null;
130 * create - create a new ProjectGroup in the database.
132 * @param string The project name.
133 * @param string The project description.
134 * @param int Whether it is (1) public or (0) private .
135 * @param string The email address to send new notifications to.
136 * @return boolean success.
138 function create($project_name,$description,$is_public=1,$send_all_posts_to='') {
139 if (strlen($project_name) < 3) {
140 $this->setError(_('Title Must Be At Least 5 Characters'));
143 if (strlen($description) < 10) {
144 $this->setError(_('Document Description Must Be At Least 10 Characters'));
147 if ($send_all_posts_to) {
148 $invalid_mails = validate_emails($send_all_posts_to);
149 if (count($invalid_mails) > 0) {
150 $this->setInvalidEmailError();
155 $perm =& $this->Group->getPermission( session_get_user() );
157 if (!$perm || !is_object($perm) || !$perm->isPMAdmin()) {
158 $this->setPermissionDeniedError();
162 $sql="INSERT INTO project_group_list (group_id,project_name,is_public,
163 description,send_all_posts_to)
164 VALUES ('".$this->Group->getId()."','". htmlspecialchars($project_name) ."','$is_public',
165 '". htmlspecialchars($description) ."','$send_all_posts_to')";
168 $result=db_query($sql);
171 $this->setError('Error Adding ProjectGroup: '.db_error());
174 $this->group_project_id=db_insertid($result,'project_group_list','group_project_id');
175 $this->fetchData($this->group_project_id);
182 * fetchData - re-fetch the data for this ProjectGroup from the database.
184 * @param int The project group ID.
185 * @return boolean success.
187 function fetchData($group_project_id) {
188 $res=db_query("SELECT * FROM project_group_list_vw
189 WHERE group_project_id='$group_project_id'
190 AND group_id='". $this->Group->getID() ."'");
191 if (!$res || db_numrows($res) < 1) {
192 $this->setError('ProjectGroup:: Invalid group_project_id');
195 $this->data_array =& db_fetch_array($res);
196 db_free_result($res);
201 * getGroup - get the Group object this ProjectGroup is associated with.
203 * @return object The Group object.
205 function &getGroup() {
210 * getID - get this GroupProjectID.
212 * @return int The group_project_id #.
215 return $this->data_array['group_project_id'];
219 * getOpenCount - get the count of open tracker items in this tracker type.
221 * @return int The count.
223 function getOpenCount() {
224 return $this->data_array['open_count'];
228 * getTotalCount - get the total number of tracker items in this tracker type.
230 * @return int The total count.
232 function getTotalCount() {
233 return $this->data_array['count'];
237 * isPublic - Is this projectGroup open to the general public.
239 * @return boolean allow.
241 function isPublic() {
242 return $this->data_array['is_public'];
246 * getName - get the name of this projectGroup.
248 * @return string The name of this projectGroup.
251 return $this->data_array['project_name'];
255 * getSendAllPostsTo - an optional email address to send all task updates to.
257 * @return string The email address.
259 function getSendAllPostsTo() {
260 return $this->data_array['send_all_posts_to'];
264 * getDescription - the description of this ProjectGroup.
266 * @return string The description.
268 function getDescription() {
269 return $this->data_array['description'];
273 * getStatuses - Return result set of statuses.
275 * @returns Database result set.
277 function getStatuses () {
278 if (!$this->statuses) {
279 $sql='SELECT * FROM project_status';
280 $this->statuses=db_query($sql);
282 return $this->statuses;
286 * getCategories - Return result set of categories.
288 * @returns Database result set.
290 function getCategories () {
291 if (!$this->categories) {
292 $sql="SELECT category_id,category_name
293 FROM project_category
294 WHERE group_project_id='".$this->getID()."'";
295 $this->categories=db_query($sql);
297 return $this->categories;
301 * getCategoryObjects - Array of ProjectCategory objects set up for this artifact type.
303 * @return array Of ProjectCategory objects.
305 function &getCategoryObjects() {
306 $res = $this->getCategories();
308 while ($arr = db_fetch_array($res)) {
309 $cats[] = new ProjectCategory($this,$arr);
315 * getTechnicians - Return a result set of pm technicians in this group.
317 * @returns Datbase result set.
319 function getTechnicians () {
320 if (!$this->technicians) {
321 $sql="SELECT users.user_id, users.realname
322 FROM users, role_setting, user_group
323 WHERE users.user_id=user_group.user_id
324 AND role_setting.role_id=user_group.role_id
325 AND role_setting.ref_id='". $this->getID() ."'
326 AND role_setting.value IN (1,2)
327 AND role_setting.section_name='pm'
328 ORDER BY users.realname";
329 $this->technicians=db_query($sql);
331 return $this->technicians;
335 * getTechnicianObjects - Array of User objects set up for this artifact type.
337 * @return array Of User objects.
339 function &getTechnicianObjects() {
340 $res = $this->getTechnicians();
341 $arr =& util_result_column_to_array($res,0);
342 return user_get_objects($arr);
346 * update - update a ProjectGroup in the database.
348 * @param string The project name.
349 * @param string The project description.
350 * @param string The email address to send new notifications to.
351 * @return boolean success.
353 function update($project_name,$description,$send_all_posts_to='') {
354 if (strlen($project_name) < 3) {
355 $this->setError(_('Title Must Be At Least 5 Characters'));
358 if (strlen($description) < 10) {
359 $this->setError(_('Document Description Must Be At Least 10 Characters'));
363 if ($send_all_posts_to) {
364 $invalid_mails = validate_emails($send_all_posts_to);
365 if (count($invalid_mails) > 0) {
366 $this->setInvalidEmailError();
372 if (!$this->userIsAdmin()) {
373 $this->setPermissionDeniedError();
377 $sql="UPDATE project_group_list SET
378 project_name='". htmlspecialchars($project_name) ."',
379 description='". htmlspecialchars($description) ."',
380 send_all_posts_to='$send_all_posts_to'
381 WHERE group_id='".$this->Group->getID()."'
382 AND group_project_id='".$this->getID()."'";
385 if (!$res || db_affected_rows($res) < 1) {
386 $this->setError('Error On Update: '.db_error().$sql);
393 * delete - delete this subproject and all its related data.
395 * @param bool I'm Sure.
396 * @param bool I'm REALLY sure.
397 * @return bool true/false;
399 function delete($sure, $really_sure) {
400 if (!$sure || !$really_sure) {
401 $this->setMissingParamsError();
404 if (!$this->userIsAdmin()) {
405 $this->setPermissionDeniedError();
411 $sql = "DELETE FROM project_assigned_to
412 WHERE EXISTS (SELECT project_task_id FROM project_task
413 WHERE group_project_id='".$this->getID()."'
414 AND project_task.project_task_id=project_assigned_to.project_task_id)";
415 $res = db_query($sql);
419 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
423 $sql = "DELETE FROM project_dependencies
424 WHERE EXISTS (SELECT project_task_id FROM project_task
425 WHERE group_project_id='".$this->getID()."'
426 AND project_task.project_task_id=project_dependencies.project_task_id)";
427 $res = db_query($sql);
431 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
435 $sql = "DELETE FROM project_history
436 WHERE EXISTS (SELECT project_task_id FROM project_task
437 WHERE group_project_id='".$this->getID()."'
438 AND project_task.project_task_id=project_history.project_task_id)";
439 $res = db_query($sql);
443 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
447 $sql = "DELETE FROM project_messages
448 WHERE EXISTS (SELECT project_task_id FROM project_task
449 WHERE group_project_id='".$this->getID()."'
450 AND project_task.project_task_id=project_messages.project_task_id)";
451 $res = db_query($sql);
455 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
459 $sql = "DELETE FROM project_task_artifact
460 WHERE EXISTS (SELECT project_task_id FROM project_task
461 WHERE group_project_id='".$this->getID()."'
462 AND project_task.project_task_id=project_task_artifact.project_task_id)";
463 $res = db_query($sql);
467 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
471 $sql = "DELETE FROM rep_time_tracking
472 WHERE EXISTS (SELECT project_task_id FROM project_task
473 WHERE group_project_id='".$this->getID()."'
474 AND project_task.project_task_id=rep_time_tracking.project_task_id)";
475 $res = db_query($sql);
479 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
483 $sql = "DELETE FROM project_task
484 WHERE group_project_id='".$this->getID()."'";
485 $res = db_query($sql);
489 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
493 $sql = "DELETE FROM project_category WHERE group_project_id='".$this->getID()."'";
494 $res = db_query($sql);
498 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
502 $sql = "DELETE FROM project_group_list
503 WHERE group_project_id='".$this->getID()."'";
504 $res = db_query($sql);
508 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
512 $sql = "DELETE FROM project_counts_agg
513 WHERE group_project_id='".$this->getID()."'";
514 $res = db_query($sql);
518 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
529 USER PERMISSION FUNCTIONS
534 * userCanView - determine if the user can view this subproject.
536 * @return boolean user_can_view.
538 function userCanView() {
539 if ($this->isPublic()) {
542 if (!session_loggedin()) {
546 // You must have a role in this project if this subproject is not public
548 if ($this->getCurrentUserPerm() >= 0) {
558 * userIsAdmin - see if the logged-in user's perms are >= 2 or Group PMAdmin.
560 * @return boolean user_is_admin.
562 function userIsAdmin() {
563 if (!session_loggedin()) {
566 $perm =& $this->Group->getPermission( session_get_user() );
568 if (($this->getCurrentUserPerm() >= 2) || ($perm->isPMAdmin())) {
577 * userIsTechnician - see if the logged-in user's perms are >= 1 or Group PMAdmin.
579 * @return boolean user_is_technician.
581 function userIsTechnician() {
582 if (!session_loggedin()) {
585 $perm =& $this->Group->getPermission( session_get_user() );
587 if (($this->getCurrentUserPerm() >= 1) || ($perm->isPMAdmin())) {
596 * getCurrentUserPerm - get the logged-in user's perms from the role data
598 * @return int perm level for the logged-in user.
600 function getCurrentUserPerm() {
601 if (!session_loggedin()) {
604 if (!isset($this->current_user_perm)) {
605 $sql="SELECT role_setting.value
606 FROM role_setting, user_group
607 WHERE role_setting.ref_id='". $this->getID() ."'
608 AND user_group.role_id = role_setting.role_id
609 AND user_group.user_id='".user_getid()."'
610 AND role_setting.section_name='pm'";
611 $this->current_user_perm=db_result(db_query($sql),0,0);
613 return $this->current_user_perm;
621 // c-file-style: "bsd"