You are here

office_hours.install in Office Hours 7

Same filename and directory in other branches
  1. 8 office_hours.install
  2. 6.2 office_hours.install
  3. 6 office_hours.install

Install, update and uninstall functions for the Office hours module.

File

office_hours.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Office hours module.
 */

/**
 * Implements hook_field_schema().
 */
function office_hours_field_schema($field) {
  switch ($field['type']) {
    case 'office_hours':
      $db_columns = array(
        'day' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'starthours' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'endhours' => array(
          'type' => 'int',
          'not null' => FALSE,
        ),
        'comment' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => FALSE,
        ),
      );
      break;
  }
  return array(
    'columns' => $db_columns,
  );
}

/**
 * Change value of 'day' column from 0-13 range to normal 0-6 day range.
 *
 * This allows for more then 2 hours blocks per day.
 */
function office_hours_update_7100() {

  // Avoid problems when updating from 1.0 / 1.1 to 1.6.
  // Do the db_add_field('comment') first.
  office_hours_update_7102();

  // Now do the update to 1.2
  _office_hours_update_7100_daynum();
}

/**
 * Add a Comment column.
 */
function office_hours_update_7102() {
  $db_columns = array(
    'comment' => array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    ),
  );
  $fields = field_info_fields();
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'office_hours') {
      $schema = array(
        'columns' => $db_columns,
      );
      foreach ($field['storage']['details']['sql'] as $type => $table_info) {
        foreach ($table_info as $table_name => $columns) {
          $column_name = _field_sql_storage_columnname($field_name, 'comment');
          if (!db_field_exists($table_name, $column_name)) {
            db_add_field($table_name, $column_name, $schema['columns']['comment']);
          }
        }
      }
    }
  }
  field_cache_clear();
}

/**
 * Helper function for hook_update_N.
 *
 * Change value of 'day' column from 0-13 range to normal 0-6 day range.
 * This allows for more then 2 hours blocks per day.
 */
function _office_hours_update_7100_daynum() {

  // Step 1: Collect field_ids for office hours fields.
  $ids = array();
  $fields = field_read_fields();
  foreach ($fields as $field) {
    if ($field['type'] == 'office_hours') {
      $ids[] = $field['id'];
    }
  }
  if (empty($ids)) {
    return;
  }

  // Step 2: Collect fields of type 'office_hours' via id.
  $oh_fields = array();
  $instances = field_info_instances();
  foreach ($instances as $entity => $bundles) {
    foreach ($bundles as $bundle => $fields) {
      foreach ($fields as $field_name => $field) {

        // Check each field for the correct $field_id
        foreach ($ids as $key => $id) {
          if ($field['field_id'] == $id) {
            $oh_fields[$field['id']] = $field;
          }
        }
      }
    }
  }

  // Step 3: Update entities.
  foreach ($oh_fields as $id => $field) {
    $entity_type = $field['entity_type'];
    $bundle = $field['bundle'];
    $field_id = $field['field_id'];
    $field_name = $field['field_name'];
    unset($entities);
    $query = new EntityFieldQuery();
    $entities = $query
      ->entityCondition('entity_type', $entity_type)
      ->entityCondition('bundle', $bundle)
      ->execute();

    // Remove extra layer from data structure.
    $entities = $entities[$entity_type];
    ksort($entities);

    // We may run in time problems. Allow restart using a variable.
    $id = -1;
    $latest_id = variable_get('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id, $id);

    // Convert the 'day' column to from 0-13 to 0-6 day range.
    foreach ($entities as $id => $entity) {
      if ($id > $latest_id) {

        // Non-node Entity types (like 'taxonomy_term') need enhancing.
        // Copied from entity_create_stub_entity() (removed in D8).
        $info = entity_get_info($entity_type);

        // $entity->{$info['entity keys']['id']} = $entity->id;
        if (!empty($info['entity keys']['revision']) && isset($entity->vid)) {
          $entity->{$info['entity keys']['revision']} = $entity->vid;
        }
        if (!empty($info['entity keys']['bundle']) && isset($bundle)) {
          $entity->{$info['entity keys']['bundle']} = $bundle;
        }
        $full_entities = array(
          $id => $entity,
        );
        field_attach_load($entity_type, $full_entities, FIELD_LOAD_CURRENT, array(
          'field_id' => $field_id,
        ));
        $empty = TRUE;
        foreach ($full_entities as $id => $full_entity) {
          $entity_field =& $full_entity->{$field_name};
          foreach ($entity_field as $langcode => &$items) {
            foreach ($items as $index => &$item) {
              $empty = FALSE;
              $item['day'] = (int) ($item['day'] / 2);
            }
          }
        }

        // In order to avoid time-outs, only update objects with changed data.
        if (!$empty) {

          // Prevent core-error #985642, which should be fixed per D7.22
          $full_entity->original = $full_entity;

          // field_attach_presave($entity_type, $full_entity);  // This unnecessarily shifts the days.
          field_attach_update($entity_type, $full_entity);
          variable_set('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id, $id);
        }
      }
    }
  }

  // If we did not break, remove variables.
  foreach ($oh_fields as $id => $field) {
    $entity_type = $field['entity_type'];
    $bundle = $field['bundle'];
    $field_id = $field['field_id'];
    $field_name = $field['field_name'];
    variable_del('office_hours-' . $entity_type . '-' . $bundle . '-' . $field_id);
  }
}

Functions

Namesort descending Description
office_hours_field_schema Implements hook_field_schema().
office_hours_update_7100 Change value of 'day' column from 0-13 range to normal 0-6 day range.
office_hours_update_7102 Add a Comment column.
_office_hours_update_7100_daynum Helper function for hook_update_N.