You are here

function image_gallery_page in Image 5.2

Same name and namespace in other branches
  1. 5 contrib/image_gallery/image_gallery.module \image_gallery_page()
  2. 6 contrib/image_gallery/image_gallery.pages.inc \image_gallery_page()
  3. 7 contrib/image_gallery/image_gallery.pages.inc \image_gallery_page()

Image gallery callback, displays an image gallery

1 string reference to 'image_gallery_page'
image_gallery_menu in contrib/image_gallery/image_gallery.module

File

contrib/image_gallery/image_gallery.module, line 188

Code

function image_gallery_page($type = NULL, $tid = 0) {
  $galleries = taxonomy_get_tree(_image_gallery_get_vid(), $tid, -1, 1);
  for ($i = 0; $i < count($galleries); $i++) {
    $galleries[$i]->count = taxonomy_term_count_nodes($galleries[$i]->tid, 'image');
    $tree = taxonomy_get_tree(_image_gallery_get_vid(), $galleries[$i]->tid, -1);
    $descendant_tids = array_merge(array(
      $galleries[$i]->tid,
    ), array_map('_taxonomy_get_tid_from_term', $tree));

    // The values of $descendant_tids should be safe for raw inclusion in the
    // SQL since they're all loaded from integer fields in the database.
    $sql = "SELECT n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (" . implode(',', $descendant_tids) . ") AND n.type = 'image' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC";
    if ($nid = db_result(db_query_range(db_rewrite_sql($sql), 0, 1))) {
      $galleries[$i]->latest = node_load(array(
        'nid' => $nid,
      ));
    }
  }
  $images = array();
  if ($tid) {

    /* Depending on whether the Views module is enabled, either:
     * - Build (and theme) the image gallery ourselves.
     * - Embed a view to show the image gallery, appending it to the themed
     *   child gallery list.
     */
    if (image_gallery_is_using_views()) {
      $view = views_get_view('image_gallery');
    }
    else {

      // Allow images to be sorted in a useful order.
      $query = "SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d ";
      $count_query = "SELECT COUNT(DISTINCT(n.nid)) FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d ";
      $args = array(
        $tid,
      );
      switch (variable_get('image_gallery_sort_order', IMAGE_GALLERY_SORT_CREATE_DESC)) {
        case IMAGE_GALLERY_SORT_CREATE_DESC:
          $query .= 'ORDER BY n.sticky DESC, n.created DESC';
          break;
        case IMAGE_GALLERY_SORT_CREATE_ASC:
          $query .= 'ORDER BY n.sticky DESC, n.created ASC';
          break;
        case IMAGE_GALLERY_SORT_FILENAME:
          $query = "SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid\n            INNER JOIN {image} i ON n.nid = i.nid INNER JOIN {files} f ON i.fid = f.fid\n            WHERE n.status = 1 AND n.type = 'image' AND t.tid = %d AND f.filename = '%s'\n            ORDER BY n.sticky DESC, f.filepath";
          $args[] = IMAGE_ORIGINAL;
          break;
        case IMAGE_GALLERY_SORT_TITLE:
          $query .= 'ORDER BY n.sticky DESC, n.title ASC';
          break;
      }
      $result = pager_query(db_rewrite_sql($query), variable_get('image_images_per_page', 6), 0, db_rewrite_sql($count_query), $args);
      while ($node = db_fetch_object($result)) {
        $images[] = node_load(array(
          'nid' => $node->nid,
        ));
      }
    }
    $gallery = taxonomy_get_term($tid);
    $parents = taxonomy_get_parents($tid);
    foreach ($parents as $parent) {
      $breadcrumb[] = array(
        'path' => 'image/tid/' . $parent->tid,
        'title' => $parent->name,
      );
    }
    $breadcrumb[] = array(
      'path' => 'image',
      'title' => t('Image galleries'),
    );
    $breadcrumb = array_reverse($breadcrumb);
    drupal_set_title(check_plain($gallery->name));
  }
  $breadcrumb[] = array(
    'path' => $_GET['q'],
  );
  menu_set_location($breadcrumb);
  $content = theme('image_gallery', $galleries, $images);
  if ($view) {
    $content .= views_build_view('embed', $view, array(
      $tid,
    ), $view->use_pager, $view->nodes_per_page, 0, 0);
  }
  return $content;
}