You are here

function taxonomy_term_depth_batch_callbacks_update_term_depth in Taxonomy Term Depth 7

Same name and namespace in other branches
  1. 8.2 taxonomy_term_depth.batch.inc \taxonomy_term_depth_batch_callbacks_update_term_depth()
  2. 8 taxonomy_term_depth.batch.inc \taxonomy_term_depth_batch_callbacks_update_term_depth()
2 string references to 'taxonomy_term_depth_batch_callbacks_update_term_depth'
taxonomy_term_depth_batch_depth_update_form_submit in ./taxonomy_term_depth.batch.inc
taxonomy_term_depth_install in ./taxonomy_term_depth.install
Implements hook_install()

File

./taxonomy_term_depth.batch.inc, line 44

Code

function taxonomy_term_depth_batch_callbacks_update_term_depth($options, &$context) {
  $sandbox =& $context['sandbox'];

  // Build query
  $query = db_select('taxonomy_term_data', 'ttd');
  $query
    ->fields('ttd', array(
    'tid',
  ));
  $query
    ->groupBy('ttd.tid');

  // Count ALL elements and save the value for further usage
  if (!isset($sandbox['count'])) {

    // Clear all depths first
    db_update('taxonomy_term_data')
      ->fields(array(
      'depth' => NULL,
    ))
      ->execute();
    $sandbox['count'] = $query
      ->countQuery()
      ->execute()
      ->fetchField();
  }
  $sandbox += array(
    'position' => 0,
    'bunch' => 20,
  );

  //$sandbox['position'] += $sandbox['bunch'];
  $query
    ->range($sandbox['position'], $sandbox['bunch']);
  foreach ($query
    ->execute() as $row) {

    // Forcely rebuild data in database
    taxonomy_term_depth_get_by_tid($row->tid, TRUE);
    $sandbox['position']++;
  }
  $context['finished'] = $sandbox['count'] ? $sandbox['position'] / $sandbox['count'] : 1;
  $context['finished'] = $context['finished'] > 1 ? 1 : $context['finished'];
}