File: GroupJoinRequest.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (306 lines) | stat: -rw-r--r-- 8,391 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
 * Gforge group_join_request Facility
 *
 * Copyright 2005 (c) GForge Group
 *
 * @version   $Id: GroupJoinRequest.class 4384 2005-05-20 19:16:26Z marcelo $
 *
 * This file is part of Gforge.
 *
 * Gforge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Gforge distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gforge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */

require_once('common/include/Error.class');
require_once('common/include/Validator.class');

/*
function &groupjoinrequest_get_object($group_id,$user_id,$data=false) {
	global $GROUPJOINREQUEST_OBJ;
	if (!isset($GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"])) {
		if ($data) {
			//the db result handle was passed in
		} else {
			$res=db_query("SELECT * FROM group_join_request
				WHERE group_id='$group_id' AND user_id='$user_id'");

			if (db_numrows($res) <1 ) {
				$GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]=false;
				return false;
			}
			$data =& db_fetch_array($res);

		}
		$grp =& group_get_object($group_id);
		$GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"]= new GroupJoinRequest($grp,$user_id,$data);

	}

	return $GROUPJOINREQUEST_OBJ["_".$group_id."_".$user_id."_"];
}
*/
function &get_group_join_requests($Group) {
	if (!$Group || !is_object($Group) || $Group->isError()) {
		return false;
	} else {
		$res=db_query("SELECT * FROM group_join_request WHERE group_id='".$Group->getID()."'");
		while ($arr = db_fetch_array($res)) {
			$reqs[] = new GroupJoinRequest($Group,$arr['user_id'],$arr);
		}
		return $reqs;
	}
}

class GroupJoinRequest extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var  array   $data_array.
	 */
	var $data_array;

	var $Group;
	/**
	 *  Constructor.
	 *
	 *  @param  Group 	The Group object.
	 *  @param  int 	The user_id.
	 *  @param  array 	The associative array of data.
	 *  @return boolean success.
	 */
	function GroupJoinRequest($Group=false, $user_id=false, $arr=false) {
		$this->error(); 

		if (!$Group || !is_object($Group)) {
			$this->setError('GroupJoinRequest:: No Valid Group Object');
				return false;
		}
		if ($Group->isError()) {
			$this->setError('GroupJoinRequest:: '.$Group->getErrorMessage());
				return false;
		}
		$this->Group =& $Group;
		if ($user_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($group_id,$user_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				//
				//      Verify this message truly belongs to this Group
				//
				if ($this->data_array['group_id'] != $this->Group->getID()) {
					$this->setError('group_id in db result does not match Group Object');
					return false;
				}
			} 
		}
		return true;
	}

	/**
	 * create - create a new GroupJoinRequest in the database.
	 *
	 *	@param	int4 user_id.
	 *	@param	text comments.
	 *	@param	int4 request_date.
	 * @return boolean Success.
	 */
	function create($user_id,$comments) {
		global $Language;
		
		$v = new Validator();
		$v->check($user_id, "user_id");
		//$v->check($comments, "comments");
		if (!$v->isClean()) {
			$this->setError($v->formErrorMsg("Must include "));
			return false;
		}

		// Check if user is already a member of the project
		$perm =& $this->Group->getPermission( user_get_object($user_id) );
		if ($perm && is_object($perm) && $perm->isMember()) {
			$this->setError($Language->getText('project_joinrequest','already_member'));
			return false;
		}

		// Check if user has already submitted a request
		$sql = "SELECT * FROM group_join_request WHERE group_id='".$this->Group->getID()."' AND user_id='".$user_id."'";
		$result = db_query($sql);
		if (db_numrows($result)) {
			$this->setError($Language->getText('project_joinrequest','already_requested'));
			return false;
		}

		db_begin();

		$sql="INSERT INTO group_join_request (group_id,user_id,comments,request_date)
			VALUES ('".$this->Group->getID()."','".$user_id."',
			'".addslashes(htmlspecialchars($comments))."','".time()."')";
		$result=db_query($sql);
		if (!$result || db_affected_rows($result) < 1) {
			$this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
			db_rollback();
			return false;
		} else {
			if (!$this->fetchData($group_id,$user_id)) {
				db_rollback();
				return false;
			} else {
				$this->sendJoinNotice();
				db_commit();
				return true;
			}
		}
	}

    /**
	 *  fetchData - re-fetch the data for this GroupJoinRequest from the database.
	 *
	 *  @param  int  The group_id.
	 *  @param  int  The user_id.
	 *  @return     boolean success.
	 */
	function fetchData($group_id,$user_id) {
	        $res=db_query("SELECT * FROM group_join_request
	                WHERE
					user_id='$user_id'
	        		AND group_id='". $this->Group->getID() ."'");
	        if (!$res || db_numrows($res) < 1) {
	                $this->setError('GroupJoinRequest::fetchData() Invalid ID '.db_error());
	                return false;
			}
	        $this->data_array =& db_fetch_array($res);
	        db_free_result($res);
	        return true;
	}

	/**
	 *      getID - get this GroupJoinRequest ID
	 *
	 *      @return int The group_id.
	 * /
	function getID() {
		return $this->data_array['group_id'];
	}

	/**
	 *      getGroup - get the group object.
	 *
	 *      @return Group The Group.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *      getUserId - get the field user_id.
	 *
	 *      @return int4 The field.
	 */
	function getUserId() {
		return $this->data_array['user_id'];
	}

	/**
	 *      getComments - get the field comments.
	 *
	 *      @return text The field.
	 * /
	function getComments() {
		return $this->data_array['comments'];
	}

	/**
	 *      getRequestDate - get the field request_date.
	 *
	 *      @return int4 The field.
	 */
	function getRequestDate() {
		return $this->data_array['request_date'];
	}

	/**
	 *	sendJoinNotice() - 
	 *
	 *	@return boolean	true/false.
	 */
	function sendJoinNotice() {
		global $Language;
		$user =& session_get_user();
		$admins =& $this->Group->getAdmins();
		for ($i=0; $i<count($admins); $i++) {
			$emails[]=$admins[$i]->getEmail();
		}
		$email=implode($emails,',');
		$subject = $Language->getText('project_joinrequest','join_notice_subject',array($this->Group->getPublicName()));
		$comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
		$body = $Language->getText('project_joinrequest','join_notice_body',array($user->getRealName(),$GLOBALS['sys_default_domain'],$this->Group->getId(),$comments));
		$body = str_replace("\\n","\n",$body);

		return util_send_message($email,$subject,$body);
	}

	/**
	 *	reject()
	 *
	 *	@return	boolean	success.
	 */
	function reject() {
		global $Language;
		$user =& user_get_object($this->getUserId());
		$subject = $Language->getText('project_joinrequest','join_notice_subject',array($this->Group->getPublicName()));
		$body = $Language->getText('project_joinrequest','denied',array($this->Group->getPublicName()));
		util_send_message($user->getEmail(),$subject,$body);
		return $this->delete(1);
	}

	/**
	 *	delete() - delete this row from the database.
	 *
	 *	@param	boolean	I'm Sure.
	 *	@return	boolean	true/false.
	 */
	function delete($sure) {
		if (!$sure) {
			$this->setError('Must be sure before deleting');
			return false;
		}
		$perm =& $this->Group->getPermission( session_get_user() );
		if (!$perm || !is_object($perm)) {
			$this->setPermissionDeniedError();
			return false;
		} elseif ($perm->isError()) {
			$this->setPermissionDeniedError();
			return false;
		} elseif (!$perm->isAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		} else {
			$res=db_query("DELETE FROM group_join_request WHERE 
				group_id='".$this->Group->getID()."' 
				AND user_id='".$this->getUserId()."'");
			if (!$res || db_affected_rows($res) < 1) {
				$this->setError('Could Not Delete: '.db_error());
			} else {
				return true;
			}
		}
	}
}

?>