You are here

function smartqueue_taxonomy_nodequeue_subqueues in Nodequeue 6.2

Same name and namespace in other branches
  1. 5.2 smartqueue.module \smartqueue_taxonomy_nodequeue_subqueues()
  2. 7.3 smartqueue.module \smartqueue_taxonomy_nodequeue_subqueues()
  3. 7.2 modules/smartqueue/smartqueue.module \smartqueue_taxonomy_nodequeue_subqueues()

Implementation of hook_nodequeue_subqueues()

File

./smartqueue.module, line 87

Code

function smartqueue_taxonomy_nodequeue_subqueues(&$queue, $node) {
  foreach (explode('-', $queue->reference) as $vid) {
    $vids[$vid] = array();
  }
  foreach ($node->taxonomy as $key => $value) {

    // This unfortunate scar is needed because $node->taxonomy looks different when saving a node versus loading.
    if (!is_object($value)) {

      // $node comes from a node form submission
      foreach ((array) $value as $tid) {
        if (isset($vids[$key])) {
          $vids[$key][] = $tid;
        }
      }
    }
    else {

      // $node comes from node_load()
      if (isset($vids[$value->vid])) {
        $vids[$value->vid][] = $value->tid;
      }
    }
  }
  if ($queue->use_parents) {

    // Replace taxonomy IDs by their parents'
    foreach ($vids as $vid => &$tids) {
      $tids = smartqueue_taxonomy_get_parents($tids);
    }
  }

  // Forbid NO terms being set, but allow
  // various non-terms to be set.
  $empty = TRUE;
  foreach ($vids as $vid => $tids) {
    if (!empty($tids)) {
      $empty = FALSE;
    }
    if (!count($vids[$vid])) {
      $vids[$vid][] = 0;
    }
  }
  if ($empty) {
    return;
  }
  $references = smartqueue_build_string(array_filter($vids));

  // Because of how we built this, the last one will always be all zeros. Lose it.

  //array_pop($references);

  // We're returning an array of references for efficiency, but we also have
  // to check to see if the references we've generated exist. If they don't,
  // we have to create them.
  $exists = array();
  $subqueues = nodequeue_load_subqueues_by_reference(array(
    $queue->qid => $references,
  ));
  foreach ($subqueues as $subqueue) {
    $exists[$subqueue->reference] = TRUE;
  }
  foreach ($references as $reference) {
    if (empty($exists[$reference])) {
      nodequeue_add_subqueue($queue, smartqueue_taxonomy_nodequeue_subqueue_title($queue, $reference), $reference);
    }
  }
  return $references;
}