- <?php
- * @file
- * Definition of views_handler_field_bulk_form.
- */
-
- * Defines a actions-based bulk operation form element.
- *
- * This handler is not exposed directly in the UI, instead individual entity
- * types that support bulk operations should extend this class and expose their
- * own implementation via hook_views_data().
- */
- class views_handler_field_bulk_form extends views_handler_field {
-
-
- * Overrides views_handler_field::option_definition().
- */
- function option_definition() {
- $options = parent::option_definition();
- $options['include_exclude'] = array(
- 'default' => 'exclude',
- );
- $options['selected_actions'] = array(
- 'default' => array(),
- );
- return $options;
- }
-
-
- * Overrides views_handler_field::options_form().
- */
- function options_form(&$form, &$form_state) {
- parent::options_form($form, $form_state);
-
- $form['include_exclude'] = array(
- '#type' => 'radios',
- '#title' => t('Available actions'),
- '#options' => array(
- 'exclude' => t('All actions, except selected'),
- 'include' => t('Only selected actions'),
- ),
- '#default_value' => $this->options['include_exclude'],
- );
- $form['selected_actions'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Selected actions'),
- '#options' => $this->get_bulk_options(FALSE),
- '#default_value' => $this->options['selected_actions'],
- );
- }
-
-
- * Overrides views_handler::options_validate().
- */
- function options_validate(&$form, &$form_state) {
- parent::options_validate($form, $form_state);
-
- $form_state['values']['options']['selected_actions'] = array_filter($form_state['values']['options']['selected_actions']);
- }
-
-
- * Overrides views_handler_field::render().
- */
- function render($values) {
- return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
- }
-
-
- * Overrides views_handler_field::pre_render().
- */
- function pre_render(&$values) {
- parent::pre_render($values);
-
-
-
- if (!empty($this->view->style_plugin) && $this->view->style_plugin instanceof views_plugin_style_table) {
-
- $this->options['element_label_class'] .= 'select-all';
-
- $this->options['label'] = '';
- }
- }
-
-
- * Form constructor for the bulk form.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form.
- */
- function views_form(&$form, &$form_state) {
-
- $form['#attached']['js'][] = 'core/misc/tableselect.js';
-
-
- $form[$this->options['id']]['#tree'] = TRUE;
- foreach ($this->view->result as $row_index => $row) {
- $form[$this->options['id']][$row_index] = array(
- '#type' => 'checkbox',
-
-
- '#title' => t('Update this item'),
- '#title_display' => 'invisible',
- '#return_value' => $row->{$this->field_alias},
- '#default_value' => !empty($form_state['values'][$this->options['id']][$row_index]) ? 1 : NULL,
- '#attributes' => array('data-tableselect-id' => $this->options['id'])
- );
- }
-
-
- $form['header'] = array(
- '#weight' => -100,
- );
-
-
-
- $form['header'][$this->options['id']] = array(
- '#type' => 'container',
- '#attributes' => array('class' => array('views-bulk-form')),
- );
- $form['header'][$this->options['id']]['action'] = array(
- '#title' => t('Action'),
- '#title_display' => 'none',
- '#type' => 'select',
- '#options' => $this->get_bulk_options(),
- '#empty_option' => '- ' . t('Choose an action') . ' -',
- '#required' => TRUE,
- );
-
-
- $actions['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Execute'),
- );
- $form['header'][$this->options['id']]['actions'] = $actions;
- }
-
-
- * Submit handler for the bulk form.
- *
- * @param array $form
- * An associative array containing the structure of the form.
- * @param array $form_state
- * An associative array containing the current state of the form.
- */
- function views_form_submit(&$form, &$form_state) {
- if ($form_state['step'] == 'views_form_views_form') {
- $action_name = $form_state['values']['action'];
- $action_info = actions_get_info($form_state['values']['action']);
- $count = 0;
-
-
- $selected = array_filter($form_state['values'][$this->options['id']]);
- if (!empty($selected)) {
- $entity_type = $this->get_entity_type();
- $entities = entity_load($entity_type, $selected);
- $context = array();
- foreach ($entities as $entity) {
- if (actions_execute($action_name, $entity, $context) !== FALSE) {
- $count++;
- }
- }
-
-
- if (isset($context['redirect'])) {
- actions_redirect($context['redirect']);
- }
- }
- else {
- backdrop_set_message(t('No items selected.'), 'error');
- }
-
- if ($count) {
- backdrop_set_message(format_plural($count, '%action was applied to @count item.', '%action was applied to @count items.', array('%action' => $action_info['label'])));
- }
- }
- }
-
-
- * Returns the available operations for this form.
- *
- * @param bool $filtered
- * (optional) Whether to filter actions to selected actions.
- *
- * @return array
- * An associative array of operations, suitable for a select element.
- */
- protected function get_bulk_options($filtered = TRUE) {
-
- $actions = actions_get_info();
-
- $entity_type = $this->get_entity_type();
- $options = array();
-
- foreach ($actions as $action_name => $action_info) {
- if ($filtered) {
- $in_selected = in_array($action_name, $this->options['selected_actions']);
-
-
- if (($this->options['include_exclude'] == 'include') && !$in_selected) {
- continue;
- }
-
-
- elseif (($this->options['include_exclude'] == 'exclude') && $in_selected) {
- continue;
- }
- }
-
- if ($action_info['type'] == $entity_type) {
- $options[$action_name] = $action_info['label'];
- }
- }
-
- return $options;
- }
-
-
- * Determine the entity type of the bulk operation field.
- */
- protected function get_entity_type() {
- $base_table = isset($this->table) ? $this->table : $this->view->base_table;
- foreach (entity_get_info() as $entity_type => $info) {
- if (isset($info['base table']) && $info['base table'] == $base_table) {
- return $entity_type;
- }
- elseif (isset($info['revision table']) && $info['revision table'] == $base_table) {
- $this->revision = TRUE;
- return $entity_type;
- }
- }
- return FALSE;
- }
- }