You are here

function farm_movement_update_7001 in farmOS 7

Migrate movement data into new field_farm_movement field collections.

File

modules/farm/farm_movement/farm_movement.install, line 50
Code for farm movement installation/updates.

Code

function farm_movement_update_7001(&$sandbox) {

  // Process this in passes of 50 movements at a time.
  $sandbox['#finished'] = 0;
  $limit = 10;

  // Keep track of progress.
  if (!isset($sandbox['progress'])) {

    // Start out at zero.
    $sandbox['progress'] = 0;

    // Count how many movement logs there are.
    $sandbox['max'] = db_select('log')
      ->fields(NULL, array(
      'id',
    ))
      ->condition('type', 'farm_movement')
      ->countQuery()
      ->execute()
      ->fetchField();
  }

  // Fetch the next set of movements.
  $query = db_select('log', 'l')
    ->fields('l', array(
    'id',
  ))
    ->condition('type', 'farm_movement')
    ->orderBy('id', 'ASC')
    ->range($sandbox['progress'], $limit);
  $results = $query
    ->execute();

  // Iterate over the results.
  while ($id = $results
    ->fetchField()) {

    // Increment progress.
    $sandbox['progress']++;

    // If the id is empty, skip it.
    if (empty($id)) {
      continue;
    }

    // Load the log.
    $log = log_load($id);

    // If the log didn't load for whatever reason, skip it.
    if (empty($log)) {
      continue;
    }

    // Create an entity wrapper for the log.
    $log_wrapper = entity_metadata_wrapper('log', $log);

    // If the movement field collection does not exist, create it.
    if (empty($log_wrapper->field_farm_movement
      ->value())) {

      // Create a new movement field_collection entity.
      $movement = entity_create('field_collection_item', array(
        'field_name' => 'field_farm_movement',
      ));

      // Attach the movement to the log.
      $movement
        ->setHostEntity('log', $log);
    }

    // Copy the "movement to" and "geometry" fields from the movement log
    // itself to the new field collection.
    $fields = array(
      'field_farm_move_to',
      'field_farm_geofield',
    );
    foreach ($fields as $field) {
      if (!empty($log_wrapper->{$field}
        ->value())) {
        $log_wrapper->field_farm_movement->{$field}
          ->set($log_wrapper->{$field}
          ->value());
      }
    }

    // Save the movement field collection and log.
    $log_wrapper->field_farm_movement
      ->save(TRUE);
    log_save($log);
  }

  // Tell Drupal whether or not we're finished.
  if ($sandbox['max'] > 0) {
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }
  else {
    $sandbox['#finished'] = 1;
  }

  // If we are finished, delete the old fields from movement logs.
  if ($sandbox['#finished'] == 1) {

    // Delete field_farm_move_to.
    $field = field_info_instance('log', 'field_farm_move_to', 'farm_movement');
    field_delete_instance($field);

    // Delete field_farm_geofield.
    $field = field_info_instance('log', 'field_farm_geofield', 'farm_movement');
    field_delete_instance($field);
  }
}