You are here

function pmorganization_update_7301 in Drupal PM (Project Management) 8

Same name and namespace in other branches
  1. 7.3 pmorganization/pmorganization.install \pmorganization_update_7301()

Build pmorganization_index for all nodes.

File

pmorganization/pmorganization.install, line 129
PM Organization install file.

Code

function pmorganization_update_7301(&$sandbox) {

  // Retrieve all records.
  $result = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->orderBy('n.nid', 'ASC')
    ->execute();

  // Use the sandbox to store the information needed to track progression.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = $result
      ->rowCount();
    $sandbox['messages'] = array();
    $sandbox['current_node'] = -1;
  }

  // Process nodes by groups of 50.
  $limit = 50;

  // Retrieve the next group of nids.
  $query = db_select('node', 'n')
    ->extend('PagerDefault');
  $result = $query
    ->fields('n', array(
    'nid',
  ))
    ->orderBy('n.nid', 'ASC')
    ->condition('n.nid', $sandbox['current_node'], '>')
    ->limit($limit)
    ->execute();
  foreach ($result as $row) {
    $node = node_load($row->nid);

    // Rebuild the pmorganization_index.
    pmorganization_delete_node_index($node);
    pmorganization_build_node_index($node);

    // Update our progress information.
    $sandbox['progress']++;
    $sandbox['current_node'] = $row->nid;
  }
  $sandbox['#finished'] = $sandbox['progress'] >= $sandbox['max'] ? TRUE : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished']) {
    return t('PM Organization index rebuild has been completed.');
  }
}