| 1 |
<?php
|
| 2 |
// $Id: signup_status.module,v 1.26 2009/09/20 00:47:44 dww Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Implements signup statuses
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
|
| 11 |
define('SIGNUP_STATUS_MANAGE_PERMISSION', 'manage signup status codes');
|
| 12 |
|
| 13 |
//////////////////////////////////////////////////////////////////////////////
|
| 14 |
// Core API hooks
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_perm().
|
| 18 |
*/
|
| 19 |
function signup_status_perm() {
|
| 20 |
return array(SIGNUP_STATUS_MANAGE_PERMISSION);
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_menu().
|
| 25 |
*/
|
| 26 |
function signup_status_menu() {
|
| 27 |
$items['admin/settings/signup_status'] = array(
|
| 28 |
'title' => 'Signup status',
|
| 29 |
'description' => 'Configure signup status settings.',
|
| 30 |
'page callback' => 'drupal_get_form',
|
| 31 |
'page arguments' => array('signup_status_admin_settings_form', 4),
|
| 32 |
'access arguments' => array(SIGNUP_STATUS_MANAGE_PERMISSION),
|
| 33 |
'file' => 'signup_status.admin.inc',
|
| 34 |
);
|
| 35 |
$items['admin/settings/signup_status/delete'] = array(
|
| 36 |
'title' => 'Configure status code',
|
| 37 |
'page callback' => 'drupal_get_form',
|
| 38 |
'page arguments' => array('signup_status_admin_delete', 4),
|
| 39 |
'type' => MENU_CALLBACK,
|
| 40 |
'access arguments' => array(SIGNUP_STATUS_MANAGE_PERMISSION),
|
| 41 |
'file' => 'signup_status.admin.inc',
|
| 42 |
);
|
| 43 |
return $items;
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Implementation of hook_action_info().
|
| 48 |
*/
|
| 49 |
function signup_status_action_info() {
|
| 50 |
return array(
|
| 51 |
'signup_status_alter_action' => array(
|
| 52 |
'type' => 'signup',
|
| 53 |
'description' => t('Alter signup status'),
|
| 54 |
'configurable' => TRUE,
|
| 55 |
),
|
| 56 |
);
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Implement hook_theme().
|
| 61 |
*/
|
| 62 |
function signup_status_theme() {
|
| 63 |
return array(
|
| 64 |
'signup_status_admin_settings_form' => array(
|
| 65 |
'file' => 'signup_status.admin.inc',
|
| 66 |
'arguments' => array(
|
| 67 |
'form' => NULL,
|
| 68 |
),
|
| 69 |
),
|
| 70 |
);
|
| 71 |
}
|
| 72 |
|
| 73 |
//////////////////////////////////////////////////////////////////////////////
|
| 74 |
// Views integration
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Implementation of hook_views_api().
|
| 78 |
*/
|
| 79 |
function signup_status_views_api() {
|
| 80 |
return array(
|
| 81 |
'api' => 2.0,
|
| 82 |
'path' => drupal_get_path('module', 'signup_status') .'/views',
|
| 83 |
);
|
| 84 |
}
|
| 85 |
|
| 86 |
//////////////////////////////////////////////////////////////////////////////
|
| 87 |
// Actions integration
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Perform the action to alter the signup status of a given signup.
|
| 91 |
*/
|
| 92 |
function signup_status_alter_action(&$signup, $context) {
|
| 93 |
if ($context['signup_status'] != $signup->status) {
|
| 94 |
$status_codes = signup_status_codes();
|
| 95 |
$signup->old_status = $signup->status;
|
| 96 |
$signup->status = $context['signup_status'];
|
| 97 |
db_query("UPDATE {signup_log} SET status = %d WHERE sid = %d", $signup->status, $signup->sid);
|
| 98 |
watchdog('action', 'Set status of %signup_label to %status_name.', array('%signup_label' => $signup->label, '%status_name' => $status_codes[$signup->status]['name']));
|
| 99 |
_signup_status_change('update', $signup);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* Create the configuration form to select which signup status to use.
|
| 105 |
*/
|
| 106 |
function signup_status_alter_action_form($context) {
|
| 107 |
$options = array();
|
| 108 |
foreach (signup_status_codes() as $cid => $code) {
|
| 109 |
$options[$cid] = $code['name'];
|
| 110 |
}
|
| 111 |
$form['signup_status'] = array(
|
| 112 |
'#type' => 'select',
|
| 113 |
'#title' => t('Signup status'),
|
| 114 |
'#options' => $options,
|
| 115 |
);
|
| 116 |
return $form;
|
| 117 |
}
|
| 118 |
|
| 119 |
/**
|
| 120 |
* Validate the form to select which signup status to use for the alter action.
|
| 121 |
*/
|
| 122 |
function signup_status_alter_action_validate($form, $form_state) {
|
| 123 |
if ($form_state['values']['signup_status'] == 0) {
|
| 124 |
form_set_error('signup_status', t('You must select a status.'));
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Submit handler for the form to select which signup status to use.
|
| 130 |
*/
|
| 131 |
function signup_status_alter_action_submit($form, $form_state) {
|
| 132 |
return array('signup_status' => $form_state['values']['signup_status']);
|
| 133 |
}
|
| 134 |
|
| 135 |
//////////////////////////////////////////////////////////////////////////////
|
| 136 |
// Signup integration
|
| 137 |
|
| 138 |
/**
|
| 139 |
* Implementation of hook_signup_cancel().
|
| 140 |
*/
|
| 141 |
function signup_status_signup_cancel($signup, $node) {
|
| 142 |
_signup_status_change('delete', $signup);
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implement hook_signup_data_alter().
|
| 147 |
*/
|
| 148 |
function signup_status_signup_data_alter(&$signup, $form_values) {
|
| 149 |
// Save the signup status in the $signup object.
|
| 150 |
if (!empty($form_values['signup_status'])) {
|
| 151 |
$signup->status = $form_values['signup_status'];
|
| 152 |
}
|
| 153 |
|
| 154 |
// See if the current status should impact the signup total or not.
|
| 155 |
if (!empty($signup->status)) {
|
| 156 |
$signup_status_codes = signup_status_codes();
|
| 157 |
if (empty($signup_status_codes[$signup->status]['mod_signup_count'])) {
|
| 158 |
$signup->count_towards_limit = 0;
|
| 159 |
}
|
| 160 |
else {
|
| 161 |
$signup->count_towards_limit = 1;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Implement hook_signup_insert().
|
| 168 |
*/
|
| 169 |
function signup_status_signup_insert($signup) {
|
| 170 |
_signup_status_change('add', $signup);
|
| 171 |
}
|
| 172 |
|
| 173 |
// Note: we don't want to always invoke _signup_status_change('update') via
|
| 174 |
// hook_signup_update(), since by the time that hook is invoked, we don't
|
| 175 |
// actually know if the status itself changed during the edit. So, we still
|
| 176 |
// have our own submit handler on the signup_edit_form for this.
|
| 177 |
|
| 178 |
//////////////////////////////////////////////////////////////////////////////
|
| 179 |
// Signup form altering
|
| 180 |
|
| 181 |
/**
|
| 182 |
* Helper function to generate the signup status form element.
|
| 183 |
*
|
| 184 |
* @param $current_status
|
| 185 |
* Optional code for the current signup status to use as the default.
|
| 186 |
*
|
| 187 |
* @return
|
| 188 |
* FormAPI array defining the signup status form element.
|
| 189 |
*/
|
| 190 |
function _signup_status_status_form_element($current_status = NULL) {
|
| 191 |
$element = array();
|
| 192 |
$options = array();
|
| 193 |
foreach (signup_status_codes() as $cid => $code) {
|
| 194 |
if ($code['show_on_form']) {
|
| 195 |
$options[$cid] = $code['name'];
|
| 196 |
}
|
| 197 |
}
|
| 198 |
if (!empty($options)) {
|
| 199 |
if (!isset($current_status)) {
|
| 200 |
$options = array(-1 => t('- Please choose -')) + $options;
|
| 201 |
}
|
| 202 |
$element = array(
|
| 203 |
'#type' => 'select',
|
| 204 |
'#title' => t('Status'),
|
| 205 |
'#options' => $options,
|
| 206 |
'#weight' => 1,
|
| 207 |
'#required' => TRUE,
|
| 208 |
);
|
| 209 |
if (isset($current_status)) {
|
| 210 |
$element['#default_value'] = $current_status;
|
| 211 |
}
|
| 212 |
}
|
| 213 |
return $element;
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Alter the signup form to add a status selector, if available.
|
| 218 |
*/
|
| 219 |
function signup_status_form_signup_form_alter(&$form, $form_state) {
|
| 220 |
$status_element = _signup_status_status_form_element();
|
| 221 |
if (!empty($status_element)) {
|
| 222 |
$form['collapse']['signup_status'] = $status_element;
|
| 223 |
$form['collapse']['submit']['#weight'] = 2;
|
| 224 |
$form['#validate'][] = 'signup_status_signup_form_validate_status';
|
| 225 |
// The status is saved automatically during signup_save_signup(), and
|
| 226 |
// we're notified about the change via hook_signup_insert(), so we don't
|
| 227 |
// need our own submit handler here at all.
|
| 228 |
}
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Validate the status field on the altered signup form.
|
| 233 |
*/
|
| 234 |
function signup_status_signup_form_validate_status($form, $form_state) {
|
| 235 |
if (!isset($form_state['values']['signup_status']) || $form_state['values']['signup_status'] == -1) {
|
| 236 |
form_set_error('signup_status', t('You must select a valid status.'));
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Alter the signup edit form to add a status selector, if available.
|
| 242 |
*/
|
| 243 |
function signup_status_form_signup_edit_form_alter(&$form, $form_state) {
|
| 244 |
$signup = $form['#signup'];
|
| 245 |
$status_element = _signup_status_status_form_element($signup->status);
|
| 246 |
if (!empty($status_element)) {
|
| 247 |
$form['elements']['signup_status'] = $status_element;
|
| 248 |
if (empty($form['elements']['save'])) {
|
| 249 |
// The user can't edit their own signup, disable the status selector.
|
| 250 |
$form['elements']['signup_status']['#disabled'] = TRUE;
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
// Even though the status itself is recorded to the DB automagically via
|
| 254 |
// signup_save_signup(), we need to see if we should invoke
|
| 255 |
// _signup_status_change('update') based on if the status actually
|
| 256 |
// changed or not, and that requires our own submit handler, since
|
| 257 |
// hook_signup_changed() doesn't provide enough info for us to decide.
|
| 258 |
$form['elements']['save']['#submit'][] = 'signup_status_alter_signup_edit_form_submit';
|
| 259 |
$form['elements']['save']['#weight'] = 2;
|
| 260 |
$form['elements']['cancel-signup']['#weight'] = 3;
|
| 261 |
}
|
| 262 |
}
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* Handle submission of the altered signup edit form.
|
| 267 |
*
|
| 268 |
* This is just responsible for seeing if the status actually changed, and if
|
| 269 |
* so, calling _signup_status_change('update').
|
| 270 |
*
|
| 271 |
* @see signup_status_form_signup_edit_form_alter()
|
| 272 |
* @see _signup_status_change()
|
| 273 |
*/
|
| 274 |
function signup_status_alter_signup_edit_form_submit($form, &$form_state) {
|
| 275 |
$values = $form_state['values'];
|
| 276 |
$signup = $form['#signup'];
|
| 277 |
if (isset($values['signup_status']) && $values['signup_status'] != $signup->status) {
|
| 278 |
$signup->old_status = $signup->status;
|
| 279 |
$signup->status = $values['signup_status'];
|
| 280 |
_signup_status_change('update', $signup);
|
| 281 |
}
|
| 282 |
}
|
| 283 |
|
| 284 |
/**
|
| 285 |
* Alter the broadcast form to allow broadcasting to just a single status.
|
| 286 |
*/
|
| 287 |
function signup_status_form_signup_broadcast_form_alter(&$form, $form_state) {
|
| 288 |
$options = array();
|
| 289 |
$codes = signup_status_codes();
|
| 290 |
$options[-1] = t('- All -');
|
| 291 |
foreach ($codes as $cid => $code) {
|
| 292 |
$options[$cid] = $code['name'];
|
| 293 |
}
|
| 294 |
|
| 295 |
$form['signup_status_codes'] = array(
|
| 296 |
'#type' => 'select',
|
| 297 |
'#title' => t('Limit recipients'),
|
| 298 |
'#description' => t('Send this email to a specific set of users based on signup status.'),
|
| 299 |
'#multiple' => TRUE,
|
| 300 |
'#options' => $options,
|
| 301 |
'#default_value' => -1,
|
| 302 |
);
|
| 303 |
$form['send']['#weight'] = 10;
|
| 304 |
|
| 305 |
$form['#validate'][] = 'signup_status_signup_broadcast_form_validate';
|
| 306 |
// Replace the default submit handler in case the user restricts by status.
|
| 307 |
foreach ($form['#submit'] as $key => $handler) {
|
| 308 |
if ($handler == 'signup_broadcast_form_submit') {
|
| 309 |
unset($form['#submit'][$key]);
|
| 310 |
break;
|
| 311 |
}
|
| 312 |
}
|
| 313 |
$form['#submit'][] = 'signup_status_signup_broadcast_form_submit';
|
| 314 |
}
|
| 315 |
|
| 316 |
/**
|
| 317 |
* Validate altered broadcast form.
|
| 318 |
*/
|
| 319 |
function signup_status_signup_broadcast_form_validate($form, &$form_state) {
|
| 320 |
$codes = $form_state['values']['signup_status_codes'];
|
| 321 |
if (!in_array(-1, $codes)) {
|
| 322 |
$signups = signup_get_signups($form_state['values']['nid']);
|
| 323 |
$count = 0;
|
| 324 |
foreach ($signups as $signup) {
|
| 325 |
if (in_array($signup->status, $codes)) {
|
| 326 |
$count++;
|
| 327 |
}
|
| 328 |
}
|
| 329 |
if (!$count) {
|
| 330 |
form_set_error('signup_status_codes', t('No users are signed up with that status.'));
|
| 331 |
}
|
| 332 |
}
|
| 333 |
}
|
| 334 |
|
| 335 |
/**
|
| 336 |
* Handles submission of the altered broadcast form.
|
| 337 |
*/
|
| 338 |
function signup_status_signup_broadcast_form_submit($form, &$form_state) {
|
| 339 |
global $user;
|
| 340 |
|
| 341 |
if (user_access('administer site configuration')) {
|
| 342 |
$from = $form_state['values']['from'];
|
| 343 |
}
|
| 344 |
else {
|
| 345 |
$from = $user->mail;
|
| 346 |
}
|
| 347 |
|
| 348 |
$signups = array();
|
| 349 |
$codes = $form_state['values']['signup_status_codes'];
|
| 350 |
if (!in_array(-1, $codes)) {
|
| 351 |
foreach (signup_get_signups($form_state['values']['nid']) as $signup) {
|
| 352 |
if (in_array($signup->status, $codes)) {
|
| 353 |
$signups[] = $signup;
|
| 354 |
}
|
| 355 |
}
|
| 356 |
}
|
| 357 |
|
| 358 |
module_load_include('inc', 'signup', 'includes/broadcast');
|
| 359 |
signup_send_broadcast($form_state['values']['nid'], $form_state['values']['subject'], $form_state['values']['message'], $from, !empty($form_state['values']['copy']), $signups);
|
| 360 |
}
|
| 361 |
|
| 362 |
//////////////////////////////////////////////////////////////////////////////
|
| 363 |
// Auxiliary functions
|
| 364 |
|
| 365 |
/**
|
| 366 |
* Retrieve all available status codes.
|
| 367 |
*
|
| 368 |
* @return
|
| 369 |
* An array of status code arrays, keyed using the status code id, cid.
|
| 370 |
* Each status code array contains the following keys / values:
|
| 371 |
* - "name": The display name of the status code.
|
| 372 |
* - "description": The long-form description of the status code.
|
| 373 |
* - "mod_signup_count": A boolean value stating whether signups using the
|
| 374 |
* status code should modify the total signup count (i.e. for the "wait
|
| 375 |
* listed" status code).
|
| 376 |
* - "show_on_form": A boolean value stating if this status should be
|
| 377 |
* available on the signup form.
|
| 378 |
*/
|
| 379 |
function signup_status_codes() {
|
| 380 |
static $codes = array();
|
| 381 |
if (empty($codes)) {
|
| 382 |
$result = db_query("SELECT * FROM {signup_status_codes} ORDER BY weight");
|
| 383 |
while ($row = db_fetch_array($result)) {
|
| 384 |
$codes[$row['cid']] = $row;
|
| 385 |
}
|
| 386 |
}
|
| 387 |
return $codes;
|
| 388 |
}
|
| 389 |
|
| 390 |
/**
|
| 391 |
* Fire the hook on the signup status change.
|
| 392 |
* @param $action
|
| 393 |
* A string representing signup status change.
|
| 394 |
* @param $signup
|
| 395 |
* A signup object.
|
| 396 |
*/
|
| 397 |
function _signup_status_change($action, $signup) {
|
| 398 |
module_invoke_all('signup_status', $action, $signup);
|
| 399 |
}
|
| 400 |
|
| 401 |
/**
|
| 402 |
* Implement hook_token_list() (from token.module)
|
| 403 |
*/
|
| 404 |
function signup_status_token_list($type) {
|
| 405 |
$tokens = array();
|
| 406 |
if ($type == 'signup') {
|
| 407 |
$tokens['signup'] = array(
|
| 408 |
'signup-status' => t('The current signup status.'),
|
| 409 |
'signup-status-raw' => t('Unfiltered current signup status. WARNING - raw user input.'),
|
| 410 |
'signup-status-description' => t('The description of the current signup status.'),
|
| 411 |
'signup-status-description-raw' => t('Unfiltered description of the current signup status. WARNING - raw user input.'),
|
| 412 |
'signup-status-previous' => t('The previous signup status (only available when a signup is being modified).'),
|
| 413 |
'signup-status-previous-raw' => t('Unfiltered previous signup status (only available when a signup is being modified). WARNING - raw user input.'),
|
| 414 |
'signup-status-previous-description' => t('The description of the previous signup status (only available when a signup is being modified).'),
|
| 415 |
'signup-status-previous-description-raw' => t('Unfiltered description of the previous signup status (only available when a signup is being modified). WARNING - raw user input.'),
|
| 416 |
);
|
| 417 |
}
|
| 418 |
return $tokens;
|
| 419 |
}
|
| 420 |
|
| 421 |
/**
|
| 422 |
* Implement hook_token_values() (from token.module)
|
| 423 |
*/
|
| 424 |
function signup_status_token_values($type = 'all', $object = NULL) {
|
| 425 |
$values = array();
|
| 426 |
if ($type == 'signup') {
|
| 427 |
$values = array(
|
| 428 |
'signup-status' => '',
|
| 429 |
'signup-status-raw' => '',
|
| 430 |
'signup-status-description' => '',
|
| 431 |
'signup-status-description-raw' => '',
|
| 432 |
'signup-status-previous' => '',
|
| 433 |
'signup-status-previous-raw' => '',
|
| 434 |
'signup-status-description-previous' => '',
|
| 435 |
'signup-status-description-previous-raw' => '',
|
| 436 |
);
|
| 437 |
$codes = signup_status_codes();
|
| 438 |
if (!empty($object->status)) {
|
| 439 |
$values['signup-status'] = check_plain($codes[$object->status]['name']);
|
| 440 |
$values['signup-status-raw'] = $codes[$object->status]['name'];
|
| 441 |
$values['signup-status-description'] = filter_xss($codes[$object->status]['description']);
|
| 442 |
$values['signup-status-description-raw'] = $codes[$object->status]['description'];
|
| 443 |
}
|
| 444 |
if (!empty($object->old_status)) {
|
| 445 |
$values['signup-status-previous'] = check_plain($codes[$object->old_status]['name']);
|
| 446 |
$values['signup-status-previous-raw'] = $codes[$object->old_status]['name'];
|
| 447 |
$values['signup-status-description-previous'] = filter_xss($codes[$object->old_status]['description']);
|
| 448 |
$values['signup-status-description-previous-raw'] = $codes[$object->old_status]['description'];
|
| 449 |
}
|
| 450 |
}
|
| 451 |
return $values;
|
| 452 |
}
|
| 453 |
|