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 $perm =& $this->Group->getPermission( user_get_object($user_id) );
110 if ($perm && is_object($perm) && $perm->isMember()) {
111 $this->setError(_('You are already a member of this project.'));
115 // Check if user has already submitted a request
116 $result = db_query_params ('SELECT * FROM group_join_request WHERE group_id=$1 AND user_id=$2',
117 array ($this->Group->getID(),
119 if (db_numrows($result)) {
120 $this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
126 $result = db_query_params ('INSERT INTO group_join_request (group_id,user_id,comments,request_date)
127 VALUES ($1, $2, $3, $4)',
128 array ($this->Group->getID(),
130 htmlspecialchars ($comments),
132 if (!$result || db_affected_rows($result) < 1) {
133 $this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
137 if (!$this->fetchData($this->Group->getID(),$user_id)) {
141 $this->sendJoinNotice();
149 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
151 * @param int The group_id.
152 * @param int The user_id.
153 * @return boolean success.
155 function fetchData($group_id,$user_id) {
156 $res = db_query_params ('SELECT * FROM group_join_request WHERE user_id=$1 AND group_id=$2',
158 $this->Group->getID())) ;
159 if (!$res || db_numrows($res) < 1) {
160 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
163 $this->data_array =& db_fetch_array($res);
164 db_free_result($res);
169 * getID - get this GroupJoinRequest ID
171 * @return int The group_id.
174 return $this->data_array['group_id'];
178 * getGroup - get the group object.
180 * @return Group The Group.
182 function &getGroup() {
187 * getUserId - get the field user_id.
189 * @return int4 The field.
191 function getUserId() {
192 return $this->data_array['user_id'];
196 * getComments - get the field comments.
198 * @return text The field.
200 function getComments() {
201 return $this->data_array['comments'];
205 * getRequestDate - get the field request_date.
207 * @return int4 The field.
209 function getRequestDate() {
210 return $this->data_array['request_date'];
216 * @return boolean true/false.
218 function sendJoinNotice() {
219 $user =& session_get_user();
220 $admins =& $this->Group->getAdmins();
221 for ($i=0; $i<count($admins); $i++) {
222 setup_gettext_for_user ($admins[$i]) ;
224 $email=$admins[$i]->getEmail();
225 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
226 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
227 $body = sprintf(_('%1$s has requested to join your project.
228 You can approve this request here: %2$s
230 Comments by the user:
232 $user->getRealName(),
233 util_make_url ('/project/admin/?group_id='.$this->Group->getId()),
235 $body = str_replace("\\n","\n",$body);
237 util_send_message($email,$subject,$body);
239 setup_gettext_from_context();
246 * @return boolean success.
249 $user =& user_get_object($this->getUserId());
250 setup_gettext_for_user ($user) ;
251 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
252 $body = sprintf(_('Your request to join the %1$s project was denied by an administrator.'), $this->Group->getPublicName());
253 util_send_message($user->getEmail(),$subject,$body);
254 setup_gettext_from_context();
255 return $this->delete(1);
262 function send_accept_mail() {
263 $user =& user_get_object($this->getUserId());
264 setup_gettext_for_user ($user) ;
265 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
266 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
267 util_send_message($user->getEmail(),$subject,$body);
268 setup_gettext_from_context();
272 * delete() - delete this row from the database.
274 * @param boolean I'm Sure.
275 * @return boolean true/false.
277 function delete($sure) {
279 $this->setError('Must be sure before deleting');
282 $perm =& $this->Group->getPermission( session_get_user() );
283 if (!$perm || !is_object($perm)) {
284 $this->setPermissionDeniedError();
286 } elseif ($perm->isError()) {
287 $this->setPermissionDeniedError();
289 } elseif (!$perm->isAdmin()) {
290 $this->setPermissionDeniedError();
293 $res = db_query_params ('DELETE FROM group_join_request WHERE group_id=$1 AND user_id=$2',
294 array ($this->Group->getID(),
295 $this->getUserId()));
296 if (!$res || db_affected_rows($res) < 1) {
297 $this->setError('Could Not Delete: '.db_error());
307 // c-file-style: "bsd"