You are here

function node_gallery_update_7004 in Node Gallery 7

Migrate Node Gallery galleries.

File

./node_gallery.install, line 367
Install, update and uninstall functions for the node_gallery module.

Code

function node_gallery_update_7004(&$sandbox) {
  if (!db_table_exists('node_gallery_galleries_d6')) {
    return;
  }
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = db_query("SELECT COUNT(*) FROM {node_gallery_galleries_d6}")
      ->fetchField();
    $sandbox['processed'] = 0;
  }

  // Number of galleries to process per batch.
  $count = 500;
  $result = db_query_range("SELECT g.gid as ngid, g.cover_image as cover_item, COUNT(n.nid) as item_count, SUM(n.status) as pub_item_count FROM {node_gallery_galleries_d6} g LEFT JOIN {node_gallery_images_d6} i ON g.gid = i.gid LEFT JOIN {node} n ON i.nid = n.nid GROUP BY g.gid ORDER BY g.gid", $sandbox['processed'], $count);
  foreach ($result as $row) {

    // Change NULL values to 0.
    $row = (array) $row;
    $row['pub_item_count'] = (int) $row['pub_item_count'];
    $row['item_count'] = (int) $row['item_count'];
    db_insert('node_gallery_galleries')
      ->fields($row)
      ->execute();
    $sandbox['processed']++;
  }
  if (!empty($sandbox['total'])) {
    $sandbox['#finished'] = $sandbox['processed'] / $sandbox['total'];
    if ($sandbox['#finished'] === 1) {
      field_cache_clear();
    }
  }
}