You are here

function node_gallery_api_get_item_nids in Node Gallery 7

Get all item NIDs for a Gallery

6 calls to node_gallery_api_get_item_nids()
node_gallery_api_entity_delete in ./node_gallery_api.module
Implements hook_entity_delete().
node_gallery_api_entity_insert in ./node_gallery_api.module
Implements hook_entity_insert().
node_gallery_api_get_items in ./node_gallery_api.inc
Retrieves image properties sorted by the image sort view in a gallery, the returned objects are NOT complete nodes.
node_gallery_api_node_update in ./node_gallery_api.module
Implements hook_node_update().
_node_gallery_api_cache_sorted_item_nids in ./node_gallery_api.inc
Builds the caches for a gallery and saves them to the db.

... See full list

File

./node_gallery_api.inc, line 726
Node Gallery API function

Code

function node_gallery_api_get_item_nids($ngid, $sorted = TRUE, $filtered = TRUE, $fallback = FALSE, $reset = FALSE) {
  static $nids = array();
  $cache_keys[] = 'node_gallery';
  $cache_keys[] = $ngid;
  $cache_keys[] = 'item_nids';
  $cache_keys[] = $sorted ? 'sort-true' : 'sort-false';
  $cache_keys[] = $filtered ? 'filtered-true' : 'filtered-false';
  $cache_keys[] = $fallback ? 'fallback-true' : 'fallback-false';
  $cache_key = implode(':', $cache_keys);
  if (!isset($nids[$cache_key]) || $reset) {
    if (!$reset && ($cache = cache_get($cache_key)) && !empty($cache->data)) {
      $nids[$cache_key] = $cache->data;
    }
    else {
      if (!$fallback) {
        $gallery = node_load($ngid);
        $relationship_type = node_gallery_api_get_relationship_type($gallery->type);
        $view_name = '';
        $view_display = '';
        if (!empty($relationship_type->settings['item_view']['view_navigator_item_display'])) {
          list($view_name, $view_display) = explode(':', $relationship_type->settings['item_view']['view_navigator_item_display']);
        }
        if (isset($view_name)) {
          $view = views_get_view($view_name);
          if (isset($view->display[$view_display])) {
            $view
              ->set_display($view_display);
            $view
              ->set_arguments(array(
              $gallery->nid,
            ));
            if (!$sorted) {
              $sorts = $view
                ->get_items('sort', $view_display);
              foreach ($sorts as $sort) {
                $view
                  ->set_item($view_display, 'sort', $sort['id'], NULL);
              }
            }
            if (!$filtered) {
              $filters = $view
                ->get_items('filter', $view_display);
              foreach ($filters as $filter) {
                $view
                  ->set_item($view_display, 'filter', $filter['id'], NULL);
              }
            }
            $view
              ->build();
            if (isset($view->field['nid'])) {
              $query = $view->build_info['query'];
              $query_field_name = $view->field['nid']->field_alias;
            }
            else {
              $message = t("Found the image sort view, but the 'Node: Nid' field wasn't found.", array());
            }
          }
        }
      }
      if (isset($message) || $fallback || !isset($query)) {
        $query_field_name = 'nid';
        $query = db_select('node_gallery_relationship', 'ng')
          ->fields('ng', array(
          'nid',
        ));
        $query
          ->join('node', 'n', 'ng.nid = n.nid');

        // Use our default sql.
        if (isset($message)) {
          if (user_access('administer node gallery')) {
            drupal_set_message($message, 'warning');
          }
          watchdog('node_gallery', $message, NULL, WATCHDOG_WARNING);
        }
        if ($filtered) {
          $query
            ->condition('n.status', 1);
        }
        if ($sorted) {
          $query
            ->orderBy('ng.weight', 'ASC');
          $query
            ->orderBy('n.nid', 'ASC');
        }
        $query
          ->condition('ng.ngid', $ngid);
      }
      $result = $query
        ->execute();
      $nids[$cache_key] = array();
      foreach ($result as $r) {
        $nids[$cache_key][] = $r->{$query_field_name};
      }
      cache_set($cache_key, $nids[$cache_key], 'cache', CACHE_TEMPORARY);
    }
  }
  return $nids[$cache_key];
}