You are here

function ctools_term_has_parent_ctools_access_check in Chaos Tool Suite (ctools) 7

Check for access.

1 string reference to 'ctools_term_has_parent_ctools_access_check'
term_has_parent.inc in plugins/access/term_has_parent.inc
Plugin to provide access control based upon a parent term.

File

plugins/access/term_has_parent.inc, line 102
Plugin to provide access control based upon a parent term.

Code

function ctools_term_has_parent_ctools_access_check($conf, $context) {

  // As far as I know there should always be a context at this point, but this
  // is safe.
  if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
    return FALSE;
  }

  // Get the $vid.
  if (!isset($conf['vid'])) {
    return FALSE;
  }
  $vid = $conf['vid'];

  // we'll start looking up the hierarchy from our context term id.
  $current_term = $context->data->tid;
  $term = '';

  // Scan up the tree.
  while (TRUE) {

    // Select parent as term_parent to avoid PHP5 complications with the parent keyword.
    // @todo: Find a way to reduce the number of queries required for really deep hierarchies.
    $term = db_query("SELECT parent AS term_parent, tid AS tid FROM {taxonomy_term_hierarchy} th WHERE th.tid = :tid", array(
      ':tid' => $current_term,
    ))
      ->fetchObject();

    // If no term is found, get out of the loop.
    if (!$term || empty($term->tid)) {
      break;
    }

    // Check the term selected, if the user asked it to.
    if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) {
      return TRUE;
    }

    // Did we find the parent TID we were looking for?
    if (isset($conf['vid_' . $vid][$term->tid])) {

      // YES, we're done!
      return TRUE;
    }

    // Nope, we didn't find it.
    // If this is the top of the hierarchy, stop scanning.
    if ($term->term_parent == 0) {
      break;
    }

    // Update the parent, and keep scanning.
    $current_term = $term->term_parent;
  }
  return FALSE;
}