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.
56 * @param bool|Group $Group $Group The Group object.
57 * @param bool|int $user_id The user_id.
58 * @param array|bool $arr The associative array of data.
59 * @return boolean success.
61 function GroupJoinRequest($Group = false, $user_id = false, $arr = false) {
64 if (!$Group || !is_object($Group)) {
65 $this->setError('GroupJoinRequest:: No Valid Group Object');
68 if ($Group->isError()) {
69 $this->setError('GroupJoinRequest:: '.$Group->getErrorMessage());
72 $this->Group =& $Group;
74 if (!$arr || !is_array($arr)) {
75 if (!$this->fetchData($Group->getID(), $user_id)) {
79 $this->data_array =& $arr;
81 // Verify this message truly belongs to this Group
83 if ($this->data_array['group_id'] != $this->Group->getID()) {
84 $this->setError('group_id in db result does not match Group Object');
93 * create - create a new GroupJoinRequest in the database.
95 * @param int $user_id user_id.
96 * @param string $comments comments.
97 * @param bool $send_email whether to send an email to the admin(s)
98 * @return boolean Success.
100 function create($user_id, $comments, $send_email = true) {
101 $v = new Validator();
102 $v->check($user_id, "user_id");
103 $v->check(trim($comments), "comments");
104 if (!$v->isClean()) {
105 $this->setError($v->formErrorMsg(_("Must include ")));
109 // Check if user is already a member of the project
110 $user = user_get_object($user_id);
111 foreach ($user->getGroups(true) as $p) {
112 if ($p->getID() == $this->Group->getID()) {
113 $this->setError(_('You are already a member of this project.'));
118 // Check if user has already submitted a request
119 $result = db_query_params('SELECT * FROM group_join_request WHERE group_id=$1 AND user_id=$2',
120 array($this->Group->getID(),
122 if (db_numrows($result)) {
123 $this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
129 $result = db_query_params('INSERT INTO group_join_request (group_id,user_id,comments,request_date)
130 VALUES ($1, $2, $3, $4)',
131 array($this->Group->getID(),
133 htmlspecialchars($comments),
135 if (!$result || db_affected_rows($result) < 1) {
136 $this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
141 if (!$this->fetchData($this->Group->getID(), $user_id)) {
146 $this->sendJoinNotice();
153 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
155 * @param int $group_id The group_id.
156 * @param int $user_id The user_id.
157 * @return boolean success.
159 function fetchData($group_id, $user_id) {
160 $res = db_query_params('SELECT * FROM group_join_request WHERE user_id=$1 AND group_id=$2',
162 $this->Group->getID()));
163 if (!$res || db_numrows($res) < 1) {
164 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
167 $this->data_array = db_fetch_array($res);
168 db_free_result($res);
173 * getID - get this GroupJoinRequest ID
175 * @return int The group_id.
178 return $this->data_array['group_id'];
182 * getGroup - get the group object.
184 * @return Group The Group.
186 function &getGroup() {
191 * getUserId - get the field user_id.
193 * @return int The field.
195 function getUserId() {
196 return $this->data_array['user_id'];
200 * getComments - get the field comments.
202 * @return text The field.
204 function getComments() {
205 return $this->data_array['comments'];
209 * getRequestDate - get the field request_date.
211 * @return int The field.
213 function getRequestDate() {
214 return $this->data_array['request_date'];
220 * @return boolean true/false.
222 function sendJoinNotice() {
223 $user =& session_get_user();
224 $admins =& $this->Group->getAdmins();
225 for ($i = 0; $i < count($admins); $i++) {
226 setup_gettext_for_user($admins[$i]);
228 $email = $admins[$i]->getEmail();
229 $subject = sprintf(_('Request to Join Project %1$s from %2$s (%3$s)'),
230 $this->Group->getPublicName(),
231 $user->getRealName(),
234 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
235 $body = sprintf(_('%1$s (%2$s) has requested to join your project.
236 You can approve this request here: %3$s
238 Comments by the user:
240 $user->getRealName(),
241 $user->getUnixName(),
242 util_make_url('/project/admin/users.php?group_id='.$this->Group->getId()),
244 $body = str_replace("\\n", "\n", $body);
246 util_send_message($email, $subject, $body);
248 setup_gettext_from_context();
255 * @return boolean success.
258 $user =& user_get_object($this->getUserId());
259 setup_gettext_for_user($user);
260 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
261 $body = sprintf(_('Your request to join the %1$s project was denied by an administrator.'), $this->Group->getPublicName());
262 util_send_message($user->getEmail(), $subject, $body);
263 setup_gettext_from_context();
264 return $this->delete(1);
271 function send_accept_mail() {
272 $user =& user_get_object($this->getUserId());
273 setup_gettext_for_user($user);
274 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
275 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
276 util_send_message($user->getEmail(), $subject, $body);
277 setup_gettext_from_context();
281 * delete() - delete this row from the database.
283 * @param boolean $sure I'm Sure.
284 * @return boolean true/false.
286 function delete($sure) {
288 $this->setError('Must be sure before deleting');
291 if (!forge_check_perm('project_admin', $this->Group->getID())) {
292 $this->setPermissionDeniedError();
295 $res = db_query_params('DELETE FROM group_join_request WHERE group_id=$1 AND user_id=$2',
296 array($this->Group->getID(),
297 $this->getUserId()));
298 if (!$res || db_affected_rows($res) < 1) {
299 $this->setError('Could Not Delete: '.db_error());
309 // c-file-style: "bsd"