3 * Gforge group_join_request Facility
5 * Copyright 2005 (c) GForge Group
9 * This file is part of Gforge.
11 * Gforge is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Gforge distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Gforge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'include/Validator.class.php';
30 function &groupjoinrequest_get_object($group_id,$user_id,$data=false) {
31 global $GROUPJOINREQUEST_OBJ;
32 if (!isset($GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"])) {
34 //the db result handle was passed in
36 $res=db_query("SELECT * FROM group_join_request
37 WHERE group_id='$group_id' AND user_id='$user_id'");
39 if (db_numrows($res) <1 ) {
40 $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]=false;
43 $data =& db_fetch_array($res);
46 $grp =& group_get_object($group_id);
47 $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]= new GroupJoinRequest($grp,$user_id,$data);
51 return $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"];
54 function &get_group_join_requests($Group) {
55 if (!$Group || !is_object($Group) || $Group->isError()) {
58 $res=db_query("SELECT * FROM group_join_request WHERE group_id='".$Group->getID()."'");
59 while ($arr = db_fetch_array($res)) {
60 $reqs[] = new GroupJoinRequest($Group,$arr['user_id'],$arr);
66 class GroupJoinRequest extends Error {
69 * Associative array of data from db.
71 * @var array $data_array.
79 * @param Group The Group object.
80 * @param int The user_id.
81 * @param array The associative array of data.
82 * @return boolean success.
84 function GroupJoinRequest($Group=false, $user_id=false, $arr=false) {
87 if (!$Group || !is_object($Group)) {
88 $this->setError('GroupJoinRequest:: No Valid Group Object');
91 if ($Group->isError()) {
92 $this->setError('GroupJoinRequest:: '.$Group->getErrorMessage());
95 $this->Group =& $Group;
97 if (!$arr || !is_array($arr)) {
98 if (!$this->fetchData($Group->getID(),$user_id)) {
102 $this->data_array =& $arr;
104 // Verify this message truly belongs to this Group
106 if ($this->data_array['group_id'] != $this->Group->getID()) {
107 $this->setError('group_id in db result does not match Group Object');
116 * create - create a new GroupJoinRequest in the database.
118 * @param int4 user_id.
119 * @param text comments.
120 * @param int4 request_date.
121 * @return boolean Success.
123 function create($user_id,$comments) {
124 $v = new Validator();
125 $v->check($user_id, "user_id");
126 //$v->check($comments, "comments");
127 if (!$v->isClean()) {
128 $this->setError($v->formErrorMsg("Must include "));
132 // Check if user is already a member of the project
133 $perm =& $this->Group->getPermission( user_get_object($user_id) );
134 if ($perm && is_object($perm) && $perm->isMember()) {
135 $this->setError(_('You are already a member of this project.'));
139 // Check if user has already submitted a request
140 $sql = "SELECT * FROM group_join_request WHERE group_id='".$this->Group->getID()."' AND user_id='".$user_id."'";
141 $result = db_query($sql);
142 if (db_numrows($result)) {
143 $this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
149 $sql="INSERT INTO group_join_request (group_id,user_id,comments,request_date)
150 VALUES ('".$this->Group->getID()."','".$user_id."',
151 '".addslashes(htmlspecialchars($comments))."','".time()."')";
152 $result=db_query($sql);
153 if (!$result || db_affected_rows($result) < 1) {
154 $this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
158 if (!$this->fetchData($this->Group->getID(),$user_id)) {
162 $this->sendJoinNotice();
170 * fetchData - re-fetch the data for this GroupJoinRequest from the database.
172 * @param int The group_id.
173 * @param int The user_id.
174 * @return boolean success.
176 function fetchData($group_id,$user_id) {
177 $res=db_query("SELECT * FROM group_join_request
180 AND group_id='". $this->Group->getID() ."'");
181 if (!$res || db_numrows($res) < 1) {
182 $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
185 $this->data_array =& db_fetch_array($res);
186 db_free_result($res);
191 * getID - get this GroupJoinRequest ID
193 * @return int The group_id.
196 return $this->data_array['group_id'];
200 * getGroup - get the group object.
202 * @return Group The Group.
204 function &getGroup() {
209 * getUserId - get the field user_id.
211 * @return int4 The field.
213 function getUserId() {
214 return $this->data_array['user_id'];
218 * getComments - get the field comments.
220 * @return text The field.
222 function getComments() {
223 return $this->data_array['comments'];
227 * getRequestDate - get the field request_date.
229 * @return int4 The field.
231 function getRequestDate() {
232 return $this->data_array['request_date'];
238 * @return boolean true/false.
240 function sendJoinNotice() {
241 $user =& session_get_user();
242 $admins =& $this->Group->getAdmins();
243 for ($i=0; $i<count($admins); $i++) {
244 setup_gettext_for_user ($admins[$i]) ;
246 $email=$admins[$i]->getEmail();
247 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
248 $comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
249 $body = sprintf(_('%1$s has requested to join your project.
250 You can approve this request here: %2$s
252 Comments by the user:
254 $user->getRealName(),
255 util_make_url ('/project/admin/?group_id='.$this->Group->getId()),
257 $body = str_replace("\\n","\n",$body);
259 $ret = util_send_message($email,$subject,$body);
260 if (! $ret) { return $ret; }
263 setup_gettext_from_browser () ;
270 * @return boolean success.
273 $user =& user_get_object($this->getUserId());
274 setup_gettext_for_user ($user) ;
275 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
276 $body = sprintf(_('Your request to join the %1$s project was denied by an administrator.'), $this->Group->getPublicName());
277 util_send_message($user->getEmail(),$subject,$body);
278 setup_gettext_from_browser () ;
279 return $this->delete(1);
286 function send_accept_mail() {
287 $user =& user_get_object($this->getUserId());
288 setup_gettext_for_user ($user) ;
289 $subject = sprintf(_('Request to Join Project %1$s'), $this->Group->getPublicName());
290 $body = sprintf(_('Your request to join the %1$s project was granted by an administrator.'), $this->Group->getPublicName());
291 util_send_message($user->getEmail(),$subject,$body);
292 setup_gettext_from_browser () ;
296 * delete() - delete this row from the database.
298 * @param boolean I'm Sure.
299 * @return boolean true/false.
301 function delete($sure) {
303 $this->setError('Must be sure before deleting');
306 $perm =& $this->Group->getPermission( session_get_user() );
307 if (!$perm || !is_object($perm)) {
308 $this->setPermissionDeniedError();
310 } elseif ($perm->isError()) {
311 $this->setPermissionDeniedError();
313 } elseif (!$perm->isAdmin()) {
314 $this->setPermissionDeniedError();
317 $res=db_query("DELETE FROM group_join_request WHERE
318 group_id='".$this->Group->getID()."'
319 AND user_id='".$this->getUserId()."'");
320 if (!$res || db_affected_rows($res) < 1) {
321 $this->setError('Could Not Delete: '.db_error());
332 // c-file-style: "bsd"