You are here

class ctools_context_required in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/context.inc \ctools_context_required

Used to create a method of comparing if a list of contexts match a required context type.

Hierarchy

Expanded class hierarchy of ctools_context_required

1 string reference to 'ctools_context_required'
ctools_context_optional::ctools_context_optional in includes/context.inc

File

includes/context.inc, line 93
Contains code related to the ctools system of 'context'.

View source
class ctools_context_required {
  var $keywords = '';

  /**
   * If set, the title will be used in the selector to identify
   * the context. This is very useful when multiple contexts
   * are required to inform the user will be used for what.
   */
  var $title = NULL;

  /**
   * Test to see if this context is required.
   */
  var $required = TRUE;

  /**
   *
   * @param $title
   *   The first parameter should be the 'title' of the context for use
   *   in UYI selectors when multiple contexts qualify.
   * @param ...
   *   One or more keywords to use for matching which contexts are allowed.
   */
  function ctools_context_required($title) {
    $args = func_get_args();
    $this->title = array_shift($args);

    // If we were given restrictions at the end, store them.
    if (count($args) > 1 && is_array(end($args))) {
      $this->restrictions = array_pop($args);
    }
    if (count($args) == 1) {
      $args = array_shift($args);
    }
    $this->keywords = $args;
  }
  function filter($contexts) {
    $result = array();

    // See which of these contexts are valid
    foreach ((array) $contexts as $cid => $context) {
      if ($context
        ->is_type($this->keywords)) {

        // Compare to see if our contexts were met.
        if (!empty($this->restrictions) && !empty($context->restrictions)) {
          foreach ($this->restrictions as $key => $values) {

            // If we have a restriction, the context must either not have that
            // restriction listed, which means we simply don't know what it is,
            // or there must be an intersection of the restricted values on
            // both sides.
            if (!is_array($values)) {
              $values = array(
                $values,
              );
            }
            if (!empty($context->restrictions[$key]) && !array_intersect($values, $context->restrictions[$key])) {
              continue 2;
            }
          }
        }
        $result[$cid] = $context;
      }
    }
    return $result;
  }
  function select($contexts, $context) {
    if (!is_array($contexts)) {
      $contexts = array(
        $contexts,
      );
    }

    // Due to a bug, some contexts got recorded with an id of 0. This will
    // convert them to the correct ID allowing for some earlier panels
    // to continue to work.
    if (!empty($context) && $context[strlen($context) - 1] === '0') {
      $context[strlen($context) - 1] = 1;
    }
    if (empty($context) || empty($contexts[$context])) {
      return FALSE;
    }
    return $contexts[$context];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ctools_context_required::$keywords property
ctools_context_required::$required property Test to see if this context is required. 1
ctools_context_required::$title property If set, the title will be used in the selector to identify the context. This is very useful when multiple contexts are required to inform the user will be used for what.
ctools_context_required::ctools_context_required function
ctools_context_required::filter function 1
ctools_context_required::select function 1