File: GroupJoinRequest.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (310 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download | duplicates (3)
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
307
308
309
310
<?php
/**
 * FusionForge
 *
 * Copyright 2005, GForge, LLC
 * Copyright 2009-2010, Roland Mas
 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
 *
 * This file is part of FusionForge. FusionForge 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 Licence, or (at your option)
 * any later version.
 *
 * FusionForge is 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 FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

require_once $gfcommon.'include/Error.class.php';
require_once $gfcommon.'include/Validator.class.php';

function &get_group_join_requests($Group) {
	$reqs = array();
	if ($Group && is_object($Group) && !$Group->isError()) {
		$res = db_query_params('SELECT * FROM group_join_request WHERE group_id=$1',
			array($Group->getID()));
		if (db_numrows($res)) {
			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 bool|Group $Group   $Group   The Group object.
     * @param bool|int $user_id The user_id.
     * @param array|bool $arr     The associative array of data.
     * @return \GroupJoinRequest
     */
	function __construct($Group = false, $user_id = false, $arr = false) {
		$this->error();

		if (!$Group || !is_object($Group)) {
			$this->setError(_('No Valid Group Object'));
			return;
		}
		if ($Group->isError()) {
			$this->setError('GroupJoinRequest: '.$Group->getErrorMessage());
			return;
		}
		$this->Group =& $Group;
		if ($user_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($Group->getID(), $user_id)) {
					return;
				}
			} 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;
				}
			}
		}
	}

	/**
	 * create - create a new GroupJoinRequest in the database.
	 *
	 * @param int    $user_id    user_id.
	 * @param string $comments   comments.
	 * @param bool   $send_email whether to send an email to the admin(s)
	 * @return boolean Success.
	 */
	function create($user_id, $comments, $send_email = true) {
		$v = new Validator();
		$v->check($user_id, "user_id");
		$v->check(trim($comments), "comments");
		if (!$v->isClean()) {
			$this->setError($v->formErrorMsg(_("Must include ")));
			return false;
		}

		// Check if user is already a member of the project
		$user = user_get_object($user_id);
		foreach ($user->getGroups(true) as $p) {
			if ($p->getID() == $this->Group->getID()) {
				$this->setError(_('You are already a member of this project.'));
				return false;
			}
		}

		// Check if user has already submitted a request
		$result = db_query_params('SELECT * FROM group_join_request WHERE group_id=$1 AND user_id=$2',
			array($this->Group->getID(),
				$user_id));
		if (db_numrows($result)) {
			$this->setError(_('You have already sent a request to the project administrators. Please wait for their reply.'));
			return false;
		}

		db_begin();

		$result = db_query_params('INSERT INTO group_join_request (group_id,user_id,comments,request_date)
			VALUES ($1, $2, $3, $4)',
			array($this->Group->getID(),
				$user_id,
				htmlspecialchars($comments),
				time()));
		if (!$result || db_affected_rows($result) < 1) {
			$this->setError('GroupJoinRequest::create() Posting Failed '.db_error());
			db_rollback();
			return false;
		}

		if (!$this->fetchData($this->Group->getID(), $user_id)) {
			db_rollback();
			return false;
		}
		if ($send_email) {
			$this->sendJoinNotice();
		}
		db_commit();
		return true;
	}

	/**
	 *  fetchData - re-fetch the data for this GroupJoinRequest from the database.
	 *
	 * @param  int  $group_id The group_id.
	 * @param  int  $user_id  The user_id.
	 * @return     boolean success.
	 */
	function fetchData($group_id, $user_id) {
		$res = db_query_params('SELECT * FROM group_join_request WHERE user_id=$1 AND group_id=$2',
			array($user_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 int 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 int The field.
	 */
	function getRequestDate() {
		return $this->data_array['request_date'];
	}

	/**
	 *    sendJoinNotice() -
	 *
	 * @return boolean    true/false.
	 */
	function sendJoinNotice() {
		$user =& session_get_user();
		$admins =& $this->Group->getAdmins();
		for ($i = 0; $i < count($admins); $i++) {
			setup_gettext_for_user($admins[$i]);

			$email = $admins[$i]->getEmail();
			$subject = sprintf(_('Request to Join Project %1$s from %2$s (%3$s)'),
				$this->Group->getPublicName(),
				$user->getRealName(),
				$user->getUnixName()
			);
			$comments = util_unconvert_htmlspecialchars($this->data_array["comments"]);
			$body = sprintf(_('%1$s (%2$s) has requested to join your project.'),
							 $user->getRealName(), $user->getUnixName());
			$body .= "\n";
			$body .= sprintf(_('You can approve this request here: %s'),
							 util_make_url('/project/admin/users.php?group_id='.$this->Group->getId()));
			$body .= "\n\n";
			$body .= _('Comments by the user:');
			$body .= "\n";
			$body .= $comments;
			$body = str_replace("\\n", "\n", $body);

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

	/**
	 *    reject()
	 *
	 * @return    boolean    success.
	 */
	function reject() {
		$user = user_get_object($this->getUserId());
		setup_gettext_for_user($user);
		$subject = sprintf(_('Request to Join Project %s'), $this->Group->getPublicName());
		$body = sprintf(_('Your request to join the %s project was denied by an administrator.'), $this->Group->getPublicName());
		util_send_message($user->getEmail(), $subject, $body);
		setup_gettext_from_context();
		return $this->delete(1);
	}

	/**
	 *    send_accept_mail()
	 *
	 */
	function send_accept_mail() {
		$user = user_get_object($this->getUserId());
		setup_gettext_for_user($user);
		$subject = sprintf(_('Request to Join Project %s'), $this->Group->getPublicName());
		$body = sprintf(_('Your request to join the %s project was granted by an administrator.'), $this->Group->getPublicName());
		util_send_message($user->getEmail(), $subject, $body);
		setup_gettext_from_context();
	}

	/**
	 *    delete() - delete this row from the database.
	 *
	 * @param    boolean    $sure I'm Sure.
	 * @return    boolean    true/false.
	 */
	function delete($sure) {
		if (!$sure) {
			$this->setError(_('Must be sure before deleting'));
			return false;
		}
		if (!forge_check_perm('project_admin', $this->Group->getID())) {
			$this->setPermissionDeniedError();
			return false;
		} else {
			$res = db_query_params('DELETE FROM group_join_request WHERE group_id=$1 AND user_id=$2',
				array($this->Group->getID(),
					$this->getUserId()));
			if (!$res || db_affected_rows($res) < 1) {
				$this->setError(_('Delete failed')._(': ').db_error());
                return false;
			} else {
				return true;
			}
		}
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End: