You are here

function fc_rebuild_process in Field Complete 7

Rebuild the 'fc' table to incorporate all existing entities

1 string reference to 'fc_rebuild_process'
fc_rebuild_submit in ./fc.admin.inc

File

./fc.admin.inc, line 106
Field Complete - Provides field-based completeness for any entity - admin.

Code

function fc_rebuild_process($entity_type, $entity_info, &$context) {
  $idKey = $entity_info['entity keys']['id'];
  $baseTable = $entity_info['base table'];
  if (empty($context['sandbox']['initialised'])) {
    $context['sandbox']['initialised'] = TRUE;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['progress'] = 0;

    // We need to ensure there's no 'zero' ID - true for 'users'
    $context['sandbox']['max'] = db_select($baseTable, 'e')
      ->condition($idKey, $context['sandbox']['current_id'], '>')
      ->countQuery()
      ->execute()
      ->fetchField();
    if ($context['sandbox']['max'] == 0) {
      return;
    }
  }
  $ids = db_select($baseTable, 'e')
    ->fields('e', array(
    $idKey,
  ))
    ->condition($idKey, $context['sandbox']['current_id'], '>')
    ->range(0, 10)
    ->orderBy("e.{$idKey}", 'ASC')
    ->execute()
    ->fetchCol();
  if (!empty($ids)) {
    $args = array(
      '%entity_type' => $entity_type,
      '%idKey' => $idKey,
      '%id' => NULL,
    );
    foreach (entity_load($entity_type, $ids) as $entity) {
      $entity->fc = fcComplete::build($entity_type, $entity);
      $entity->fc
        ->completeness();
      $entity->fc
        ->save(fcComplete::SKIP_IF_EXISTS);
      $context['sandbox']['current_id'] = $args['%id'] = $entity->{$idKey};
      $context['sandbox']['progress']++;
    }
    $context['message'] = t('Now processing %entity_type with %idKey=%id', $args);
  }
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['results'][] = t('Processed %count entities for %entity_type', array(
      '%count' => $context['sandbox']['max'],
      '%entity_type' => $entity_type,
    ));
    $context['finished'] = TRUE;
  }
}