3 * FusionForge project manager
5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/Error.class.php';
29 * Fetches a ProjectGroup object from the database
31 * @param group_project_id the projectgroup id
32 * @param data whether or not the db result handle is passed in
33 * @return the ProjectGroup object
35 function &projectgroup_get_object($group_project_id,$data=false) {
36 global $PROJECTGROUP_OBJ;
37 if (!isset($PROJECTGROUP_OBJ["_".$group_project_id."_"])) {
39 //the db result handle was passed in
41 $res=db_query("SELECT * FROM project_group_list_vw
42 WHERE group_project_id='$group_project_id'");
43 if (db_numrows($res) <1 ) {
44 $PROJECTGROUP_OBJ["_".$group_project_id."_"]=false;
47 $data =& db_fetch_array($res);
49 $Group =& group_get_object($data["group_id"]);
50 $PROJECTGROUP_OBJ["_".$group_project_id."_"]= new ProjectGroup($Group,$group_project_id,$data);
52 return $PROJECTGROUP_OBJ["_".$group_project_id."_"];
56 class ProjectGroup extends Error {
59 * Associative array of data from db.
61 * @var array $data_array.
74 var $current_user_perm;
79 * @param object The Group object to which this forum is associated.
80 * @param int The group_project_id.
81 * @param array The associative array of data.
82 * @return boolean success.
84 function ProjectGroup(&$Group, $group_project_id=false, $arr=false) {
86 if (!$Group || !is_object($Group)) {
87 $this->setError('ProjectGroup:: No Valid Group Object');
90 if ($Group->isError()) {
91 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
94 $this->Group =& $Group;
96 if ($group_project_id) {
97 if (!$arr || !is_array($arr)) {
98 if (!$this->fetchData($group_project_id)) {
102 $this->data_array =& $arr;
103 if ($this->data_array['group_id'] != $this->Group->getID()) {
104 $this->setError('Group_id in db result does not match Group Object');
109 // Make sure they can even access this object
111 if (!$this->userCanView()) {
112 $this->setPermissionDeniedError();
113 $this->data_array = null;
121 * create - create a new ProjectGroup in the database.
123 * @param string The project name.
124 * @param string The project description.
125 * @param int Whether it is (1) public or (0) private .
126 * @param string The email address to send new notifications to.
127 * @return boolean success.
129 function create($project_name,$description,$is_public=1,$send_all_posts_to='') {
130 if (strlen($project_name) < 3) {
131 $this->setError(_('Title Must Be At Least 5 Characters'));
134 if (strlen($description) < 10) {
135 $this->setError(_('Document Description Must Be At Least 10 Characters'));
138 if ($send_all_posts_to) {
139 $invalid_mails = validate_emails($send_all_posts_to);
140 if (count($invalid_mails) > 0) {
141 $this->setInvalidEmailError();
146 $perm =& $this->Group->getPermission( session_get_user() );
148 if (!$perm || !is_object($perm) || !$perm->isPMAdmin()) {
149 $this->setPermissionDeniedError();
153 $sql="INSERT INTO project_group_list (group_id,project_name,is_public,
154 description,send_all_posts_to)
155 VALUES ('".$this->Group->getId()."','". htmlspecialchars($project_name) ."','$is_public',
156 '". htmlspecialchars($description) ."','$send_all_posts_to')";
159 $result=db_query($sql);
162 $this->setError('Error Adding ProjectGroup: '.db_error());
165 $this->group_project_id=db_insertid($result,'project_group_list','group_project_id');
166 $this->fetchData($this->group_project_id);
173 * fetchData - re-fetch the data for this ProjectGroup from the database.
175 * @param int The project group ID.
176 * @return boolean success.
178 function fetchData($group_project_id) {
179 $res=db_query("SELECT * FROM project_group_list_vw
180 WHERE group_project_id='$group_project_id'
181 AND group_id='". $this->Group->getID() ."'");
182 if (!$res || db_numrows($res) < 1) {
183 $this->setError('ProjectGroup:: Invalid group_project_id');
186 $this->data_array =& db_fetch_array($res);
187 db_free_result($res);
192 * getGroup - get the Group object this ProjectGroup is associated with.
194 * @return object The Group object.
196 function &getGroup() {
201 * getID - get this GroupProjectID.
203 * @return int The group_project_id #.
206 return $this->data_array['group_project_id'];
210 * getOpenCount - get the count of open tracker items in this tracker type.
212 * @return int The count.
214 function getOpenCount() {
215 return $this->data_array['open_count'];
219 * getTotalCount - get the total number of tracker items in this tracker type.
221 * @return int The total count.
223 function getTotalCount() {
224 return $this->data_array['count'];
228 * isPublic - Is this projectGroup open to the general public.
230 * @return boolean allow.
232 function isPublic() {
233 return $this->data_array['is_public'];
237 * getName - get the name of this projectGroup.
239 * @return string The name of this projectGroup.
242 return $this->data_array['project_name'];
246 * getSendAllPostsTo - an optional email address to send all task updates to.
248 * @return string The email address.
250 function getSendAllPostsTo() {
251 return $this->data_array['send_all_posts_to'];
255 * getDescription - the description of this ProjectGroup.
257 * @return string The description.
259 function getDescription() {
260 return $this->data_array['description'];
264 * getStatuses - Return result set of statuses.
266 * @returns Database result set.
268 function getStatuses () {
269 if (!$this->statuses) {
270 $sql='SELECT * FROM project_status';
271 $this->statuses=db_query($sql);
273 return $this->statuses;
277 * getCategories - Return result set of categories.
279 * @returns Database result set.
281 function getCategories () {
282 if (!$this->categories) {
283 $sql="SELECT category_id,category_name
284 FROM project_category
285 WHERE group_project_id='".$this->getID()."'";
286 $this->categories=db_query($sql);
288 return $this->categories;
292 * getCategoryObjects - Array of ProjectCategory objects set up for this artifact type.
294 * @return array Of ProjectCategory objects.
296 function &getCategoryObjects() {
297 $res = $this->getCategories();
299 while ($arr = db_fetch_array($res)) {
300 $cats[] = new ProjectCategory($this,$arr);
306 * getTechnicians - Return a result set of pm technicians in this group.
308 * @returns Datbase result set.
310 function getTechnicians () {
311 if (!$this->technicians) {
312 $sql="SELECT users.user_id, users.realname
313 FROM users, role_setting, user_group
314 WHERE users.user_id=user_group.user_id
315 AND role_setting.role_id=user_group.role_id
316 AND role_setting.ref_id='". $this->getID() ."'
317 AND role_setting.value::integer IN (1,2)
318 AND role_setting.section_name='pm'
319 ORDER BY users.realname";
320 $this->technicians=db_query($sql);
322 return $this->technicians;
326 * getTechnicianObjects - Array of User objects set up for this artifact type.
328 * @return array Of User objects.
330 function &getTechnicianObjects() {
331 $res = $this->getTechnicians();
332 $arr =& util_result_column_to_array($res,0);
333 return user_get_objects($arr);
337 * update - update a ProjectGroup in the database.
339 * @param string The project name.
340 * @param string The project description.
341 * @param string The email address to send new notifications to.
342 * @return boolean success.
344 function update($project_name,$description,$send_all_posts_to='') {
345 if (strlen($project_name) < 3) {
346 $this->setError(_('Title Must Be At Least 5 Characters'));
349 if (strlen($description) < 10) {
350 $this->setError(_('Document Description Must Be At Least 10 Characters'));
354 if ($send_all_posts_to) {
355 $invalid_mails = validate_emails($send_all_posts_to);
356 if (count($invalid_mails) > 0) {
357 $this->setInvalidEmailError();
363 if (!$this->userIsAdmin()) {
364 $this->setPermissionDeniedError();
368 $sql="UPDATE project_group_list SET
369 project_name='". htmlspecialchars($project_name) ."',
370 description='". htmlspecialchars($description) ."',
371 send_all_posts_to='$send_all_posts_to'
372 WHERE group_id='".$this->Group->getID()."'
373 AND group_project_id='".$this->getID()."'";
376 if (!$res || db_affected_rows($res) < 1) {
377 $this->setError('Error On Update: '.db_error().$sql);
384 * delete - delete this subproject and all its related data.
386 * @param bool I'm Sure.
387 * @param bool I'm REALLY sure.
388 * @return bool true/false;
390 function delete($sure, $really_sure) {
391 if (!$sure || !$really_sure) {
392 $this->setMissingParamsError();
395 if (!$this->userIsAdmin()) {
396 $this->setPermissionDeniedError();
402 $sql = "DELETE FROM project_assigned_to
403 WHERE EXISTS (SELECT project_task_id FROM project_task
404 WHERE group_project_id='".$this->getID()."'
405 AND project_task.project_task_id=project_assigned_to.project_task_id)";
406 $res = db_query($sql);
410 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
414 $sql = "DELETE FROM project_dependencies
415 WHERE EXISTS (SELECT project_task_id FROM project_task
416 WHERE group_project_id='".$this->getID()."'
417 AND project_task.project_task_id=project_dependencies.project_task_id)";
418 $res = db_query($sql);
422 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
426 $sql = "DELETE FROM project_history
427 WHERE EXISTS (SELECT project_task_id FROM project_task
428 WHERE group_project_id='".$this->getID()."'
429 AND project_task.project_task_id=project_history.project_task_id)";
430 $res = db_query($sql);
434 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
438 $sql = "DELETE FROM project_messages
439 WHERE EXISTS (SELECT project_task_id FROM project_task
440 WHERE group_project_id='".$this->getID()."'
441 AND project_task.project_task_id=project_messages.project_task_id)";
442 $res = db_query($sql);
446 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
450 $sql = "DELETE FROM project_task_artifact
451 WHERE EXISTS (SELECT project_task_id FROM project_task
452 WHERE group_project_id='".$this->getID()."'
453 AND project_task.project_task_id=project_task_artifact.project_task_id)";
454 $res = db_query($sql);
458 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
462 $sql = "DELETE FROM rep_time_tracking
463 WHERE EXISTS (SELECT project_task_id FROM project_task
464 WHERE group_project_id='".$this->getID()."'
465 AND project_task.project_task_id=rep_time_tracking.project_task_id)";
466 $res = db_query($sql);
470 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
474 $sql = "DELETE FROM project_task
475 WHERE group_project_id='".$this->getID()."'";
476 $res = db_query($sql);
480 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
484 $sql = "DELETE FROM project_category WHERE group_project_id='".$this->getID()."'";
485 $res = db_query($sql);
489 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
493 $sql = "DELETE FROM project_group_list
494 WHERE group_project_id='".$this->getID()."'";
495 $res = db_query($sql);
499 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
503 $sql = "DELETE FROM project_counts_agg
504 WHERE group_project_id='".$this->getID()."'";
505 $res = db_query($sql);
509 $this->setError('DATABASE '.db_error().' QUERY='.$sql);
520 USER PERMISSION FUNCTIONS
525 * userCanView - determine if the user can view this subproject.
527 * @return boolean user_can_view.
529 function userCanView() {
530 if ($this->isPublic()) {
533 if (!session_loggedin()) {
537 // You must have a role in this project if this subproject is not public
539 if ($this->getCurrentUserPerm() >= 0) {
549 * userIsAdmin - see if the logged-in user's perms are >= 2 or Group PMAdmin.
551 * @return boolean user_is_admin.
553 function userIsAdmin() {
554 if (!session_loggedin()) {
557 $perm =& $this->Group->getPermission( session_get_user() );
559 if (($this->getCurrentUserPerm() >= 2) || ($perm->isPMAdmin())) {
568 * userIsTechnician - see if the logged-in user's perms are >= 1 or Group PMAdmin.
570 * @return boolean user_is_technician.
572 function userIsTechnician() {
573 if (!session_loggedin()) {
576 $perm =& $this->Group->getPermission( session_get_user() );
578 if (($this->getCurrentUserPerm() >= 1) || ($perm->isPMAdmin())) {
587 * getCurrentUserPerm - get the logged-in user's perms from the role data
589 * @return int perm level for the logged-in user.
591 function getCurrentUserPerm() {
592 if (!session_loggedin()) {
595 if (!isset($this->current_user_perm)) {
596 $sql="SELECT role_setting.value::integer
597 FROM role_setting, user_group
598 WHERE role_setting.ref_id='". $this->getID() ."'
599 AND user_group.role_id = role_setting.role_id
600 AND user_group.user_id='".user_getid()."'
601 AND role_setting.section_name='pm'";
602 $this->current_user_perm=db_result(db_query($sql),0,0);
604 return $this->current_user_perm;
612 // c-file-style: "bsd"