5 * Copyright 2005, GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'include/Validator.class.php';
29 function &get_group_join_requests($Group) {
30 if (!$Group || !is_object($Group) || $Group->isError()) {
33 $res = db_query_params ('SELECT * FROM group_join_request WHERE group_id=$1',
34 array ($Group->getID())) ;
35 while ($arr = db_fetch_array($res)) {
36 $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 int4 request_date.
97 * @return boolean Success.
99 function create($user_id,$comments) {
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 $result = db_query_params ('SELECT * FROM user_group WHERE group_id=$1 AND user_id=$2',
110 array ($this->Group->getID(),
112 if (db_numrows($result)) {
113 $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());
139 if (!$this->fetchData($this->Group->getID(),$user_id)) {
143 $this->sendJoinNotice();
151 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
153 * @param int The group_id.
154 * @param int The user_id.
155 * @return boolean success.
157 function fetchData($group_id,$user_id) {
158 $res = db_query_params ('SELECT * FROM group_join_request WHERE user_id=$1 AND group_id=$2',
160 $this->Group->getID())) ;
161 if (!$res || db_numrows($res) < 1) {
162 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
165 $this->data_array =& db_fetch_array($res);
166 db_free_result($res);
171 * getID - get this GroupJoinRequest ID
173 * @return int The group_id.
176 return $this->data_array['group_id'];
180 * getGroup - get the group object.
182 * @return Group The Group.
184 function &getGroup() {
189 * getUserId - get the field user_id.
191 * @return int4 The field.
193 function getUserId() {
194 return $this->data_array['user_id'];
198 * getComments - get the field comments.
200 * @return text The field.
202 function getComments() {
203 return $this->data_array['comments'];
207 * getRequestDate - get the field request_date.
209 * @return int4 The field.
211 function getRequestDate() {
212 return $this->data_array['request_date'];
218 * @return boolean true/false.
220 function sendJoinNotice() {
221 $user =& session_get_user();
222 $admins =& $this->Group->getAdmins();
223 for ($i=0; $i<count($admins); $i++) {
224 setup_gettext_for_user ($admins[$i]) ;
226 $email=$admins[$i]->getEmail();
227 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
228 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
229 $body = sprintf(_('%1$s has requested to join your project.
230 You can approve this request here: %2$s
232 Comments by the user:
234 $user->getRealName(),
235 util_make_url ('/project/admin/?group_id='.$this->Group->getId()),
237 $body = str_replace("\\n","\n",$body);
239 util_send_message($email,$subject,$body);
241 setup_gettext_from_context();
248 * @return boolean success.
251 $user =& user_get_object($this->getUserId());
252 setup_gettext_for_user ($user) ;
253 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
254 $body = sprintf(_('Your request to join the %1$s project was denied by an administrator.'), $this->Group->getPublicName());
255 util_send_message($user->getEmail(),$subject,$body);
256 setup_gettext_from_context();
257 return $this->delete(1);
264 function send_accept_mail() {
265 $user =& user_get_object($this->getUserId());
266 setup_gettext_for_user ($user) ;
267 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
268 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
269 util_send_message($user->getEmail(),$subject,$body);
270 setup_gettext_from_context();
274 * delete() - delete this row from the database.
276 * @param boolean I'm Sure.
277 * @return boolean true/false.
279 function delete($sure) {
281 $this->setError('Must be sure before deleting');
284 $perm =& $this->Group->getPermission( session_get_user() );
285 if (!$perm || !is_object($perm)) {
286 $this->setPermissionDeniedError();
288 } elseif ($perm->isError()) {
289 $this->setPermissionDeniedError();
291 } elseif (!$perm->isAdmin()) {
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"