5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009-2010, Roland Mas
7 * Copyright 2010-2011, Franck Villaume - Capgemini
8 * Copyright 2010-2011, Alain Peyrat - Alcatel-Lucent
9 * Copyright 2012, 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
60 * @param int Result set handle ("SELECT * FROM groups WHERE group_id=xx")
61 * @return object 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);
153 class Group extends Error {
155 * Associative array of data from db.
157 * @var array $data_array.
162 * array of User objects.
164 * @var array $membersArr.
169 * Whether the use is an admin/super user of this project.
171 * @var bool $is_admin.
176 * Artifact types result handle.
178 * @var int $types_res.
183 * Associative array of data for plugins.
185 * @var array $plugins_data.
191 * Associative array of data for the group menu.
193 * @var array $menu_data.
198 * Group - Group object constructor - use group_get_object() to instantiate.
200 * @param int Required - group_id of the group you want to instantiate.
201 * @param int Database result from select query OR associative array of all columns.
202 * @return boolean success or not
204 function Group($id = false, $res = false) {
207 //setting up an empty object
208 //probably going to call create()
212 if (!$this->fetchData($id)) {
217 // Assoc array was passed in
219 if (is_array($res)) {
220 $this->data_array =& $res;
222 if (db_numrows($res) < 1) {
223 //function in class we extended
224 $this->setError(_('Group Not Found'));
225 $this->data_array=array();
228 //set up an associative array for use by other functions
229 $this->data_array = db_fetch_array_by_row($res, 0);
237 * fetchData - May need to refresh database fields if an update occurred.
239 * @param int The group_id.
240 * @return boolean success or not
242 function fetchData($group_id) {
243 $res = db_query_params ('SELECT * FROM groups WHERE group_id=$1',
245 if (!$res || db_numrows($res) < 1) {
246 $this->setError(sprintf(_('fetchData():: %s'),db_error()));
249 $this->data_array = db_fetch_array($res);
254 * create - Create new group.
256 * This method should be called on empty Group object.
257 * It will add an entry for a pending group/project (status 'P')
259 * @param object The User object.
260 * @param string The full name of the user.
261 * @param string The Unix name of the user.
262 * @param string The new group description.
263 * @param string The purpose of the group.
264 * @param boolean Whether to send an email or not
265 * @param int The id of the project this new project is based on
266 * @return boolean success or not
268 function create(&$user, $group_name, $unix_name, $description, $purpose, $unix_box = 'shell1',
269 $scm_box = 'cvs1', $is_public = 1, $send_mail = true, $built_from_template = 0) {
270 // $user is ignored - anyone can create pending group
273 if ($this->getID()!=0) {
274 $this->setError(_('Group::create: Group object already exists'));
276 } else if (!$this->validateGroupName($group_name)) {
278 } else if (!account_groupnamevalid($unix_name)) {
279 $this->setError(_('Invalid Unix name'));
281 } else if (!$SYS->sysUseUnixName($unix_name)) {
282 $this->setError(_('Unix name already taken'));
284 } else if (db_numrows(db_query_params('SELECT group_id FROM groups WHERE unix_group_name=$1',
285 array($unix_name))) > 0) {
286 $this->setError(_('Unix name already taken'));
288 } else if (strlen($purpose)<10) {
289 $this->setError(_('Please describe your Registration Purpose in a more comprehensive manner'));
291 } else if (strlen($purpose)>1500) {
292 $this->setError(_('The Registration Purpose text is too long. Please make it smaller than 1500 bytes.'));
294 } else if (strlen($description)<10) {
295 $this->setError(_('Describe in a more comprehensive manner your project.'));
299 // Check if sys_use_project_vhost for homepage
300 if (forge_get_config('use_project_vhost')) {
301 $homepage = $unix_name.".".forge_get_config('web_host');
303 $homepage = forge_get_config('web_host')."/www/".$unix_name."/";
308 $res = db_query_params('
324 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)',
325 array (htmlspecialchars ($group_name),
327 htmlspecialchars($description),
328 $unix_name.".".forge_get_config('web_host'),
333 htmlspecialchars($purpose),
336 md5(util_randbytes()),
337 $built_from_template));
338 if (!$res || db_affected_rows($res) < 1) {
339 $this->setError(sprintf(_('ERROR: Could not create group: %s'),db_error()));
344 $id = db_insertid($res, 'groups', 'group_id');
346 $this->setError(sprintf(_('ERROR: Could not get group id: %s'),db_error()));
351 if (!$this->fetchData($id)) {
356 $gjr = new GroupJoinRequest($this);
357 $gjr->create($user->getID(),
358 'Fake GroupJoinRequest to store the creator of a project',
361 $hook_params = array();
362 $hook_params['group'] = $this;
363 $hook_params['group_id'] = $this->getID();
364 $hook_params['group_name'] = $group_name;
365 $hook_params['unix_group_name'] = $unix_name;
366 plugin_hook("group_create", $hook_params);
370 $this->sendNewProjectNotificationEmail();
378 * updateAdmin - Update core properties of group object.
380 * This function require site admin privilege.
382 * @param object User requesting operation (for access control).
383 * @param boolean Whether group is publicly accessible (0/1).
384 * @param int Group type (1-project, 2-foundry).
385 * @param string Machine on which group's home directory located.
386 * @param string Domain which serves group's WWW.
390 function updateAdmin(&$user, $is_public, $type_id, $unix_box, $http_domain) {
391 $perm =& $this->getPermission();
393 if (!$perm || !is_object($perm)) {
394 $this->setError(_('Could not get permission.'));
398 if (!$perm->isSuperUser()) {
399 $this->setError(_('Permission denied.'));
405 $res = db_query_params('
407 SET type_id=$1, unix_box=$2, http_domain=$3
414 if (!$res || db_affected_rows($res) < 1) {
415 $this->setError(_('ERROR: DB: Could not change group properties: %s'),db_error());
420 // Log the audit trail
421 if ($type_id != $this->data_array['type_id']) {
422 $this->addHistory('type_id', $this->data_array['type_id']);
424 if ($unix_box != $this->data_array['unix_box']) {
425 $this->addHistory('unix_box', $this->data_array['unix_box']);
427 if ($http_domain != $this->data_array['http_domain']) {
428 $this->addHistory('http_domain', $this->data_array['http_domain']);
431 if (!$this->fetchData($this->getID())) {
440 * update - Update number of common properties.
442 * Unlike updateAdmin(), this function accessible to project admin.
444 * @param object User requesting operation (for access control).
445 * @param boolean Whether group is publicly accessible (0/1).
446 * @param string Project's license (string ident).
447 * @param int Group type (1-project, 2-foundry).
448 * @param string Machine on which group's home directory located.
449 * @param string Domain which serves group's WWW.
450 * @return int status.
453 function update(&$user, $group_name, $homepage, $short_description, $use_mail, $use_survey, $use_forum,
454 $use_pm, $use_pm_depend_box, $use_scm, $use_news, $use_docman,
455 $new_doc_address, $send_all_docs, $logo_image_id,
456 $use_ftp, $use_tracker, $use_frs, $use_stats, $tags, $is_public) {
458 $perm =& $this->getPermission();
460 if (!$perm || !is_object($perm)) {
461 $this->setError(_('Could not get permission.'));
465 if (!$perm->isAdmin()) {
466 $this->setError(_('Permission denied.'));
470 // Validate some values
471 if ($this->getPublicName() != $group_name) {
472 if (!$this->validateGroupName($group_name)) {
477 if ($new_doc_address) {
478 $invalid_mails = validate_emails($new_doc_address);
479 if (count($invalid_mails) > 0) {
480 $this->setError(sprintf(ngettext('New Doc Address Appeared Invalid: %s', 'New Doc Addresses Appeared Invalid: %s', count($invalid_mails)),implode(',',$invalid_mails)));
485 // in the database, these all default to '1',
486 // so we have to explicity set 0
499 if (!$use_pm_depend_box) {
500 $use_pm_depend_box = 0;
523 if (!$send_all_docs) {
527 $homepage = ltrim($homepage);
529 $homepage = util_make_url('/projects/' . $this->getUnixName() . '/');
532 if (strlen(htmlspecialchars($short_description))>255) {
533 $this->setError(_('Error updating project information: Maximum length for Project Description is 255 chars.'));
539 //XXX not yet actived logo_image_id='$logo_image_id',
540 $res = db_query_params('UPDATE groups
543 short_description=$3,
548 use_pm_depend_box=$8,
558 array(htmlspecialchars($group_name),
560 htmlspecialchars($short_description),
577 $this->setError(sprintf(_('Error updating project information: %s'), db_error()));
582 if (!$this->setUseDocman($use_docman)) {
583 $this->setError(sprintf(_('Error updating project information: use_docman %s'), db_error()));
588 if ($this->setTags($tags) === false) {
593 $hook_params = array();
594 $hook_params['group'] = $this;
595 $hook_params['group_id'] = $this->getID();
596 $hook_params['group_homepage'] = $homepage;
597 $hook_params['group_name'] = htmlspecialchars($group_name);
598 $hook_params['group_description'] = htmlspecialchars($short_description);
599 $hook_params['group_ispublic'] = $is_public;
600 if (!plugin_hook("group_update", $hook_params)) {
601 if (!$this->isError()) {
602 $this->setError(_('Error updating project information in plugin_hook group_update'));
608 // Log the audit trail
609 $this->addHistory('Changed Public Info', '');
611 if (!$this->fetchData($this->getID())) {
620 * getID - Simply return the group_id for this object.
622 * @return int group_id.
625 return $this->data_array['group_id'];
629 * getType() - Foundry, project, etc.
631 * @return int The type flag from the database.
634 return $this->data_array['type_id'];
639 * getStatus - the status code.
641 * Statuses char include I,H,A,D,P.
642 * TODO : document what these mean :
649 function getStatus() {
650 return $this->data_array['status'];
654 * setStatus - set the status code.
656 * Statuses include I,H,A,D,P.
657 * TODO : document what these mean :
664 * @param object User requesting operation (for access control).
665 * @param string Status value.
666 * @return boolean success.
669 function setStatus(&$user, $status) {
672 if (!forge_check_global_perm('approve_projects')) {
673 $this->setPermissionDeniedError();
677 // Projects in 'A' status can only go to 'H' or 'D'
678 // Projects in 'D' status can only go to 'A'
679 // Projects in 'P' status can only go to 'A' OR 'D'
680 // Projects in 'I' status can only go to 'P'
681 // Projects in 'H' status can only go to 'A' OR 'D'
682 $allowed_status_changes = array(
683 'AH'=>1,'AD'=>1,'DA'=>1,'PA'=>1,'PD'=>1,
684 'IP'=>1,'HA'=>1,'HD'=>1
687 // Check that status transition is valid
688 if ($this->getStatus() != $status
689 && !array_key_exists($this->getStatus(). $status,$allowed_status_changes)) {
690 $this->setError(_('Invalid Status Change'));
696 $res = db_query_params('UPDATE groups
698 WHERE group_id=$2', array($status, $this->getID()));
700 if (!$res || db_affected_rows($res) < 1) {
701 $this->setError(sprintf(_('ERROR: DB: Could not change group status: %s'),db_error()));
707 // Activate system group, if not yet
708 if (!$SYS->sysCheckGroup($this->getID())) {
709 if (!$SYS->sysCreateGroup($this->getID())) {
710 $this->setError($SYS->getErrorMessage());
715 if (!$this->activateUsers()) {
720 /* Otherwise, the group is not active, and make sure that
721 System group is not active either */
722 } else if ($SYS->sysCheckGroup($this->getID())) {
723 if (!$SYS->sysRemoveGroup($this->getID())) {
724 $this->setError($SYS->getErrorMessage());
730 $hook_params = array();
731 $hook_params['group'] = $this;
732 $hook_params['group_id'] = $this->getID();
733 $hook_params['status'] = $status;
734 plugin_hook("group_setstatus", $hook_params);
738 // Log the audit trail
739 if ($status != $this->getStatus()) {
740 $this->addHistory('Status', $this->getStatus());
743 $this->data_array['status'] = $status;
748 * isProject - Simple boolean test to see if it's a project or not.
750 * @return boolean is_project.
752 function isProject() {
753 if ($this->getType()==1) {
761 * isPublic - Wrapper around RBAC to check if a project is anonymously readable
763 * @return boolean is_public.
765 function isPublic() {
766 $ra = RoleAnonymous::getInstance() ;
767 return $ra->hasPermission('project_read', $this->getID());
771 * isActive - Database field status of 'A' returns true.
773 * @return boolean is_active.
775 function isActive() {
776 if ($this->getStatus()=='A') {
784 * isTemplate - Simply returns the is_template flag from the database.
786 * @return boolean is_template.
788 function isTemplate() {
789 return $this->data_array['is_template'];
793 * setAsTemplate - Set the template status of a project
795 * @param boolean is_template.
797 function setAsTemplate($booleanparam) {
799 $booleanparam = $booleanparam ? 1 : 0;
800 $res = db_query_params('UPDATE groups SET is_template=$1 WHERE group_id=$2',
801 array($booleanparam, $this->getID()));
803 $this->data_array['is_template']=$booleanparam;
813 * getTemplateProject - Return the project template this project is built from
815 * @return object The template project
817 function getTemplateProject() {
818 return group_get_object($this->data_array['built_from_template']);
822 * getUnixName - the unix_name
824 * @return string unix_name.
826 function getUnixName() {
827 return strtolower($this->data_array['unix_group_name']);
831 * getPublicName - the full-length public name.
833 * @return string The group_name.
835 function getPublicName() {
836 return $this->data_array['group_name'];
840 * getRegisterPurpose - the text description of the purpose of this project.
842 * @return string The description.
844 function getRegisterPurpose() {
845 return $this->data_array['register_purpose'];
849 * getDescription - the text description of this project.
851 * @return string The description.
853 function getDescription() {
854 return $this->data_array['short_description'];
858 * getStartDate - the unix time this project was registered.
860 * @return int (unix time) of registration.
862 function getStartDate() {
863 return $this->data_array['register_time'];
867 * getLogoImageID - the id of the logo in the database for this project.
869 * @return int The ID of logo image in db_images table (or 100 if none).
871 function getLogoImageID() {
872 return $this->data_array['logo_image_id'];
876 * getUnixBox - the hostname of the unix box where this project is located.
878 * @return string The name of the unix machine for the group.
880 function getUnixBox() {
881 return $this->data_array['unix_box'];
885 * getSCMBox - the hostname of the scm box where this project is located.
887 * @return string The name of the unix machine for the group.
889 function getSCMBox() {
890 return $this->data_array['scm_box'];
893 * setSCMBox - the hostname of the scm box where this project is located.
895 * @param string The name of the new SCM_BOX
897 function setSCMBox($scm_box) {
899 if ($scm_box == $this->data_array['scm_box']) {
904 $res = db_query_params('UPDATE groups SET scm_box=$1 WHERE group_id=$2', array($scm_box, $this->getID ()));
906 $this->addHistory('scm_box', $this->data_array['scm_box']);
907 $this->data_array['scm_box'] = $scm_box;
912 $this->setError(_("Couldn't insert SCM_BOX to database"));
916 $this->setError(_("SCM Box can't be empty"));
922 * getDomain - the hostname.domain where their web page is located.
924 * @return string The name of the group [web] domain.
926 function getDomain() {
927 return $this->data_array['http_domain'];
931 * getRegistrationPurpose - the text description of the purpose of this project.
933 * @return string The application for project hosting.
935 function getRegistrationPurpose() {
936 return $this->data_array['register_purpose'];
941 * getAdmins() - Get array of Admin user objects.
943 * @return array Array of User objects.
945 function &getAdmins() {
946 $roles = RBACEngine::getInstance()->getRolesByAllowedAction ('project_admin', $this->getID());
950 foreach ($roles as $role) {
951 if (! ($role instanceof RoleExplicit)) {
954 if ($role->getHomeProject() == NULL
955 || $role->getHomeProject()->getID() != $this->getID()) {
959 foreach ($role->getUsers() as $u) {
960 $user_ids[] = $u->getID();
963 return user_get_objects(array_unique($user_ids));
967 Common Group preferences for tools
971 * enableAnonSCM - whether or not this group has opted to enable Anonymous SCM.
973 * @return boolean enable_scm.
975 function enableAnonSCM() {
976 $r = RoleAnonymous::getInstance();
977 return $r->hasPermission('scm', $this->getID(), 'read');
980 function SetUsesAnonSCM($booleanparam) {
982 $booleanparam = $booleanparam ? 1 : 0;
983 $r = RoleAnonymous::getInstance();
984 $r->setSetting('scm', $this->getID(), $booleanparam);
989 * enablePserver - whether or not this group has opted to enable Pserver.
991 * @return boolean enable_pserver.
993 function enablePserver() {
994 if ($this->usesSCM()) {
995 return $this->data_array['enable_pserver'];
1001 function SetUsesPserver($booleanparam) {
1003 $booleanparam = $booleanparam ? 1 : 0;
1004 $res = db_query_params('UPDATE groups SET enable_pserver=$1 WHERE group_id=$2',
1005 array($booleanparam, $this->getID()));
1007 $this->data_array['enable_pserver'] = $booleanparam;
1016 * usesSCM - whether or not this group has opted to use SCM.
1018 * @return boolean uses_scm.
1020 function usesSCM() {
1021 if (forge_get_config('use_scm')) {
1022 return $this->data_array['use_scm'];
1029 * setUseSCM - Set the SCM usage
1031 * @param boolean enabled/disabled
1033 function setUseSCM($booleanparam) {
1035 $booleanparam = $booleanparam ? 1 : 0 ;
1036 $res = db_query_params('UPDATE groups SET use_scm=$1 WHERE group_id=$2',
1037 array($booleanparam, $this->getID()));
1039 $this->data_array['use_scm']=$booleanparam;
1049 * usesMail - whether or not this group has opted to use mailing lists.
1051 * @return boolean uses_mail.
1053 function usesMail() {
1054 if (forge_get_config('use_mail')) {
1055 return $this->data_array['use_mail'];
1062 * setUseMail - Set the mailing-list usage
1064 * @param boolean enabled/disabled
1066 function setUseMail($booleanparam) {
1068 $booleanparam = $booleanparam ? 1 : 0 ;
1069 $res = db_query_params('UPDATE groups SET use_mail=$1 WHERE group_id=$2',
1070 array($booleanparam, $this->getID()));
1072 $this->data_array['use_mail']=$booleanparam;
1082 * usesNews - whether or not this group has opted to use news.
1084 * @return boolean uses_news.
1086 function usesNews() {
1087 if (forge_get_config('use_news')) {
1088 return $this->data_array['use_news'];
1095 * usesForum - whether or not this group has opted to use discussion forums.
1097 * @return boolean uses_forum.
1099 function usesForum() {
1100 if (forge_get_config('use_forum')) {
1101 return $this->data_array['use_forum'];
1108 * setUseForum - Set the forum usage
1110 * @param boolean enabled/disabled
1112 function setUseForum($booleanparam) {
1114 $booleanparam = $booleanparam ? 1 : 0;
1115 $res = db_query_params('UPDATE groups SET use_forum=$1 WHERE group_id=$2',
1116 array($booleanparam, $this->getID()));
1118 $this->data_array['use_forum']=$booleanparam;
1128 * usesStats - whether or not this group has opted to use stats.
1130 * @return boolean uses_stats.
1132 function usesStats() {
1133 return $this->data_array['use_stats'];
1137 * usesFRS - whether or not this group has opted to use file release system.
1139 * @return boolean uses_frs.
1141 function usesFRS() {
1142 if (forge_get_config('use_frs')) {
1143 return $this->data_array['use_frs'];
1150 * setUseFRS - Set the FRS usage
1152 * @param boolean enabled/disabled
1154 function setUseFRS($booleanparam) {
1156 $booleanparam = $booleanparam ? 1 : 0;
1157 $res = db_query_params('UPDATE groups SET use_frs=$1 WHERE group_id=$2',
1158 array ($booleanparam, $this->getID()));
1160 $this->data_array['use_frs']=$booleanparam;
1170 * usesTracker - whether or not this group has opted to use tracker.
1172 * @return boolean uses_tracker.
1174 function usesTracker() {
1175 if (forge_get_config('use_tracker')) {
1176 return $this->data_array['use_tracker'];
1183 * setUseTracker - Set the tracker usage
1185 * @param boolean enabled/disabled
1187 function setUseTracker ($booleanparam) {
1189 $booleanparam = $booleanparam ? 1 : 0 ;
1190 $res = db_query_params ('UPDATE groups SET use_tracker=$1 WHERE group_id=$2',
1191 array ($booleanparam, $this->getID()));
1193 $this->data_array['use_tracker']=$booleanparam;
1203 * useCreateOnline - whether or not this group has opted to use create online documents option.
1205 * @return boolean use_docman_create_online.
1207 function useCreateOnline() {
1208 if (forge_get_config('use_docman')) {
1209 return $this->data_array['use_docman_create_online'];
1216 * usesDocman - whether or not this group has opted to use docman.
1218 * @return boolean use_docman.
1220 function usesDocman() {
1221 if (forge_get_config('use_docman')) {
1222 return $this->data_array['use_docman'];
1229 * setUseDocman - Set the docman usage
1231 * @param boolean enabled/disabled
1233 function setUseDocman($booleanparam) {
1235 $booleanparam = $booleanparam ? 1 : 0;
1236 $res = db_query_params('UPDATE groups SET use_docman = $1 WHERE group_id = $2',
1237 array($booleanparam, $this->getID()));
1239 // check if / doc_group exists, if not create it
1240 $trashdir = db_query_params('select groupname from doc_groups where groupname = $1 and group_id = $2',
1241 array('.trash', $this->getID()));
1242 if ($trashdir && db_numrows($trashdir) == 0) {
1243 $resinsert = db_query_params('insert into doc_groups (groupname, group_id, stateid) values ($1, $2, $3)',
1244 array('.trash', $this->getID(), '2'));
1250 $this->data_array['use_docman'] = $booleanparam;
1260 * useDocmanSearch - whether or not this group has opted to use docman search engine.
1262 * @return boolean use_docman_search.
1264 function useDocmanSearch() {
1265 if (forge_get_config('use_docman')) {
1266 return $this->data_array['use_docman_search'];
1273 * useWebdav - whether or not this group has opted to use webdav interface.
1275 * @return boolean use_docman_search.
1277 function useWebdav() {
1278 if (forge_get_config('use_webdav')) {
1279 return $this->data_array['use_webdav'];
1286 * usesFTP - whether or not this group has opted to use FTP.
1288 * @return boolean uses_ftp.
1290 function usesFTP() {
1291 if (forge_get_config('use_ftp')) {
1292 return $this->data_array['use_ftp'];
1299 * usesSurvey - whether or not this group has opted to use surveys.
1301 * @return boolean uses_survey.
1303 function usesSurvey() {
1304 if (forge_get_config('use_survey')) {
1305 return $this->data_array['use_survey'];
1312 * usesPM - whether or not this group has opted to Project Manager.
1314 * @return boolean uses_projman.
1317 if (forge_get_config('use_pm')) {
1318 return $this->data_array['use_pm'];
1325 * setUsePM - Set the PM usage
1327 * @param boolean enabled/disabled
1329 function setUsePM($booleanparam) {
1331 $booleanparam = $booleanparam ? 1 : 0;
1332 $res = db_query_params('UPDATE groups SET use_pm=$1 WHERE group_id=$2',
1333 array($booleanparam, $this->getID()));
1335 $this->data_array['use_pm']=$booleanparam;
1345 * getPlugins - get a list of all available group plugins
1347 * @return array array containing plugin_id => plugin_name
1349 function getPlugins() {
1350 if (!isset($this->plugins_data)) {
1351 $this->plugins_data = array();
1352 $res = db_query_params('SELECT group_plugin.plugin_id, plugins.plugin_name
1353 FROM group_plugin, plugins
1354 WHERE group_plugin.group_id=$1
1355 AND group_plugin.plugin_id=plugins.plugin_id', array($this->getID()));
1356 $rows = db_numrows($res);
1358 for ($i=0; $i<$rows; $i++) {
1359 $plugin_id = db_result($res, $i, 'plugin_id');
1360 $this->plugins_data[$plugin_id] = db_result($res, $i, 'plugin_name');
1363 return $this->plugins_data;
1367 * usesPlugin - returns true if the group uses a particular plugin
1369 * @param string name of the plugin
1370 * @return boolean whether plugin is being used or not
1372 function usesPlugin($pluginname) {
1373 $plugins_data = $this->getPlugins();
1374 foreach ($plugins_data as $p_id => $p_name) {
1375 if ($p_name == $pluginname) {
1383 * added for Codendi compatibility
1384 * usesServices - returns true if the group uses a particular plugin or feature
1386 * @param string name of the plugin
1387 * @return boolean whether plugin is being used or not
1389 function usesService($feature) {
1390 $plugins_data = $this->getPlugins();
1391 $pm = plugin_manager_get_object();
1392 foreach ($plugins_data as $p_id => $p_name) {
1393 if ($p_name == $feature) {
1396 if ($pm->getPluginByName($p_name)->provide($feature)) {
1404 * setPluginUse - enables/disables plugins for the group
1406 * @param string name of the plugin
1407 * @param boolean the new state
1408 * @return string database result
1410 function setPluginUse($pluginname, $val=true) {
1411 if ($val == $this->usesPlugin($pluginname)) {
1412 // State is already good, returning
1415 $res = db_query_params('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1416 array($pluginname));
1417 $rows = db_numrows($res);
1419 // Error: no plugin by that name
1422 $plugin_id = db_result($res,0,'plugin_id');
1424 unset($this->plugins_data);
1426 $res = db_query_params('INSERT INTO group_plugin (group_id, plugin_id) VALUES ($1, $2)',
1427 array($this->getID(),
1431 $res = db_query_params('DELETE FROM group_plugin WHERE group_id=$1 AND plugin_id=$2',
1432 array($this->getID(),
1436 $this->normalizeAllRoles () ;
1440 * getDocEmailAddress - get email address(es) to send doc notifications to.
1442 * @return string email address.
1444 function getDocEmailAddress() {
1445 return $this->data_array['new_doc_address'];
1449 * DocEmailAll - whether or not this group has opted to use receive notices on all doc updates.
1451 * @return boolean email_on_all_doc_updates.
1453 function docEmailAll() {
1454 return $this->data_array['send_all_docs'];
1459 * getHomePage - The URL for this project's home page.
1461 * @return string homepage URL.
1463 function getHomePage() {
1464 return $this->data_array['homepage'];
1468 * getTags - Tags of this project.
1470 * @return string List of tags. Comma separated
1472 function getTags() {
1473 $sql = 'SELECT name FROM project_tags WHERE group_id = $1';
1474 $res = db_query_params($sql, array($this->getID()));
1475 return join(', ', util_result_column_to_array($res));
1479 * setTags - Set tags of this project.
1481 * @return string database result.
1483 function setTags($tags) {
1485 $sql = 'DELETE FROM project_tags WHERE group_id=$1';
1486 $res = db_query_params($sql, array($this->getID()));
1488 $this->setError('Deleting old tags: '.db_error());
1492 $inserted = array();
1493 $tags_array = preg_split('/[;,]/', $tags);
1494 foreach ($tags_array as $tag) {
1495 $tag = preg_replace('/[\t\r\n]/', ' ', $tag);
1496 // Allowed caracteres: [A-Z][a-z][0-9] -_&'#+.
1497 if (preg_match('/[^[:alnum:]| |\-|_|\&|\'|#|\+|\.]/', $tag)) {
1498 $this->setError(_('Bad tag name, you only can use the following characters: [A-Z][a-z][0-9]-_&\'#+. and space'));
1503 if ($tag == '' || array_search($tag, $inserted) !== false) continue;
1504 $sql = 'INSERT INTO project_tags (group_id,name) VALUES ($1, $2)';
1505 $res = db_query_params($sql, array($this->getID(), $tag));
1507 $this->setError(_('Setting tags:') . ' ' .
1519 * getPermission - Return a Permission for this Group
1521 * @return object The Permission.
1523 function &getPermission() {
1524 return permission_get_object($this);
1528 function delete($sure, $really_sure, $really_really_sure) {
1529 if (!$sure || !$really_sure || !$really_really_sure) {
1530 $this->setMissingParamsError(_('Please tick all checkboxes.'));
1533 if ($this->getID() == forge_get_config('news_group') ||
1534 $this->getID() == 1 ||
1535 $this->getID() == forge_get_config('stats_group') ||
1536 $this->getID() == forge_get_config('peer_rating_group')) {
1537 $this->setError(_('Cannot Delete System Group'));
1540 $perm = $this->getPermission();
1541 if (!$perm || !is_object($perm)) {
1542 $this->setPermissionDeniedError();
1544 } elseif ($perm->isError()) {
1545 $this->setPermissionDeniedError();
1547 } elseif (!$perm->isSuperUser()) {
1548 $this->setPermissionDeniedError();
1554 // Remove all the members
1556 $members = $this->getMembers();
1557 foreach ($members as $i) {
1558 if(!$this->removeUser($i->getID())) {
1559 $this->setError(_('Could not properly remove member:').' '.$i->getID());
1563 // Failsafe until user_group table is gone
1564 $res = db_query_params('DELETE FROM user_group WHERE group_id=$1',
1565 array($this->getID()));
1567 // unlink roles from this project
1568 $ra = RoleAnonymous::getInstance();
1569 $rl = RoleLoggedIn::getInstance();
1570 $ra->unlinkProject($this);
1571 $rl->unlinkProject($this);
1572 // @todo : unlink all the other roles created in the project...
1577 $atf = new ArtifactTypeFactory($this);
1578 $at_arr = $atf->getArtifactTypes();
1579 foreach ($at_arr as $i) {
1580 if (!is_object($i)) {
1583 if (!$i->delete(1,1)) {
1584 $this->setError(_('Could not properly delete the tracker:').' '.$i->getErrorMessage());
1591 $ff = new ForumFactory($this);
1592 $f_arr = $ff->getForums();
1593 foreach ($f_arr as $i) {
1594 if (!is_object($i)) {
1597 if(!$i->delete(1,1)) {
1598 $this->setError(_('Could not properly delete the forum:').' '.$i->getErrorMessage());
1603 // Delete Subprojects
1605 $pgf = new ProjectGroupFactory($this);
1606 $pg_arr = $pgf->getProjectGroups();
1607 foreach ($pg_arr as $i) {
1608 if (!is_object($i)) {
1611 if (!$i->delete(1,1)) {
1612 $this->setError(_('Could not properly delete the ProjectGroup:').' '.$i->getErrorMessage());
1617 // Delete FRS Packages
1619 $res = db_query_params('SELECT * FROM frs_package WHERE group_id=$1',
1620 array($this->getID()));
1622 $this->setError(_('Error FRS Packages: ').db_error());
1627 while ($arr = db_fetch_array($res)) {
1628 $frsp=new FRSPackage($this, $arr['package_id'], $arr);
1629 if (!$frsp->delete(1, 1)) {
1630 $this->setError(_('Could not properly delete the FRSPackage:').' '.$frsp->getErrorMessage());
1637 $news_group=group_get_object(forge_get_config('news_group'));
1638 $res = db_query_params('SELECT forum_id FROM news_bytes WHERE group_id=$1',
1639 array($this->getID()));
1641 $this->setError(_('Error Deleting News: ').db_error());
1646 for ($i=0; $i<db_numrows($res); $i++) {
1647 $Forum = new Forum($news_group,db_result($res,$i,'forum_id'));
1648 if (!$Forum->delete(1,1)) {
1649 $this->setError(_("Could Not Delete News Forum: %d"),$Forum->getID());
1653 $res = db_query_params('DELETE FROM news_bytes WHERE group_id=$1',
1654 array($this->getID()));
1656 $this->setError(_('Error Deleting News: ').db_error());
1664 $res = db_query_params('DELETE FROM doc_data WHERE group_id=$1',
1665 array($this->getID()));
1667 $this->setError(_('Error Deleting Documents: ').db_error());
1672 $res = db_query_params('DELETE FROM doc_groups WHERE group_id=$1',
1673 array($this->getID()));
1675 $this->setError(_('Error Deleting Documents: ').db_error());
1683 $res=db_query_params('DELETE FROM project_tags WHERE group_id=$1', array($this->getID()));
1685 $this->setError(_('Error Deleting Tags: ').db_error());
1691 // Delete group history
1693 $res = db_query_params('DELETE FROM group_history WHERE group_id=$1',
1694 array($this->getID()));
1696 $this->setError(_('Error Deleting Project History: ').db_error());
1702 // Delete group plugins
1704 $res = db_query_params('DELETE FROM group_plugin WHERE group_id=$1',
1705 array($this->getID()));
1707 $this->setError(_('Error Deleting Project Plugins: ').db_error());
1713 // Delete group cvs stats
1715 $res = db_query_params ('DELETE FROM stats_cvs_group WHERE group_id=$1',
1716 array ($this->getID())) ;
1718 $this->setError(_('Error Deleting SCM Statistics: ').db_error());
1726 $sf = new SurveyFactory($this);
1727 $s_arr =& $sf->getSurveys();
1728 foreach ($s_arr as $i) {
1729 if (!is_object($i)) {
1732 if (!$i->delete()) {
1733 $this->setError(_('Could not properly delete the survey'));
1739 // Delete SurveyQuestions
1741 $sqf = new SurveyQuestionFactory($this);
1742 $sq_arr =& $sqf->getSurveyQuestions();
1743 foreach ($sq_arr as $i) {
1744 if (!is_object($i)) {
1747 if (!$i->delete()) {
1748 $this->setError(_('Could not properly delete the survey questions'));
1754 // Delete Mailing List Factory
1756 $mlf = new MailingListFactory($this);
1757 $ml_arr = $mlf->getMailingLists();
1758 foreach ($ml_arr as $i) {
1759 if (!is_object($i)) {
1762 if (!$i->delete(1,1)) {
1763 $this->setError(_('Could not properly delete the mailing list'));
1771 $res = db_query_params('DELETE FROM trove_group_link WHERE group_id=$1',
1772 array($this->getID()));
1774 $this->setError(_('Error Deleting Trove: ').db_error());
1779 $res = db_query_params('DELETE FROM trove_agg WHERE group_id=$1',
1780 array($this->getID()));
1782 $this->setError(_('Error Deleting Trove: ').db_error());
1790 $res = db_query_params('DELETE FROM project_sums_agg WHERE group_id=$1',
1791 array($this->getID()));
1793 $this->setError(_('Error Deleting Counters: ').db_error());
1798 $res = db_query_params('INSERT INTO deleted_groups (unix_group_name, delete_date, isdeleted) VALUES ($1, $2, $3)',
1799 array($this->getUnixName(),
1803 $this->setError(_('Error Deleting Project:').' '.db_error());
1808 // Delete entry in groups.
1809 $res = db_query_params('DELETE FROM groups WHERE group_id=$1',
1810 array($this->getID()));
1812 $this->setError(_('Error Deleting Project:').' '.db_error());
1819 $hook_params = array();
1820 $hook_params['group'] = $this;
1821 $hook_params['group_id'] = $this->getID();
1822 plugin_hook("group_delete", $hook_params);
1824 if (forge_get_config('upload_dir') != '' && $this->getUnixName()) {
1825 exec('/bin/rm -rf '.forge_get_config('upload_dir').'/'.$this->getUnixName().'/');
1827 if (forge_get_config('ftp_upload_dir') != '' && $this->getUnixName()) {
1828 exec('/bin/rm -rf '.forge_get_config('ftp_upload_dir').'/'.$this->getUnixName().'/');
1833 $res = db_query_params('DELETE FROM rep_group_act_monthly WHERE group_id=$1',
1834 array ($this->getID()));
1835 //echo 'rep_group_act_monthly'.db_error();
1836 $res = db_query_params('DELETE FROM rep_group_act_weekly WHERE group_id=$1',
1837 array ($this->getID()));
1838 //echo 'rep_group_act_weekly'.db_error();
1839 $res = db_query_params('DELETE FROM rep_group_act_daily WHERE group_id=$1',
1840 array ($this->getID()));
1841 //echo 'rep_group_act_daily'.db_error();
1842 unset($this->data_array);
1847 Basic functions to add/remove users to/from a group
1848 and update their permissions
1852 * addUser - controls adding a user to a group.
1854 * @param string Unix name of the user to add OR integer user_id.
1855 * @param int The role_id this user should have.
1856 * @return boolean success.
1859 function addUser($user_identifier,$role_id) {
1862 Admins can add users to groups
1865 if (!forge_check_perm ('project_admin', $this->getID())) {
1866 $this->setPermissionDeniedError();
1872 get user id for this user's unix_name
1874 if (is_int ($user_identifier)) { // user_id or user_name
1875 $res_newuser = db_query_params ('SELECT * FROM users WHERE user_id=$1', array ($user_identifier)) ;
1877 $res_newuser = db_query_params ('SELECT * FROM users WHERE user_name=$1', array ($user_identifier)) ;
1879 if (db_numrows($res_newuser) > 0) {
1881 // make sure user is active
1883 if (db_result($res_newuser,0,'status') != 'A') {
1884 $this->setError(_('User is not active. Only active users can be added.'));
1890 // user was found - set new user_id var
1892 $user_id = db_result($res_newuser,0,'user_id');
1894 $role = new Role($this, $role_id);
1895 if (!$role || !is_object($role)) {
1896 $this->setError(_('Error Getting Role Object'));
1899 } elseif ($role->isError()) {
1900 $this->setError('addUser::roleget::'.$role->getErrorMessage());
1905 $role->addUser(user_get_object($user_id)) ;
1906 if (!$SYS->sysCheckCreateGroup($this->getID())){
1907 $this->setError($SYS->getErrorMessage());
1911 if (!$SYS->sysCheckCreateUser($user_id)) {
1912 $this->setError($SYS->getErrorMessage());
1916 if (!$SYS->sysGroupCheckUser($this->getID(),$user_id)) {
1917 $this->setError($SYS->getErrorMessage());
1923 // user doesn't exist
1925 $this->setError(_('ERROR: User does not exist'));
1930 $hook_params['group'] = $this;
1931 $hook_params['group_id'] = $this->getID();
1932 $hook_params['user'] = user_get_object($user_id);
1933 $hook_params['user_id'] = $user_id;
1934 plugin_hook ("group_adduser", $hook_params);
1939 $this->addHistory('Added User',$user_identifier);
1945 * removeUser - controls removing a user from a group.
1947 * Users can remove themselves.
1949 * @param int The ID of the user to remove.
1950 * @return boolean success.
1952 function removeUser($user_id) {
1955 if ($user_id != user_getid()
1956 && !forge_check_perm('project_admin', $this->getID())) {
1957 $this->setPermissionDeniedError();
1963 $user = user_get_object($user_id);
1964 $roles = RBACEngine::getInstance()->getAvailableRolesForUser($user);
1966 foreach ($roles as $role) {
1967 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
1968 $found_role = $role;
1972 if ($found_role == NULL) {
1973 $this->setError(sprintf(_('ERROR: User not removed: %s')));
1977 $found_role->removeUser($user);
1978 if (!$SYS->sysGroupCheckUser($this->getID(), $user_id)) {
1979 $this->setError($SYS->getErrorMessage());
1985 // reassign open artifacts to id=100
1987 $res = db_query_params('UPDATE artifact SET assigned_to=100
1988 WHERE group_artifact_id
1989 IN (SELECT group_artifact_id
1990 FROM artifact_group_list
1991 WHERE group_id=$1 AND status_id=1 AND assigned_to=$2)',
1992 array($this->getID(),
1995 $this->setError(_('ERROR: DB: artifact:').' '.db_error());
2001 // reassign open tasks to id=100
2002 // first have to purge any assignments that would cause
2003 // conflict with existing assignment to 100
2005 $res = db_query_params('DELETE FROM project_assigned_to
2006 WHERE project_task_id IN (SELECT pt.project_task_id
2007 FROM project_task pt, project_group_list pgl, project_assigned_to pat
2008 WHERE pt.group_project_id = pgl.group_project_id
2009 AND pat.project_task_id=pt.project_task_id
2010 AND pt.status_id=1 AND pgl.group_id=$1
2011 AND pat.assigned_to_id=$2)
2012 AND assigned_to_id=100',
2013 array($this->getID(),
2016 $this->setError(sprintf(_('ERROR: DB: project_assigned_to %d: %s'), 1, db_error()));
2020 $res = db_query_params('UPDATE project_assigned_to SET assigned_to_id=100
2021 WHERE project_task_id IN (SELECT pt.project_task_id
2022 FROM project_task pt, project_group_list pgl
2023 WHERE pt.group_project_id = pgl.group_project_id
2024 AND pt.status_id=1 AND pgl.group_id=$1)
2025 AND assigned_to_id=$2',
2026 array($this->getID(),
2029 $this->setError(sprintf(_('ERROR: DB: project_assigned_to %d: %s'), 2, db_error()));
2035 // Remove user from system
2037 if (!$SYS->sysGroupRemoveUser($this->getID(), $user_id)) {
2038 $this->setError($SYS->getErrorMessage());
2043 $hook_params['group'] = $this;
2044 $hook_params['group_id'] = $this->getID();
2045 $hook_params['user'] = user_get_object($user_id);
2046 $hook_params['user_id'] = $user_id;
2047 plugin_hook ("group_removeuser", $hook_params);
2050 $this->addHistory('Removed User',$user_id);
2057 * updateUser - controls updating a user's role in this group.
2059 * @param int The ID of the user.
2060 * @param int The role_id to set this user to.
2061 * @return boolean success.
2063 function updateUser($user_id,$role_id) {
2065 if (!forge_check_perm ('project_admin', $this->getID())) {
2066 $this->setPermissionDeniedError();
2070 $newrole = RBACEngine::getInstance()->getRoleById ($role_id) ;
2071 if (!$newrole || !is_object($newrole)) {
2072 $this->setError(_('Could Not Get Role'));
2074 } elseif ($newrole->isError()) {
2075 $this->setError(sprintf(_('Role: %s'),$role->getErrorMessage()));
2077 } elseif ($newrole->getHomeProject() == NULL
2078 || $newrole->getHomeProject()->getID() != $this->getID()) {
2079 $this->setError(_('Wrong destination role'));
2082 $user = user_get_object ($user_id) ;
2083 $roles = RBACEngine::getInstance()->getAvailableRolesForUser ($user) ;
2084 $found_role = NULL ;
2085 foreach ($roles as $role) {
2086 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
2087 $found_role = $role ;
2091 if ($found_role == NULL) {
2092 $this->setError(sprintf(_('ERROR: User not removed: %s')));
2096 $found_role->removeUser ($user) ;
2097 $newrole->addUser ($user) ;
2099 $this->addHistory('Updated User',$user_id);
2104 * addHistory - Makes an audit trail entry for this project.
2106 * @param string The name of the field.
2107 * @param string The Old Value for this $field_name.
2108 * @return database result handle.
2111 function addHistory($field_name, $old_value) {
2112 return db_query_params ('INSERT INTO group_history(group_id,field_name,old_value,mod_by,adddate)
2113 VALUES ($1,$2,$3,$4,$5)',
2114 array ($this->getID(),
2122 * activateUsers - Make sure that group members have unix accounts.
2124 * Setup unix accounts for group members. Can be called even
2125 * if members are already active.
2129 function activateUsers() {
2131 Activate member(s) of the project
2134 $members = $this->getUsers (true) ;
2136 foreach ($members as $member) {
2138 foreach (RBACEngine::getInstance()->getAvailableRolesForUser ($member) as $role) {
2139 if ($role->getHomeProject() && $role->getHomeProject()->getID() == $this->getID()) {
2142 if (!$this->addUser($member->getUnixName(),$role->getID())) {
2153 * getMembers - returns array of User objects for this project
2155 * @return array of User objects for this group.
2157 function getMembers() {
2158 return $this->getUsers (true) ;
2162 * replaceTemplateStrings - fill-in some blanks with project name
2164 * @param string Template string
2165 * @return string String after replacements
2167 function replaceTemplateStrings($string) {
2168 $string = str_replace ('UNIXNAME', $this->getUnixName(), $string) ;
2169 $string = str_replace ('PUBLICNAME', $this->getPublicName(), $string) ;
2170 $string = str_replace ('DESCRIPTION', $this->getDescription(), $string) ;
2175 * approve - Approve pending project.
2177 * @param object The User object who is doing the updating.
2180 function approve(&$user) {
2181 global $gfcommon,$gfwww;
2182 require_once $gfcommon.'widget/WidgetLayoutManager.class.php';
2184 if ($this->getStatus()=='A') {
2185 $this->setError(_("Group already active"));
2191 // Step 1: Activate group and create LDAP entries
2192 if (!$this->setStatus($user, 'A')) {
2197 // Switch to system language for item creation
2198 setup_gettext_from_sys_lang();
2200 // Create default roles
2201 $idadmin_group = NULL;
2202 foreach (get_group_join_requests ($this) as $gjr) {
2203 $idadmin_group = $gjr->getUserID();
2206 if ($idadmin_group == NULL) {
2207 $idadmin_group = $user->getID();
2210 $template = $this->getTemplateProject();
2211 $id_mappings = array();
2212 $seen_admin_role = false;
2214 // Copy roles from template project
2215 foreach($template->getRoles() as $oldrole) {
2216 if ($oldrole->getHomeProject() != NULL) {
2217 $role = new Role($this);
2219 // Need to use a different role name so that the permissions aren't set from the hardcoded defaults
2220 $role->create('TEMPORARY ROLE NAME', $data, true);
2221 $role->setName($oldrole->getName());
2222 if ($oldrole->getSetting ('project_admin', $template->getID())) {
2223 $seen_admin_role = true;
2227 $role->linkProject($this);
2229 $id_mappings['role'][$oldrole->getID()] = $role->getID();
2230 // Reuse the project_admin permission
2231 $role->setSetting ('project_admin', $this->getID(), $oldrole->getSetting ('project_admin', $template->getID())) ;
2235 if (!$seen_admin_role) {
2236 $role = new Role($this);
2237 $adminperms = array ('project_admin' => array ($this->getID() => 1)) ;
2238 $role_id = $role->create ('Admin', $adminperms, true) ;
2241 $roles = $this->getRoles() ;
2242 foreach ($roles as $r) {
2243 if ($r->getHomeProject() == NULL) {
2246 if ($r->getSetting ('project_admin', $this->getID())) {
2247 $r->addUser(user_get_object ($idadmin_group));
2251 // Temporarily switch to the submitter's identity
2252 $saved_session = session_get_user();
2253 session_set_internal($idadmin_group);
2256 if (forge_get_config('use_tracker')) {
2257 $this->setUseTracker ($template->usesTracker());
2258 if ($template->usesTracker()) {
2259 $oldatf = new ArtifactTypeFactory($template);
2260 foreach ($oldatf->getArtifactTypes() as $o) {
2261 $t = new ArtifactType ($this) ;
2262 $t->create ($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),$o->isPublic(),$o->allowsAnon(),$o->emailAll(),$o->getEmailAddress(),$o->getDuePeriod()/86400,0,$o->getSubmitInstructions(),$o->getBrowseInstructions()) ;
2263 $id_mappings['tracker'][$o->getID()] = $t->getID();
2264 $t->cloneFieldsFrom ($o->getID());
2269 if (forge_get_config('use_pm')) {
2270 $this->setUsePM ($template->usesPM());
2271 if ($template->usesPM()) {
2272 $oldpgf = new ProjectGroupFactory($template);
2273 foreach ($oldpgf->getProjectGroups() as $o) {
2274 $pg = new ProjectGroup($this);
2275 $pg->create($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),$o->isPublic(),$o->getSendAllPostsTo());
2276 $id_mappings['pm'][$o->getID()] = $pg->getID();
2281 if (forge_get_config('use_forum')) {
2282 $this->setUseForum($template->usesForum()) ;
2283 if ($template->usesForum()) {
2284 $oldff = new ForumFactory($template) ;
2285 foreach ($oldff->getForums() as $o) {
2286 $f = new Forum($this);
2287 $f->create($this->replaceTemplateStrings($o->getName()),$this->replaceTemplateStrings($o->getDescription()),0,$o->getSendAllPostsTo(),1,0,0);
2288 $id_mappings['forum'][$o->getID()] = $f->getID();
2293 if (forge_get_config('use_docman')) {
2294 $this->setUseDocman($template->usesDocman());
2295 if ($template->usesDocman()) {
2296 $olddgf = new DocumentGroupFactory($template);
2297 // First pass: create all docgroups
2298 $id_mappings['docman_docgroup'][0] = 0;
2299 foreach ($olddgf->getDocumentGroups() as $o) {
2300 $ndgf = new DocumentGroup($this);
2301 // .trash is a reserved directory
2302 if ($o->getName() != '.trash' && $o->getParentID() == 0) {
2303 $ndgf->create($this->replaceTemplateStrings($o->getName()));
2304 $id_mappings['docman_docgroup'][$o->getID()] = $ndgf->getID();
2307 // Second pass: restore hierarchy links
2308 foreach ($olddgf->getDocumentGroups() as $o) {
2309 $ndgf = new DocumentGroup($this);
2310 if ($o->getName() != '.trash' && $o->getParentID() == 0) {
2311 $ndgf->fetchData($id_mappings['docman_docgroup'][$o->getID()]);
2312 $ndgf->update($ndgf->getName(), $id_mappings['docman_docgroup'][$o->getParentID()]);
2318 if (forge_get_config('use_frs')) {
2319 $this->setUseFRS ($template->usesFRS());
2320 if ($template->usesFRS()) {
2321 foreach (get_frs_packages($template) as $o) {
2322 $newp = new FRSPackage($this);
2323 $nname = $this->replaceTemplateStrings($o->getName());
2324 $newp->create ($nname, $o->isPublic());
2329 if (forge_get_config('use_mail')) {
2330 $this->setUseMail($template->usesMail()) ;
2331 if ($template->usesMail()) {
2332 $oldmlf = new MailingListFactory($template);
2333 foreach ($oldmlf->getMailingLists() as $o) {
2334 $ml = new MailingList($this);
2335 $nname = preg_replace ('/^'.$template->getUnixName().'-/','',$o->getName()) ;
2337 $ndescription = $this->replaceTemplateStrings($o->getDescription()) ;
2338 $ml->create($nname, $ndescription, $o->isPublic());
2343 // Disabled to keep user choice from registration page.
2344 // $this->setUseSCM ($template->usesSCM()) ;
2346 foreach ($template->getPlugins() as $plugin_id => $plugin_name) {
2347 if (preg_match('/^scm/', $plugin_name) && ($plugin_name != 'scmhook')) {
2348 $this->setPluginUse($plugin_name);
2352 foreach ($template->getRoles() as $oldrole) {
2353 $newrole = RBACEngine::getInstance()->getRoleById ($id_mappings['role'][$oldrole->getID()]) ;
2354 if ($oldrole->getHomeProject() != NULL
2355 && $oldrole->getHomeProject()->getID() == $template->getID()) {
2356 $newrole->setPublic ($oldrole->isPublic()) ;
2358 $oldsettings = $oldrole->getSettingsForProject ($template) ;
2360 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
2361 foreach ($sections as $section) {
2362 $newrole->setSetting ($section, $this->getID(), $oldsettings[$section][$template->getID()]) ;
2365 $sections = array ('tracker', 'pm', 'forum') ;
2366 foreach ($sections as $section) {
2367 if (isset ($oldsettings[$section])) {
2368 foreach ($oldsettings[$section] as $k => $v) {
2369 // Only copy perms for tools that have been copied
2370 if (isset ($id_mappings[$section][$k])) {
2371 $newrole->setSetting ($section,
2372 $id_mappings[$section][$k],
2380 $lm = new WidgetLayoutManager();
2381 $lm->createDefaultLayoutForProject ($this->getID(), $template->getID()) ;
2383 $params = array () ;
2384 $params['template'] = $template ;
2385 $params['project'] = $this ;
2386 $params['id_mappings'] = $id_mappings ;
2387 plugin_hook_by_reference ('clone_project_from_template', $params) ;
2389 // Disable everything
2390 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',
2391 array ($this->getID())) ;
2394 $this->normalizeAllRoles();
2395 $this->activateUsers();
2397 // Delete fake join request
2398 foreach (get_group_join_requests ($this) as $gjr) {
2399 $gjr->delete(true) ;
2402 // Switch back to user preference
2403 session_set_internal($saved_session->getID());
2404 setup_gettext_from_context();
2408 $this->sendApprovalEmail();
2409 $this->addHistory('Approved', 'x');
2412 // Plugin can make approve operation there
2414 $params[0] = $idadmin_group;
2415 $params[1] = $this->getID();
2416 plugin_hook('group_approved', $params);
2424 * sendApprovalEmail - Send new project email.
2426 * @return boolean success.
2429 function sendApprovalEmail() {
2430 $admins = RBACEngine::getInstance()->getUsersByAllowedAction ('project_admin', $this->getID()) ;
2432 if (count($admins) < 1) {
2433 $this->setError(_("Group does not have any administrators."));
2437 // send one email per admin
2438 foreach ($admins as $admin) {
2439 setup_gettext_for_user ($admin) ;
2441 $message=sprintf(_('Your project registration for %4$s has been approved.
2443 Project Full Name: %1$s
2444 Project Unix Name: %2$s
2446 Your DNS will take up to a day to become active on our site.
2447 Your web site is accessible through your shell account. Please read
2448 site documentation (see link below) about intended usage, available
2449 services, and directory layout of the account.
2452 own project page in %4$s while logged in, you will find
2453 additional menu functions to your left labeled \'Project Admin\'.
2455 We highly suggest that you now visit %4$s and create a public
2456 description for your project. This can be done by visiting your project
2457 page while logged in, and selecting \'Project Admin\' from the menus
2458 on the left (or by visiting %3$s
2461 Your project will also not appear in the Trove Software Map (primary
2462 list of projects hosted on %4$s which offers great flexibility in
2463 browsing and search) until you categorize it in the project administration
2464 screens. So that people can find your project, you should do this now.
2465 Visit your project while logged in, and select \'Project Admin\' from the
2468 Enjoy the system, and please tell others about %4$s. Let us know
2469 if there is anything we can do to help you.
2472 htmlspecialchars_decode($this->getPublicName()),
2473 $this->getUnixName(),
2474 util_make_url ('/project/admin/?group_id='.$this->getID()),
2475 forge_get_config ('forge_name'));
2477 util_send_message($admin->getEmail(), sprintf(_('%1$s Project Approved'), forge_get_config ('forge_name')), $message);
2479 setup_gettext_from_context();
2487 * sendRejectionEmail - Send project rejection email.
2489 * This function sends out a rejection message to a user who
2490 * registered a project.
2492 * @param int The id of the response to use.
2493 * @param string The rejection message.
2494 * @return boolean completion status.
2497 function sendRejectionEmail($response_id, $message="zxcv") {
2498 $submitters = array () ;
2499 foreach (get_group_join_requests ($this) as $gjr) {
2500 $submitters[] = user_get_object($gjr->getUserID());
2503 if (count ($submitters) < 1) {
2504 $this->setError(_("Group does not have any administrators."));
2508 foreach ($submitters as $admin) {
2509 setup_gettext_for_user($admin);
2511 $response=sprintf(_('Your project registration for %3$s has been denied.
2513 Project Full Name: %1$s
2514 Project Unix Name: %2$s
2516 Reasons for negative decision:
2518 '), $this->getPublicName(), $this->getUnixName(), forge_get_config('forge_name'));
2520 // Check to see if they want to send a custom rejection response
2521 if ($response_id == 0) {
2522 $response .= $message;
2524 $response .= db_result(
2525 db_query_params('SELECT response_text FROM canned_responses WHERE response_id=$1', array ($response_id)),
2530 util_send_message($admin->getEmail(), sprintf(_('%1$s Project Denied'), forge_get_config ('forge_name')), $response);
2531 setup_gettext_from_context();
2538 * sendNewProjectNotificationEmail - Send new project notification email.
2540 * This function sends out a notification email to the
2541 * SourceForge admin user when a new project is
2544 * @return boolean success.
2547 function sendNewProjectNotificationEmail() {
2548 // Get the user who wants to register the project
2549 $submitters = array();
2550 foreach (get_group_join_requests ($this) as $gjr) {
2551 $submitters[] = user_get_object($gjr->getUserID());
2553 if (count ($submitters) < 1) {
2554 $this->setError(_("Could not find user who has submitted the project."));
2558 $admins = RBACEngine::getInstance()->getUsersByAllowedAction ('approve_projects', -1) ;
2560 if (count($admins) < 1) {
2561 $this->setError(_("There is no administrator to send the mail to."));
2565 foreach ($admins as $admin) {
2566 $admin_email = $admin->getEmail () ;
2567 setup_gettext_for_user ($admin) ;
2569 $message = sprintf(_('New %1$s Project Submitted
2571 Project Full Name: %2$s
2572 Submitted Description: %3$s
2574 forge_get_config ('forge_name'),
2575 htmlspecialchars_decode($this->getPublicName()),
2576 htmlspecialchars_decode($this->getRegistrationPurpose()));
2578 foreach ($submitters as $submitter) {
2579 $message .= sprintf(_('Submitter: %1$s (%2$s)
2581 $submitter->getRealName(),
2582 $submitter->getUnixName());
2585 $message .= sprintf (_('
2586 Please visit the following URL to approve or reject this project:
2588 util_make_url ('/admin/approve-pending.php')) ;
2589 util_send_message($admin_email, sprintf(_('New %1$s Project Submitted'), forge_get_config ('forge_name')), $message);
2590 setup_gettext_from_context();
2594 $email = $submitter->getEmail() ;
2595 setup_gettext_for_user ($submitter) ;
2597 $message=sprintf(_('New %1$s Project Submitted
2599 Project Full Name: %2$s
2600 Submitted Description: %3$s
2602 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'));
2604 util_send_message($email, sprintf(_('New %1$s Project Submitted'), forge_get_config ('forge_name')), $message);
2605 setup_gettext_from_context();
2614 * validateGroupName - Validate the group name
2616 * @param string Group name.
2618 * @return boolean an error false and set an error is the group name is invalide otherwise return true
2620 function validateGroupName($group_name) {
2621 if (strlen($group_name)<3) {
2622 $this->setError(_('Group name is too short'));
2624 } else if (strlen(htmlspecialchars($group_name))>50) {
2625 $this->setError(_('Group name is too long'));
2627 } else if (group_get_object_by_publicname($group_name)) {
2628 $this->setError(_('Group name already taken'));
2636 * getRolesId - Get Ids of the roles of the group.
2638 * @return array Role ids of this group.
2640 function getRolesId() {
2641 $role_ids = array();
2643 $res = db_query_params('SELECT role_id FROM pfo_role WHERE home_group_id=$1',
2644 array($this->getID()));
2645 while ($arr = db_fetch_array($res)) {
2646 $role_ids[] = $arr['role_id'];
2648 $res = db_query_params('SELECT role_id FROM role_project_refs WHERE group_id=$1',
2649 array($this->getID()));
2650 while ($arr = db_fetch_array($res)) {
2651 $role_ids[] = $arr['role_id'];
2654 return array_unique($role_ids);
2658 * getRoles - Get the roles of the group.
2660 * @return array Roles of this group.
2662 function getRoles() {
2665 $roles = $this->getRolesId();
2666 $engine = RBACEngine::getInstance();
2667 foreach ($roles as $role_id) {
2668 $result[] = $engine->getRoleById ($role_id);
2674 function normalizeAllRoles() {
2675 $roles = $this->getRoles();
2677 foreach ($roles as $r) {
2678 $r->normalizeData();
2683 * getUnixStatus - Status of activation of unix account.
2685 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
2687 function getUnixStatus() {
2688 return $this->data_array['unix_status'];
2692 * setUnixStatus - Sets status of activation of unix account.
2694 * @param string The unix status.
2700 * @return boolean success.
2702 function setUnixStatus($status) {
2705 $res = db_query_params ('UPDATE groups SET unix_status=$1 WHERE group_id=$2',
2710 $this->setError(sprintf(_('ERROR - Could Not Update Group Unix Status: %s'),db_error()));
2714 if ($status == 'A') {
2715 if (!$SYS->sysCheckCreateGroup($this->getID())) {
2716 $this->setError($SYS->getErrorMessage());
2721 if ($SYS->sysCheckGroup($this->getID())) {
2722 if (!$SYS->sysRemoveGroup($this->getID())) {
2723 $this->setError($SYS->getErrorMessage());
2730 $this->data_array['unix_status']=$status;
2737 * getUsers - Get the users of a group
2739 * @return array of user's objects.
2741 function getUsers($onlylocal = true) {
2742 if (!isset($this->membersArr)) {
2743 $this->membersArr = array () ;
2746 foreach ($this->getRoles() as $role) {
2748 && ($role->getHomeProject() == NULL || $role->getHomeProject()->getID() != $this->getID())) {
2751 foreach ($role->getUsers() as $user) {
2752 $ids[] = $user->getID() ;
2755 $ids = array_unique ($ids) ;
2756 foreach ($ids as $id) {
2757 $u = user_get_object ($id) ;
2758 if ($u->isActive()) {
2759 $this->membersArr[] = $u ;
2763 return $this->membersArr;
2766 function setDocmanCreateOnlineStatus($status) {
2768 /* if we activate search engine, we probably want to reindex */
2769 $res = db_query_params('UPDATE groups SET use_docman_create_online=$1 WHERE group_id=$2',
2770 array($status, $this->getID()));
2773 $this->setError(sprintf(_('ERROR - Could Not Update Group DocmanCreateOnline Status: %s'),db_error()));
2777 $this->data_array['use_docman_create_online']=$status;
2783 function setDocmanWebdav($status) {
2785 /* if we activate search engine, we probably want to reindex */
2786 $res = db_query_params('UPDATE groups SET use_webdav=$1 WHERE group_id=$2',
2791 $this->setError(sprintf(_('ERROR - Could Not Update Group UseWebdab Status: %s'),db_error()));
2795 $this->data_array['use_webdav']=$status;
2801 function setDocmanSearchStatus($status) {
2803 /* if we activate search engine, we probably want to reindex */
2804 $res = db_query_params('UPDATE groups SET use_docman_search=$1, force_docman_reindex=$1 WHERE group_id=$2',
2809 $this->setError(sprintf(_('ERROR - Could Not Update Group UseDocmanSearch Status: %s'),db_error()));
2813 $this->data_array['use_docman_search']=$status;
2819 function setDocmanForceReindexSearch($status) {
2821 /* if we activate search engine, we probably want to reindex */
2822 $res = db_query_params('UPDATE groups SET force_docman_reindex=$1 WHERE group_id=$2',
2827 $this->setError(sprintf(_('ERROR - Could Not Update Group force_docman_reindex %s'),db_error()));
2831 $this->data_array['force_docman_reindex']=$status;
2837 function setStorageAPI($type) {
2841 function getStorageAPI() {
2847 * group_getname() - get the group name
2849 * @param int The group ID
2853 function group_getname ($group_id = 0) {
2854 $grp = group_get_object($group_id);
2856 return $grp->getPublicName();
2863 * group_getunixname() - get the unixname for a group
2865 * @param int The group ID
2869 function group_getunixname ($group_id) {
2870 $grp = group_get_object($group_id);
2872 return $grp->getUnixName();
2879 * group_get_result() - Get the group object result ID.
2881 * @param int The group ID
2885 function &group_get_result($group_id=0) {
2886 $grp = group_get_object($group_id);
2888 return $grp->getData();
2894 function getAllProjectTags($onlyvisible = true) {
2895 $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',
2898 if (!$res || db_numrows($res) == 0) {
2904 while ($arr = db_fetch_array($res)) {
2906 $group_id = $arr[1];
2907 if (!isset($result[$tag])) {
2908 $result[$tag] = array();
2911 if (!$onlyvisible || forge_check_perm('project_read', $group_id)) {
2912 $p = group_get_object($group_id);
2913 $result[$tag][] = array('unix_group_name' => $p->getUnixName(),
2914 'group_id' => $group_id);
2922 * Utility class to compare project based in various criteria (names, unixnames, id, ...)
2925 class ProjectComparator {
2926 var $criterion = 'name' ;
2928 function Compare ($a, $b) {
2929 switch ($this->criterion) {
2932 $namecmp = strcoll ($a->getPublicName(), $b->getPublicName()) ;
2933 if ($namecmp != 0) {
2936 /* If several projects share a same public name */
2937 return strcoll ($a->getUnixName(), $b->getUnixName()) ;
2940 return strcmp ($a->getUnixName(), $b->getUnixName()) ;
2943 $aid = $a->getID() ;
2944 $bid = $b->getID() ;
2948 return ($a < $b) ? -1 : 1;
2954 function sortProjectList (&$list, $criterion='name') {
2955 $cmp = new ProjectComparator () ;
2956 $cmp->criterion = $criterion ;
2958 return usort ($list, array ($cmp, 'Compare')) ;
2963 // c-file-style: "bsd"