5 * Copyright 2005, GForge, LLC
6 * Copyright 2009-2010, Roland Mas
7 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'include/Error.class.php';
26 require_once $gfcommon.'include/Validator.class.php';
28 function &get_group_join_requests($Group) {
30 if ($Group && is_object($Group) && !$Group->isError()) {
31 $res = db_query_params ('SELECT * FROM group_join_request WHERE group_id=$1',
32 array ($Group->getID())) ;
33 if (db_numrows ($res)) {
34 while ($arr = db_fetch_array($res)) {
35 $reqs[] = new GroupJoinRequest($Group,$arr['user_id'],$arr);
42 class GroupJoinRequest extends Error {
45 * Associative array of data from db.
47 * @var array $data_array.
55 * @param Group The Group object.
56 * @param int The user_id.
57 * @param array The associative array of data.
58 * @return boolean success.
60 function GroupJoinRequest($Group=false, $user_id=false, $arr=false) {
63 if (!$Group || !is_object($Group)) {
64 $this->setError('GroupJoinRequest:: No Valid Group Object');
67 if ($Group->isError()) {
68 $this->setError('GroupJoinRequest:: '.$Group->getErrorMessage());
71 $this->Group =& $Group;
73 if (!$arr || !is_array($arr)) {
74 if (!$this->fetchData($Group->getID(),$user_id)) {
78 $this->data_array =& $arr;
80 // Verify this message truly belongs to this Group
82 if ($this->data_array['group_id'] != $this->Group->getID()) {
83 $this->setError('group_id in db result does not match Group Object');
92 * create - create a new GroupJoinRequest in the database.
94 * @param int4 user_id.
95 * @param text comments.
96 * @param bool whether to send an email to the admin(s)
97 * @return boolean Success.
99 function create($user_id,$comments,$send_email=true) {
100 $v = new Validator();
101 $v->check($user_id, "user_id");
102 $v->check(trim($comments), "comments");
103 if (!$v->isClean()) {
104 $this->setError($v->formErrorMsg(_("Must include ")));
108 // Check if user is already a member of the project
109 $user = user_get_object ($user_id) ;
110 foreach ($user->getGroups(true) as $p) {
111 if ($p->getID() == $this->Group->getID()) {
112 $this->setError(_('You are already a member of this project.'));
117 // Check if user has already submitted a request
118 $result = db_query_params ('SELECT * FROM group_join_request WHERE group_id=$1 AND user_id=$2',
119 array ($this->Group->getID(),
121 if (db_numrows($result)) {
122 $this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
128 $result = db_query_params ('INSERT INTO group_join_request (group_id,user_id,comments,request_date)
129 VALUES ($1, $2, $3, $4)',
130 array ($this->Group->getID(),
132 htmlspecialchars ($comments),
134 if (!$result || db_affected_rows($result) < 1) {
135 $this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
140 if (!$this->fetchData($this->Group->getID(),$user_id)) {
145 $this->sendJoinNotice();
152 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
154 * @param int The group_id.
155 * @param int The user_id.
156 * @return boolean success.
158 function fetchData($group_id,$user_id) {
159 $res = db_query_params ('SELECT * FROM group_join_request WHERE user_id=$1 AND group_id=$2',
161 $this->Group->getID())) ;
162 if (!$res || db_numrows($res) < 1) {
163 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
166 $this->data_array = db_fetch_array($res);
167 db_free_result($res);
172 * getID - get this GroupJoinRequest ID
174 * @return int The group_id.
177 return $this->data_array['group_id'];
181 * getGroup - get the group object.
183 * @return Group The Group.
185 function &getGroup() {
190 * getUserId - get the field user_id.
192 * @return int4 The field.
194 function getUserId() {
195 return $this->data_array['user_id'];
199 * getComments - get the field comments.
201 * @return text The field.
203 function getComments() {
204 return $this->data_array['comments'];
208 * getRequestDate - get the field request_date.
210 * @return int4 The field.
212 function getRequestDate() {
213 return $this->data_array['request_date'];
219 * @return boolean true/false.
221 function sendJoinNotice() {
222 $user =& session_get_user();
223 $admins =& $this->Group->getAdmins();
224 for ($i=0; $i<count($admins); $i++) {
225 setup_gettext_for_user ($admins[$i]) ;
227 $email=$admins[$i]->getEmail();
228 $subject = sprintf(_('Request to Join Project %1$s from %2$s (%3$s)'),
229 $this->Group->getPublicName(),
230 $user->getRealName(),
233 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
234 $body = sprintf(_('%1$s (%2$s) has requested to join your project.
235 You can approve this request here: %3$s
237 Comments by the user:
239 $user->getRealName(),
240 $user->getUnixName(),
241 util_make_url ('/project/admin/users.php?group_id='.$this->Group->getId()),
243 $body = str_replace("\\n","\n",$body);
245 util_send_message($email,$subject,$body);
247 setup_gettext_from_context();
254 * @return boolean success.
257 $user =& user_get_object($this->getUserId());
258 setup_gettext_for_user ($user) ;
259 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
260 $body = sprintf(_('Your request to join the %1$s project was denied by an administrator.'), $this->Group->getPublicName());
261 util_send_message($user->getEmail(),$subject,$body);
262 setup_gettext_from_context();
263 return $this->delete(1);
270 function send_accept_mail() {
271 $user =& user_get_object($this->getUserId());
272 setup_gettext_for_user ($user) ;
273 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
274 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
275 util_send_message($user->getEmail(),$subject,$body);
276 setup_gettext_from_context();
280 * delete() - delete this row from the database.
282 * @param boolean I'm Sure.
283 * @return boolean true/false.
285 function delete($sure) {
287 $this->setError('Must be sure before deleting');
290 if (! forge_check_perm ('project_admin', $this->Group->getID())) {
291 $this->setPermissionDeniedError();
294 $res = db_query_params ('DELETE FROM group_join_request WHERE group_id=$1 AND user_id=$2',
295 array ($this->Group->getID(),
296 $this->getUserId()));
297 if (!$res || db_affected_rows($res) < 1) {
298 $this->setError('Could Not Delete: '.db_error());
308 // c-file-style: "bsd"