You are here

function node_gallery_set_gallery_cover_image in Node Gallery 6.3

Sets the cover image in the DB if necessary.

Parameters

$image: A reference to the node object of a node gallery image.

2 calls to node_gallery_set_gallery_cover_image()
node_gallery_manage_images_submit in ./node_gallery.pages.inc
node_gallery_nodeapi in ./node_gallery.module
Implements hook_nodeapi().

File

./node_gallery.inc, line 817
Shared functions for node_gallery

Code

function node_gallery_set_gallery_cover_image($image) {
  if ($image->gid > 0) {
    $gallery = new stdClass();
    $gallery->gid = $image->gid;
    if ($image->is_cover) {
      if (!empty($image->oldgid)) {

        // this image is the cover image for another gallery, it's being moved. Retrieve a new cover for the old gallery
        $new_cover = db_result(db_query('SELECT nid FROM {node_gallery_images} WHERE gid = %d AND nid <> %d LIMIT 1', $image->oldgid, $image->nid));
        if ($new_cover !== FALSE) {
          db_query("UPDATE {node_gallery_galleries} SET cover_image = %d WHERE gid = %d", $new_cover, $image->oldgid);
        }
        else {

          // Gallery is now empty, set cover to NULL.
          db_query("UPDATE {node_gallery_galleries} SET cover_image = NULL WHERE gid = %d", $image->oldgid);
        }
      }
      else {
        $gallery->cover_image = $image->nid;
      }
    }

    // check if the gallery already has a cover
    $sql = "SELECT count(*) from {node_gallery_galleries} where gid = %d and cover_image IS NULL";
    $count = db_result(db_query($sql, $image->gid));
    if ($count > 0) {

      // This is the first image being added to the gallery.  Make it the cover.
      $gallery->cover_image = $image->nid;
    }
    if (isset($gallery->cover_image)) {
      drupal_write_record('node_gallery_galleries', $gallery, 'gid');
    }
  }
}