View source
<?php
function registration_views_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'registration_views') . '/includes',
);
}
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;
}
function registration_views_entity_info_alter(&$entity_info) {
$entity_info['registration']['views controller class'] = 'RegistrationViewsController';
}
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',
),
),
);
}
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;
}
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;
}
function registration_views_set_state_action_submit($form, $form_state) {
$return = array();
$return['new_state'] = $form_state['values']['registration_state'];
return $return;
}
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';
$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;
}
}
function registration_views_views_data_alter(&$data) {
$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];
$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'],
),
);
$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'],
),
);
$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',
),
);
$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',
),
);
$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',
),
);
$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',
),
);
}
}
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();
}
function render($values) {
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
}
function views_form(&$form, &$form_state) {
$form[$this->options['id']] = array(
'#type' => 'container',
'#tree' => TRUE,
);
foreach ($this->view->result as $row_index => $row) {
$form[$this->options['id']][$row_index] = array(
'#type' => 'select',
'#default_value' => $row->{$this->aliases['state']},
'#options' => registration_get_states_options(),
'#required' => TRUE,
);
}
}
function views_form_submit($form, &$form_state) {
$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),
)));
}
}