diff --git a/admin_select.module b/admin_select.module index dce925e..8110f56 100644 --- a/admin_select.module +++ b/admin_select.module @@ -124,14 +124,7 @@ function admin_select_defaults_submit($form, $form_state) { */ function admin_select_form_user_profile_form_alter(&$form, &$form_state) { if ($form['#user_category'] == 'account' && user_access('select administration menu')) { - $info = admin_select_get_admin_select_info(); - $options = array(); - foreach ($info as $key => $item) { - if (!empty($item['access'])) { - $options[$key] = $item['title']; - } - } - asort($options); + $options = admin_select_get_options_array(); $form['admin_select'] = array( '#type' => 'fieldset', '#title' => t('Administration Menu select settings'), @@ -148,6 +141,27 @@ function admin_select_form_user_profile_form_alter(&$form, &$form_state) { } /** + * Get the available options array for a select element. + * + * @param bool $check_access + * Whether or not to check the user's access to the possible menu options. + * + * @return array + * The options array. + */ +function admin_select_get_options_array($check_access = TRUE) { + $info = admin_select_get_admin_select_info($check_access); + $options = array(); + foreach ($info as $key => $item) { + if (!empty($item['access']) || !$check_access) { + $options[$key] = $item['title']; + } + } + asort($options); + return $options; +} + +/** * Implements hook_user_presave(). */ function admin_select_user_presave(&$edit, $account, $category) { @@ -254,3 +268,74 @@ function theme_admin_select_defaults($variables) { $output .= drupal_render_children($form); return $output; } + +/** + * Implements hook_action_info(). + */ +function admin_select_action_info() { + return array( + 'admin_select_set_admin_menu_action' => array( + 'label' => t("Set user's admin menu"), + 'type' => 'user', + 'configurable' => TRUE, + 'triggers' => array('any'), + ), + ); +} + +/** + * Sets the admin select setting for a user. + * + * @param object $entity + * The user to operate on. + * @param array $context + * An array with the following elements: + * - 'admin_select': The admin select menu to use. + * + * @see admin_select_set_admin_menu_action_form() + * @see admin_select_set_admin_menu_action_submit() + * + * @ingroup actions + */ +function admin_select_set_admin_menu_action($user, $context) { + $edit = array( + 'data' => array( + 'admin_select' => $context['admin_select'] ? $context['admin_select'] : '', + ), + ); + user_save($user, $edit); +} + +/** + * Generates the settings form for admin_select_set_admin_menu_action(). + * + * @param $context + * Array of additional information about what triggered the action. + * + * @return array + * A form array. + * + * @see admin_select_set_admin_menu_action_submit() + * + * @ingroup forms + */ +function admin_select_set_admin_menu_action_form($context) { + $form = array(); + $options = admin_select_get_options_array(FALSE); + $form['admin_select'] = array( + '#type' => 'select', + '#title' => t('Administraion menu'), + '#options' => array('' => t('None')) + $options, + '#description' => t('Select the administration menu to use for these users.'), + ); + return $form; +} + +/** + * Saves settings form for admin_select_set_admin_menu_action(). + * + * @see admin_select_set_admin_menu_action_form() + */ +function admin_select_set_admin_menu_action_submit($form, &$form_state) { + return array('admin_select' => $form_state['values']['admin_select']); +}