View source
<?php
use Roomify\Bat\Event\Event;
use Roomify\Bat\Calendar\Calendar;
use Roomify\Bat\Store\DrupalDBStore;
use Roomify\Bat\Unit\Unit;
if (module_exists('rules')) {
include_once 'bat_event.rules.inc';
}
function bat_event_menu() {
$items = array();
$items['admin/bat/config/events/event/%/edit'] = array(
'title' => 'Event States',
'description' => 'Edit event state.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'bat_event_states_edit_event_form',
5,
),
'access arguments' => array(
'configure bat settings',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/bat/config/events/event/%/delete'] = array(
'title' => 'Event States',
'description' => 'Delete event state.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'bat_event_states_delete_event_form',
5,
),
'access arguments' => array(
'configure bat settings',
),
'type' => MENU_NORMAL_ITEM,
);
$items['bat_event/state_event/autocomplete/%/%/%'] = array(
'page callback' => 'bat_event_reference_autocomplete',
'page arguments' => array(
3,
4,
5,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['admin/bat/events/event-types/manage/%bat_event_type/states'] = array(
'title' => 'Manage states',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'bat_event_states_form',
5,
),
'access arguments' => array(
'configure bat settings',
),
'type' => MENU_LOCAL_TASK,
'weight' => 0,
);
return $items;
}
function bat_event_entity_info() {
$return['bat_event'] = array(
'label' => t('Events'),
'entity class' => 'BatEvent',
'controller class' => 'BatEventController',
'base table' => 'bat_events',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'event_id',
'bundle' => 'type',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'creation callback' => 'bat_event_create',
'access callback' => 'bat_event_access',
'access arguments' => array(
'user key' => 'uid',
'access tag' => 'bat_event_access',
),
'permission labels' => array(
'singular' => t('event'),
'plural' => t('events'),
),
'module' => 'bat_event',
'admin ui' => array(
'path' => 'admin/bat/events',
'file' => 'bat_event.admin.inc',
'controller class' => 'BatEventUIController',
'menu wildcard' => '%bat_event',
),
'metadata controller class' => 'BatEventMetadataController',
);
$return['bat_event_type'] = array(
'label' => t('Event Type'),
'entity class' => 'BatEventType',
'controller class' => 'BatEventTypeController',
'base table' => 'bat_event_type',
'fieldable' => FALSE,
'bundle of' => 'bat_event',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'bat_event_type_access',
'module' => 'bat_event',
'admin ui' => array(
'path' => 'admin/bat/events/event-types',
'file' => 'bat_event_type.admin.inc',
'controller class' => 'BatEventTypeUIController',
),
);
return $return;
}
function bat_event_entity_info_alter(&$entity_info) {
foreach (bat_event_get_types() as $type => $info) {
$entity_info['bat_event']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/bat/events/event-types/manage/%bat_event_type',
'real path' => 'admin/bat/events/event-types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array(
'administer bat_event_type entities',
),
),
);
}
}
function bat_event_type_add_event_state_reference($entity) {
field_info_cache_clear();
if (field_read_field('event_state_reference') === FALSE) {
$field = array(
'field_name' => 'event_state_reference',
'type' => 'bat_event_state_reference',
'cardinality' => 1,
'locked' => 1,
'settings' => array(),
);
field_create_field($field);
}
field_cache_clear();
if (field_read_instance('bat_event', 'event_state_reference', $entity->type) === FALSE) {
$instance = array(
'field_name' => 'event_state_reference',
'entity_type' => 'bat_event',
'label' => 'State',
'bundle' => $entity->type,
'required' => FALSE,
'widget' => array(
'type' => 'bat_event_reference_autocomplete',
),
'settings' => array(
'event_type' => $entity->type,
),
);
field_create_instance($instance);
}
}
function bat_event_type_add_target_entity_field($entity) {
field_info_cache_clear();
$entity_info = entity_get_info($entity->target_entity_type);
$field_name = 'event_' . $entity->target_entity_type . '_reference';
if (field_read_field($field_name) === FALSE) {
$field = array(
'field_name' => $field_name,
'type' => 'entityreference',
'cardinality' => 1,
'locked' => 1,
'settings' => array(
'target_type' => $entity->target_entity_type,
),
);
field_create_field($field);
}
field_cache_clear();
if (field_read_instance('bat_event', $field_name, $entity->type) === FALSE) {
$instance = array(
'field_name' => $field_name,
'entity_type' => 'bat_event',
'label' => $entity_info['label'],
'bundle' => $entity->type,
'required' => FALSE,
'widget' => array(
'type' => 'entityreference_autocomplete',
),
);
field_create_instance($instance);
}
}
function bat_event_permission() {
$permissions = array(
'administer bat_event_type entities' => array(
'title' => t('Administer event types'),
'description' => t('Allows users to add event types and configure their fields.'),
'restrict access' => TRUE,
),
);
$entity_info = entity_get_info('bat_event');
$labels = $entity_info['permission labels'];
if (!empty($entity_info['entity keys']['bundle'])) {
foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
$permissions['view calendar data for any ' . $bundle_name . ' event'] = array(
'title' => t('View calendar data for any %bundle @entity_type', array(
'@entity_type' => $labels['plural'],
'%bundle' => $bundle_info['label'],
)),
);
}
}
return $permissions + bat_entity_access_permissions('bat_event');
}
function bat_event_form_views_exposed_form_alter(&$form, &$form_state) {
if ($form['#id'] == 'views-exposed-form-events-page-1') {
$form['start_date']['#type'] = 'date_popup';
$form['start_date']['#date_format'] = variable_get('bat_daily_date_format', 'Y-m-d');
$form['start_date']['#date_label_position'] = 'before';
$form['end_date']['#type'] = 'date_popup';
$form['end_date']['#date_format'] = variable_get('bat_daily_date_format', 'Y-m-d');
$form['end_date']['#date_label_position'] = 'before';
$form['#attached']['css'][] = array(
'data' => '.views-exposed-form .views-exposed-widget { height: 70px; }',
'type' => 'inline',
);
}
}
function bat_event_get_states($event_type = NULL) {
$event_states =& drupal_static(__FUNCTION__, array());
if (empty($event_states)) {
$query = db_select('bat_event_state', 'n')
->fields('n', array());
$states = $query
->execute()
->fetchAll();
foreach ($states as $event_state) {
$event_states[$event_state->id] = array(
'id' => $event_state->id,
'machine_name' => $event_state->machine_name,
'event_type' => $event_state->event_type,
'label' => $event_state->label,
'color' => $event_state->color,
'calendar_label' => $event_state->calendar_label,
'locked' => $event_state->locked,
'blocking' => $event_state->blocking,
'default_state' => $event_state->default_state,
);
}
}
if ($event_type !== NULL) {
$states = array();
foreach ($event_states as $id => $state) {
if ($state['event_type'] == $event_type) {
$states[$id] = $state;
}
}
return $states;
}
else {
return $event_states;
}
}
function bat_event_load_state($state_id) {
$event_states = bat_event_get_states();
if (isset($event_states[$state_id])) {
return $event_states[$state_id];
}
return FALSE;
}
function bat_event_load_state_by_machine_name($state_machine_name) {
$event_states =& drupal_static(__FUNCTION__, array());
if (!isset($event_states[$state_machine_name])) {
$state_id = db_select('bat_event_state', 'n')
->fields('n', array(
'id',
))
->condition('machine_name', $state_machine_name)
->execute()
->fetchField();
$event_states[$state_machine_name] = bat_event_load_state($state_id);
}
return $event_states[$state_machine_name];
}
function bat_event_states_get_default($event_type) {
$state_id = db_select('bat_event_state', 'n')
->fields('n', array(
'id',
))
->condition('event_type', $event_type)
->condition('default_state', 1)
->execute()
->fetchField();
return bat_event_load_state($state_id);
}
function bat_event_states_form($form, &$form_state, $event_type) {
if ($event_type->fixed_event_states) {
$event_states = bat_event_get_states($event_type->type);
$header = array(
t('ID'),
t('Machine name'),
t('Label'),
t('Blocking'),
t('Operations'),
);
$default_state = 0;
$rows = array();
foreach ($event_states as $event_state) {
$operations = array();
$operations[] = array(
'title' => t('Edit'),
'href' => 'admin/bat/config/events/event/' . $event_state['id'] . '/edit',
);
if (!$event_state['locked']) {
$operations[] = array(
'title' => t('Delete'),
'href' => 'admin/bat/config/events/event/' . $event_state['id'] . '/delete',
);
}
$rows[] = array(
$event_state['id'],
$event_state['machine_name'],
$event_state['label'],
$event_state['blocking'],
theme('links', array(
'links' => $operations,
'attributes' => array(
'class' => array(
'links',
'inline',
),
),
)),
);
if ($event_state['default_state']) {
$default_state = count($rows) - 1;
}
}
if (!empty($rows)) {
$form['states'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => FALSE,
'#default_value' => $default_state,
'#prefix' => '<div id="event-state-wrapper">',
'#suffix' => '</div>',
);
$form['set_default'] = array(
'#type' => 'submit',
'#value' => t('Set default state'),
'#limit_validation_errors' => array(
array(
'states',
),
),
'#submit' => array(),
'#ajax' => array(
'callback' => 'bat_event_states_form_set_default',
'wrapper' => 'event-state-wrapper',
),
);
}
$form['new_state'] = array(
'#type' => 'fieldset',
'#title' => 'Create new state',
);
$form['event_type'] = array(
'#type' => 'hidden',
'#value' => $event_type->type,
);
$form['new_state']['label'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#required' => TRUE,
);
$form['new_state']['machine_name'] = array(
'#type' => 'machine_name',
'#maxlength' => 32,
'#machine_name' => array(
'exists' => 'bat_event_load_state_by_machine_name',
'source' => array(
'new_state',
'label',
),
),
);
$form['new_state']['color'] = array(
'#type' => 'textfield',
'#title' => t('Color'),
'#size' => 12,
'#maxlength' => 7,
'#element_validate' => array(
'bat_event_validate_hex_color',
),
'#dependency' => array(
'edit-row-options-colors-legend' => array(
'type',
),
),
'#prefix' => '<div class="bat-colorpicker-wrapper form-wrapper">',
'#suffix' => '<div class="bat-colorpicker"></div></div>',
'#attributes' => array(
'class' => array(
'bat-edit-colorpicker',
),
),
'#attached' => array(
'library' => array(
array(
'system',
'farbtastic',
),
),
'js' => array(
drupal_get_path('module', 'bat_event') . '/js/bat_color.js',
),
),
'#required' => TRUE,
);
$form['new_state']['calendar_label'] = array(
'#type' => 'textfield',
'#title' => t('Calendar label'),
'#size' => 10,
'#maxlength' => 50,
'#required' => TRUE,
);
$form['new_state']['blocking'] = array(
'#type' => 'checkbox',
'#title' => t('Blocking'),
);
$form['new_state']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add state'),
);
}
else {
$form['empty'] = array(
'#markup' => t('This event type cannot define fixed states!'),
);
}
return $form;
}
function bat_event_states_form_set_default($form, &$form_state) {
$state = $form_state['values']['states'];
db_update('bat_event_state')
->fields(array(
'default_state' => 0,
))
->condition('event_type', $form['event_type']['#value'])
->execute();
db_update('bat_event_state')
->fields(array(
'default_state' => 1,
))
->condition('id', $form['states']['#options'][$state][0])
->execute();
drupal_set_message(t('Default state set!'));
return $form['states'];
}
function bat_event_states_form_submit($form, &$form_state) {
$event_type = $form_state['values']['event_type'];
$event_state = array(
'label' => $form_state['values']['label'],
'color' => $form_state['values']['color'],
'calendar_label' => $form_state['values']['calendar_label'],
'blocking' => $form_state['values']['blocking'],
'machine_name' => $form_state['values']['machine_name'],
);
bat_event_save_state($event_state, $event_type);
}
function bat_event_save_state($event_state, $event_type) {
if (!empty($event_state['id'])) {
db_merge('bat_event_state')
->fields(array(
'machine_name' => $event_state['machine_name'],
'label' => $event_state['label'],
'color' => $event_state['color'],
'calendar_label' => $event_state['calendar_label'],
'blocking' => $event_state['blocking'],
))
->condition('id', $event_state['id'])
->execute();
}
elseif (!empty($event_state['machine_name']) && bat_event_load_state_by_machine_name($event_state['machine_name'])) {
db_merge('bat_event_state')
->fields(array(
'label' => $event_state['label'],
'color' => $event_state['color'],
'calendar_label' => $event_state['calendar_label'],
'blocking' => $event_state['blocking'],
))
->condition('machine_name', $event_state['machine_name'])
->execute();
}
else {
$max_id = db_query("SELECT MAX(id) as max_id FROM {bat_event_state}")
->fetchCol();
$event_state['id'] = reset($max_id) + 1;
$event_state['event_type'] = $event_type;
drupal_write_record('bat_event_state', $event_state);
}
drupal_static_reset('bat_event_get_states');
drupal_static_reset('bat_event_load_state_by_machine_name');
}
function bat_event_states_edit_event_form($form, &$form_state, $event_id) {
$state = db_select('bat_event_state', 'n')
->fields('n', array())
->condition('id', $event_id)
->execute()
->fetchAssoc();
$form['state_id'] = array(
'#type' => 'hidden',
'#value' => $event_id,
);
$form['event_type'] = array(
'#type' => 'hidden',
'#value' => $state['event_type'],
);
$form['label'] = array(
'#type' => 'textfield',
'#title' => t('State Label'),
'#default_value' => $state['label'],
'#required' => TRUE,
);
$form['color'] = array(
'#type' => 'textfield',
'#title' => t('Color'),
'#size' => 12,
'#maxlength' => 7,
'#default_value' => $state['color'],
'#element_validate' => array(
'bat_event_validate_hex_color',
),
'#dependency' => array(
'edit-row-options-colors-legend' => array(
'type',
),
),
'#prefix' => '<div class="bat-colorpicker-wrapper form-wrapper">',
'#suffix' => '<div class="bat-colorpicker"></div></div>',
'#attributes' => array(
'class' => array(
'bat-edit-colorpicker',
),
),
'#attached' => array(
'library' => array(
array(
'system',
'farbtastic',
),
),
'js' => array(
drupal_get_path('module', 'bat_event') . '/js/bat_color.js',
),
),
'#required' => TRUE,
);
$form['new_state']['calendar_label'] = array(
'#type' => 'textfield',
'#title' => t('Calendar label'),
'#size' => 10,
'#maxlength' => 50,
'#default_value' => $state['calendar_label'],
'#required' => TRUE,
);
$form['new_state']['blocking'] = array(
'#type' => 'checkbox',
'#title' => t('Blocking'),
'#default_value' => $state['blocking'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update State'),
);
return $form;
}
function bat_event_states_edit_event_form_submit($form, &$form_state) {
db_update('bat_event_state')
->fields(array(
'label' => $form_state['values']['label'],
'color' => $form_state['values']['color'],
'calendar_label' => $form_state['values']['calendar_label'],
'blocking' => $form_state['values']['blocking'],
))
->condition('id', $form_state['values']['state_id'])
->execute();
$form_state['redirect'] = 'admin/bat/events/event-types/manage/' . $form_state['values']['event_type'] . '/states';
}
function bat_event_states_delete_event_form($form, &$form_state, $event_id) {
$state_event = bat_event_load_state($event_id);
if ($state_event === FALSE || $state_event['locked']) {
return $form;
}
$form_state['state_event'] = $event_id;
$form_state['event_type'] = $state_event['event_type'];
$form['#submit'][] = 'bat_event_states_delete_event_form_submit';
$form = confirm_form($form, t('Are you sure you want to delete Event State %state_name ?', array(
'%state_name' => $state_event['label'],
)), 'admin/bat/config/events', '<p>' . t('This action cannot be undone.') . '</p>', t('Delete'), t('Cancel'), 'confirm');
return $form;
}
function bat_event_states_delete_event_form_submit($form, &$form_state) {
db_delete('bat_event_state')
->condition('id', $form_state['state_event'])
->execute();
$form_state['redirect'] = 'admin/bat/events/event-types/manage/' . $form_state['event_type'] . '/states';
}
function bat_event_access($op, $event = NULL, $account = NULL) {
return bat_entity_access($op, $event, $account, 'bat_event');
}
function bat_event_add_access() {
if (user_access('administer bat_event_type entities')) {
return TRUE;
}
$types = bat_event_get_types();
foreach ($types as $type) {
if (bat_event_access('create', bat_event_create(array(
'type' => $type->type,
'uid' => 0,
)))) {
return TRUE;
}
}
return FALSE;
}
function bat_event_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer bat_event_type entities', $account);
}
function bat_event_query_bat_event_access_alter(QueryAlterableInterface $query) {
foreach ($query
->getTables() as $table) {
if ($table['table'] === 'bat_events') {
bat_entity_access_query_alter($query, 'bat_event', $table['alias']);
break;
}
}
}
function bat_event_types_ids() {
$event_types = array();
$types = bat_event_get_types();
foreach ($types as $type) {
$event_types[$type->id] = $type->label;
}
return $event_types;
}
function bat_event_get_types($type_name = NULL, $reset = FALSE) {
$types = entity_load_multiple_by_name('bat_event_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
function bat_event_type_load($type, $reset = FALSE) {
return bat_event_get_types($type, $reset);
}
function bat_event_load($event_id, $reset = FALSE) {
$events = bat_event_load_multiple(array(
$event_id,
), array(), $reset);
return reset($events);
}
function bat_event_load_multiple($event_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('bat_event', $event_ids, $conditions, $reset);
}
function bat_event_delete(BatEvent $event) {
bat_event_delete_multiple(array(
$event->event_id,
));
}
function bat_event_delete_multiple(array $event_ids) {
entity_get_controller('bat_event')
->delete($event_ids);
}
function bat_event_create($values = array()) {
return entity_get_controller('bat_event')
->create($values);
}
function bat_event_save(BatEvent $event) {
return $event
->save();
}
function bat_event_type_create($values = array()) {
return entity_get_controller('bat_event_type')
->create($values);
}
function bat_event_type_save(BatEventType $type) {
$type
->save();
}
function bat_event_type_delete(BatEventType $type) {
$type
->delete();
}
function bat_event_uri(BatEvent $event) {
return array(
'path' => 'event/' . $event->event_id,
);
}
function bat_event_page_title(BatEvent $event) {
return '';
}
function bat_event_ids($conditions = array()) {
$events = array();
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'bat_event')
->execute();
if (count($result) > 0) {
$entities = bat_event_load_multiple(array_keys($result['bat_event']), $conditions);
foreach ($entities as $event) {
$wrapper = entity_metadata_wrapper('bat_event', $event);
$events[$wrapper->event_id
->value()] = $wrapper->name
->value();
}
}
return $events;
}
function bat_event_page_view($event, $view_mode = 'full') {
$controller = entity_get_controller('bat_event');
$content = $controller
->view(array(
$event->event_id => $event,
));
return $content;
}
function bat_event_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'bat_event') . '/views',
);
}
function bat_event_theme() {
return array(
'bat_event_add_list' => array(
'variables' => array(
'content' => array(),
),
'file' => 'bat_event.admin.inc',
),
'bat_event' => array(
'render element' => 'elements',
'template' => 'bat_event',
),
);
}
function bat_event_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/bat/events') {
$item = menu_get_item('admin/bat/events/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function bat_event_get_unit_bundles($bat_event_type_name = '') {
$matches = array();
if ($bat_event_type_name) {
$result = db_select('bat_event_type')
->fields('bat_event_type', array(
'type',
))
->condition('type', db_like($bat_event_type_name) . '%', 'LIKE')
->range(0, 10)
->execute();
foreach ($result as $unit_bundle) {
$matches[$unit_bundle->type] = check_plain($unit_bundle->type);
}
}
drupal_json_output($matches);
}
class BatEvent extends Entity {
public $start_date_object;
public $end_date_object;
public function __construct($values = array()) {
parent::__construct($values, 'bat_event');
if (isset($this->start_date)) {
$this->start_date_object = new DateTime($this->start_date);
}
if (isset($this->end_date)) {
$this->end_date_object = new DateTime($this->end_date);
}
}
public function delete_event() {
entity_get_controller($this->entityType)
->delete_event($this);
}
protected function defaultLabel() {
$event_type = bat_event_type_load($this->type);
if (isset($event_type->default_event_label_field_name) && $event_type->default_event_label_field_name != '') {
$event_wrapper = entity_metadata_wrapper('bat_event', $this);
$value = $event_wrapper->{$event_type->default_event_label_field_name}
->value(array(
'sanitize' => TRUE,
));
if (is_object($value)) {
$field_info = field_info_field($event_type->default_event_label_field_name);
if ($field_info['type'] == 'entityreference') {
return entity_label($field_info['settings']['target_type'], $value);
}
}
elseif ($value) {
return $value;
}
}
if ($state_info = bat_event_load_state($this
->getEventValue())) {
return $state_info['label'];
}
return '';
}
protected function defaultUri() {
return array(
'path' => 'event/' . $this->event_id,
);
}
public function getEventValue() {
if ($field = $this
->getEventValueField()) {
$field_info = field_info_field($field);
$values = field_get_items('bat_event', $this, $field);
if (!empty($values)) {
if ($field_info['type'] == 'bat_event_state_reference') {
return $values[0]['state_id'];
}
elseif ($field_info['type'] == 'commerce_price') {
return $values[0]['amount'];
}
elseif ($field_info['type'] == 'text' || $field_info['type'] == 'number_integer' || $field_info['type'] == 'number_decimal') {
return $values[0]['value'];
}
}
else {
return FALSE;
}
}
}
public function getEventValueFormatter() {
if ($field = $this
->getEventValueDefaultField()) {
$field_info_instance = field_info_instance('bat_event_type', $field, $this->type);
if (isset($field_info_instance['display']['default']['type'])) {
return $field_info_instance['display']['default']['type'];
}
}
return FALSE;
}
public function getEventValueField() {
$type_bundle = bat_event_type_load($this->type);
if (isset($type_bundle->default_event_value_field_ids[$this->type])) {
return $type_bundle->default_event_value_field_ids[$this->type];
}
if ($type_bundle->fixed_event_states == 1) {
return 'event_state_reference';
}
return FALSE;
}
}
class BatEventType extends Entity {
public $type;
public $label;
public function __construct($values = array()) {
parent::__construct($values, 'bat_event_type');
}
}
class BatEventMetadataController extends EntityDefaultMetadataController {
public function entityPropertyInfo() {
$info = parent::entityPropertyInfo();
$properties = array(
'event_id',
'type',
'language',
'created',
'changed',
'uid',
);
foreach ($properties as $property) {
if (isset($info['bat_event']['properties'][$property])) {
$info['bat_event']['properties'][$property]['getter callback'] = 'entity_property_verbatim_get';
$info['bat_event']['properties'][$property]['setter callback'] = 'entity_property_verbatim_set';
}
}
$info['bat_event']['properties']['start_date'] = array(
'label' => t('Start Date'),
'description' => t('The start date for the event.'),
'type' => 'date',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'bat_event_date_property_verbatim_set',
'schema field' => 'start_date',
);
$info['bat_event']['properties']['end_date'] = array(
'label' => t('End Date'),
'description' => t('The end date for the event.'),
'type' => 'date',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'bat_event_date_property_verbatim_set',
'schema field' => 'end_date',
);
return $info;
}
}
function bat_event_date_property_verbatim_set(&$data, $name, $value, $langcode, $type, $info) {
$date = new DateTime();
$date
->setTimestamp($value);
$name = isset($info['schema field']) ? $info['schema field'] : $name;
if (is_array($data) || is_object($data) && $data instanceof ArrayAccess) {
$data[$name] = $date
->format('Y-m-d H:i:s');
}
elseif (is_object($data)) {
$data->{$name} = $date
->format('Y-m-d H:i:s');
}
}
class BatEventController extends EntityAPIController {
public function create(array $values = array()) {
$event_type = bat_event_type_load($values['type'], TRUE);
$values += array(
'event_id' => '',
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'data' => array(),
);
$event = parent::create($values);
return $event;
}
public function save($entity, DatabaseTransaction $transaction = NULL) {
$entity->start_date_object = new DateTime($entity->start_date);
$entity->end_date_object = new DateTime($entity->end_date);
$event_type = bat_event_type_load($entity->type);
$target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
if (!isset($entity->is_new)) {
$entity->original = entity_load_unchanged($this->entityType, $entity->{$this->idKey});
if ($entity->original->start_date != '' && $entity->original->end_date != '' && field_get_items('bat_event', $entity->original, $target_field_name) !== FALSE) {
$event_target_entity_reference = field_get_items('bat_event', $entity->original, $target_field_name);
$target_entity_id = $event_target_entity_reference[0]['target_id'];
if ($target_entity = entity_load_single($event_type->target_entity_type, $target_entity_id)) {
$unit = new Unit($target_entity_id, $target_entity
->getEventDefaultValue($event_type->type));
$this
->batStoreSave($unit, new \DateTime($entity->original->start_date), new \DateTime($entity->original->end_date), $event_type->type, $event_type->event_granularity, $unit
->getDefaultValue(), $entity->event_id, TRUE);
}
}
}
parent::save($entity);
if (field_get_items('bat_event', $entity, $target_field_name) !== FALSE) {
if (isset($event_type->default_event_value_field_ids[$entity->type])) {
$field = $event_type->default_event_value_field_ids[$entity->type];
$field_info = field_info_field($field);
$values = field_get_items('bat_event', $entity, $field);
if (!empty($values)) {
if ($field_info['type'] == 'bat_event_state_reference') {
$event_value = $values[0]['state_id'];
}
elseif ($field_info['type'] == 'commerce_price') {
$event_value = $values[0]['amount'];
}
elseif ($field_info['type'] == 'text' || $field_info['type'] == 'number_integer' || $field_info['type'] == 'number_decimal') {
$event_value = $values[0]['value'];
}
}
}
else {
$event_state_reference = field_get_items('bat_event', $entity, 'event_state_reference');
$event_value = $event_state_reference[0]['state_id'];
}
$event_target_entity_reference = field_get_items('bat_event', $entity, $target_field_name);
$target_entity_id = $event_target_entity_reference[0]['target_id'];
if ($target_entity = entity_load_single($event_type->target_entity_type, $target_entity_id)) {
$unit = new Unit($target_entity_id, $target_entity
->getEventDefaultValue($event_type->type));
$this
->batStoreSave($unit, new \DateTime($entity->start_date), new \DateTime($entity->end_date), $event_type->type, $event_type->event_granularity, $event_value, $entity->event_id);
}
}
}
public function batStoreSave(Unit $unit, \DateTime $start_date, \DateTime $end_date, $event_type, $granularity, $event_state, $event_id, $remove = FALSE) {
$prefix = bat_get_db_prefix();
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE, $prefix);
$event_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_EVENT, $prefix);
$units = array(
$unit,
);
$state_calendar = new Calendar($units, $state_store);
$event_calendar = new Calendar($units, $event_store);
$state_event = new Event($start_date, $end_date, $unit, $event_state);
if (!$remove) {
$event_id_event = new Event($start_date, $end_date, $unit, $event_id);
}
else {
$event_id_event = new Event($start_date, $end_date, $unit, 0);
}
$state_calendar
->addEvents(array(
$state_event,
), $granularity);
$event_calendar
->addEvents(array(
$event_id_event,
), $granularity);
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
foreach ($ids as $id) {
$event = bat_event_load($id);
$this
->deleteEvent($event);
}
parent::delete($ids);
}
public function deleteEvent(BatEvent $event) {
$event_type = bat_event_type_load($event->type);
$target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
if (field_get_items('bat_event', $event, $target_field_name) !== FALSE && isset($event->start_date) && isset($event->end_date)) {
$event_target_entity_reference = field_get_items('bat_event', $event, $target_field_name);
$target_entity_id = $event_target_entity_reference[0]['target_id'];
if ($target_entity = entity_load_single($event_type->target_entity_type, $target_entity_id)) {
$unit = new Unit($target_entity_id, $target_entity
->getEventDefaultValue($event->type));
$this
->batStoreSave($unit, clone $event->start_date_object, clone $event->end_date_object, $event->type, $event_type->event_granularity, $unit
->getDefaultValue(), $event->event_id, TRUE);
}
}
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
return $content;
}
}
class BatEventTypeController extends EntityAPIControllerExportable {
public function create(array $values = array()) {
$values += array(
'id' => '',
'is_new' => TRUE,
'data' => '',
);
$event_type = parent::create($values);
return $event_type;
}
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (empty($entity->{$this->idKey})) {
bat_event_create_event_type_schema($entity->type);
bat_event_type_add_target_entity_field($entity);
if (isset($entity->fixed_event_states)) {
if ($entity->fixed_event_states) {
bat_event_type_add_event_state_reference($entity);
}
}
}
return parent::save($entity);
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
parent::delete($ids);
foreach ($ids as $id) {
bat_event_delete_event_type_schema($id);
bat_event_delete_states_by_type($id);
}
}
public function export($entity, $prefix = '') {
$vars = get_object_vars($entity);
unset($vars[$this->statusKey], $vars[$this->moduleKey], $vars['is_new']);
if ($this->nameKey != $this->idKey) {
unset($vars[$this->idKey]);
}
if ($entity->fixed_event_states) {
$event_states = array();
foreach (bat_event_get_states($entity->type) as $state) {
$event_states[] = array(
'machine_name' => $state['machine_name'],
'label' => $state['label'],
'color' => $state['color'],
'calendar_label' => $state['calendar_label'],
'locked' => $state['locked'],
'blocking' => $state['blocking'],
'default_state' => $state['default_state'],
);
}
$vars['event_states'] = $event_states;
}
return entity_var_json_export($vars, $prefix);
}
public function import($export) {
$vars = drupal_json_decode($export);
if (is_array($vars)) {
if (isset($vars['event_states'])) {
$event_type = $vars['type'];
foreach ($vars['event_states'] as $event_state) {
bat_event_save_state($event_state, $event_type);
}
}
return $this
->create($vars);
}
return FALSE;
}
}
function bat_event_field_info() {
return array(
'bat_event_state_reference' => array(
'label' => t('BAT Event State Reference'),
'description' => t('A reference to an Event State.'),
'settings' => array(
'event_type' => '',
),
'default_widget' => 'bat_event_state_reference_autocomplete',
'default_formatter' => 'bat_event_state_reference_default',
'property_type' => 'bat_event_state_reference',
'property_callbacks' => array(
'bat_event_state_reference_info_callback',
),
),
);
}
function bat_event_state_reference_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
$name = $field['field_name'];
$property =& $info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
$property['type'] = $field['cardinality'] != 1 ? 'list<bat_event_state_reference>' : 'bat_event_state_reference';
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'bat_event_state_entity_metadata_field_verbatim_set';
$property['property info'] = bat_event_state_reference_field_data_property_info();
unset($property['query callback']);
}
function bat_event_state_entity_metadata_field_verbatim_set($entity, $name, $items, $langcode, $entity_type) {
$field = field_info_field($name);
$langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode);
if (isset($items['state'])) {
$state = bat_event_load_state_by_machine_name($items['state']);
$items['state_id'] = $state['id'];
unset($items['state']);
}
$value = $field['cardinality'] == 1 ? array(
$items,
) : (array) $items;
$entity->{$name}[$langcode] = array_filter($value);
drupal_static_reset('field_language');
}
function bat_event_state_reference_field_data_property_info($name = NULL) {
return array(
'state_id' => array(
'label' => t('State ID'),
'type' => 'integer',
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
),
);
}
function bat_event_field_is_empty($item, $field) {
return empty($item['state_id']);
}
function bat_event_field_formatter_info() {
$ret = array(
'bat_event_state_reference_default' => array(
'label' => t('BAT Event State'),
'description' => t('BAT Event State default formatter.'),
'field types' => array(
'bat_event_state_reference',
'entityreference',
),
),
);
return $ret;
}
function bat_event_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
}
function bat_event_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$state = bat_event_load_state($item['state_id']);
$element[] = array(
'#markup' => $state['label'],
);
}
return $element;
}
function bat_event_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form = array();
$event_types_options = array();
$event_types = bat_event_get_types();
foreach ($event_types as $event_type) {
if ($event_type->fixed_event_states) {
$event_types_options[$event_type->type] = $event_type->label;
}
}
$form['event_type'] = array(
'#type' => 'select',
'#title' => t('Event type'),
'#options' => $event_types_options,
'#default_value' => isset($settings['event_type']) ? $settings['event_type'] : '',
'#required' => TRUE,
'#empty_option' => t('- Select -'),
);
return $form;
}
function bat_event_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$ids = array();
foreach ($items as $delta => $item) {
if (is_array($item) && !empty($item['state_id'])) {
if (is_numeric($item['state_id'])) {
$ids[] = $item['state_id'];
}
else {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'invalid_state_id',
'message' => t('%name: invalid input.', array(
'%name' => $instance['label'],
)),
);
}
}
}
}
function bat_event_potential_references($field, $options = array()) {
$options += array(
'string' => '',
'match' => 'contains',
'ids' => array(),
'limit' => 0,
);
$results =& drupal_static(__FUNCTION__, array());
$cid = $field['field_name'] . ':' . $options['match'] . ':' . ($options['string'] !== '' ? $options['string'] : implode('-', $options['ids'])) . ':' . $options['limit'];
if (!isset($results[$cid])) {
$references = FALSE;
if ($references === FALSE) {
$references = _bat_event_potential_references($field, $options);
}
$results[$cid] = !empty($references) ? $references : array();
}
return $results[$cid];
}
function _bat_event_potential_references($field, $options) {
$query = db_select('bat_event_state', 'u');
$event_id_alias = $query
->addField('u', 'id');
$event_label_alias = $query
->addField('u', 'label');
$query
->condition('event_type', $options['event_type']);
if ($options['string'] !== '') {
switch ($options['match']) {
case 'contains':
$query
->condition('u.label', '%' . $options['string'] . '%', 'LIKE');
break;
case 'starts_with':
$query
->condition('u.label', $options['string'] . '%', 'LIKE');
break;
case 'equals':
default:
$query
->condition('u.label', $options['string']);
break;
}
}
$result = $query
->execute()
->fetchAll();
$references = array();
foreach ($result as $event) {
$references[$event->id] = array(
'title' => $event->label,
'rendered' => check_plain($event->label),
);
}
return $references;
}
function bat_event_reference_autocomplete($entity_type, $bundle, $field_name, $string = '') {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
$options = array(
'string' => $string,
'match' => $instance['widget']['settings']['autocomplete_match'],
'limit' => 10,
'event_type' => $instance['settings']['event_type'],
);
$references = bat_event_potential_references($field, $options);
$matches = array();
foreach ($references as $id => $row) {
$suggestion = preg_replace('/<a href="([^<]*)">([^<]*)<\\/a>/', '$2', $row['rendered']);
$matches[$row['title'] . " [state_id:{$id}]"] = '<div class="reference-autocomplete">' . $suggestion . '</div>';
}
drupal_json_output($matches);
}
function bat_event_field_widget_info() {
return array(
'bat_event_reference_autocomplete' => array(
'label' => t('Autocomplete text field'),
'description' => t('Display the list of referenceable units as a textfield with autocomplete behaviour.'),
'field types' => array(
'bat_event_state_reference',
),
'settings' => array(
'autocomplete_match' => 'contains',
'size' => 60,
'autocomplete_path' => 'bat_event/state_event/autocomplete',
),
),
);
}
function bat_event_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'bat_event_reference_autocomplete':
$element += array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['state_id']) ? $items[$delta]['state_id'] : NULL,
'#autocomplete_path' => $instance['widget']['settings']['autocomplete_path'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/' . $field['field_name'],
'#size' => $instance['widget']['settings']['size'],
'#maxlength' => 255,
'#element_validate' => array(
'bat_event_autocomplete_validate',
),
'#value_callback' => 'bat_event_autocomplete_value',
);
break;
}
return array(
'state_id' => $element,
);
}
function bat_event_autocomplete_value($element, $input = FALSE, $form_state = array()) {
if ($input === FALSE) {
$state_id = $element['#default_value'];
if (!empty($state_id)) {
if ($state = bat_event_load_state($state_id)) {
$value = $state['label'];
$value .= ' [state_id:' . $state_id . ']';
return $value;
}
else {
return '';
}
}
}
}
function bat_event_autocomplete_validate($element, &$form_state, $form) {
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$value = $element['#value'];
$state_id = NULL;
if (!empty($value)) {
preg_match('/^(?:\\s*|(.*) )?\\[\\s*state_id\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
if (!empty($matches)) {
list(, $title, $state_id) = $matches;
if (!empty($title)) {
$state = bat_event_load_state($state_id);
$real_title = $state['label'];
if (trim($title) != trim($real_title)) {
form_error($element, t('%name: title mismatch. Please check your selection.', array(
'%name' => $instance['label'],
)));
}
}
}
else {
$options = array(
'string' => $value,
'match' => 'equals',
'limit' => 1,
);
$references = bat_event_potential_references($field, $options);
if ($references) {
$state_id = key($references);
}
else {
form_error($element, t('%name: unable to find an event state with that title.', array(
'%name' => $instance['label'],
)));
}
}
}
form_set_value($element, $state_id, $form_state);
}
function bat_event_field_widget_error($element, $error, $form, &$form_state) {
form_error($element['state_id'], $error['message']);
}
function bat_event_delete_event_type_schema($machine_name) {
foreach (bat_event_get_event_type_tables($machine_name) as $name) {
db_drop_table($name);
}
}
function bat_event_delete_states_by_type($type) {
db_delete('bat_event_state')
->condition('event_type', $type)
->execute();
drupal_static_reset('bat_event_get_states');
drupal_static_reset('bat_event_load_state_by_machine_name');
}
function bat_event_get_event_type_tables($machine_name) {
return array(
'bat_event_' . $machine_name . '_day_state',
'bat_event_' . $machine_name . '_day_event',
'bat_event_' . $machine_name . '_hour_state',
'bat_event_' . $machine_name . '_hour_event',
'bat_event_' . $machine_name . '_minute_state',
'bat_event_' . $machine_name . '_minute_event',
);
}
function bat_event_create_event_type_schema($machine_name) {
$schema = array();
$schema['bat_event_' . $machine_name . '_day_state'] = array(
'description' => 'Holds the state of each unit for a given day',
'fields' => bat_event_generate_day_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'month_key' => array(
'unit_id',
'year',
'month',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
$schema['bat_event_' . $machine_name . '_day_event'] = array(
'description' => 'Holds the event that determined the state of a unit',
'fields' => bat_event_generate_day_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'month_key' => array(
'unit_id',
'year',
'month',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
$schema['bat_event_' . $machine_name . '_hour_state'] = array(
'description' => 'Holds the state of the unit',
'fields' => bat_event_generate_hour_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'day_key' => array(
'unit_id',
'year',
'month',
'day',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
$schema['bat_event_' . $machine_name . '_hour_event'] = array(
'description' => 'Holds the event that determined the state of a unit',
'fields' => bat_event_generate_hour_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'day_key' => array(
'unit_id',
'year',
'month',
'day',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
$schema['bat_event_' . $machine_name . '_minute_state'] = array(
'description' => 'Holds the state of the unit',
'fields' => bat_event_generate_minute_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'day_key' => array(
'unit_id',
'year',
'month',
'day',
'hour',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
$schema['bat_event_' . $machine_name . '_minute_event'] = array(
'description' => 'Holds the event that determined the state of a unit',
'fields' => bat_event_generate_minute_schema_fields(),
'indexes' => array(
'unit_id' => array(
'unit_id',
),
'year' => array(
'year',
),
'month' => array(
'month',
),
),
'unique keys' => array(
'day_key' => array(
'unit_id',
'year',
'month',
'day',
'hour',
),
),
'foreign keys' => array(
'unit_id' => array(
'table' => 'bat_units',
'columns' => array(
'unit_id' => 'unit_id',
),
),
),
);
foreach ($schema as $name => $table) {
db_create_table($name, $table);
}
}
function bat_event_generate_day_schema_fields() {
$fields = array(
'unit_id' => array(
'description' => 'Identifier for a unit.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'year' => array(
'description' => 'The calendar year for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'month' => array(
'description' => 'The month for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
);
for ($i = 1; $i <= 31; $i++) {
$fields['d' . $i] = array(
'description' => 'D' . $i,
'type' => 'int',
'not null' => TRUE,
'default' => '0',
);
}
return $fields;
}
function bat_event_generate_hour_schema_fields() {
$fields = array(
'unit_id' => array(
'description' => 'Identifier for a unit.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'year' => array(
'description' => 'The calendar year for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'month' => array(
'description' => 'The month for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'day' => array(
'description' => 'The day for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
);
for ($i = 0; $i <= 23; $i++) {
$fields['h' . $i] = array(
'description' => 'H' . $i,
'type' => 'int',
'not null' => TRUE,
'default' => '0',
);
}
return $fields;
}
function bat_event_generate_minute_schema_fields() {
$fields = array(
'unit_id' => array(
'description' => 'Identifier for a unit.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'year' => array(
'description' => 'The calendar year for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'month' => array(
'description' => 'The month for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'day' => array(
'description' => 'The day for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'hour' => array(
'description' => 'The hour for which this availability row is relevant',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
);
for ($i = 0; $i <= 59; $i++) {
if ($i <= 9) {
$m = '0' . $i;
}
else {
$m = $i;
}
$fields['m' . $m] = array(
'description' => 'M' . $m,
'type' => 'int',
'not null' => TRUE,
'default' => '0',
);
}
return $fields;
}
function bat_event_validate_hex_color($element, &$form_state) {
if (!preg_match('/^#[a-f0-9]{6}$/i', $element['#value'])) {
form_error($element, t('This is not a valid hexadecimal color!'));
}
}
function bat_event_form_bat_type_bundle_form_alter(&$form, &$form_state) {
$type_bundle = $form_state['bat_type_bundle'];
if (!isset($type_bundle->is_new)) {
$fields_options = array();
$fields = field_info_instances('bat_type', $type_bundle->type);
foreach ($fields as $field) {
$fields_options[$field['field_name']] = $field['field_name'];
}
$form['events'] = array(
'#type' => 'fieldset',
'#group' => 'additional_settings',
'#title' => t('Events'),
'#tree' => TRUE,
'#weight' => 80,
);
$event_types = bat_event_get_types();
foreach ($event_types as $event_type) {
$form['events'][$event_type->type] = array(
'#type' => 'select',
'#title' => t('Select your default @event field', array(
'@event' => $event_type->label,
)),
'#options' => $fields_options,
'#default_value' => isset($type_bundle->default_event_value_field_ids[$event_type->type]) ? $type_bundle->default_event_value_field_ids[$event_type->type] : NULL,
'#empty_option' => t('- Select a field -'),
);
}
$form['actions']['submit']['#submit'][] = 'bat_event_form_bat_type_bundle_form_submit';
}
}
function bat_event_form_bat_type_bundle_form_submit($form, &$form_state) {
foreach ($form_state['values']['events'] as $event => $field) {
$form_state['bat_type_bundle']->default_event_value_field_ids[$event] = $field;
}
$form_state['bat_type_bundle']
->save();
}
function bat_event_views_pre_render(&$view) {
if ($view->base_table == 'bat_events') {
if (isset($view->field['start_date']->options['custom_date_format'])) {
$view->field['start_date']->options['custom_date_format'] = variable_get('bat_date_format', 'Y-m-d H:i');
}
if (isset($view->field['end_date']->options['custom_date_format'])) {
$view->field['end_date']->options['custom_date_format'] = variable_get('bat_date_format', 'Y-m-d H:i');
}
}
}
function bat_event_get_matching_units(DateTime $start_date, DateTime $end_date, $valid_name_states, $type_id, $event_type, $intersect = FALSE, $use_costraints = TRUE) {
$calendar = bat_event_get_calendar($type_id, $event_type);
return bat_event_get_matching_units_from_calendar($calendar, $start_date, $end_date, $valid_name_states, $intersect, TRUE, $use_costraints);
}
function bat_event_get_calendar($type_id, $event_type) {
$state_store = new DrupalDBStore($event_type, DrupalDBStore::BAT_STATE, bat_get_db_prefix());
$drupal_units = bat_unit_load_multiple(FALSE, array(
'type_id' => $type_id,
));
$bat_units = array();
foreach ($drupal_units as $unit_id => $unit) {
$bat_units[] = new Unit($unit_id, $unit
->getEventDefaultValue($event_type));
}
$calendar = new Calendar($bat_units, $state_store);
return $calendar;
}
function bat_event_get_matching_units_from_calendar($calendar, DateTime $start_date, DateTime $end_date, $valid_name_states, $intersect = FALSE, $reset = TRUE, $use_costraints = TRUE) {
$valid_states = array();
foreach ($valid_name_states as $name) {
$state = bat_event_load_state_by_machine_name($name);
$valid_states[] = $state['id'];
}
$constraints = array();
if ($use_costraints) {
foreach (bat_event_constraints_get_info() as $constraint) {
$constraints[] = $constraint['constraint'];
}
}
$response = $calendar
->getMatchingUnits($start_date, $end_date, $valid_states, $constraints, $intersect, $reset);
$valid_unit_ids = array_keys($response
->getIncluded());
if (count($valid_unit_ids)) {
return $valid_unit_ids;
}
return FALSE;
}
function bat_event_get_calendar_response(DateTime $start_date, DateTime $end_date, $valid_name_states, $type_id, $event_type, $intersect = FALSE, $use_costraints = TRUE) {
$results = array(
'included' => array(),
'excluded' => array(),
);
$valid_states = array();
foreach ($valid_name_states as $name) {
$state = bat_event_load_state_by_machine_name($name);
$valid_states[] = $state['id'];
}
$constraints = array();
if ($use_costraints) {
foreach (bat_event_constraints_get_info() as $constraint) {
$constraints[] = $constraint['constraint'];
}
}
$calendar = bat_event_get_calendar($type_id, $event_type);
$response = $calendar
->getMatchingUnits($start_date, $end_date, $valid_states, $constraints, $intersect);
$valid_unit_ids = array_keys($response
->getIncluded());
$excluded = array();
foreach ($response
->getExcluded() as $unit_id => $ex) {
if (isset($ex['constraint'])) {
$p = $ex['constraint']
->toString();
$excluded[$unit_id] = t($p['text'], $p['args']);
}
else {
$excluded[$unit_id] = '';
}
}
$results['excluded'] = $excluded;
if (count($valid_unit_ids)) {
$results['included'] = $valid_unit_ids;
}
return $results;
}
function bat_event_constraints_get_info() {
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['bat_event_constraints_info'] =& drupal_static(__FUNCTION__);
}
$constraints_info =& $drupal_static_fast['bat_event_constraints_info'];
if (empty($constraints_info)) {
if ($cache = cache_get('bat_event_constraints_info')) {
$constraints_info = $cache->data;
}
else {
$constraints_info = module_invoke_all('bat_event_constraints_info');
drupal_alter('bat_event_constraints_info', $constraints_info);
cache_set('bat_event_constraints_info', $constraints_info);
}
}
return $constraints_info;
}
function bat_event_action_info() {
return array(
'bat_event_unit_set_state' => array(
'type' => 'bat_unit',
'label' => t('Assign fixed-state event to units'),
'behavior' => array(
'bat_event_unit_set_state',
),
'configurable' => TRUE,
'vbo_configurable' => FALSE,
'triggers' => array(
'any',
),
),
);
}
function bat_event_unit_set_state(&$unit, $context) {
$type = bat_type_load($unit->type_id);
$type_bundle = bat_type_bundle_load($type->type);
$event_state = $context['form_values']['event_state'];
$event_type = $context['form_values']['event_type'];
$start_date = new DateTime($context['form_values']['bat_start_date']);
$end_date = new DateTime($context['form_values']['bat_end_date']);
$end_date
->sub(new DateInterval('PT1M'));
if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
$event = bat_event_create(array(
'type' => $event_type,
'start_date' => $start_date
->format('Y-m-d H:i:s'),
'end_date' => $end_date
->format('Y-m-d H:i:s'),
'uid' => $type->uid,
'created' => REQUEST_TIME,
));
$event->event_bat_unit_reference[LANGUAGE_NONE][0]['target_id'] = $unit->unit_id;
$state = bat_event_load_state_by_machine_name($event_state);
$event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $state['id'];
$event
->save();
}
}
function bat_event_unit_set_state_form($context, &$form_state) {
$form = array();
$event_types_options = array();
$event_types = bat_event_get_types();
foreach ($event_types as $event_type) {
if ($event_type->fixed_event_states) {
$event_types_options[$event_type->type] = $event_type->label;
}
}
$form += bat_date_range_fields();
$form['event_type'] = array(
'#type' => 'select',
'#title' => t('Event type'),
'#options' => $event_types_options,
'#required' => TRUE,
'#ajax' => array(
'callback' => 'bat_event_unit_set_state_form_callback',
'wrapper' => 'event-state-wrapper',
),
);
if (isset($form_state['values']['event_type'])) {
$state_options = array();
foreach (bat_event_get_states($form_state['values']['event_type']) as $state) {
$state_options[$state['machine_name']] = $state['label'];
}
$form['event_state'] = array(
'#type' => 'select',
'#title' => t('Event state'),
'#options' => $state_options,
'#required' => TRUE,
'#prefix' => '<div id="event-state-wrapper">',
'#suffix' => '</div>',
);
}
else {
$form['event_state'] = array(
'#prefix' => '<div id="event-state-wrapper">',
'#suffix' => '</div>',
);
}
return $form;
}
function bat_event_unit_set_state_form_callback($form, &$form_state) {
return $form['event_state'];
}
function bat_event_unit_set_state_submit($form, &$form_state) {
return array(
'form_values' => $form_state['values'],
);
}