You are here

class BatEventController in Booking and Availability Management Tools for Drupal 7

The Controller for BatEvent entities.

Hierarchy

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

Namesort descending Modifiers Type Description Overrides
BatEventController::batStoreSave public function Handles saving to the BatStore.
BatEventController::buildContent public function Overriding the buildContent function to add entity specific fields. Overrides EntityAPIController::buildContent
BatEventController::create 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::create
BatEventController::delete public function Overrides EntityAPIController::delete
BatEventController::deleteEvent public function
BatEventController::save public function Implements EntityAPIControllerInterface. Overrides EntityAPIController::save
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
EntityAPIController::$bundleKey protected property
EntityAPIController::$cacheComplete protected property
EntityAPIController::$defaultRevisionKey protected property
EntityAPIController::buildQuery protected function Overrides DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery 1
EntityAPIController::deleteRevision public function Implements EntityAPIControllerRevisionableInterface::deleteRevision(). Overrides EntityAPIControllerRevisionableInterface::deleteRevision
EntityAPIController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export 1
EntityAPIController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
EntityAPIController::invoke public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::invoke 1
EntityAPIController::load public function Overridden. Overrides DrupalDefaultEntityController::load 1
EntityAPIController::query public function Builds and executes the query for loading.
EntityAPIController::renderEntityProperty protected function Renders a single entity property.
EntityAPIController::resetCache public function Overrides DrupalDefaultEntityController::resetCache(). Overrides DrupalDefaultEntityController::resetCache 1
EntityAPIController::saveRevision protected function Saves an entity revision.
EntityAPIController::view public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::view 1
EntityAPIController::__construct public function Overridden. Overrides DrupalDefaultEntityController::__construct 1