Index: fasttoggle.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/fasttoggle/fasttoggle.module,v retrieving revision 1.4.2.2 diff -u -r1.4.2.2 fasttoggle.module --- fasttoggle.module 3 Feb 2007 21:26:43 -0000 1.4.2.2 +++ fasttoggle.module 13 Apr 2007 23:47:06 -0000 @@ -30,9 +30,9 @@ $user = user_load(array('uid' => arg(2))); if ($user->uid) { $items[] = array( - 'path' => 'admin/user/'. arg(2) .'/toggle/status', + 'path' => 'admin/user/'. arg(2) .'/toggle', 'title' => t('status'), - 'callback' => 'fasttoggle_user_status', + 'callback' => 'fasttoggle_user_option', 'callback arguments' => array($user), 'access' => user_access('administer users'), 'type' => MENU_CALLBACK @@ -121,7 +121,7 @@ $links = array(); if ($type == 'node' && user_access('administer nodes')) { - $options = fasttoggle_get_node_options(); + $options = fasttoggle_get_options('node'); $links['fasttoggle_status'] = fasttoggle($options['status'][intval($node->status)], "node/$node->nid/toggle/status", FALSE, 'status_'. $node->nid); $links['fasttoggle_sticky'] = fasttoggle($options['sticky'][intval($node->sticky)], "node/$node->nid/toggle/sticky", FALSE, 'sticky_'. $node->nid); @@ -136,7 +136,7 @@ * POST. Otherwise, display a confirmation form. */ function fasttoggle_node_option($node, $option) { - $options = fasttoggle_get_node_options(); + $options = fasttoggle_get_options('node'); // Check if the action is valid if (isset($options[$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $option .'_'. $node->nid, TRUE)) { @@ -173,22 +173,37 @@ /** * Confirmation form for the option change of a node. */ -function fasttoggle_node_option_confirm($node, $status) { +function fasttoggle_node_option_confirm($node, $option) { return confirm_form(array(), - t('Are you sure you want to set %title to %status?', array('%title' => $node->title, '%status' => $status)), + t('Are you sure you want to set %title to %option?', array('%title' => $node->title, '%option' => $option)), $_GET['destination'] ? $_GET['destination'] : 'node/'. $node->nid, '', t('Change'), t('Cancel')); } +/** + * Return an array of toggleable node options and the name of each state. + */ +function fasttoggle_get_options($type) { + return module_invoke_all('fasttoggle_options', $type); +} -// an array of toggleable node options and the name of each state -function fasttoggle_get_node_options() { - return array( - 'status' => array(0 => t('not published'), 1 => t('published')), - 'sticky' => array(0 => t('not sticky'), 1 => t('sticky')), - 'promote' => array(0 => t('not promoted'), 1 => t('promoted')), - ); +/** + * Implementation of hook_fasttoggle_options(). + */ +function fasttoggle_fasttoggle_options($type) { + switch ($type) { + case 'node': + return array( + 'status' => array(0 => t('not published'), 1 => t('published')), + 'sticky' => array(0 => t('not sticky'), 1 => t('sticky')), + 'promote' => array(0 => t('not promoted'), 1 => t('promoted')), + ); + case 'user': + return array( + 'status' => array(0 => t('blocked'), 1 => t('active')), + ); + } } @@ -196,16 +211,20 @@ * Menu callback. Toggle the status of a user if the action is confirmed via * POST. Otherwise, display a confirmation form. */ -function fasttoggle_user_status($user) { - if (isset($_GET['token']) && drupal_valid_token($_GET['token'], 'status_'. $user->uid, TRUE)) { +function fasttoggle_user_option($user, $option) { + $options = fasttoggle_get_options('user'); + + if (isset($options[$option]) && isset($_GET['token']) && drupal_valid_token($_GET['token'], $option .'_'. $user->uid, TRUE)) { if (isset($_POST['confirm']) && $_POST['confirm']) { - $user = user_save($user, array('status' => !$user->status)); - // Output the new status for the updated link text on AJAX changes + $array = array($option => !$user->$option); + $user = user_save($user, $array); + + // Output the new option for the updated link text on AJAX changes if (isset($_POST['javascript']) && $_POST['javascript']) { drupal_set_header('Content-Type: text/javascript; charset=utf-8'); echo drupal_to_js(array( - 'text' => $user->status ? t('active') : t('blocked'), + 'text' => $options[$option][intval($array[$option])], )); exit; } @@ -217,7 +236,7 @@ // The action is not confirmed. The user came here through a regular link; // no AJAX was involved. That means, we need a confirmation form so that // we get a POST form. - return drupal_get_form('fasttoggle_user_status_confirm', $user); + return drupal_get_form('fasttoggle_user_option_confirm', $user, $options[$option][intval($array[$option])]); } } else { @@ -229,10 +248,10 @@ /** * Confirmation form for the status change of a user. */ -function fasttoggle_user_status_confirm($user) { +function fasttoggle_user_option_confirm($user, $option) { return confirm_form(array(), - $status ? t('Are you sure you want to unblock the user %user?', array('%user' => $user->name)) : t('Are you sure you want to block the user %user?', array('%user' => $user->name)), + t('Are you sure you want to set %user to %option?', array('%user' => $user->name, '%option' => $option)), $_GET['destination'] ? $_GET['destination'] : 'user/'. $user->uid, '', - $status ? t('Unblock') : t('Block'), t('Cancel')); + t('Change'), t('Cancel')); }