You are here

function node_gallery_get_gallery_list in Node Gallery 6.2

Same name and namespace in other branches
  1. 6.3 node_gallery.inc \node_gallery_get_gallery_list()
1 call to node_gallery_get_gallery_list()
node_gallery_list in ./node_gallery.pages.inc

File

./node_gallery.inc, line 78
Node gallery models

Code

function node_gallery_get_gallery_list($uid = NULL, $gallery_type = NULL) {
  $items = array();
  $gallery_types = empty($gallery_type) ? node_gallery_get_types() : array(
    $gallery_type,
  );
  $args = $gallery_types;
  $sql = "SELECT n.nid, n.title, n.type, n.created, n.uid FROM {node} n \n    WHERE n.type IN (" . db_placeholders($gallery_types, 'varchar') . ") AND n.status = 1";
  if ($uid) {
    $sql .= " AND n.uid = %d";
    $args[] = $uid;
  }
  $result = pager_query(db_rewrite_sql($sql), variable_get('node_gallery_page_number', 20), 0, NULL, $args);
  while ($object = db_fetch_object($result)) {
    $items[$object->nid] = $object;
  }
  if (!empty($items)) {
    $gids = array_keys($items);

    //we only need cover image when display gallery list;
    $result = db_query("SELECT ng.*, f.* FROM {node_galleries} ng INNER JOIN {files} f ON ng.fid = f.fid WHERE ng.gid IN (" . db_placeholders($gids) . ") AND ng.is_cover > 0", $gids);
    $i = 0;
    while ($object = db_fetch_object($result)) {
      if (!empty($object->nid) && !empty($object->gid)) {

        //we can't use $object->gid because it could be null;
        $items[$object->gid]->images[] = $object;
        $i++;
      }
    }

    //get the images count for display;
    $result = db_query("SELECT ng.gid, COUNT(ng.nid) AS count FROM {node_galleries} ng JOIN {node} n on n.nid = ng.nid WHERE gid IN (" . db_placeholders($gids) . ") and n.status = 1 GROUP BY gid", $gids);
    while ($r = db_fetch_array($result)) {
      $items[$r['gid']]->images_count = $r['count'];
    }
  }
  return $items;
}