| 1 |
<?php
|
| 2 |
// $Id: invite_admin.inc,v 1.3 2008/09/02 17:02:14 smk Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administration functions for invite module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback; display invite settings form.
|
| 11 |
*/
|
| 12 |
function invite_settings() {
|
| 13 |
$roles = user_roles(FALSE, 'send invitations');
|
| 14 |
if (count($roles) == 0) {
|
| 15 |
drupal_set_message(t('Please enable the <em>send invitations</em> permission for at least one role. This can be done on the <a href="!permissions-url">Permissions page</a>.', array('!permissions-url' => url('admin/user/permissions'))));
|
| 16 |
}
|
| 17 |
$target_roles = user_roles(TRUE);
|
| 18 |
|
| 19 |
// General settings.
|
| 20 |
$form['general'] = array(
|
| 21 |
'#type' => 'fieldset',
|
| 22 |
'#title' => t('General settings'),
|
| 23 |
'#collapsible' => TRUE,
|
| 24 |
'#collapsed' => TRUE,
|
| 25 |
);
|
| 26 |
$form['general']['invite_target_role_default'] = array(
|
| 27 |
'#type' => 'select',
|
| 28 |
'#title' => t('Default target role'),
|
| 29 |
'#default_value' => variable_get('invite_target_role_default', DRUPAL_AUTHENTICATED_RID),
|
| 30 |
'#options' => $target_roles,
|
| 31 |
'#description' => t('Choose the default role that invited users will be added to when they register. For example, <em>authenticated user</em>.'),
|
| 32 |
'#required' => TRUE,
|
| 33 |
);
|
| 34 |
$form['general']['invite_expiry'] = array(
|
| 35 |
'#type' => 'select',
|
| 36 |
'#title' => t('Invitation expiry'),
|
| 37 |
'#default_value' => variable_get('invite_expiry', 30),
|
| 38 |
'#options' => drupal_map_assoc(array(1, 3, 7, 14, 30, 60)),
|
| 39 |
'#description' => t('Set the expiry period for user invitations, in days.'),
|
| 40 |
'#multiple' => FALSE,
|
| 41 |
'#required' => TRUE,
|
| 42 |
);
|
| 43 |
|
| 44 |
// Role settings.
|
| 45 |
$form['role'] = array(
|
| 46 |
'#type' => 'fieldset',
|
| 47 |
'#title' => t('Role settings'),
|
| 48 |
'#description' => t('Note: Permission related settings can be found at the <a href="!permissions-url">Permissions page</a>.', array('!permissions-url' => url('admin/user/permissions'))),
|
| 49 |
'#collapsible' => TRUE,
|
| 50 |
'#collapsed' => TRUE,
|
| 51 |
);
|
| 52 |
|
| 53 |
foreach ($roles as $rid => $role) {
|
| 54 |
$form['role'][$rid] = array(
|
| 55 |
'#type' => 'fieldset',
|
| 56 |
'#title' => t('@role settings', array('@role' => drupal_ucfirst($role))),
|
| 57 |
'#collapsible' => TRUE,
|
| 58 |
'#collapsed' => TRUE,
|
| 59 |
);
|
| 60 |
$form['role'][$rid]['invite_target_role_'. $rid] = array(
|
| 61 |
'#type' => 'select',
|
| 62 |
'#title' => t('Target role'),
|
| 63 |
'#default_value' => variable_get('invite_target_role_'. $rid, DRUPAL_AUTHENTICATED_RID),
|
| 64 |
'#options' => $target_roles,
|
| 65 |
'#description' => t('You may choose to add invited users to another role (in addition to the default role set in the general section) when they have been invited by a member of %role.', array('%role' => $role)),
|
| 66 |
'#required' => TRUE,
|
| 67 |
);
|
| 68 |
$form['role'][$rid]['invite_maxnum_'. $rid] = array(
|
| 69 |
'#type' => 'select',
|
| 70 |
'#title' => t('Invitation limit'),
|
| 71 |
'#default_value' => variable_get('invite_maxnum_'. $rid, INVITE_UNLIMITED),
|
| 72 |
'#options' => array(5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100, 500 => 500, 1000 => 1000, INVITE_UNLIMITED => t('unlimited')),
|
| 73 |
'#description' => t('Allows to limit the total number of invitations members of %role can send.', array('%role' => $role)),
|
| 74 |
'#multiple' => FALSE,
|
| 75 |
'#required' => TRUE,
|
| 76 |
);
|
| 77 |
}
|
| 78 |
|
| 79 |
// E-mail settings.
|
| 80 |
$form['email'] = array(
|
| 81 |
'#type' => 'fieldset',
|
| 82 |
'#title' => t('E-mail settings'),
|
| 83 |
'#collapsible' => TRUE,
|
| 84 |
'#collapsed' => TRUE,
|
| 85 |
);
|
| 86 |
$form['email']['invite_subject'] = array(
|
| 87 |
'#type' => 'textfield',
|
| 88 |
'#title' => t('Subject'),
|
| 89 |
'#default_value' => variable_get('invite_subject', t('[inviter-raw] has sent you an invite!')),
|
| 90 |
'#size' => 30,
|
| 91 |
'#maxlength' => 64,
|
| 92 |
'#description' => t('Type the default subject of the invitation e-mail.') .' '. t('Use the syntax [token] if you want to insert a replacement pattern.'),
|
| 93 |
'#required' => TRUE,
|
| 94 |
);
|
| 95 |
$form['email']['invite_subject_editable'] = array(
|
| 96 |
'#type' => 'checkbox',
|
| 97 |
'#title' => t('Editable subject'),
|
| 98 |
'#description' => t('Choose whether users should be able to customize the subject.'),
|
| 99 |
'#default_value' => variable_get('invite_subject_editable', FALSE),
|
| 100 |
);
|
| 101 |
$form['email']['invite_default_mail_template'] = array(
|
| 102 |
'#type' => 'textarea',
|
| 103 |
'#title' => t('Mail template'),
|
| 104 |
'#default_value' => _invite_get_mail_template(),
|
| 105 |
'#required' => TRUE,
|
| 106 |
'#description' => t('Use the syntax [token] if you want to insert a replacement pattern.'),
|
| 107 |
);
|
| 108 |
$form['email']['token_help'] = array(
|
| 109 |
'#title' => t('Replacement patterns'),
|
| 110 |
'#type' => 'fieldset',
|
| 111 |
'#collapsible' => TRUE,
|
| 112 |
'#collapsed' => TRUE,
|
| 113 |
);
|
| 114 |
$form['email']['token_help']['help'] = array(
|
| 115 |
'#value' => theme('invite_token_help', array('user', 'profile', 'invite')),
|
| 116 |
);
|
| 117 |
$form['email']['invite_use_users_email'] = array(
|
| 118 |
'#type' => 'radios',
|
| 119 |
'#title' => t('<em>From</em> e-mail address'),
|
| 120 |
'#description' => t('Choose which e-mail address will be in the From: header for the invitation mails sent; <em>site</em> or <em>inviter</em>. <em>Site</em> will use the default e-mail address of the site, whereas <em>inviter</em> will use the e-mail address of the user who is sending the invitation. Alternatively, you can set this value manually by clicking on <em>advanced settings</em> below.'),
|
| 121 |
'#options' => array(t('site'), t('inviter')),
|
| 122 |
'#default_value' => variable_get('invite_use_users_email', 0),
|
| 123 |
);
|
| 124 |
$form['email']['invite_use_users_email_replyto'] = array(
|
| 125 |
'#type' => 'radios',
|
| 126 |
'#title' => t('<em>Reply-To</em> e-mail address'),
|
| 127 |
'#description' => t('Choose which e-mail address will be in the Reply-To: header for the invitation mails sent; <em>site</em> or <em>inviter</em>. <em>Site</em> will use the default e-mail address of the site, whereas <em>inviter</em> will use the e-mail address of the user who is sending the invitation. Alternatively, you can set this value manually by clicking on <em>advanced settings</em> below.'),
|
| 128 |
'#options' => array(t('site'), t('inviter')),
|
| 129 |
'#default_value' => variable_get('invite_use_users_email_replyto', 0),
|
| 130 |
);
|
| 131 |
$form['email']['advanced'] = array(
|
| 132 |
'#type' => 'fieldset',
|
| 133 |
'#title' => t('Advanced settings'),
|
| 134 |
'#collapsible' => TRUE,
|
| 135 |
'#collapsed' => TRUE,
|
| 136 |
'#description' => t('<strong>Note:</strong> unless these fields are blank, they will override the radio button choices above.'),
|
| 137 |
);
|
| 138 |
$form['email']['advanced']['invite_manual_from'] = array(
|
| 139 |
'#type' => 'textfield',
|
| 140 |
'#title' => t('Manually override <em>From</em> e-mail address'),
|
| 141 |
'#default_value' => variable_get('invite_manual_from', ''),
|
| 142 |
'#description' => t('The e-mail address the invitation e-mail is sent from.'),
|
| 143 |
);
|
| 144 |
$form['email']['advanced']['invite_manual_reply_to'] = array(
|
| 145 |
'#type' => 'textfield',
|
| 146 |
'#title' => t('Manually override <em>Reply-To</em> e-mail address'),
|
| 147 |
'#default_value' => variable_get('invite_manual_reply_to', ''),
|
| 148 |
'#description' => t('The e-mail address you want recipients to reply to.'),
|
| 149 |
);
|
| 150 |
|
| 151 |
// Invite page customization.
|
| 152 |
$form['custom'] = array(
|
| 153 |
'#type' => 'fieldset',
|
| 154 |
'#title' => t('Invite page customization'),
|
| 155 |
'#collapsible' => TRUE,
|
| 156 |
'#collapsed' => TRUE,
|
| 157 |
);
|
| 158 |
$form['custom']['invite_page_title'] = array(
|
| 159 |
'#type' => 'textfield',
|
| 160 |
'#title' => t('Invite page title'),
|
| 161 |
'#default_value' => variable_get('invite_page_title', t('Invite a friend')),
|
| 162 |
'#description' => t('The title of the page where users invite friends.'),
|
| 163 |
'#required' => TRUE,
|
| 164 |
);
|
| 165 |
|
| 166 |
return system_settings_form($form);
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Return a list of all users that have invited someone.
|
| 171 |
*/
|
| 172 |
function invite_admin_overview() {
|
| 173 |
$header = array(
|
| 174 |
array('data' => t('Username'), 'field' => 'u1.name', 'sort' => 'asc'),
|
| 175 |
array('data' => t('Total'), 'field' => 'invites'),
|
| 176 |
t('Successful'),
|
| 177 |
t('Pending'),
|
| 178 |
t('Expired'),
|
| 179 |
t('Remaining'),
|
| 180 |
t('Operations'),
|
| 181 |
);
|
| 182 |
|
| 183 |
$now = time();
|
| 184 |
$filter = $filter_args = $query = NULL;
|
| 185 |
$output = drupal_get_form('invite_admin_filter_form');
|
| 186 |
if (isset($_SESSION[INVITE_ADMIN_SESSION])) {
|
| 187 |
$filter = " INNER JOIN {users} u2 ON u2.uid = i.invitee WHERE LOWER(u2.name) LIKE LOWER('%s%%')";
|
| 188 |
$filter_args = $_SESSION[INVITE_ADMIN_SESSION];
|
| 189 |
$query = 'filter='. $filter_args;
|
| 190 |
unset($_SESSION[INVITE_ADMIN_SESSION]);
|
| 191 |
}
|
| 192 |
$sql = "SELECT i.uid, u1.name, COUNT(*) AS invites, u1.data FROM {invite} i INNER JOIN {users} u1 ON u1.uid = i.uid". $filter ." GROUP BY i.uid, u1.name, u1.data";
|
| 193 |
$sql .= tablesort_sql($header);
|
| 194 |
$count_sql = "SELECT COUNT(DISTINCT i.uid) FROM {invite} i". $filter;
|
| 195 |
$result = pager_query($sql, 50, 0, $count_sql, $filter_args);
|
| 196 |
$rows = array();
|
| 197 |
|
| 198 |
while ($row = db_fetch_object($result)) {
|
| 199 |
$cells = array();
|
| 200 |
$cells[] = theme('username', $row);
|
| 201 |
$cells[] = $row->invites;
|
| 202 |
$cells[] = invite_count($row->uid, 'accepted');
|
| 203 |
$cells[] = invite_count($row->uid, 'pending');
|
| 204 |
$cells[] = invite_count($row->uid, 'expired');
|
| 205 |
$remaining = invite_get_remaining_invites($row);
|
| 206 |
$cells[] = ($remaining == INVITE_UNLIMITED) ? '∞' : $remaining;
|
| 207 |
$cells[] = l(t('details'), "admin/user/invite/details/$row->uid", array('query' => $query));
|
| 208 |
$rows[] = $cells;
|
| 209 |
}
|
| 210 |
|
| 211 |
$output .= theme('table', $header, $rows, array('id' => 'invite'));
|
| 212 |
if (!$rows) {
|
| 213 |
$output .= t('No inviters found.');
|
| 214 |
}
|
| 215 |
else {
|
| 216 |
$output .= theme('pager', array(), 50, 0);
|
| 217 |
}
|
| 218 |
|
| 219 |
return $output;
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* Creates the user filter form for the admin overview.
|
| 224 |
*/
|
| 225 |
function invite_admin_filter_form() {
|
| 226 |
$form['filter'] = array(
|
| 227 |
'#type' => 'fieldset',
|
| 228 |
'#title' => t('Filter'),
|
| 229 |
'#prefix' => '<div class="container-inline">',
|
| 230 |
'#suffix' => '</div>',
|
| 231 |
);
|
| 232 |
$form['filter']['filter'] = array(
|
| 233 |
'#type' => 'textfield',
|
| 234 |
'#title' => t('Find an invited user'),
|
| 235 |
'#default_value' => isset($_SESSION[INVITE_ADMIN_SESSION]) ? $_SESSION[INVITE_ADMIN_SESSION] : '',
|
| 236 |
'#autocomplete_path' => 'user/autocomplete',
|
| 237 |
'#size' => 20,
|
| 238 |
);
|
| 239 |
$form['filter']['op'] = array(
|
| 240 |
'#type' => 'submit',
|
| 241 |
'#value' => t('Search'),
|
| 242 |
);
|
| 243 |
return $form;
|
| 244 |
}
|
| 245 |
|
| 246 |
function invite_admin_filter_form_submit($form, &$form_state) {
|
| 247 |
if ($form_state['values']['filter']) {
|
| 248 |
$_SESSION[INVITE_ADMIN_SESSION] = $form_state['values']['filter'];
|
| 249 |
}
|
| 250 |
}
|
| 251 |
|
| 252 |
/**
|
| 253 |
* Return a list of invites by a user.
|
| 254 |
*
|
| 255 |
* @param $account
|
| 256 |
* A user object.
|
| 257 |
*/
|
| 258 |
function invite_admin_details($account) {
|
| 259 |
$now = time();
|
| 260 |
$status_sort = '';
|
| 261 |
if (isset($_GET['order']) && $_GET['order'] == t('Status')) {
|
| 262 |
$sort = db_escape_string($_GET['sort']);
|
| 263 |
if ($_GET['sort'] == 'asc') {
|
| 264 |
$status_sort = "IF(i.joined != 0, 0, 1) $sort, IF(i.expiry < $now, 0, 1) $sort, i.canceled";
|
| 265 |
}
|
| 266 |
else {
|
| 267 |
$status_sort = "i.canceled $sort, IF(i.expiry < $now, 0, 1) $sort, IF(i.joined != 0, 0, 1)";
|
| 268 |
}
|
| 269 |
}
|
| 270 |
$header = array(
|
| 271 |
array('data' => t('E-mail'), 'field' => 'i.email', 'sort' => 'asc'),
|
| 272 |
array('data' => t('Username'), 'field' => 'if(u.name IS NULL, 0, 1) DESC, u.name'),
|
| 273 |
array('data' => t('Created'), 'field' => 'i.created'),
|
| 274 |
array('data' => t('Expires'), 'field' => 'i.expiry'),
|
| 275 |
array('data' => t('Status'), 'field' => $status_sort),
|
| 276 |
);
|
| 277 |
|
| 278 |
$output = '';
|
| 279 |
$filter = $filter_args = NULL;
|
| 280 |
if (isset($_GET['filter']) && $_GET['filter'] != '') {
|
| 281 |
$filter = " AND LOWER(u.name) LIKE '%s%%'";
|
| 282 |
$filter_args = $_GET['filter'];
|
| 283 |
$output .= drupal_get_form('invite_admin_details_filter_form', $account->uid, $_GET['filter']);
|
| 284 |
}
|
| 285 |
$sql = "SELECT i.email, i.invitee AS uid, u.name, i.created, i.expiry, i.joined, i.canceled FROM {invite} i LEFT JOIN {users} u ON u.uid = i.invitee AND u.uid <> 0 WHERE i.uid = %d". $filter;
|
| 286 |
$sql .= tablesort_sql($header);
|
| 287 |
$result = pager_query($sql, 50, 0, NULL, $account->uid, $filter_args);
|
| 288 |
$rows = array();
|
| 289 |
|
| 290 |
while ($row = db_fetch_object($result)) {
|
| 291 |
$cells = array();
|
| 292 |
$cells[] = check_plain($row->email);
|
| 293 |
$cells[] = $row->joined ? theme('username', $row) : '';
|
| 294 |
$cells[] = format_date($row->created, 'custom', 'Y-m-d');
|
| 295 |
$cells[] = format_date($row->expiry, 'custom', 'Y-m-d');
|
| 296 |
$cells[] = $row->canceled ? t('Withdrawn') : ($row->joined ? t('Joined') : ($row->expiry < $now ? t('Expired') : t('Pending')));
|
| 297 |
$rows[] = $cells;
|
| 298 |
}
|
| 299 |
|
| 300 |
$output .= theme('table', $header, $rows, array('id' => 'invite'));
|
| 301 |
if (!$rows) {
|
| 302 |
$output .= t('No invitees found.');
|
| 303 |
}
|
| 304 |
else {
|
| 305 |
$output .= theme('pager', array(), 50, 0);
|
| 306 |
}
|
| 307 |
|
| 308 |
return $output;
|
| 309 |
}
|
| 310 |
|
| 311 |
function invite_admin_details_filter_form($uid, $filter) {
|
| 312 |
$form['#action'] = "admin/user/invite/details/$uid";
|
| 313 |
$form['filter'] = array(
|
| 314 |
'#type' => 'fieldset',
|
| 315 |
'#title' => t('Filter'),
|
| 316 |
'#prefix' => '<div class="container-inline">',
|
| 317 |
'#suffix' => '</div>',
|
| 318 |
);
|
| 319 |
$form['filter']['filter'] = array(
|
| 320 |
'#type' => 'item',
|
| 321 |
'#title' => t('Active filter'),
|
| 322 |
'#value' => check_plain($filter),
|
| 323 |
);
|
| 324 |
$form['filter']['op'] = array(
|
| 325 |
'#type' => 'button',
|
| 326 |
'#value' => t('Clear'),
|
| 327 |
'#prefix' => ' ',
|
| 328 |
);
|
| 329 |
return $form;
|
| 330 |
}
|
| 331 |
|
| 332 |
/**
|
| 333 |
* Menu callback; display an overview of sent invitations.
|
| 334 |
*
|
| 335 |
* @param $page
|
| 336 |
* Which invites to list: accepted, pending, or expired.
|
| 337 |
*/
|
| 338 |
function invite_user_overview($page = 'accepted') {
|
| 339 |
global $user;
|
| 340 |
|
| 341 |
$rows = array();
|
| 342 |
$time = time();
|
| 343 |
$profile_access = user_access('access user profiles');
|
| 344 |
$allow_delete = user_access('withdraw accepted invitations');
|
| 345 |
|
| 346 |
switch ($page) {
|
| 347 |
case 'accepted':
|
| 348 |
default:
|
| 349 |
$sql = "SELECT i.*, u.uid AS account FROM {invite} i LEFT JOIN {users} u ON u.uid = i.invitee AND u.uid <> 0 WHERE i.uid = %d AND i.joined <> 0 AND canceled = 0 ORDER BY u.uid DESC";
|
| 350 |
break;
|
| 351 |
case 'pending':
|
| 352 |
$sql = "SELECT * FROM {invite} WHERE uid = %d AND joined = 0 AND canceled = 0 AND expiry >= %d ORDER BY expiry DESC";
|
| 353 |
break;
|
| 354 |
case 'expired':
|
| 355 |
$sql = "SELECT * FROM {invite} WHERE uid = %d AND joined = 0 AND canceled = 0 AND expiry < %d ORDER BY expiry DESC";
|
| 356 |
break;
|
| 357 |
}
|
| 358 |
|
| 359 |
$result = pager_query($sql, 50, 0, NULL, $user->uid, $time);
|
| 360 |
|
| 361 |
while ($invite = db_fetch_object($result)) {
|
| 362 |
$row = array();
|
| 363 |
switch ($page) {
|
| 364 |
case 'accepted':
|
| 365 |
default:
|
| 366 |
$account_exists = !is_null($invite->account);
|
| 367 |
|
| 368 |
if ($profile_access && $account_exists) {
|
| 369 |
$row[] = l($invite->email, 'user/'. $invite->invitee, array('title' => t('View user profile.')));
|
| 370 |
}
|
| 371 |
else {
|
| 372 |
$row[] = check_plain($invite->email);
|
| 373 |
}
|
| 374 |
$row[] = $account_exists ? t('Accepted') : t('Deleted');
|
| 375 |
$row[] = $allow_delete ? l(t('withdraw'), "invite/withdraw/$page/$invite->reg_code") : '';
|
| 376 |
$row[] = '';
|
| 377 |
break;
|
| 378 |
|
| 379 |
case 'pending':
|
| 380 |
case 'expired':
|
| 381 |
$expired = ($invite->expiry < $time);
|
| 382 |
|
| 383 |
$row[] = check_plain($invite->email);
|
| 384 |
$row[] = $expired ? t('Expired') : t('Pending');
|
| 385 |
$row[] = l(t('withdraw'), "invite/withdraw/$page/$invite->reg_code");
|
| 386 |
$row[] = $expired ? l(t('resend'), "invite/resend/$invite->reg_code") : '';
|
| 387 |
break;
|
| 388 |
}
|
| 389 |
$rows[] = $row;
|
| 390 |
}
|
| 391 |
|
| 392 |
return theme('invite_user_overview', $rows);
|
| 393 |
}
|
| 394 |
|
| 395 |
/**
|
| 396 |
* Theme function; display the invite overview table.
|
| 397 |
*
|
| 398 |
* @param $items
|
| 399 |
* An array of table rows.
|
| 400 |
*
|
| 401 |
* @ingroup themeable
|
| 402 |
*/
|
| 403 |
function theme_invite_user_overview($items) {
|
| 404 |
if (count($items) > 0) {
|
| 405 |
$headers = array(t('E-mail'), t('Status'), array('data' => t('Operations'), 'colspan' => 2));
|
| 406 |
$output = theme('table', $headers, $items, array('id' => 'invites'));
|
| 407 |
$output .= theme('pager', NULL, 50, 0);
|
| 408 |
}
|
| 409 |
else {
|
| 410 |
$output = t('No invitations available.');
|
| 411 |
}
|
| 412 |
|
| 413 |
return $output;
|
| 414 |
}
|
| 415 |
|