You are here

function og_theme_update_7000 in Organic groups theme 7

Same name and namespace in other branches
  1. 7.2 og_theme.install \og_theme_update_7000()

Upgrade from 6.x version of Organic groups theme.

File

./og_theme.install, line 13
Install, update, and uninstall functions for the Group theme module.

Code

function og_theme_update_7000(&$sandbox) {
  $sandbox['#finished'] = 0;
  if (!isset($sandbox['total'])) {

    // Initialize state for future calls.
    $sandbox['last'] = 0;
    $sandbox['count'] = 0;

    // Get the group types. Group update has ran already, so we can use
    // Group's API.
    // @see og_theme_update_dependencies()
    $groups = FALSE;
    foreach (node_type_get_types() as $type) {
      if (og_is_group_type('node', $type->type)) {

        // Add group theme field to the content type.
        og_create_field(OG_THEME_FIELD, 'node', $type->type);
        $groups = TRUE;
      }
    }
    if ($groups) {
      $query = db_select('node_revision', 'nr');
      $query
        ->innerJoin('node', 'n', 'n.vid = nr.vid');
      $query
        ->innerJoin('og', 'og', 'n.nid = og.nid');
      $sandbox['total'] = $query
        ->isNotNull('og.og_theme')
        ->countQuery()
        ->execute()
        ->fetchField();
    }
  }
  else {
    $found = FALSE;
    if ($sandbox['total']) {

      // Operate on every revision of every node, in batches.
      $batch_size = 200;
      $query = db_select('node_revision', 'nr');
      $query
        ->innerJoin('node', 'n', 'n.vid = nr.vid');
      $query
        ->innerJoin('og', 'og', 'n.nid = og.nid');
      $query
        ->fields('nr', array(
        'nid',
        'vid',
      ))
        ->fields('n', array(
        'type',
      ))
        ->fields('og', array(
        'og_theme',
      ))
        ->condition('nr.vid', $sandbox['last'], '>')
        ->isNotNull('og.og_theme')
        ->orderBy('nr.vid', 'ASC')
        ->range(0, $batch_size);
      $revisions = $query
        ->execute();
      foreach ($revisions as $revision) {

        // Create a dummy node object.
        $node = (object) array(
          'nid' => $revision->nid,
          'vid' => $revision->vid,
          'type' => $revision->type,
        );

        // Set field values.
        $node->{OG_THEME_FIELD}[LANGUAGE_NONE][0]['value'] = $revision->og_theme;
        field_attach_update('node', $node);
        $sandbox['last'] = $revision->vid;
        $sandbox['count'] += 1;
      }
      $sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
    }
    if (!$found) {

      // All nodes are processed.
      $sandbox['#finished'] = 1;
    }
  }
}