5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009-2010, Roland Mas
7 * Copyright 2010-2011, Franck Villaume - Capgemini
8 * Copyright 2010-2012, Alain Peyrat - Alcatel-Lucent
9 * Copyright 2012-2013, Franck Villaume - TrivialDev
10 * http://fusionforge.org
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 require_once $gfcommon.'tracker/ArtifactTypes.class.php';
29 require_once $gfcommon.'tracker/ArtifactTypeFactory.class.php';
30 require_once $gfcommon.'forum/Forum.class.php';
31 require_once $gfcommon.'forum/ForumFactory.class.php';
32 require_once $gfcommon.'pm/ProjectGroup.class.php';
33 require_once $gfcommon.'pm/ProjectGroupFactory.class.php';
34 require_once $gfcommon.'include/Role.class.php';
35 require_once $gfcommon.'frs/FRSPackage.class.php';
36 require_once $gfcommon.'docman/DocumentGroup.class.php';
37 require_once $gfcommon.'docman/DocumentGroupFactory.class.php';
38 require_once $gfcommon.'mail/MailingList.class.php';
39 require_once $gfcommon.'mail/MailingListFactory.class.php';
40 require_once $gfcommon.'survey/SurveyFactory.class.php';
41 require_once $gfcommon.'survey/SurveyQuestionFactory.class.php';
42 require_once $gfcommon.'include/gettext.php';
43 require_once $gfcommon.'include/GroupJoinRequest.class.php';
48 * group_get_object() - Get the group object.
50 * group_get_object() is useful so you can pool group objects/save database queries
51 * You should always use this instead of instantiating the object directly.
53 * You can now optionally pass in a db result handle. If you do, it re-uses that query
54 * to instantiate the objects.
56 * IMPORTANT! That db result must contain all fields
57 * from groups table or you will have problems
59 * @param int $group_id Required
60 * @param int|bool $res Result set handle ("SELECT * FROM groups WHERE group_id=xx")
61 * @return Group|bool A group object or false on failure
63 function &group_get_object($group_id, $res = false) {
64 //create a common set of group objects
65 //saves a little wear on the database
67 //automatically checks group_type and
68 //returns appropriate object
71 if (!isset($GROUP_OBJ["_".$group_id."_"])) {
73 //the db result handle was passed in
75 $res = db_query_params('SELECT * FROM groups WHERE group_id=$1', array($group_id)) ;
77 if (!$res || db_numrows($res) < 1) {
78 $GROUP_OBJ["_".$group_id."_"]=false;
81 check group type and set up object
83 if (db_result($res,0,'type_id') == 1) {
85 $GROUP_OBJ["_".$group_id."_"] = new Group($group_id, $res);
88 $GROUP_OBJ["_".$group_id."_"] = false;
92 return $GROUP_OBJ["_".$group_id."_"];
95 function &group_get_objects($id_arr) {
98 // Note: if we don't do this, the result may be corrupted
102 foreach ($id_arr as $id) {
104 // See if this ID already has been fetched in the cache
106 if (!isset($GROUP_OBJ["_".$id."_"])) {
110 if (count($fetch) > 0) {
111 $res=db_query_params('SELECT * FROM groups WHERE group_id = ANY ($1)',
112 array(db_int_array_to_any_clause($fetch)));
113 while ($arr = db_fetch_array($res)) {
114 $GROUP_OBJ["_".$arr['group_id']."_"] = new Group($arr['group_id'],$arr);
117 foreach ($id_arr as $id) {
118 $return[] =& $GROUP_OBJ["_".$id."_"];
123 function &group_get_active_projects() {
124 $res = db_query_params('SELECT group_id FROM groups WHERE status=$1',
126 return group_get_objects(util_result_column_to_array($res,0));
129 function &group_get_template_projects() {
130 $res=db_query_params ('SELECT group_id FROM groups WHERE is_template=1 AND status != $1',
132 return group_get_objects (util_result_column_to_array($res,0)) ;
135 function &group_get_object_by_name($groupname) {
136 $res = db_query_params('SELECT * FROM groups WHERE unix_group_name=$1', array($groupname));
137 return group_get_object(db_result($res, 0, 'group_id'), $res);
140 function &group_get_objects_by_name($groupname_arr) {
141 $res = db_query_params('SELECT group_id FROM groups WHERE unix_group_name = ANY ($1)',
142 array(db_string_array_to_any_clause($groupname_arr)));
143 $arr =& util_result_column_to_array($res,0);
144 return group_get_objects($arr);
147 function &group_get_object_by_publicname($groupname) {
148 $res = db_query_params('SELECT * FROM groups WHERE lower(group_name) LIKE $1',
149 array(htmlspecialchars(strtolower($groupname))));
150 return group_get_object(db_result($res, 0, 'group_id'), $res);
154 * get_public_active_projects_asc() - Get a list of rows for public active projects (initially in trove/full_list)
156 * @param int Opional Maximum number of rows to limit query length
158 function get_public_active_projects_asc($max_query_limit = -1) {
160 $res_grp = db_query_params ('
161 SELECT group_id, group_name, unix_group_name, short_description, register_time
163 WHERE status = $1 AND type_id=1 AND group_id>4 AND register_time > 0
164 ORDER BY group_name ASC
169 while ($row_grp = db_fetch_array($res_grp)) {
170 if (!forge_check_perm ('project_read', $row_grp['group_id'])) {
173 $projects[] = $row_grp;
179 class Group extends Error {
181 * Associative array of data from db.
183 * @var array $data_array.
188 * array of User objects.
190 * @var array $membersArr.
195 * Whether the use is an admin/super user of this project.
197 * @var bool $is_admin.
202 * Artifact types result handle.
204 * @var int $types_res.
209 * Associative array of data for plugins.
211 * @var array $plugins_data.
217 * Associative array of data for the group menu.
219 * @var array $menu_data.
224 * Group - Group object constructor - use group_get_object() to instantiate.
226 * @param int|bool $id Required - Id of the group you want to instantiate.
227 * @param int|bool $res Database result from select query OR associative array of all columns.
228 * @return boolean Success or not
230 function __construct($id = false, $res = false) {
233 //setting up an empty object
234 //probably going to call create()
238 if (!$this->fetchData($id)) {
243 // Assoc array was passed in
245 if (is_array($res)) {
246 $this->data_array =& $res;
248 if (db_numrows($res) < 1) {
249 //function in class we extended
250 $this->setError(_('Group Not Found'));
251 $this->data_array=array();
254 //set up an associative array for use by other functions
255 $this->data_array = db_fetch_array_by_row($res, 0);
263 * fetchData - May need to refresh database fields if an update occurred.
265 * @param int $group_id The group_id.
266 * @return boolean success or not
268 function fetchData($group_id) {
269 $res = db_query_params ('SELECT * FROM groups WHERE group_id=$1',
271 if (!$res || db_numrows($res) < 1) {
272 $this->setError(sprintf(_('fetchData():: %s'),db_error()));
275 $this->data_array = db_fetch_array($res);
280 * create - Create new group.
282 * This method should be called on empty Group object.
283 * It will add an entry for a pending group/project (status 'P')
285 * @param object The User object.
286 * @param string The full name of the user.
287 * @param string The Unix name of the user.
288 * @param string The new group description.
289 * @param string The purpose of the group.
290 * @param boolean Whether to send an email or not
291 * @param int The id of the project this new project is based on
292 * @return boolean success or not
294 function create(&$user, $group_name, $unix_name, $description, $purpose, $unix_box = 'shell1',
295 $scm_box = 'cvs1', $is_public = 1, $send_mail = true, $built_from_template = 0) {
296 // $user is ignored - anyone can create pending group
299 if ($this->getID()!=0) {
300 $this->setError(_('Group::create: Group object already exists'));
302 } elseif (!$this->validateGroupName($group_name)) {
304 } elseif (!account_groupnamevalid($unix_name)) {
305 $this->setError(_('Invalid Unix name'));
307 } elseif (!$SYS->sysUseUnixName($unix_name)) {
308 $this->setError(_('Unix name already taken'));
310 } elseif (db_numrows(db_query_params('SELECT group_id FROM groups WHERE unix_group_name=$1',
311 array($unix_name))) > 0) {
312 $this->setError(_('Unix name already taken'));
314 } elseif (strlen($purpose)<10) {
315 $this->setError(_('Please describe your Registration Purpose in a more comprehensive manner'));
317 } elseif (strlen($purpose)>1500) {
318 $this->setError(_('The Registration Purpose text is too long. Please make it smaller than 1500 bytes.'));
320 } elseif (strlen($description)<10) {
321 $this->setError(_('Describe in a more comprehensive manner your project.'));
325 // Check if sys_use_project_vhost for homepage
326 if (forge_get_config('use_project_vhost')) {
327 $homepage = $unix_name.".".forge_get_config('web_host');
329 $homepage = forge_get_config('web_host')."/www/".$unix_name."/";
334 $res = db_query_params('
349 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)',
350 array (htmlspecialchars ($group_name),
352 htmlspecialchars($description),
358 htmlspecialchars($purpose),
360 md5(util_randbytes()),
361 $built_from_template));
362 if (!$res || db_affected_rows($res) < 1) {
363 $this->setError(sprintf(_('ERROR: Could not create group: %s'),db_error()));
368 $id = db_insertid($res, 'groups', 'group_id');
370 $this->setError(sprintf(_('ERROR: Could not get group id: %s'),db_error()));
375 if (!$this->fetchData($id)) {
380 $gjr = new GroupJoinRequest($this);
381 $gjr->create($user->getID(),
382 'Fake GroupJoinRequest to store the creator of a project',
385 $hook_params = array();
386 $hook_params['group'] = $this;
387 $hook_params['group_id'] = $this->getID();
388 $hook_params['group_name'] = $group_name;
389 $hook_params['unix_group_name'] = $unix_name;
390 plugin_hook("group_create", $hook_params);
394 $this->sendNewProjectNotificationEmail();
402 * updateAdmin - Update core properties of group object.
404 * This function require site admin privilege.
406 * @param object User requesting operation (for access control).
407 * @param boolean Whether group is publicly accessible (0/1).
408 * @param int Group type (1-project, 2-foundry).
409 * @param string Machine on which group's home directory located.
410 * @param string Domain which serves group's WWW.
414 function updateAdmin(&$user, $is_public, $type_id, $unix_box, $http_domain) {
415 $perm =& $this->getPermission();
417 if (!$perm || !is_object($perm)) {
418 $this->setError(_('Could not get permission.'));
422 if (!$perm->isSuperUser()) {
423 $this->setError(_('Permission denied.'));
429 $res = db_query_params('
431 SET type_id=$1, unix_box=$2, http_domain=$3
438 if (!$res || db_affected_rows($res) < 1) {
439 $this->setError(_('ERROR: DB: Could not change group properties: %s'),db_error());
444 // Log the audit trail
445 if ($type_id != $this->data_array['type_id']) {
446 $this->addHistory('type_id', $this->data_array['type_id']);
448 if ($unix_box != $this->data_array['unix_box']) {
449 $this->addHistory('unix_box', $this->data_array['unix_box']);
451 if ($http_domain != $this->data_array['http_domain']) {
452 $this->addHistory('http_domain', $this->data_array['http_domain']);
455 if (!$this->fetchData($this->getID())) {
464 * update - Update number of common properties.
466 * Unlike updateAdmin(), this function accessible to project admin.
468 * @param object User requesting operation (for access control).
469 * @param boolean Whether group is publicly accessible (0/1).
470 * @param string Project's license (string ident).
471 * @param int Group type (1-project, 2-foundry).
472 * @param string Machine on which group's home directory located.
473 * @param string Domain which serves group's WWW.
474 * @return int status.
477 function update(&$user, $group_name, $homepage, $short_description, $use_mail, $use_survey, $use_forum,
478 $use_pm, $use_pm_depend_box, $use_scm, $use_news, $use_docman,
479 $new_doc_address, $send_all_docs, $logo_image_id,
480 $use_ftp, $use_tracker, $use_frs, $use_stats, $tags, $use_activity, $is_public) {
482 $perm =& $this->getPermission();
484 if (!$perm || !is_object($perm)) {
485 $this->setError(_('Could not get permission.'));
489 if (!$perm->isAdmin()) {
490 $this->setError(_('Permission denied.'));
494 // Validate some values
495 if ($this->getPublicName() != $group_name) {
496 if (!$this->validateGroupName($group_name)) {
501 if ($new_doc_address) {
502 $invalid_mails = validate_emails($new_doc_address);
503 if (count($invalid_mails) > 0) {
504 $this->setError(sprintf(ngettext('New Doc Address Appeared Invalid: %s', 'New Doc Addresses Appeared Invalid: %s', count($invalid_mails)),implode(',',$invalid_mails)));
509 // in the database, these all default to '1',
510 // so we have to explicitly set 0
523 if (!$use_pm_depend_box) {
524 $use_pm_depend_box = 0;
547 if (!$use_activity) {
550 if (!$send_all_docs) {
554 $homepage = ltrim($homepage);
556 $homepage = util_make_url('/projects/' . $this->getUnixName() . '/');
559 if (strlen(htmlspecialchars($short_description))<10) {
560 $this->setError(_('Describe in a more comprehensive manner your project.'));
566 //XXX not yet actived logo_image_id='$logo_image_id',
567 $res = db_query_params('UPDATE groups
570 short_description=$3,
575 use_pm_depend_box=$8,
586 array(htmlspecialchars($group_name),
588 htmlspecialchars($short_description),
605 if (!$res || db_affected_rows($res) < 1) {
606 $this->setError(sprintf(_('Error updating project information: %s'), db_error()));
611 if (!$this->setUseDocman($use_docman)) {
612 $this->setError(sprintf(_('Error updating project information: use_docman %s'), db_error()));
617 if ($this->setTags($tags) === false) {
622 $hook_params = array();
623 $hook_params['group'] = $this;
624 $hook_params['group_id'] = $this->getID();
625 $hook_params['group_homepage'] = $homepage;
626 $hook_params['group_name'] = htmlspecialchars($group_name);
627 $hook_params['group_description'] = htmlspecialchars($short_description);
628 $hook_params['group_ispublic'] = $is_public;
629 if (!plugin_hook("group_update", $hook_params)) {
630 if (!$this->isError()) {
631 $this->setError(_('Error updating project information in plugin_hook group_update'));
637 // Log the audit trail
638 $this->addHistory('Changed Public Info', '');
640 if (!$this->fetchData($this->getID())) {
649 * getID - Simply return the group_id for this object.
651 * @return int group_id.
654 return $this->data_array['group_id'];
658 * getType() - Foundry, project, etc.
660 * @return int The type flag from the database.
663 return $this->data_array['type_id'];
668 * getStatus - the status code.
670 * Statuses char include I,H,A,D,P.
671 * TODO : document what these mean :
678 function getStatus() {
679 return $this->data_array['status'];
683 * setStatus - set the status code.
685 * Statuses include I,H,A,D,P.
686 * TODO : document what these mean :
693 * @param object User requesting operation (for access control).
694 * @param string Status value.
695 * @return boolean success.
698 function setStatus(&$user, $status) {
701 if (!forge_check_global_perm('approve_projects')) {
702 $this->setPermissionDeniedError();
706 // Projects in 'A' status can only go to 'H' or 'D'
707 // Projects in 'D' status can only go to 'A'
708 // Projects in 'P' status can only go to 'A' OR 'D'
709 // Projects in 'I' status can only go to 'P'
710 // Projects in 'H' status can only go to 'A' OR 'D'
711 $allowed_status_changes = array(
712 'AH'=>1,'AD'=>1,'DA'=>1,'PA'=>1,'PD'=>1,
713 'IP'=>1,'HA'=>1,'HD'=>1
716 // Check that status transition is valid
717 if ($this->getStatus() != $status
718 && !array_key_exists($this->getStatus(). $status, $allowed_status_changes)) {
719 $this->setError(_('Invalid Status Change From: ').$this->getStatus(). _(' To: '.$status));
725 $res = db_query_params('UPDATE groups
727 WHERE group_id=$2', array($status, $this->getID()));
729 if (!$res || db_affected_rows($res) < 1) {
730 $this->setError(sprintf(_('ERROR: DB: Could not change group status: %s'),db_error()));
736 // Activate system group, if not yet
737 if (!$SYS->sysCheckGroup($this->getID())) {
738 if (!$SYS->sysCreateGroup($this->getID())) {
739 $this->setError($SYS->getErrorMessage());
744 if (!$this->activateUsers()) {
749 /* Otherwise, the group is not active, and make sure that
750 System group is not active either */
751 } elseif ($SYS->sysCheckGroup($this->getID())) {
752 if (!$SYS->sysRemoveGroup($this->getID())) {
753 $this->setError($SYS->getErrorMessage());
759 $hook_params = array();
760 $hook_params['group'] = $this;
761 $hook_params['group_id'] = $this->getID();
762 $hook_params['status'] = $status;
763 plugin_hook("group_setstatus", $hook_params);
767 // Log the audit trail
768 if ($status != $this->getStatus()) {
769 $this->addHistory('Status', $this->getStatus());
772 $this->data_array['status'] = $status;
777 * isProject - Simple boolean test to see if it's a project or not.
779 * @return boolean is_project.
781 function isProject() {
782 if ($this->getType()==1) {
790 * isPublic - Wrapper around RBAC to check if a project is anonymously readable
792 * @return boolean is_public.
794 function isPublic() {
795 $ra = RoleAnonymous::getInstance() ;
796 return $ra->hasPermission('project_read', $this->getID());
800 * isActive - Database field status of 'A' returns true.
802 * @return boolean is_active.
804 function isActive() {
805 if ($this->getStatus()=='A') {
813 * isTemplate - Simply returns the is_template flag from the database.
815 * @return boolean is_template.
817 function isTemplate() {
818 return $this->data_array['is_template'];
822 * setAsTemplate - Set the template status of a project
824 * @param boolean is_template.
826 function setAsTemplate($booleanparam) {
828 $booleanparam = $booleanparam ? 1 : 0;
829 $res = db_query_params('UPDATE groups SET is_template=$1 WHERE group_id=$2',
830 array($booleanparam, $this->getID()));
832 $this->data_array['is_template']=$booleanparam;
842 * getTemplateProject - Return the project template this project is built from
844 * @return object The template project
846 function getTemplateProject() {
847 return group_get_object($this->data_array['built_from_template']);
851 * getUnixName - the unix_name
853 * @return string unix_name.
855 function getUnixName() {
856 return strtolower($this->data_array['unix_group_name']);
860 * getPublicName - the full-length public name.
862 * @return string The group_name.
864 function getPublicName() {
865 return $this->data_array['group_name'];
869 * getRegisterPurpose - the text description of the purpose of this project.
871 * @return string The description.
873 function getRegisterPurpose() {
874 return $this->data_array['register_purpose'];
878 * getDescription - the text description of this project.
880 * @return string The description.
882 function getDescription() {
883 return $this->data_array['short_description'];
887 * getStartDate - the unix time this project was registered.
889 * @return int (unix time) of registration.
891 function getStartDate() {
892 return $this->data_array['register_time'];
896 * getLogoImageID - the id of the logo in the database for this project.
898 * @return int The ID of logo image in db_images table (or 100 if none).
900 function getLogoImageID() {
901 return $this->data_array['logo_image_id'];
905 * getUnixBox - the hostname of the unix box where this project is located.
907 * @return string The name of the unix machine for the group.
909 function getUnixBox() {
910 return $this->data_array['unix_box'];
914 * getSCMBox - the hostname of the scm box where this project is located.
916 * @return string The name of the unix machine for the group.
918 function getSCMBox() {
919 return $this->data_array['scm_box'];
922 * setSCMBox - the hostname of the scm box where this project is located.
924 * @param string The name of the new SCM_BOX
926 function setSCMBox($scm_box) {
928 if ($scm_box == $this->data_array['scm_box']) {
933 $res = db_query_params('UPDATE groups SET scm_box=$1 WHERE group_id=$2', array($scm_box, $this->getID ()));
935 $this->addHistory('scm_box', $this->data_array['scm_box']);
936 $this->data_array['scm_box'] = $scm_box;
941 $this->setError(_("Couldn't insert SCM_BOX to database"));
945 $this->setError(_("SCM Box can't be empty"));
951 * getDomain - the hostname.domain where their web page is located.
953 * @return string The name of the group [web] domain.
955 function getDomain() {
956 return $this->data_array['http_domain'];
960 * getRegistrationPurpose - the text description of the purpose of this project.
962 * @return string The application for project hosting.
964 function getRegistrationPurpose() {
965 return $this->data_array['register_purpose'];
970 * getAdmins() - Get array of Admin user objects.
972 * @return array Array of User objects.
974 function &getAdmins() {
975 $roles = RBACEngine::getInstance()->getRolesByAllowedAction ('project_admin', $this->getID());
979 foreach ($roles as $role) {
980 if (! ($role instanceof RoleExplicit)) {
983 if ($role->getHomeProject() == NULL
984 || $role->getHomeProject()->getID() != $this->getID()) {
988 foreach ($role->getUsers() as $u) {
989 $user_ids[] = $u->getID();
992 return user_get_objects(array_unique($user_ids));
996 Common Group preferences for tools
1000 * enableAnonSCM - whether or not this group has opted to enable Anonymous SCM.
1002 * @return boolean enable_scm.
1004 function enableAnonSCM() {
1005 $r = RoleAnonymous::getInstance();
1006 return $r->hasPermission('scm', $this->getID(), 'read');
1009 function SetUsesAnonSCM($booleanparam) {
1011 $booleanparam = $booleanparam ? 1 : 0;
1012 $r = RoleAnonymous::getInstance();
1013 $r->setSetting('scm', $this->getID(), $booleanparam);
1018 * enablePserver - whether or not this group has opted to enable Pserver.
1020 * @return boolean enable_pserver.
1022 function enablePserver() {
1023 if ($this->usesSCM()) {
1024 return $this->data_array['enable_pserver'];
1030 function SetUsesPserver($booleanparam) {
1032 $booleanparam = $booleanparam ? 1 : 0;
1033 $res = db_query_params('UPDATE groups SET enable_pserver=$1 WHERE group_id=$2',
1034 array($booleanparam, $this->getID()));
1036 $this->data_array['enable_pserver'] = $booleanparam;
1045 * usesSCM - whether or not this group has opted to use SCM.
1047 * @return boolean uses_scm.
1049 function usesSCM() {
1050 if (forge_get_config('use_scm')) {
1051 return $this->data_array['use_scm'];
1058 * setUseSCM - Set the SCM usage
1060 * @param boolean enabled/disabled
1062 function setUseSCM($booleanparam) {
1064 $booleanparam = $booleanparam ? 1 : 0 ;
1065 $res = db_query_params('UPDATE groups SET use_scm=$1 WHERE group_id=$2',
1066 array($booleanparam, $this->getID()));
1068 $this->data_array['use_scm']=$booleanparam;
1078 * usesMail - whether or not this group has opted to use mailing lists.
1080 * @return boolean uses_mail.
1082 function usesMail() {
1083 if (forge_get_config('use_mail')) {
1084 return $this->data_array['use_mail'];
1091 * setUseMail - Set the mailing-list usage
1093 * @param boolean enabled/disabled
1095 function setUseMail($booleanparam) {
1097 $booleanparam = $booleanparam ? 1 : 0 ;
1098 $res = db_query_params('UPDATE groups SET use_mail=$1 WHERE group_id=$2',
1099 array($booleanparam, $this->getID()));
1101 $this->data_array['use_mail']=$booleanparam;
1111 * usesNews - whether or not this group has opted to use news.
1113 * @return boolean uses_news.
1115 function usesNews() {
1116 if (forge_get_config('use_news')) {
1117 return $this->data_array['use_news'];
1124 * usesActivity - whether or not this group has opted to display Project Activities.
1126 * @return boolean uses_activities.
1128 function usesActivity() {
1129 if (forge_get_config('use_activity')) {
1130 return $this->data_array['use_activity'];
1137 * usesForum - whether or not this group has opted to use discussion forums.
1139 * @return boolean uses_forum.
1141 function usesForum() {
1142 if (forge_get_config('use_forum')) {
1143 return $this->data_array['use_forum'];
1150 * setUseForum - Set the forum usage
1152 * @param boolean enabled/disabled
1154 function setUseForum($booleanparam) {
1156 $booleanparam = $booleanparam ? 1 : 0;
1157 $res = db_query_params('UPDATE groups SET use_forum=$1 WHERE group_id=$2',
1158 array($booleanparam, $this->getID()));
1160 $this->data_array['use_forum']=$booleanparam;
1170 * usesStats - whether or not this group has opted to use stats.
1172 * @return boolean uses_stats.
1174 function usesStats() {
1175 return $this->data_array['use_stats'];
1179 * usesFRS - whether or not this group has opted to use file release system.
1181 * @return boolean uses_frs.
1183 function usesFRS() {
1184 if (forge_get_config('use_frs')) {
1185 return $this->data_array['use_frs'];
1192 * setUseFRS - Set the FRS usage
1194 * @param boolean enabled/disabled
1196 function setUseFRS($booleanparam) {
1198 $booleanparam = $booleanparam ? 1 : 0;
1199 $res = db_query_params('UPDATE groups SET use_frs=$1 WHERE group_id=$2',
1200 array ($booleanparam, $this->getID()));
1202 $this->data_array['use_frs']=$booleanparam;
1212 * usesTracker - whether or not this group has opted to use tracker.
1214 * @return boolean uses_tracker.
1216 function usesTracker() {
1217 if (forge_get_config('use_tracker')) {
1218 return $this->data_array['use_tracker'];
1225 * setUseTracker - Set the tracker usage
1227 * @param boolean enabled/disabled
1229 function setUseTracker ($booleanparam) {
1231 $booleanparam = $booleanparam ? 1 : 0 ;
1232 $res = db_query_params ('UPDATE groups SET use_tracker=$1 WHERE group_id=$2',
1233 array ($booleanparam, $this->getID()));
1235 $this->data_array['use_tracker']=$booleanparam;
1245 * useCreateOnline - whether or not this group has opted to use create online documents option.
1247 * @return boolean use_docman_create_online.
1249 function useCreateOnline() {
1250 if (forge_get_config('use_docman')) {
1251 return $this->data_array['use_docman_create_online'];
1258 * usesDocman - whether or not this group has opted to use docman.
1260 * @return boolean use_docman.
1262 function usesDocman() {
1263 if (forge_get_config('use_docman')) {
1264 return $this->data_array['use_docman'];
1271 * setUseDocman - Set the docman usage
1273 * @param boolean enabled/disabled
1275 function setUseDocman($booleanparam) {
1277 $booleanparam = $booleanparam ? 1 : 0;
1278 $res = db_query_params('UPDATE groups SET use_docman = $1 WHERE group_id = $2',
1279 array($booleanparam, $this->getID()));
1281 // check if / doc_group exists, if not create it
1282 $trashdir = db_query_params('select groupname from doc_groups where groupname = $1 and group_id = $2',
1283 array('.trash', $this->getID()));
1284 if ($trashdir && db_numrows($trashdir) == 0) {
1285 $resinsert = db_query_params('insert into doc_groups (groupname, group_id, stateid) values ($1, $2, $3)',
1286 array('.trash', $this->getID(), '2'));
1292 $this->data_array['use_docman'] = $booleanparam;
1302 * useDocmanSearch - whether or not this group has opted to use docman search engine.
1304 * @return boolean use_docman_search.
1306 function useDocmanSearch() {
1307 if (forge_get_config('use_docman')) {
1308 return $this->data_array['use_docman_search'];
1315 * useWebdav - whether or not this group has opted to use webdav interface.
1317 * @return boolean use_docman_search.
1319 function useWebdav() {
1320 if (forge_get_config('use_webdav')) {
1321 return $this->data_array['use_webdav'];
1328 * usesFTP - whether or not this group has opted to use FTP.
1330 * @return boolean uses_ftp.
1332 function usesFTP() {
1333 if (forge_get_config('use_ftp')) {
1334 return $this->data_array['use_ftp'];
1341 * usesSurvey - whether or not this group has opted to use surveys.
1343 * @return boolean uses_survey.
1345 function usesSurvey() {
1346 if (forge_get_config('use_survey')) {
1347 return $this->data_array['use_survey'];
1354 * usesPM - whether or not this group has opted to Project Manager.
1356 * @return boolean uses_projman.
1359 if (forge_get_config('use_pm')) {
1360 return $this->data_array['use_pm'];
1367 * setUsePM - Set the PM usage
1369 * @param boolean enabled/disabled
1371 function setUsePM($booleanparam) {
1373 $booleanparam = $booleanparam ? 1 : 0;
1374 $res = db_query_params('UPDATE groups SET use_pm=$1 WHERE group_id=$2',
1375 array($booleanparam, $this->getID()));
1377 $this->data_array['use_pm']=$booleanparam;
1387 * getPlugins - get a list of all available group plugins
1389 * @return array array containing plugin_id => plugin_name
1391 function getPlugins() {
1392 if (!isset($this->plugins_data)) {
1393 $this->plugins_data = array();
1394 $res = db_query_params('SELECT group_plugin.plugin_id, plugins.plugin_name
1395 FROM group_plugin, plugins
1396 WHERE group_plugin.group_id=$1
1397 AND group_plugin.plugin_id=plugins.plugin_id', array($this->getID()));
1398 $rows = db_numrows($res);
1400 for ($i=0; $i<$rows; $i++) {
1401 $plugin_id = db_result($res, $i, 'plugin_id');
1402 $this->plugins_data[$plugin_id] = db_result($res, $i, 'plugin_name');
1405 return $this->plugins_data;
1409 * usesPlugin - returns true if the group uses a particular plugin
1411 * @param string $pluginname name of the plugin
1412 * @return boolean whether plugin is being used or not
1414 function usesPlugin($pluginname) {
1415 $plugins_data = $this->getPlugins();
1416 foreach ($plugins_data as $p_id => $p_name) {
1417 if ($p_name == $pluginname) {
1425 * added for Codendi compatibility
1426 * usesServices - returns true if the group uses a particular plugin or feature
1428 * @param string name of the plugin
1429 * @return boolean whether plugin is being used or not
1431 function usesService($feature) {
1432 $plugins_data = $this->getPlugins();
1433 $pm = plugin_manager_get_object();
1434 foreach ($plugins_data as $p_id => $p_name) {
1435 if ($p_name == $feature) {
1438 if ($pm->getPluginByName($p_name)->provide($feature)) {
1446 * setPluginUse - enables/disables plugins for the group
1448 * @param string name of the plugin
1449 * @param boolean the new state
1450 * @return string database result
1452 function setPluginUse($pluginname, $val=true) {
1453 if ($val == $this->usesPlugin($pluginname)) {
1454 // State is already good, returning
1457 $res = db_query_params('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1458 array($pluginname));
1459 $rows = db_numrows($res);
1461 // Error: no plugin by that name
1464 $plugin_id = db_result($res,0,'plugin_id');
1466 unset($this->plugins_data);
1468 $res = db_query_params('INSERT INTO group_plugin (group_id, plugin_id) VALUES ($1, $2)',
1469 array($this->getID(),
1473 $res = db_query_params('DELETE FROM group_plugin WHERE group_id=$1 AND plugin_id=$2',
1474 array($this->getID(),
1478 $this->normalizeAllRoles () ;
1482 * getDocEmailAddress - get email address(es) to send doc notifications to.
1484 * @return string email address.
1486 function getDocEmailAddress() {
1487 return $this->data_array['new_doc_address'];
1491 * DocEmailAll - whether or not this group has opted to use receive notices on all doc updates.
1493 * @return boolean email_on_all_doc_updates.
1495 function docEmailAll() {
1496 return $this->data_array['send_all_docs'];
1501 * getHomePage - The URL for this project's home page.
1503 * @return string homepage URL.
1505 function getHomePage() {
1506 if (!preg_match("/^[a-zA-Z][a-zA-Z0-9+.-]*:/",
1507 $this->data_array['homepage'])) {
1508 $this->data_array['homepage'] = util_url_prefix() .
1509 $this->data_array['homepage'];
1511 return $this->data_array['homepage'];
1515 * getTags - Tags of this project.
1517 * @return string List of tags. Comma separated
1519 function getTags() {
1520 $sql = 'SELECT name FROM project_tags WHERE group_id = $1';
1521 $res = db_query_params($sql, array($this->getID()));
1522 return join(', ', util_result_column_to_array($res));
1526 * setTags - Set tags of this project.
1528 * @return string database result.
1530 function setTags($tags) {
1532 $sql = 'DELETE FROM project_tags WHERE group_id=$1';
1533 $res = db_query_params($sql, array($this->getID()));
1535 $this->setError('Deleting old tags: '.db_error());
1539 $inserted = array();
1540 $tags_array = preg_split('/[;,]/', $tags);
1541 foreach ($tags_array as $tag) {
1542 $tag = preg_replace('/[\t\r\n]/', ' ', $tag);
1543 // Allowed caracteres: [A-Z][a-z][0-9] -_&'#+.
1544 if (preg_match('/[^[:alnum:]| |\-|_|\&|\'|#|\+|\.]/', $tag)) {
1545 $this->setError(_('Bad tag name, you only can use the following characters: [A-Z][a-z][0-9]-_&\'#+. and space'));
1550 if ($tag == '' || array_search($tag, $inserted) !== false) continue;
1551 $sql = 'INSERT INTO project_tags (group_id,name) VALUES ($1, $2)';
1552 $res = db_query_params($sql, array($this->getID(), $tag));
1554 $this->setError(_('Setting tags:') . ' ' .
1566 * getPermission - Return a Permission for this Group
1568 * @return object The Permission.
1570 function &getPermission() {
1571 return permission_get_object($this);
1575 function delete($sure, $really_sure, $really_really_sure) {
1576 if (!$sure || !$really_sure || !$really_really_sure) {
1577 $this->setMissingParamsError(_('Please tick all checkboxes.'));
1580 if ($this->getID() == forge_get_config('news_group') ||
1581 $this->getID() == 1 ||
1582 $this->getID() == forge_get_config('stats_group') ||
1583 $this->getID() == forge_get_config('peer_rating_group')) {
1584 $this->setError(_('Cannot Delete System Group'));
1587 $perm = $this->getPermission();
1588 if (!$perm || !is_object($perm)) {
1589 $this->setPermissionDeniedError();
1591 } elseif ($perm->isError()) {
1592 $this->setPermissionDeniedError();
1594 } elseif (!$perm->isSuperUser()) {
1595 $this->setPermissionDeniedError();
1601 // Remove all the members
1603 $members = $this->getMembers();
1604 foreach ($members as $i) {
1605 if(!$this->removeUser($i->getID())) {
1606 $this->setError(_('Could not properly remove member:').' '.$i->getID());
1611 // unlink roles from this project
1612 foreach ($this->getRoles() as $r) {
1613 if ($r->getHomeProject() == NULL
1614 || $r->getHomeProject()->getID() != $this->getID()) {
1615 $r->unlinkProject($this);
1622 if ($this->usesTracker()) {
1623 $atf = new ArtifactTypeFactory($this);
1624 $at_arr = $atf->getArtifactTypes();
1625 foreach ($at_arr as $i) {
1626 if (!is_object($i)) {
1629 if (!$i->delete(1,1)) {
1630 $this->setError(_('Could not properly delete the tracker:').' '.$i->getErrorMessage());
1639 if ($this->usesForum()) {
1640 $f_arr = $ff->getForums();
1641 foreach ($f_arr as $i) {
1642 if (!is_object($i)) {
1645 if(!$i->delete(1,1)) {
1646 $this->setError(_('Could not properly delete the forum:').' '.$i->getErrorMessage());
1652 // Delete Subprojects
1654 if ($this->usesPM()) {
1655 $pgf = new ProjectGroupFactory($this);
1656 $pg_arr = $pgf->getProjectGroups();
1657 foreach ($pg_arr as $i) {
1658 if (!is_object($i)) {
1661 if (!$i->delete(1,1)) {
1662 $this->setError(_('Could not properly delete the ProjectGroup:').' '.$i->getErrorMessage());
1668 // Delete FRS Packages
1670 $res = db_query_params('SELECT * FROM frs_package WHERE group_id=$1',
1671 array($this->getID()));
1673 $this->setError(_('Error FRS Packages: ').db_error());
1678 while ($arr = db_fetch_array($res)) {
1679 $frsp=new FRSPackage($this, $arr['package_id'], $arr);
1680 if (!$frsp->delete(1, 1)) {
1681 $this->setError(_('Could not properly delete the FRSPackage:').' '.$frsp->getErrorMessage());
1688 $news_group=group_get_object(forge_get_config('news_group'));
1689 $res = db_query_params('SELECT forum_id FROM news_bytes WHERE group_id=$1',
1690 array($this->getID()));
1692 $this->setError(_('Error Deleting News: ').db_error());
1697 for ($i=0; $i<db_numrows($res); $i++) {
1698 $Forum = new Forum($news_group,db_result($res,$i,'forum_id'));
1699 if (!$Forum->delete(1,1)) {
1700 $this->setError(_("Could Not Delete News Forum: %d"),$Forum->getID());
1704 $res = db_query_params('DELETE FROM news_bytes WHERE group_id=$1',
1705 array($this->getID()));
1707 $this->setError(_('Error Deleting News: ').db_error());
1715 $res = db_query_params('DELETE FROM doc_data WHERE group_id=$1',
1716 array($this->getID()));
1718 $this->setError(_('Error Deleting Documents: ').db_error());
1723 $res = db_query_params('DELETE FROM doc_groups WHERE group_id=$1',
1724 array($this->getID()));
1726 $this->setError(_('Error Deleting Documents: ').db_error());
1734 $res=db_query_params('DELETE FROM project_tags WHERE group_id=$1', array($this->getID()));
1736 $this->setError(_('Error Deleting Tags: ').db_error());
1742 // Delete group history
1744 $res = db_query_params('DELETE FROM group_history WHERE group_id=$1',
1745 array($this->getID()));
1747 $this->setError(_('Error Deleting Project History: ').db_error());
1753 // Delete group plugins
1755 $res = db_query_params('DELETE FROM group_plugin WHERE group_id=$1',
1756 array($this->getID()));
1758 $this->setError(_('Error Deleting Project Plugins: ').db_error());
1764 // Delete group cvs stats
1766 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE group_id=$1',
1767 array ($this->getID())) ;
1769 $this->setError(_('Error Deleting SCM Statistics: ').db_error());
1777 if ($this->usesSurvey()) {
1778 $sf = new SurveyFactory($this);
1779 $s_arr =& $sf->getSurveys();
1780 foreach ($s_arr as $i) {
1781 if (!is_object($i)) {
1784 if (!$i->delete()) {
1785 $this->setError(_('Could not properly delete the survey'));
1791 // Delete SurveyQuestions
1793 $sqf = new SurveyQuestionFactory($this);
1794 $sq_arr = $sqf->getSurveyQuestions();
1795 if (is_array($sq_arr)) {
1796 foreach ($sq_arr as $i) {
1797 if (!is_object($i)) {
1800 if (!$i->delete()) {
1801 $this->setError(_('Could not properly delete the survey questions'));
1809 // Delete Mailing List Factory
1811 if ($this->usesMail()) {
1812 $mlf = new MailingListFactory($this);
1813 $ml_arr = $mlf->getMailingLists();
1814 foreach ($ml_arr as $i) {
1815 if (!is_object($i)) {
1818 if (!$i->delete(1,1)) {
1819 $this->setError(_('Could not properly delete the mailing list'));
1828 $res = db_query_params('DELETE FROM trove_group_link WHERE group_id=$1',
1829 array($this->getID()));
1831 $this->setError(_('Error Deleting Trove: ').db_error());
1836 $res = db_query_params('DELETE FROM trove_agg WHERE group_id=$1',
1837 array($this->getID()));
1839 $this->setError(_('Error Deleting Trove: ').db_error());
1847 $res = db_query_params('DELETE FROM project_sums_agg WHERE group_id=$1',
1848 array($this->getID()));
1850 $this->setError(_('Error Deleting Counters: ').db_error());
1855 $res = db_query_params('INSERT INTO deleted_groups (unix_group_name, delete_date, isdeleted) VALUES ($1, $2, $3)',
1856 array($this->getUnixName(),
1860 $this->setError(_('Error Deleting Project:').' '.db_error());
1865 // Delete entry in groups.
1866 $res = db_query_params('DELETE FROM groups WHERE group_id=$1',
1867 array($this->getID()));
1869 $this->setError(_('Error Deleting Project:').' '.db_error());
1876 $hook_params = array();
1877 $hook_params['group'] = $this;
1878 $hook_params['group_id'] = $this->getID();
1879 plugin_hook("group_delete", $hook_params);
1881 if (forge_get_config('upload_dir') != '' && $this->getUnixName()) {
1882 exec('/bin/rm -rf '.forge_get_config('upload_dir').'/'.$this->getUnixName().'/');
1884 if (forge_get_config('ftp_upload_dir') != '' && $this->getUnixName()) {
1885 exec('/bin/rm -rf '.forge_get_config('ftp_upload_dir').'/'.$this->getUnixName().'/');
1890 $res = db_query_params('DELETE FROM rep_group_act_monthly WHERE group_id=$1',
1891 array ($this->getID()));
1892 //echo 'rep_group_act_monthly'.db_error();
1893 $res = db_query_params('DELETE FROM rep_group_act_weekly WHERE group_id=$1',
1894 array ($this->getID()));
1895 //echo 'rep_group_act_weekly'.db_error();
1896 $res = db_query_params('DELETE FROM rep_group_act_daily WHERE group_id=$1',
1897 array ($this->getID()));
1898 //echo 'rep_group_act_daily'.db_error();
1899 unset($this->data_array);
1904 Basic functions to add/remove users to/from a group
1905 and update their permissions
1909 * addUser - controls adding a user to a group.
1911 * @param string Unix name of the user to add OR integer user_id.
1912 * @param int The role_id this user should have.
1913 * @return boolean success.
1916 function addUser($user_identifier,$role_id) {
1919 Admins can add users to groups
1922 if (!forge_check_perm ('project_admin', $this->getID())) {
1923 $this->setPermissionDeniedError();
1929 get user id for this user's unix_name
1931 if (is_int ($user_identifier)) { // user_id or user_name
1932 $res_newuser = db_query_params ('SELECT * FROM users WHERE user_id=$1', array ($user_identifier)) ;
1934 $res_newuser = db_query_params ('SELECT * FROM users WHERE user_name=$1', array ($user_identifier)) ;
1936 if (db_numrows($res_newuser) > 0) {
1938 // make sure user is active
1940 if (db_result($res_newuser,0,'status') != 'A') {
1941 $this->setError(_('User is not active. Only active users can be added.'));
1947 // user was found - set new user_id var
1949 $user_id = db_result($res_newuser,0,'user_id');
1951 $role = new Role($this, $role_id);
1952 if (!$role || !is_object($role)) {
1953 $this->setError(_('Error Getting Role Object'));
1956 } elseif ($role->isError()) {
1957 $this->setError('addUser::roleget::'.$role->getErrorMessage());
1962 $role->addUser(user_get_object($user_id)) ;
1963 if (!$SYS->sysCheckCreateGroup($this->getID())){
1964 $this->setError($SYS->getErrorMessage());
1968 if (!$SYS->sysCheckCreateUser($user_id)) {
1969 $this->setError($SYS->getErrorMessage());
1973 if (!$SYS->sysGroupCheckUser($this->getID(),$user_id)) {
1974 $this->setError($SYS->getErrorMessage());
1980 // user doesn't exist
1982 $this->setError(_('ERROR: User does not exist'));
1987 $hook_params['group'] = $this;
1988 $hook_params['group_id'] = $this->getID();
1989 $hook_params['user'] = user_get_object($user_id);
1990 $hook_params['user_id'] = $user_id;
1991 plugin_hook ("group_adduser", $hook_params);
1996 $this->addHistory('Added User',$user_identifier);
2002 * removeUser - controls removing a user from a group.
2004 * Users can remove themselves.
2006 * @param int The ID of the user to remove.
2007 * @return boolean success.
2009 function removeUser($user_id) {
2012 if ($user_id != user_getid()
2013 && !forge_check_perm('project_admin', $this->getID())) {
2014 $this->setPermissionDeniedError();
2020 $user = user_get_object($user_id);
2021 $roles = RBACEngine::getInstance()->getAvailableRolesForUser($user);
2023 foreach ($roles as $role) {
2024 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
2025 $found_role = $role;
2029 if ($found_role == NULL) {
2030 $this->setError(sprintf(_('ERROR: User not removed: %s')));
2034 $found_role->removeUser($user);
2035 if (!$SYS->sysGroupCheckUser($this->getID(), $user_id)) {
2036 $this->setError($SYS->getErrorMessage());
2042 // reassign open artifacts to id=100
2044 $res = db_query_params('UPDATE artifact SET assigned_to=100
2045 WHERE group_artifact_id
2046 IN (SELECT group_artifact_id
2047 FROM artifact_group_list
2048 WHERE group_id=$1 AND status_id=1 AND assigned_to=$2)',
2049 array($this->getID(),
2052 $this->setError(_('ERROR: DB: artifact:').' '.db_error());
2058 // reassign open tasks to id=100
2059 // first have to purge any assignments that would cause
2060 // conflict with existing assignment to 100
2062 $res = db_query_params('DELETE FROM project_assigned_to
2063 WHERE project_task_id IN (SELECT pt.project_task_id
2064 FROM project_task pt, project_group_list pgl, project_assigned_to pat
2065 WHERE pt.group_project_id = pgl.group_project_id
2066 AND pat.project_task_id=pt.project_task_id
2067 AND pt.status_id=1 AND pgl.group_id=$1
2068 AND pat.assigned_to_id=$2)
2069 AND assigned_to_id=100',
2070 array($this->getID(),
2073 $this->setError(sprintf(_('ERROR: DB: project_assigned_to %d: %s'), 1, db_error()));
2077 $res = db_query_params('UPDATE project_assigned_to SET assigned_to_id=100
2078 WHERE project_task_id IN (SELECT pt.project_task_id
2079 FROM project_task pt, project_group_list pgl
2080 WHERE pt.group_project_id = pgl.group_project_id
2081 AND pt.status_id=1 AND pgl.group_id=$1)
2082 AND assigned_to_id=$2',
2083 array($this->getID(),
2086 $this->setError(sprintf(_('ERROR: DB: project_assigned_to %d: %s'), 2, db_error()));
2092 // Remove user from system
2094 if (!$SYS->sysGroupRemoveUser($this->getID(), $user_id)) {
2095 $this->setError($SYS->getErrorMessage());
2100 $hook_params['group'] = $this;
2101 $hook_params['group_id'] = $this->getID();
2102 $hook_params['user'] = user_get_object($user_id);
2103 $hook_params['user_id'] = $user_id;
2104 plugin_hook ("group_removeuser", $hook_params);
2107 $this->addHistory('Removed User',$user_id);
2114 * updateUser - controls updating a user's role in this group.
2116 * @param int The ID of the user.
2117 * @param int The role_id to set this user to.
2118 * @return boolean success.
2120 function updateUser($user_id,$role_id) {
2122 if (!forge_check_perm ('project_admin', $this->getID())) {
2123 $this->setPermissionDeniedError();
2127 $newrole = RBACEngine::getInstance()->getRoleById ($role_id) ;
2128 if (!$newrole || !is_object($newrole)) {
2129 $this->setError(_('Could Not Get Role'));
2131 } elseif ($newrole->isError()) {
2132 $this->setError(sprintf(_('Role: %s'),$role->getErrorMessage()));
2134 } elseif ($newrole->getHomeProject() == NULL
2135 || $newrole->getHomeProject()->getID() != $this->getID()) {
2136 $this->setError(_('Wrong destination role'));
2139 $user = user_get_object ($user_id) ;
2140 $roles = RBACEngine::getInstance()->getAvailableRolesForUser ($user) ;
2141 $found_role = NULL ;
2142 foreach ($roles as $role) {
2143 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
2144 $found_role = $role ;
2148 if ($found_role == NULL) {
2149 $this->setError(sprintf(_('ERROR: User not removed: %s')));
2153 $found_role->removeUser ($user) ;
2154 $newrole->addUser ($user) ;
2156 $this->addHistory('Updated User',$user_id);
2161 * addHistory - Makes an audit trail entry for this project.
2163 * @param string The name of the field.
2164 * @param string The Old Value for this $field_name.
2165 * @return database result handle.
2168 function addHistory($field_name, $old_value) {
2169 return db_query_params ('INSERT INTO group_history(group_id,field_name,old_value,mod_by,adddate)
2170 VALUES ($1,$2,$3,$4,$5)',
2171 array ($this->getID(),
2179 * activateUsers - Make sure that group members have unix accounts.
2181 * Setup unix accounts for group members. Can be called even
2182 * if members are already active.
2186 function activateUsers() {
2188 Activate member(s) of the project
2191 $members = $this->getUsers (true) ;
2193 foreach ($members as $member) {
2195 foreach (RBACEngine::getInstance()->getAvailableRolesForUser ($member) as $role) {
2196 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
2199 if (!$this->addUser($member->getUnixName(),$role->getID())) {
2210 * getMembers - returns array of User objects for this project
2212 * @return array of User objects for this group.
2214 function getMembers() {
2215 return $this->getUsers (true) ;
2219 * replaceTemplateStrings - fill-in some blanks with project name
2221 * @param string Template string
2222 * @return string String after replacements
2224 function replaceTemplateStrings($string) {
2225 $string = str_replace ('UNIXNAME', $this->getUnixName(), $string) ;
2226 $string = str_replace ('PUBLICNAME', $this->getPublicName(), $string) ;
2227 $string = str_replace ('DESCRIPTION', $this->getDescription(), $string) ;
2232 * approve - Approve pending project.
2234 * @param object The User object who is doing the updating.
2237 function approve(&$user) {
2238 global $gfcommon,$gfwww;
2239 require_once $gfcommon.'widget/WidgetLayoutManager.class.php';
2241 if ($this->getStatus()=='A') {
2242 $this->setError(_("Group already active"));
2248 // Step 1: Activate group and create LDAP entries
2249 if (!$this->setStatus($user, 'A')) {
2254 // Switch to system language for item creation
2255 setup_gettext_from_sys_lang();
2257 // Create default roles
2258 $idadmin_group = NULL;
2259 foreach (get_group_join_requests ($this) as $gjr) {
2260 $idadmin_group = $gjr->getUserID();
2263 if ($idadmin_group == NULL) {
2264 $idadmin_group = $user->getID();
2267 $template = $this->getTemplateProject();
2268 $id_mappings = array();
2269 $seen_admin_role = false;
2271 // Copy roles from template project
2272 foreach($template->getRoles() as $oldrole) {
2273 if ($oldrole->getHomeProject() != NULL) {
2274 $role = new Role($this);
2276 // Need to use a different role name so that the permissions aren't set from the hardcoded defaults
2277 $role->create('TEMPORARY ROLE NAME', $data, true);
2278 $role->setName($oldrole->getName());
2279 if ($oldrole->getSetting ('project_admin', $template->getID())) {
2280 $seen_admin_role = true;
2284 $role->linkProject($this);
2286 $id_mappings['role'][$oldrole->getID()] = $role->getID();
2287 // Reuse the project_admin permission
2288 $role->setSetting ('project_admin', $this->getID(), $oldrole->getSetting ('project_admin', $template->getID())) ;
2292 if (!$seen_admin_role) {
2293 $role = new Role($this);
2294 $adminperms = array ('project_admin' => array ($this->getID() => 1)) ;
2295 $role_id = $role->create ('Admin', $adminperms, true) ;
2298 $roles = $this->getRoles() ;
2299 foreach ($roles as $r) {
2300 if ($r->getHomeProject() == NULL) {
2303 if ($r->getSetting ('project_admin', $this->getID())) {
2304 $r->addUser(user_get_object ($idadmin_group));
2308 // Temporarily switch to the submitter's identity
2309 $saved_session = session_get_user();
2310 session_set_internal($idadmin_group);
2313 if (forge_get_config('use_tracker')) {
2314 $this->setUseTracker ($template->usesTracker());
2315 if ($template->usesTracker()) {
2316 $oldatf = new ArtifactTypeFactory($template);
2317 foreach ($oldatf->getArtifactTypes() as $o) {
2318 $t = new ArtifactType ($this) ;
2319 $t->create ($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),$o->emailAll(),$o->getEmailAddress(),$o->getDuePeriod()/86400,0,$o->getSubmitInstructions(),$o->getBrowseInstructions()) ;
2320 $id_mappings['tracker'][$o->getID()] = $t->getID();
2321 $t->cloneFieldsFrom ($o->getID());
2326 if (forge_get_config('use_pm')) {
2327 $this->setUsePM ($template->usesPM());
2328 if ($template->usesPM()) {
2329 $oldpgf = new ProjectGroupFactory($template);
2330 foreach ($oldpgf->getProjectGroups() as $o) {
2331 $pg = new ProjectGroup($this);
2332 $pg->create($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),$o->getSendAllPostsTo());
2333 $id_mappings['pm'][$o->getID()] = $pg->getID();
2338 if (forge_get_config('use_forum')) {
2339 $this->setUseForum($template->usesForum()) ;
2340 if ($template->usesForum()) {
2341 $oldff = new ForumFactory($template) ;
2342 foreach ($oldff->getForums() as $o) {
2343 $f = new Forum($this);
2344 $f->create($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),$o->getSendAllPostsTo(),1);
2345 $id_mappings['forum'][$o->getID()] = $f->getID();
2350 if (forge_get_config('use_docman')) {
2351 $this->setUseDocman($template->usesDocman());
2352 if ($template->usesDocman()) {
2353 $olddgf = new DocumentGroupFactory($template);
2354 // First pass: create all docgroups
2355 $id_mappings['docman_docgroup'][0] = 0;
2356 foreach ($olddgf->getDocumentGroups() as $o) {
2357 $ndgf = new DocumentGroup($this);
2358 // .trash is a reserved directory
2359 if ($o->getName() != '.trash' && $o->getParentID() == 0) {
2360 $ndgf->create($this->replaceTemplateStrings($o->getName()));
2361 $id_mappings['docman_docgroup'][$o->getID()] = $ndgf->getID();
2364 // Second pass: restore hierarchy links
2365 foreach ($olddgf->getDocumentGroups() as $o) {
2366 $ndgf = new DocumentGroup($this);
2367 if ($o->getName() != '.trash' && $o->getParentID() == 0) {
2368 $ndgf->fetchData($id_mappings['docman_docgroup'][$o->getID()]);
2369 $ndgf->update($ndgf->getName(), $id_mappings['docman_docgroup'][$o->getParentID()]);
2375 if (forge_get_config('use_frs')) {
2376 $this->setUseFRS ($template->usesFRS());
2377 if ($template->usesFRS()) {
2378 foreach (get_frs_packages($template) as $o) {
2379 $newp = new FRSPackage($this);
2380 $nname = $this->replaceTemplateStrings($o->getName());
2381 $newp->create ($nname, $o->isPublic());
2386 if (forge_get_config('use_mail')) {
2387 $this->setUseMail($template->usesMail()) ;
2388 if ($template->usesMail()) {
2389 $oldmlf = new MailingListFactory($template);
2390 foreach ($oldmlf->getMailingLists() as $o) {
2391 $ml = new MailingList($this);
2392 $nname = preg_replace ('/^'.$template->getUnixName().'-/','',$o->getName()) ;
2394 $ndescription = $this->replaceTemplateStrings($o->getDescription()) ;
2395 $ml->create($nname, $ndescription, $o->isPublic());
2401 /* use SCM plugin from template group */
2402 $this->setUseSCM($template->usesSCM());
2404 foreach ($template->getPlugins() as
2405 $plugin_id => $plugin_name) {
2406 $this->setPluginUse($plugin_name);
2409 /* use SCM choice from registration page */
2411 foreach ($template->getPlugins() as
2412 $plugin_id => $plugin_name) {
2413 if (substr($plugin_name, 3) == 'scm' &&
2414 $plugin_name != 'scmhook') {
2415 /* skip copying scm plugins */
2418 /* enable other plugins though */
2419 $this->setPluginUse($plugin_name);
2423 foreach ($template->getRoles() as $oldrole) {
2424 $newrole = RBACEngine::getInstance()->getRoleById ($id_mappings['role'][$oldrole->getID()]) ;
2425 if ($oldrole->getHomeProject() != NULL
2426 && $oldrole->getHomeProject()->getID() == $template->getID()) {
2427 $newrole->setPublic ($oldrole->isPublic()) ;
2429 $oldsettings = $oldrole->getSettingsForProject ($template) ;
2431 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
2432 foreach ($sections as $section) {
2433 $newrole->setSetting ($section, $this->getID(), $oldsettings[$section][$template->getID()]) ;
2436 $sections = array ('tracker', 'pm', 'forum') ;
2437 foreach ($sections as $section) {
2438 if (isset ($oldsettings[$section])) {
2439 foreach ($oldsettings[$section] as $k => $v) {
2440 // Only copy perms for tools that have been copied
2441 if (isset ($id_mappings[$section][$k])) {
2442 $newrole->setSetting ($section,
2443 $id_mappings[$section][$k],
2451 $lm = new WidgetLayoutManager();
2452 $lm->createDefaultLayoutForProject ($this->getID(), $template->getID()) ;
2454 $params = array () ;
2455 $params['template'] = $template ;
2456 $params['project'] = $this ;
2457 $params['id_mappings'] = $id_mappings ;
2458 plugin_hook_by_reference ('clone_project_from_template', $params) ;
2460 // Disable everything
2461 db_query_params ('UPDATE groups SET use_mail=0, use_survey=0, use_forum=0, use_pm=0, use_pm_depend_box=0, use_scm=0, use_news=0, use_docman=0, use_ftp=0, use_tracker=0, use_frs=0, use_stats=0 WHERE group_id=$1',
2462 array ($this->getID())) ;
2465 $this->normalizeAllRoles();
2466 $this->activateUsers();
2468 // Delete fake join request
2469 foreach (get_group_join_requests ($this) as $gjr) {
2470 $gjr->delete(true) ;
2473 // Switch back to user preference
2474 session_set_internal($saved_session->getID());
2475 setup_gettext_from_context();
2479 $this->sendApprovalEmail();
2480 $this->addHistory('Approved', 'x');
2483 // Plugin can make approve operation there
2485 $params[0] = $idadmin_group;
2486 $params[1] = $this->getID();
2487 plugin_hook('group_approved', $params);
2495 * sendApprovalEmail - Send new project email.
2497 * @return boolean success.
2500 function sendApprovalEmail() {
2501 $admins = RBACEngine::getInstance()->getUsersByAllowedAction ('project_admin', $this->getID()) ;
2503 if (count($admins) < 1) {
2504 $this->setError(_("Group does not have any administrators."));
2508 // send one email per admin
2509 foreach ($admins as $admin) {
2510 setup_gettext_for_user ($admin) ;
2512 $message=sprintf(_('Your project registration for %4$s has been approved.
2514 Project Full Name: %1$s
2515 Project Unix Name: %2$s
2517 Your DNS will take up to a day to become active on our site.
2518 Your web site is accessible through your shell account. Please read
2519 site documentation (see link below) about intended usage, available
2520 services, and directory layout of the account.
2523 own project page in %4$s while logged in, you will find
2524 additional menu functions to your left labeled \'Project Admin\'.
2526 We highly suggest that you now visit %4$s and create a public
2527 description for your project. This can be done by visiting your project
2528 page while logged in, and selecting \'Project Admin\' from the menus
2529 on the left (or by visiting %3$s
2532 Your project will also not appear in the Trove Software Map (primary
2533 list of projects hosted on %4$s which offers great flexibility in
2534 browsing and search) until you categorize it in the project administration
2535 screens. So that people can find your project, you should do this now.
2536 Visit your project while logged in, and select \'Project Admin\' from the
2539 Enjoy the system, and please tell others about %4$s. Let us know
2540 if there is anything we can do to help you.
2543 htmlspecialchars_decode($this->getPublicName()),
2544 $this->getUnixName(),
2545 util_make_url ('/project/admin/?group_id='.$this->getID()),
2546 forge_get_config ('forge_name'));
2548 util_send_message($admin->getEmail(), sprintf(_('%1$s Project Approved'), forge_get_config ('forge_name')), $message);
2550 setup_gettext_from_context();
2558 * sendRejectionEmail - Send project rejection email.
2560 * This function sends out a rejection message to a user who
2561 * registered a project.
2563 * @param int The id of the response to use.
2564 * @param string The rejection message.
2565 * @return boolean completion status.
2568 function sendRejectionEmail($response_id, $message="zxcv") {
2569 $submitters = array () ;
2570 foreach (get_group_join_requests ($this) as $gjr) {
2571 $submitters[] = user_get_object($gjr->getUserID());
2574 if (count ($submitters) < 1) {
2575 $this->setError(_("Group does not have any administrators."));
2579 foreach ($submitters as $admin) {
2580 setup_gettext_for_user($admin);
2582 $response=sprintf(_('Your project registration for %3$s has been denied.
2584 Project Full Name: %1$s
2585 Project Unix Name: %2$s
2587 Reasons for negative decision:
2589 '), $this->getPublicName(), $this->getUnixName(), forge_get_config('forge_name'));
2591 // Check to see if they want to send a custom rejection response
2592 if ($response_id == 0) {
2593 $response .= $message;
2595 $response .= db_result(
2596 db_query_params('SELECT response_text FROM canned_responses WHERE response_id=$1', array ($response_id)),
2601 util_send_message($admin->getEmail(), sprintf(_('%1$s Project Denied'), forge_get_config ('forge_name')), $response);
2602 setup_gettext_from_context();
2609 * sendNewProjectNotificationEmail - Send new project notification email.
2611 * This function sends out a notification email to the
2612 * SourceForge admin user when a new project is
2615 * @return boolean success.
2618 function sendNewProjectNotificationEmail() {
2619 // Get the user who wants to register the project
2620 $submitters = array();
2621 foreach (get_group_join_requests ($this) as $gjr) {
2622 $submitters[] = user_get_object($gjr->getUserID());
2624 if (count ($submitters) < 1) {
2625 $this->setError(_("Could not find user who has submitted the project."));
2629 $admins = RBACEngine::getInstance()->getUsersByAllowedAction ('approve_projects', -1) ;
2631 if (count($admins) < 1) {
2632 $this->setError(_("There is no administrator to send the mail to."));
2636 foreach ($admins as $admin) {
2637 $admin_email = $admin->getEmail () ;
2638 setup_gettext_for_user ($admin) ;
2640 $message = sprintf(_('New %1$s Project Submitted
2642 Project Full Name: %2$s
2643 Submitted Description: %3$s
2645 forge_get_config ('forge_name'),
2646 htmlspecialchars_decode($this->getPublicName()),
2647 htmlspecialchars_decode($this->getRegistrationPurpose()));
2649 foreach ($submitters as $submitter) {
2650 $message .= sprintf(_('Submitter: %1$s (%2$s)
2652 $submitter->getRealName(),
2653 $submitter->getUnixName());
2656 $message .= sprintf (_('
2657 Please visit the following URL to approve or reject this project:
2659 util_make_url ('/admin/approve-pending.php')) ;
2660 util_send_message($admin_email, sprintf(_('New %1$s Project Submitted'), forge_get_config ('forge_name')), $message);
2661 setup_gettext_from_context();
2665 $email = $submitter->getEmail() ;
2666 setup_gettext_for_user ($submitter) ;
2668 $message=sprintf(_('New %1$s Project Submitted
2670 Project Full Name: %2$s
2671 Submitted Description: %3$s
2673 The %1$s admin team will now examine your project submission. You will be notified of their decision.'), forge_get_config ('forge_name'), $this->getPublicName(), util_unconvert_htmlspecialchars($this->getRegistrationPurpose()), forge_get_config('web_host'));
2675 util_send_message($email, sprintf(_('New %1$s Project Submitted'), forge_get_config ('forge_name')), $message);
2676 setup_gettext_from_context();
2685 * validateGroupName - Validate the group name
2687 * @param string Group name.
2689 * @return boolean an error false and set an error is the group name is invalide otherwise return true
2691 function validateGroupName($group_name) {
2692 if (strlen($group_name)<3) {
2693 $this->setError(_('Group name is too short'));
2695 } elseif (strlen(htmlspecialchars($group_name))>50) {
2696 $this->setError(_('Group name is too long'));
2698 } elseif (group_get_object_by_publicname($group_name)) {
2699 $this->setError(_('Group name already taken'));
2707 * getRolesId - Get Ids of the roles of the group.
2709 * @return array Role ids of this group.
2711 function getRolesId() {
2712 $role_ids = array();
2714 $res = db_query_params('SELECT role_id FROM pfo_role WHERE home_group_id=$1',
2715 array($this->getID()));
2716 while ($arr = db_fetch_array($res)) {
2717 $role_ids[] = $arr['role_id'];
2719 $res = db_query_params('SELECT role_id FROM role_project_refs WHERE group_id=$1',
2720 array($this->getID()));
2721 while ($arr = db_fetch_array($res)) {
2722 $role_ids[] = $arr['role_id'];
2725 return array_unique($role_ids);
2729 * getRoles - Get the roles of the group.
2731 * @return array Roles of this group.
2733 function getRoles() {
2736 $roles = $this->getRolesId();
2737 $engine = RBACEngine::getInstance();
2738 foreach ($roles as $role_id) {
2739 $result[] = $engine->getRoleById ($role_id);
2745 function normalizeAllRoles() {
2746 $roles = $this->getRoles();
2748 foreach ($roles as $r) {
2749 $r->normalizeData();
2754 * getUnixStatus - Status of activation of unix account.
2756 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
2758 function getUnixStatus() {
2759 return $this->data_array['unix_status'];
2763 * setUnixStatus - Sets status of activation of unix account.
2765 * @param string The unix status.
2771 * @return boolean success.
2773 function setUnixStatus($status) {
2776 $res = db_query_params ('UPDATE groups SET unix_status=$1 WHERE group_id=$2',
2781 $this->setError(sprintf(_('ERROR - Could Not Update Group Unix Status: %s'),db_error()));
2785 if ($status == 'A') {
2786 if (!$SYS->sysCheckCreateGroup($this->getID())) {
2787 $this->setError($SYS->getErrorMessage());
2792 if ($SYS->sysCheckGroup($this->getID())) {
2793 if (!$SYS->sysRemoveGroup($this->getID())) {
2794 $this->setError($SYS->getErrorMessage());
2801 $this->data_array['unix_status']=$status;
2808 * getUsers - Get the users of a group
2810 * @return array of user's objects.
2812 function getUsers($onlylocal = true) {
2813 if (!isset($this->membersArr)) {
2814 $this->membersArr = array () ;
2817 foreach ($this->getRoles() as $role) {
2819 && ($role->getHomeProject() == NULL || $role->getHomeProject()->getID() != $this->getID())) {
2822 foreach ($role->getUsers() as $user) {
2823 $ids[] = $user->getID() ;
2826 $ids = array_unique ($ids) ;
2827 foreach ($ids as $id) {
2828 $u = user_get_object ($id) ;
2829 if ($u->isActive()) {
2830 $this->membersArr[] = $u ;
2834 return $this->membersArr;
2837 function setDocmanCreateOnlineStatus($status) {
2839 /* if we activate search engine, we probably want to reindex */
2840 $res = db_query_params('UPDATE groups SET use_docman_create_online=$1 WHERE group_id=$2',
2841 array($status, $this->getID()));
2844 $this->setError(sprintf(_('ERROR - Could Not Update Group DocmanCreateOnline Status: %s'),db_error()));
2848 $this->data_array['use_docman_create_online']=$status;
2854 function setDocmanWebdav($status) {
2856 /* if we activate search engine, we probably want to reindex */
2857 $res = db_query_params('UPDATE groups SET use_webdav=$1 WHERE group_id=$2',
2862 $this->setError(sprintf(_('ERROR - Could Not Update Group UseWebdab Status: %s'),db_error()));
2866 $this->data_array['use_webdav']=$status;
2872 function setDocmanSearchStatus($status) {
2874 /* if we activate search engine, we probably want to reindex */
2875 $res = db_query_params('UPDATE groups SET use_docman_search=$1, force_docman_reindex=$1 WHERE group_id=$2',
2880 $this->setError(sprintf(_('ERROR - Could Not Update Group UseDocmanSearch Status: %s'),db_error()));
2884 $this->data_array['use_docman_search']=$status;
2890 function setDocmanForceReindexSearch($status) {
2892 /* if we activate search engine, we probably want to reindex */
2893 $res = db_query_params('UPDATE groups SET force_docman_reindex=$1 WHERE group_id=$2',
2898 $this->setError(sprintf(_('ERROR - Could Not Update Group force_docman_reindex %s'),db_error()));
2902 $this->data_array['force_docman_reindex']=$status;
2911 * group_getname() - get the group name
2913 * @param int The group ID
2917 function group_getname ($group_id = 0) {
2918 $grp = group_get_object($group_id);
2920 return $grp->getPublicName();
2927 * group_getunixname() - get the unixname for a group
2929 * @param int The group ID
2933 function group_getunixname ($group_id) {
2934 $grp = group_get_object($group_id);
2936 return $grp->getUnixName();
2943 * group_get_result() - Get the group object result ID.
2945 * @param int The group ID
2949 function &group_get_result($group_id=0) {
2950 $grp = group_get_object($group_id);
2952 return $grp->getData();
2958 function getAllProjectTags($onlyvisible = true) {
2959 $res = db_query_params('SELECT project_tags.name, groups.group_id FROM groups, project_tags WHERE groups.group_id = project_tags.group_id AND groups.status = $1 ORDER BY project_tags.name, groups.group_id',
2962 if (!$res || db_numrows($res) == 0) {
2968 while ($arr = db_fetch_array($res)) {
2970 $group_id = $arr[1];
2971 if (!isset($result[$tag])) {
2972 $result[$tag] = array();
2975 if (!$onlyvisible || forge_check_perm('project_read', $group_id)) {
2976 $p = group_get_object($group_id);
2977 $result[$tag][] = array('unix_group_name' => $p->getUnixName(),
2978 'group_id' => $group_id);
2986 * Utility class to compare project based in various criteria (names, unixnames, id, ...)
2989 class ProjectComparator {
2990 var $criterion = 'name' ;
2992 function Compare ($a, $b) {
2993 switch ($this->criterion) {
2996 $namecmp = strcoll ($a->getPublicName(), $b->getPublicName()) ;
2997 if ($namecmp != 0) {
3000 /* If several projects share a same public name */
3001 return strcoll ($a->getUnixName(), $b->getUnixName()) ;
3004 return strcmp ($a->getUnixName(), $b->getUnixName()) ;
3007 $aid = $a->getID() ;
3008 $bid = $b->getID() ;
3012 return ($a < $b) ? -1 : 1;
3018 function sortProjectList (&$list, $criterion='name') {
3019 $cmp = new ProjectComparator () ;
3020 $cmp->criterion = $criterion ;
3022 return usort ($list, array ($cmp, 'Compare')) ;
3027 // c-file-style: "bsd"