You are here

registration_views.module in Entity Registration 8

Entity Registration Views integration

File

modules/registration_views/registration_views.module
View source
<?php

/**
 * @file
 * Entity Registration Views integration
 */

/**
 * Implements hook_views_api().
 */
function registration_views_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'registration_views') . '/includes',
  );
}

/**
 * Implements hook_views_data().
 */
function registration_views_views_data() {
  $data = array();
  $data['registration_entity']['table']['group'] = t('Registration Settings');
  $data['registration_entity']['table']['base'] = array(
    'field' => 'entity_id',
    'title' => t('Registration Settings'),
    'help' => t('Contains information about registration settings for entities.'),
    'weight' => -10,
  );
  $data['registration_entity']['open'] = array(
    'title' => t('Open Date'),
    'help' => t('When the entity will open for registrations.'),
    'field' => array(
      'handler' => 'registration_handler_field_entity_datetime',
    ),
  );
  $data['registration_entity']['close'] = array(
    'title' => t('Close Date'),
    'help' => t('When the entity will no longer accept registrations.'),
    'field' => array(
      'handler' => 'registration_handler_field_entity_datetime',
    ),
  );
  $data['registration_entity']['capacity_total'] = array(
    'real field' => 'capacity',
    'title' => t('Slots Total'),
    'help' => t('Maximum number of slots for an entity.'),
    'field' => array(
      'handler' => 'registration_handler_field_entity_capacity_total',
    ),
  );
  return $data;
}

/**
 * Implements hook_entity_info_alter().
 */
function registration_views_entity_info_alter(&$entity_info) {

  // Set our custom Views controller class.
  $entity_info['registration']['views controller class'] = 'RegistrationViewsController';
}

/**
 * Implements hook_action_info().
 */
function registration_views_action_info() {
  return array(
    'registration_views_set_state_action' => array(
      'type' => 'registration',
      'label' => t('Set the Registration State'),
      'behavior' => array(
        'changes_property',
      ),
      'configurable' => FALSE,
      'vbo_configurable' => TRUE,
      'triggers' => array(
        'any',
      ),
    ),
  );
}

/**
 * Implements hook_views_bulk_operation_form().
 *
 * Provides a configuration form for the VBO dialogue when configuring a view.
 */
function registration_views_set_state_action_views_bulk_operations_form($options) {
  $form = array();
  $form['default_registration_state'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => registration_get_states_options(),
    '#default_value' => !empty($options['default_registration_state']) ? $options['default_registration_state'] : '',
  );
  return $form;
}

/**
 * Per-action form for executing a bulk operation.
 *
 * Simply adds a form to select what state to set when performing a VBO
 * set_state_action.
 */
function registration_views_set_state_action_form($settings) {
  $form = array();
  $form['registration_state'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#options' => registration_get_states_options(),
    '#default_value' => !empty($settings['settings']['default_registration_state']) ? $settings['settings']['default_registration_state'] : '',
  );
  return $form;
}

/**
 * Submit handler for registration_views_set_state_action_form().
 */
function registration_views_set_state_action_submit($form, $form_state) {
  $return = array();
  $return['new_state'] = $form_state['values']['registration_state'];
  return $return;
}

/**
 * The actual action to set a Registration's State.
 */
function registration_views_set_state_action(&$node, $context) {
  $context['registration']->state = $context['new_state'];
  registration_save($context['registration']);
}
class RegistrationViewsController extends EntityDefaultViewsController {
  public function views_data() {
    $data = parent::views_data();
    $data['registration']['view_registration'] = array(
      'field' => array(
        'title' => t('View link'),
        'help' => t('Provide a link to view a registration.'),
        'handler' => 'registration_handler_field_registration_link',
      ),
    );
    $data['registration']['edit_registration'] = array(
      'field' => array(
        'title' => t('Edit link'),
        'help' => t('Provide a link to edit a registration.'),
        'handler' => 'registration_handler_field_registration_link_edit',
      ),
    );
    $data['registration']['delete_registration'] = array(
      'field' => array(
        'title' => t('Delete link'),
        'help' => t('Provide a link to delete a registration.'),
        'handler' => 'registration_handler_field_registration_link_delete',
      ),
    );
    $data['registration']['state']['relationship']['base field'] = 'name';

    // Entity Metadata does not handle registration schema well.
    $data['registration']['user_uid']['title'] = t('User');
    $data['registration']['user_uid']['help'] = t('The user registered for this event.');
    $data['registration']['user_uid']['relationship'] = array(
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'base field' => 'uid',
      'label' => t('User'),
    );
    $data['registration']['type']['relationship'] = array(
      'handler' => 'views_handler_relationship',
      'base' => 'registration_type',
      'base field' => 'name',
      'label' => t('Registration type'),
    );
    return $data;
  }

}

/**
 * Implements hook_views_data_alter().
 *
 * Create relationships between registrations, and their related entities.
 * Provide additional fields for entities with registrations.
 */
function registration_views_views_data_alter(&$data) {

  // Add a state form field option for registrations.
  $data['registration']['state_edit'] = array(
    'field' => array(
      'title' => t('State form field'),
      'help' => t('Edit the registration state'),
      'handler' => 'RegistrationViewsHandlerStateEdit',
    ),
  );
  $registration_instances = registration_get_registration_instances();
  $entity_info = entity_get_info();
  foreach ($registration_instances as $instance) {
    $entity_type = $instance['entity_type'];
    $info = $entity_info[$entity_type];

    // Entity to Registration.
    $data[$info['base table']]['registration_rel'] = array(
      'group' => t('Registration'),
      'title' => t('@entity to Registration', array(
        '@entity' => ucfirst($info['label']),
      )),
      'help' => t('The Registration associated with the @entity entity.', array(
        '@entity' => ucfirst($info['label']),
      )),
      'relationship' => array(
        'entity' => $entity_type,
        'handler' => 'registration_handler_relationship',
        'label' => t('@entity being the Registration', array(
          '@entity' => $entity_type,
        )),
        'base' => 'registration',
        'base field' => 'entity_id',
        'relationship field' => $info['entity keys']['id'],
      ),
    );

    // Relate entities to registration settings.
    $data[$info['base table']]['registration_settings_rel'] = array(
      'group' => t('Registration Settings'),
      'title' => t('@entity to Registration Settings', array(
        '@entity' => ucfirst($info['label']),
      )),
      'help' => t('The Registration settings associated with the @entity entity.', array(
        '@entity' => ucfirst($info['label']),
      )),
      'relationship' => array(
        'entity' => $entity_type,
        'handler' => 'registration_handler_relationship',
        'label' => t('@entity being the Registration', array(
          '@entity' => $entity_type,
        )),
        'base' => 'registration_entity',
        'base field' => 'entity_id',
        'relationship field' => $info['entity keys']['id'],
      ),
    );

    // Host entity: registration settings link.
    $data[$info['base table']]['registration_settings'] = array(
      'real field' => $info['entity keys']['id'],
      'group' => t('Registration Settings'),
      'title' => t('Registration Settings Link'),
      'help' => t('Provide a link to edit registration settings for an entity.'),
      'field' => array(
        'handler' => 'registration_handler_field_entity_settings_link',
      ),
    );

    // Host entity: registration status.
    $data[$info['base table']]['registration_status'] = array(
      'real field' => $info['entity keys']['id'],
      'group' => t('Registration Settings'),
      'title' => t('Registration Status'),
      'help' => t('Show registration status for an entity.'),
      'field' => array(
        'handler' => 'registration_handler_field_entity_registration_status',
      ),
    );

    // Host entity: used slots.
    $data[$info['base table']]['capacity_used'] = array(
      'real field' => $info['entity keys']['id'],
      'group' => t('Registration Settings'),
      'title' => t('Spaces used'),
      'help' => t('Current number of spaces used for this entity.'),
      'field' => array(
        'handler' => 'registration_handler_field_entity_capacity_used',
      ),
    );

    // Registration to entity.
    $data['registration']['registration_related_' . $entity_type] = array(
      'group' => t('Registration'),
      'title' => t('Registration to @entity', array(
        '@entity' => ucfirst($info['label']),
      )),
      'help' => t('The @entity entity that is associated with the Registration.', array(
        '@entity' => $info['label'],
      )),
      'relationship' => array(
        'entity' => $entity_type,
        'table' => 'registration',
        'handler' => 'registration_handler_relationship',
        'label' => t('@entity from Registration', array(
          '@entity' => $entity_type,
        )),
        'base' => $info['base table'],
        'base field' => $info['entity keys']['id'],
        'relationship field' => 'entity_id',
      ),
    );
  }
}

/**
 * A Views' field handler for editing the registration state.
 */
class RegistrationViewsHandlerStateEdit extends views_handler_field {
  function construct() {
    parent::construct();
    $this->additional_fields['registration_id'] = 'registration_id';
    $this->additional_fields['state'] = 'state';
  }
  function query() {
    $this
      ->ensure_my_table();
    $this
      ->add_additional_fields();
  }

  /**
   * Render the field contents.
   */
  function render($values) {

    // Render a Views form item placeholder.
    return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
  }

  /**
   * Add to and alter the form.
   */
  function views_form(&$form, &$form_state) {

    // Create a container for our replacements
    $form[$this->options['id']] = array(
      '#type' => 'container',
      '#tree' => TRUE,
    );

    // Iterate over the result and add our replacement fields to the form.
    foreach ($this->view->result as $row_index => $row) {

      // Add a text field to the form.  This array convention
      // corresponds to the placeholder HTML comment syntax.
      $form[$this->options['id']][$row_index] = array(
        '#type' => 'select',
        '#default_value' => $row->{$this->aliases['state']},
        '#options' => registration_get_states_options(),
        '#required' => TRUE,
      );
    }
  }

  /**
   * Form submit method.
   */
  function views_form_submit($form, &$form_state) {

    // Determine which nodes we need to update.
    $updates = array();
    foreach ($this->view->result as $row_index => $row) {
      $value = $form_state['values'][$this->options['id']][$row_index];
      if ($row->{$this->aliases['state']} != $value) {
        $updates[$row->{$this->aliases['registration_id']}] = $value;
      }
    }
    $registrations = registration_load_multiple(array_keys($updates));
    foreach ($registrations as $registration_id => $registration) {
      $registration->state = $updates[$registration_id];
      registration_save($registration);
    }
    drupal_set_message(t('Updated @num registration states.', array(
      '@num' => count($updates),
    )));
  }

}

Functions

Namesort descending Description
registration_views_action_info Implements hook_action_info().
registration_views_entity_info_alter Implements hook_entity_info_alter().
registration_views_set_state_action The actual action to set a Registration's State.
registration_views_set_state_action_form Per-action form for executing a bulk operation.
registration_views_set_state_action_submit Submit handler for registration_views_set_state_action_form().
registration_views_set_state_action_views_bulk_operations_form Implements hook_views_bulk_operation_form().
registration_views_views_api Implements hook_views_api().
registration_views_views_data Implements hook_views_data().
registration_views_views_data_alter Implements hook_views_data_alter().

Classes

Namesort descending Description
RegistrationViewsController
RegistrationViewsHandlerStateEdit A Views' field handler for editing the registration state.