class BatEventController in Booking and Availability Management Tools for Drupal 7
The Controller for BatEvent entities.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
- class \BatEventController
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
Expanded class hierarchy of BatEventController
1 string reference to 'BatEventController'
- bat_event_entity_info in modules/
bat_event/ bat_event.module - Implements hook_entity_info().
File
- modules/
bat_event/ bat_event.module, line 1290 - Manage Events - Events store the EventValue of a Unit over a period of time.
View source
class BatEventController extends EntityAPIController {
/**
* Create a event - we first set up the values that are specific
* to our event but then also go through the EntityAPIController
* function.
*
* @param array $values
* The event to create properties.
*
* @return object
* A event object with all default fields initialized.
*/
public function create(array $values = array()) {
$event_type = bat_event_type_load($values['type'], TRUE);
// Add values that are specific to our event.
$values += array(
'event_id' => '',
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'data' => array(),
);
$event = parent::create($values);
return $event;
}
/**
* {@inheritdoc}
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
// Update start_date_object and end_date_object with the new dates.
$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);
// Construct target entity reference field name using this event type's target entity type.
$target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
// We are going to be updating the event - so the first step is to remove
// the old event.
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) {
// Get the referenced entity ID.
$event_target_entity_reference = field_get_items('bat_event', $entity->original, $target_field_name);
$target_entity_id = $event_target_entity_reference[0]['target_id'];
// Load the referenced entity.
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);
// Now we store the new event.
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);
}
}
}
/**
* Handles saving to the BatStore.
*
* @param \Roomify\Bat\Unit\Unit $unit
* The unit to save.
* @param \DateTime $start_date
* @param \DateTime $end_date
* @param string $event_type
* @param string $granularity
* @param string $event_state
* @param int $event_id
* @param bool|false $remove
* Set to TRUE if the event is to be removed (event_id set to zero).
*/
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);
}
/**
* @param array $ids
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
foreach ($ids as $id) {
$event = bat_event_load($id);
$this
->deleteEvent($event);
}
parent::delete($ids);
}
/**
* @param BatEvent $event
*/
public function deleteEvent(BatEvent $event) {
$event_type = bat_event_type_load($event->type);
// Construct target entity reference field name using this event type's target entity type.
$target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
// Check if the event had a unit associated with it and if so update the
// availability calendar.
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'];
// Load the referenced entity.
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);
}
}
}
/**
* Overriding the buildContent function to add entity specific fields.
*/
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
return $content;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BatEventController:: |
public | function | Handles saving to the BatStore. | |
BatEventController:: |
public | function |
Overriding the buildContent function to add entity specific fields. Overrides EntityAPIController:: |
|
BatEventController:: |
public | function |
Create a event - we first set up the values that are specific
to our event but then also go through the EntityAPIController
function. Overrides EntityAPIController:: |
|
BatEventController:: |
public | function |
Overrides EntityAPIController:: |
|
BatEventController:: |
public | function | ||
BatEventController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIController:: |
|
DrupalDefaultEntityController:: |
protected | property | Whether this entity type should use the static cache. | |
DrupalDefaultEntityController:: |
protected | property | Static cache of entities, keyed by entity ID. | |
DrupalDefaultEntityController:: |
protected | property | Array of information about the entity. | |
DrupalDefaultEntityController:: |
protected | property | Entity type for this controller instance. | |
DrupalDefaultEntityController:: |
protected | property | Additional arguments to pass to hook_TYPE_load(). | |
DrupalDefaultEntityController:: |
protected | property | Name of the entity's ID field in the entity database table. | |
DrupalDefaultEntityController:: |
protected | property | Name of entity's revision database table field, if it supports revisions. | |
DrupalDefaultEntityController:: |
protected | property | The table that stores revisions, if the entity supports revisions. | |
DrupalDefaultEntityController:: |
protected | function | Attaches data to entities upon loading. | 4 |
DrupalDefaultEntityController:: |
protected | function | Gets entities from the static cache. | 1 |
DrupalDefaultEntityController:: |
protected | function | Stores entities in the static entity cache. | |
DrupalDefaultEntityController:: |
protected | function | Ensures integer entity IDs are valid. | |
DrupalDefaultEntityController:: |
protected | function | Callback for array_filter that removes non-integer IDs. | |
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | property | ||
EntityAPIController:: |
protected | function |
Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
public | function | Builds and executes the query for loading. | |
EntityAPIController:: |
protected | function | Renders a single entity property. | |
EntityAPIController:: |
public | function |
Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController:: |
1 |
EntityAPIController:: |
protected | function | Saves an entity revision. | |
EntityAPIController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
1 |
EntityAPIController:: |
public | function |
Overridden. Overrides DrupalDefaultEntityController:: |
1 |