You are here

function farm_log_update_7003 in farmOS 7

Populate the new movement geofields with geometry from referenced areas.

File

modules/farm/farm_log/farm_log.install, line 210
Farm Log install file.

Code

function farm_log_update_7003(&$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 by reverting the field instance component of this module
    // so that the new geofield is available
    features_revert(array(
      'farm_log' => array(
        'field_instance',
      ),
    ));

    // 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')
    ->fields(NULL, array(
    'id',
  ))
    ->condition('type', 'farm_movement')
    ->orderBy('id', 'ASC')
    ->range($sandbox['progress'], $limit);
  $results = $query
    ->execute();

  // Iterate over the results and save each one. farm_log_entity_presave() will
  // do the rest.
  while ($id = $results
    ->fetchField()) {

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

    // Load the log and save it.
    $log = log_load($id);
    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;
  }
}