5 * Copyright 2005, GForge, LLC
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 require_once $gfcommon.'include/Error.class.php';
26 require_once $gfcommon.'include/Validator.class.php';
29 function &groupjoinrequest_get_object($group_id,$user_id,$data=false) {
30 global $GROUPJOINREQUEST_OBJ;
31 if (!isset($GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"])) {
33 //the db result handle was passed in
35 $res=db_query("SELECT * FROM group_join_request
36 WHERE group_id='$group_id' AND user_id='$user_id'");
38 if (db_numrows($res) <1 ) {
39 $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]=false;
42 $data =& db_fetch_array($res);
45 $grp =& group_get_object($group_id);
46 $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]= new GroupJoinRequest($grp,$user_id,$data);
50 return $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"];
53 function &get_group_join_requests($Group) {
54 if (!$Group || !is_object($Group) || $Group->isError()) {
57 $res=db_query("SELECT * FROM group_join_request WHERE group_id='".$Group->getID()."'");
58 while ($arr = db_fetch_array($res)) {
59 $reqs[] = new GroupJoinRequest($Group,$arr['user_id'],$arr);
65 class GroupJoinRequest extends Error {
68 * Associative array of data from db.
70 * @var array $data_array.
78 * @param Group The Group object.
79 * @param int The user_id.
80 * @param array The associative array of data.
81 * @return boolean success.
83 function GroupJoinRequest($Group=false, $user_id=false, $arr=false) {
86 if (!$Group || !is_object($Group)) {
87 $this->setError('GroupJoinRequest:: No Valid Group Object');
90 if ($Group->isError()) {
91 $this->setError('GroupJoinRequest:: '.$Group->getErrorMessage());
94 $this->Group =& $Group;
96 if (!$arr || !is_array($arr)) {
97 if (!$this->fetchData($Group->getID(),$user_id)) {
101 $this->data_array =& $arr;
103 // Verify this message truly belongs to this Group
105 if ($this->data_array['group_id'] != $this->Group->getID()) {
106 $this->setError('group_id in db result does not match Group Object');
115 * create - create a new GroupJoinRequest in the database.
117 * @param int4 user_id.
118 * @param text comments.
119 * @param int4 request_date.
120 * @return boolean Success.
122 function create($user_id,$comments) {
123 $v = new Validator();
124 $v->check($user_id, "user_id");
125 //$v->check($comments, "comments");
126 if (!$v->isClean()) {
127 $this->setError($v->formErrorMsg("Must include "));
131 // Check if user is already a member of the project
132 $perm =& $this->Group->getPermission( user_get_object($user_id) );
133 if ($perm && is_object($perm) && $perm->isMember()) {
134 $this->setError(_('You are already a member of this project.'));
138 // Check if user has already submitted a request
139 $sql = "SELECT * FROM group_join_request WHERE group_id='".$this->Group->getID()."' AND user_id='".$user_id."'";
140 $result = db_query($sql);
141 if (db_numrows($result)) {
142 $this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
148 $sql="INSERT INTO group_join_request (group_id,user_id,comments,request_date)
149 VALUES ('".$this->Group->getID()."','".$user_id."',
150 '".addslashes(htmlspecialchars($comments))."','".time()."')";
151 $result=db_query($sql);
152 if (!$result || db_affected_rows($result) < 1) {
153 $this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
157 if (!$this->fetchData($this->Group->getID(),$user_id)) {
161 $this->sendJoinNotice();
169 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
171 * @param int The group_id.
172 * @param int The user_id.
173 * @return boolean success.
175 function fetchData($group_id,$user_id) {
176 $res=db_query("SELECT * FROM group_join_request
179 AND group_id='". $this->Group->getID() ."'");
180 if (!$res || db_numrows($res) < 1) {
181 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
184 $this->data_array =& db_fetch_array($res);
185 db_free_result($res);
190 * getID - get this GroupJoinRequest ID
192 * @return int The group_id.
195 return $this->data_array['group_id'];
199 * getGroup - get the group object.
201 * @return Group The Group.
203 function &getGroup() {
208 * getUserId - get the field user_id.
210 * @return int4 The field.
212 function getUserId() {
213 return $this->data_array['user_id'];
217 * getComments - get the field comments.
219 * @return text The field.
221 function getComments() {
222 return $this->data_array['comments'];
226 * getRequestDate - get the field request_date.
228 * @return int4 The field.
230 function getRequestDate() {
231 return $this->data_array['request_date'];
237 * @return boolean true/false.
239 function sendJoinNotice() {
240 $user =& session_get_user();
241 $admins =& $this->Group->getAdmins();
242 for ($i=0; $i<count($admins); $i++) {
243 setup_gettext_for_user ($admins[$i]) ;
245 $email=$admins[$i]->getEmail();
246 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
247 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
248 $body = sprintf(_('%1$s has requested to join your project.
249 You can approve this request here: %2$s
251 Comments by the user:
253 $user->getRealName(),
254 util_make_url ('/project/admin/?group_id='.$this->Group->getId()),
256 $body = str_replace("\\n","\n",$body);
258 $ret = util_send_message($email,$subject,$body);
259 if (! $ret) { return $ret; }
262 setup_gettext_from_browser () ;
269 * @return boolean success.
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 denied by an administrator.'), $this->Group->getPublicName());
276 util_send_message($user->getEmail(),$subject,$body);
277 setup_gettext_from_browser () ;
278 return $this->delete(1);
285 function send_accept_mail() {
286 $user =& user_get_object($this->getUserId());
287 setup_gettext_for_user ($user) ;
288 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
289 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
290 util_send_message($user->getEmail(),$subject,$body);
291 setup_gettext_from_browser () ;
295 * delete() - delete this row from the database.
297 * @param boolean I'm Sure.
298 * @return boolean true/false.
300 function delete($sure) {
302 $this->setError('Must be sure before deleting');
305 $perm =& $this->Group->getPermission( session_get_user() );
306 if (!$perm || !is_object($perm)) {
307 $this->setPermissionDeniedError();
309 } elseif ($perm->isError()) {
310 $this->setPermissionDeniedError();
312 } elseif (!$perm->isAdmin()) {
313 $this->setPermissionDeniedError();
316 $res=db_query("DELETE FROM group_join_request WHERE
317 group_id='".$this->Group->getID()."'
318 AND user_id='".$this->getUserId()."'");
319 if (!$res || db_affected_rows($res) < 1) {
320 $this->setError('Could Not Delete: '.db_error());
330 // c-file-style: "bsd"