3 * FusionForge project manager
5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
9 * This file is part of FusionForge.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published
13 * by the Free Software Foundation; either version 2 of the License,
14 * or (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 require_once $gfcommon.'include/Error.class.php';
30 * Fetches a ProjectGroup object from the database
32 * @param group_project_id the projectgroup id
33 * @param data whether or not the db result handle is passed in
34 * @return the ProjectGroup object
36 function &projectgroup_get_object($group_project_id,$data=false) {
37 global $PROJECTGROUP_OBJ;
38 if (!isset($PROJECTGROUP_OBJ["_".$group_project_id."_"])) {
40 //the db result handle was passed in
42 $res = db_query_params ('SELECT * FROM project_group_list_vw WHERE group_project_id=$1',
43 array ($group_project_id)) ;
44 if (db_numrows($res) <1 ) {
45 $PROJECTGROUP_OBJ["_".$group_project_id."_"]=false;
48 $data =& db_fetch_array($res);
50 $Group =& group_get_object($data["group_id"]);
51 $PROJECTGROUP_OBJ["_".$group_project_id."_"]= new ProjectGroup($Group,$group_project_id,$data);
53 return $PROJECTGROUP_OBJ["_".$group_project_id."_"];
57 class ProjectGroup extends Error {
60 * Associative array of data from db.
62 * @var array $data_array.
75 var $current_user_perm;
80 * @param object The Group object to which this forum is associated.
81 * @param int The group_project_id.
82 * @param array The associative array of data.
83 * @return boolean success.
85 function ProjectGroup(&$Group, $group_project_id=false, $arr=false) {
87 if (!$Group || !is_object($Group)) {
88 $this->setError('ProjectGroup:: No Valid Group Object');
91 if ($Group->isError()) {
92 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
95 $this->Group =& $Group;
97 if ($group_project_id) {
98 if (!$arr || !is_array($arr)) {
99 if (!$this->fetchData($group_project_id)) {
103 $this->data_array =& $arr;
104 if ($this->data_array['group_id'] != $this->Group->getID()) {
105 $this->setError('Group_id in db result does not match Group Object');
110 // Make sure they can even access this object
112 if (!$this->userCanView()) {
113 $this->setPermissionDeniedError();
114 $this->data_array = null;
122 * create - create a new ProjectGroup in the database.
124 * @param string The project name.
125 * @param string The project description.
126 * @param int Whether it is (1) public or (0) private .
127 * @param string The email address to send new notifications to.
128 * @return boolean success.
130 function create($project_name,$description,$is_public=1,$send_all_posts_to='') {
131 if (strlen($project_name) < 3) {
132 $this->setError(_('Title Must Be At Least 5 Characters'));
135 if (strlen($description) < 10) {
136 $this->setError(_('Document Description Must Be At Least 10 Characters'));
139 if ($send_all_posts_to) {
140 $invalid_mails = validate_emails($send_all_posts_to);
141 if (count($invalid_mails) > 0) {
142 $this->setInvalidEmailError();
147 $perm =& $this->Group->getPermission( session_get_user() );
149 if (!$perm || !is_object($perm) || !$perm->isPMAdmin()) {
150 $this->setPermissionDeniedError();
155 $result = db_query_params ('INSERT INTO project_group_list (group_id,project_name,is_public,description,send_all_posts_to) VALUES ($1,$2,$3,$4,$5)',
156 array ($this->Group->getId(),
157 htmlspecialchars($project_name),
159 htmlspecialchars($description),
160 $send_all_posts_to)) ;
163 $this->setError('Error Adding ProjectGroup: '.db_error());
166 $this->group_project_id=db_insertid($result,'project_group_list','group_project_id');
167 $this->fetchData($this->group_project_id);
174 * fetchData - re-fetch the data for this ProjectGroup from the database.
176 * @param int The project group ID.
177 * @return boolean success.
179 function fetchData($group_project_id) {
180 $res = db_query_params ('SELECT * FROM project_group_list_vw WHERE group_project_id=$1 AND group_id=$2',
181 array ($group_project_id,
182 $this->Group->getID())) ;
183 if (!$res || db_numrows($res) < 1) {
184 $this->setError('ProjectGroup:: Invalid group_project_id');
187 $this->data_array =& db_fetch_array($res);
188 db_free_result($res);
193 * getGroup - get the Group object this ProjectGroup is associated with.
195 * @return object The Group object.
197 function &getGroup() {
202 * getID - get this GroupProjectID.
204 * @return int The group_project_id #.
207 return $this->data_array['group_project_id'];
211 * getOpenCount - get the count of open tracker items in this tracker type.
213 * @return int The count.
215 function getOpenCount() {
216 return $this->data_array['open_count'];
220 * getTotalCount - get the total number of tracker items in this tracker type.
222 * @return int The total count.
224 function getTotalCount() {
225 return $this->data_array['count'];
229 * isPublic - Is this projectGroup open to the general public.
231 * @return boolean allow.
233 function isPublic() {
234 return $this->data_array['is_public'];
238 * getName - get the name of this projectGroup.
240 * @return string The name of this projectGroup.
243 return $this->data_array['project_name'];
247 * getSendAllPostsTo - an optional email address to send all task updates to.
249 * @return string The email address.
251 function getSendAllPostsTo() {
252 return $this->data_array['send_all_posts_to'];
256 * getDescription - the description of this ProjectGroup.
258 * @return string The description.
260 function getDescription() {
261 return $this->data_array['description'];
265 * getStatuses - Return result set of statuses.
267 * @returns Database result set.
269 function getStatuses () {
270 if (!$this->statuses) {
271 $this->statuses = db_query_params ('SELECT * FROM project_status',
274 return $this->statuses;
278 * getCategories - Return result set of categories.
280 * @returns Database result set.
282 function getCategories () {
283 if (!$this->categories) {
284 $this->categories = db_query_params ('SELECT category_id,category_name FROM project_category WHERE group_project_id=$1',
285 array ($this->getID()));
287 return $this->categories;
291 * getCategoryObjects - Array of ProjectCategory objects set up for this artifact type.
293 * @return array Of ProjectCategory objects.
295 function &getCategoryObjects() {
296 $res = $this->getCategories();
298 while ($arr = db_fetch_array($res)) {
299 $cats[] = new ProjectCategory($this,$arr);
305 * getTechnicians - Return a result set of pm technicians in this group.
307 * @returns Datbase result set.
309 function getTechnicians () {
310 if (!$this->technicians) {
311 $this->technicians = db_query_params ('SELECT users.user_id, users.realname
312 FROM users, role_setting, user_group
313 WHERE users.user_id=user_group.user_id
314 AND role_setting.role_id=user_group.role_id
315 AND role_setting.ref_id=$1
316 AND role_setting.value::integer IN (1,2)
317 AND role_setting.section_name=$2
318 ORDER BY users.realname',
319 array ($this->getID(),
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 $res = db_query_params ('UPDATE project_group_list SET project_name=$1,
369 description=$2, send_all_posts_to=$3
370 WHERE group_id=$4 AND group_project_id=$5',
371 array (htmlspecialchars($project_name),
372 htmlspecialchars($description),
374 $this->Group->getID(),
377 if (!$res || db_affected_rows($res) < 1) {
378 $this->setError('Error On Update: '.db_error().$sql);
385 * delete - delete this subproject and all its related data.
387 * @param bool I'm Sure.
388 * @param bool I'm REALLY sure.
389 * @return bool true/false;
391 function delete($sure, $really_sure) {
392 if (!$sure || !$really_sure) {
393 $this->setMissingParamsError();
396 if (!$this->userIsAdmin()) {
397 $this->setPermissionDeniedError();
403 $res = db_query_params ('DELETE FROM project_assigned_to
404 WHERE EXISTS (SELECT project_task_id FROM project_task
405 WHERE group_project_id=$1
406 AND project_task.project_task_id=project_assigned_to.project_task_id)',
407 array ($this->getID())) ;
411 $this->setError('DATABASE '.db_error());
415 $res = db_query_params ('DELETE FROM project_dependencies
416 WHERE EXISTS (SELECT project_task_id FROM project_task
417 WHERE group_project_id=$1
418 AND project_task.project_task_id=project_dependencies.project_task_id)',
419 array ($this->getID())) ;
423 $this->setError('DATABASE '.db_error());
427 $res = db_query_params ('DELETE FROM project_history
428 WHERE EXISTS (SELECT project_task_id FROM project_task
429 WHERE group_project_id=$1
430 AND project_task.project_task_id=project_history.project_task_id)',
431 array ($this->getID())) ;
435 $this->setError('DATABASE '.db_error());
439 $res = db_query_params ('DELETE FROM project_messages
440 WHERE EXISTS (SELECT project_task_id FROM project_task
441 WHERE group_project_id=$1
442 AND project_task.project_task_id=project_messages.project_task_id)',
443 array ($this->getID())) ;
447 $this->setError('DATABASE '.db_error());
451 $res = db_query_params ('DELETE FROM project_task_artifact
452 WHERE EXISTS (SELECT project_task_id FROM project_task
453 WHERE group_project_id=$1
454 AND project_task.project_task_id=project_task_artifact.project_task_id)',
455 array ($this->getID())) ;
459 $this->setError('DATABASE '.db_error());
463 $res = db_query_params ('DELETE FROM rep_time_tracking
464 WHERE EXISTS (SELECT project_task_id FROM project_task
465 WHERE group_project_id=$1
466 AND project_task.project_task_id=rep_time_tracking.project_task_id)',
467 array ($this->getID())) ;
471 $this->setError('DATABASE '.db_error());
475 $res = db_query_params ('DELETE FROM project_task
476 WHERE group_project_id=$1',
477 array ($this->getID())) ;
481 $this->setError('DATABASE '.db_error());
485 $res = db_query_params ('DELETE FROM project_category WHERE group_project_id=$1',
486 array ($this->getID())) ;
490 $this->setError('DATABASE '.db_error());
494 $res = db_query_params ('DELETE FROM project_group_list WHERE group_project_id=$1',
495 array ($this->getID())) ;
499 $this->setError('DATABASE '.db_error());
503 $res = db_query_params ('DELETE FROM project_counts_agg WHERE group_project_id=$1',
504 array ($this->getID())) ;
508 $this->setError('DATABASE '.db_error());
519 USER PERMISSION FUNCTIONS
524 * userCanView - determine if the user can view this subproject.
526 * @return boolean user_can_view.
528 function userCanView() {
529 if ($this->isPublic()) {
532 if (!session_loggedin()) {
536 // You must have a role in this project if this subproject is not public
538 if ($this->getCurrentUserPerm() >= 0) {
548 * userIsAdmin - see if the logged-in user's perms are >= 2 or Group PMAdmin.
550 * @return boolean user_is_admin.
552 function userIsAdmin() {
553 if (!session_loggedin()) {
556 $perm =& $this->Group->getPermission( session_get_user() );
558 if (($this->getCurrentUserPerm() >= 2) || ($perm->isPMAdmin())) {
567 * userIsTechnician - see if the logged-in user's perms are >= 1 or Group PMAdmin.
569 * @return boolean user_is_technician.
571 function userIsTechnician() {
572 if (!session_loggedin()) {
575 $perm =& $this->Group->getPermission( session_get_user() );
577 if (($this->getCurrentUserPerm() >= 1) || ($perm->isPMAdmin())) {
586 * getCurrentUserPerm - get the logged-in user's perms from the role data
588 * @return int perm level for the logged-in user.
590 function getCurrentUserPerm() {
591 if (!session_loggedin()) {
594 if (!isset($this->current_user_perm)) {
595 $res = db_query_params ('SELECT role_setting.value::integer
596 FROM role_setting, user_group
597 WHERE role_setting.ref_id=$1
598 AND user_group.role_id = role_setting.role_id
599 AND user_group.user_id=$2
600 AND role_setting.section_name=$3',
601 array ($this->getID(),
604 $this->current_user_perm=db_result($res,0,0);
606 return $this->current_user_perm;
614 // c-file-style: "bsd"