<?php

/**
 * @file
 * Contains code related to the ctools system of 'context'.
 *
 * Context, originally from Panels, is a method of packaging objects into
 * a more generic bundle and providing a plugin system so that a UI can
 * take advantage of them. The idea is that the context objects
 * represent 'the context' that a given operation (usually a page view)
 * is operating in or on.
 *
 * For example, when viewing a page, the 'context' is a node object. When
 * viewing a user, the 'context' is a user object. Contexts can also
 * have related contexts. For example, when viewing a 'node' you may need
 * to know something about the node author. Therefore, the node author
 * is a related context.
 */

/**
 * The context object is largely a wrapper around some other object, with
 * an interface to finding out what is contained and getting to both
 * the object and information about the object.
 *
 * Each context object has its own information, but some things are very
 * common, such as titles, data, keywords, etc. In particulare, the 'type'
 * of the context is important.
 */
#[\AllowDynamicProperties]
class ctools_context {
  /**
   * @var string|array
   *   A string naming this specific context type. The values 'any' and 'none'
   *   are special:
   *    - 'any': used in is_type() to match any other type.
   *    - 'none': used to signal the type is not defined.
   */
  public $type;

  /**
   * @var mixed
   *   The data payload for this context object.
   */
  public $data;

  /**
   * @var string
   *   The title of this object.
   */
  public $title;

  /**
   * @var string
   *   The title of the page if this object exists
   */
  public $page_title;

  /**
   * @var string
   *   The identifier (in the UI) of this object.
   */
  public $identifier;

  /**
   * @var
   */
  public $argument;

  /**
   * @var string
   */
  public $keyword;

  /**
   * @var
   */
  public $original_argument;

  /**
   * @var
   */
  public $id;

  /**
   * @var
   */
  public $plugin;

  /**
   * @var array
   */
  public $restrictions;

  /**
   * @var bool
   */
  public $empty;

  /**
   * The ctools_context constructor.
   *
   * @param string $type
   *   The type name of this context. Should be unique. Use the machine_name
   *   conventions: lowercase, short, underscores and no spaces.
   * @param mixed $data
   *   The data payload, if required for this context.
   */
  public function __construct($type = 'none', $data = NULL) {
    $this->type = $type;
    $this->data = $data;
    $this->title = t('Unknown context');
    $this->page_title = '';
    $this->identifier = '';
    $this->keyword = '';
    $this->restrictions = array();
    $this->empty = FALSE;
    // Other vars are NULL.
  }

  /**
   * Determine whether this object is of type @var $type .
   *
   * Both the internal value ($this->type) and the supplied value ($type) can
   * be a string or an array of strings, and if one or both are arrays the match
   * succeeds if at least one common element is found.
   *
   * Type names
   *
   * @param string|array $type
   *   'type' can be:
   *    - 'any' to match all types (this is true of the internal value too).
   *    - an array of type name strings, when more than one type is acceptable.
   *
   * @return bool
   *   True if the type matches, False otherwise.
   */
  public function is_type($type) {
    if ($type === 'any' || $this->type === 'any') {
      return TRUE;
    }

    $a = is_array($type) ? $type : array($type);
    $b = is_array($this->type) ? $this->type : array($this->type);
    return (bool) array_intersect($a, $b);
  }

  /**
   * Return the argument.
   *
   * @return mixed
   *   The value of $argument.
   */
  public function get_argument() {
    return $this->argument;
  }

  /**
   * Return the value of argument (or arg) variable as it was passed in.
   *
   * For example see ctools_plugin_load_function() and ctools_terms_context().
   *
   * @return mixed
   *   The original arg value.
   */
  public function get_original_argument() {
    if (!is_null($this->original_argument)) {
      return $this->original_argument;
    }
    return $this->argument;
  }

  /**
   * Return the keyword.
   *
   * @return mixed
   *   The value of $keyword.
   */
  public function get_keyword() {
    return $this->keyword;
  }

  /**
   * Return the identifier.
   *
   * @return mixed
   *   The value of $identifier.
   */
  public function get_identifier() {
    return $this->identifier;
  }

  /**
   * Return the title.
   *
   * @return mixed
   *   The value of $title.
   */
  public function get_title() {
    return $this->title;
  }

  /**
   * Return the page title.
   *
   * @return mixed
   *   The value of $page_title.
   */
  public function get_page_title() {
    return $this->page_title;
  }

}

/**
 * Used to create a method of comparing if a list of contexts
 * match a required context type.
 */
#[\AllowDynamicProperties]
class ctools_context_required {
  /**
   * @var array
   *   Keyword strings associated with the context.
   */
  public $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.
   */
  public $title;

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

  /**
   * If TRUE, skip the check in ctools_context_required::select()
   * for contexts whose names may have changed.
   */
  public $skip_name_check = FALSE;

  /**
   * The ctools_context_required constructor.
   *
   * Note: Constructor accepts a variable number of arguments, with optional
   * type-dependent args at the end of the list and one required argument,
   * the title. Note in particular that skip_name_check MUST be passed in as
   * a boolean (and not, for example, as an integer).
   *
   * @param string $title
   *   The title of the context for use in UI selectors when multiple contexts
   *   qualify.
   * @param string $keywords
   *   One or more keywords to use for matching which contexts are allowed.
   * @param array $restrictions
   *   Array of context restrictions.
   * @param bool $skip_name_check
   *   If True, skip the check in select() for contexts whose names may have
   *   changed.
   */
  public function __construct($title) {
    // If it was possible, using variadic syntax this should be:
    // __construct($title, string ...$keywords, array $restrictions = NULL, bool $skip = NULL)
    // but that form isn't allowed.
    $args = func_get_args();
    $this->title = array_shift($args);

    // If we have a boolean value at the end for $skip_name_check, store it.
    if (is_bool(end($args))) {
      $this->skip_name_check = array_pop($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;
  }

  /**
   * Filter the contexts to determine which apply in the current environment.
   *
   * A context passes the filter if:
   *  - the context matches 'type' of the required keywords (uses
   *    ctools_context::is_type(), so includes 'any' matches, etc).
   *  - AND if restrictions are present, there are some common elements between
   *    the requirement and the context.
   *
   * @param array $contexts
   *   An array of ctools_context objects (or something which will cast to an
   *   array of them). The contexts to apply the filter on.
   *
   * @return array
   *   An array of context objects, keyed with the same keys used for $contexts,
   *   which pass the filter.
   *
   * @see ctools_context::is_type()
   */
  public function filter($contexts) {
    $result = array();

    /**
     * See which of these contexts are valid.
     * @var ctools_context $context
     */
    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])
            ) {
              // Break out to check next context; this one fails the filter.
              continue 2;
            }
          }
        }
        // This context passes the filter.
        $result[$cid] = $context;
      }
    }

    return $result;
  }

  /**
   * Select one context from the list of contexts, accounting for changed IDs.
   *
   * Fundamentally, this returns $contexts[$context] or FALSE if that does not
   * exist. Additional logic accounts for changes in context names and dealing
   * with a $contexts parameter that is not an array.
   *
   * If we had requested a $context but that $context doesn't exist in our
   * context list, there is a good chance that what happened is the context
   * IDs changed. Look for another context that satisfies our requirements,
   * unless $skip_name_check is set.
   *
   * @param ctools_context|array $contexts
   *   A context, or an array of ctools_context.
   * @param string $context
   *   A context ID.
   *
   * @return bool|ctools_context
   *   The matching ctools_context, or False if no such context was found.
   */
  public function select($contexts, $context) {
    // Easier to deal with a standalone object as a 1-element array of objects.
    if (!is_array($contexts)) {
      if (is_object($contexts) && $contexts instanceof ctools_context) {
        $contexts = array($contexts->id => $contexts);
      }
      else {
        $contexts = array($contexts);
      }
    }

    // If we had requested a $context but that $context doesn't exist in our
    // context list, there is a good chance that what happened is the context
    // IDs changed. Check for another context that satisfies our requirements.
    if (!$this->skip_name_check
      && !empty($context) && !isset($contexts[$context])
    ) {
      $choices = $this->filter($contexts);

      // If we got a hit, take the first one that matches.
      if ($choices) {
        $keys = array_keys($choices);
        $context = reset($keys);
      }
    }

    if (empty($context) || empty($contexts[$context])) {
      return FALSE;
    }
    return $contexts[$context];
  }

}

/**
 * Used to compare to see if a list of contexts match an optional context. This
 * can produce empty contexts to use as placeholders.
 */
#[\AllowDynamicProperties]
class ctools_context_optional extends ctools_context_required {

  /**
   * {@inheritdoc}
   */
  public $required = FALSE;

  /**
   * Add the 'empty' context to the existing set.
   *
   * @param array &$contexts
   *   An array of ctools_context objects.
   */
  public function add_empty(&$contexts) {
    $context = new ctools_context('any');
    $context->title = t('No context');
    $context->identifier = t('No context');
    $contexts['empty'] = $context;
  }

  /**
   * Filter the contexts to determine which apply in the current environment.
   *
   * As for ctools_context_required, but we add the empty context to those
   * passed in so the check is optional (i.e. if nothing else matches, the
   * empty context will, and so there will always be at least one matched).
   *
   * @param array $contexts
   *   An array of ctools_context objects (or something which will cast to an
   *   array of them). The contexts to apply the filter on.
   *
   * @return array
   *   An array of context objects, keyed with the same keys used for $contexts,
   *   which pass the filter.
   *
   * @see ctools_context::is_type()
   */
  public function filter($contexts) {
    /**
     * @todo We are assuming here that $contexts is actually an array, whereas
     * ctools_context_required::filter only requires $contexts is convertible
     * to an array.
     */
    $this->add_empty($contexts);
    return parent::filter($contexts);
  }

  /**
   * Select and return one context from the list of applicable contexts.
   *
   * Fundamentally, this returns $contexts[$context] or the empty context if
   * that does not exist.
   *
   * @param array $contexts
   *   The applicable contexts to check.
   * @param string $context
   *   The context id to check for.
   *
   * @return bool|ctools_context
   *   The matching ctools_context, or False if no such context was found.
   *
   * @see ctools_context_required::select()
   */
  public function select($contexts, $context) {
    /**
     * @todo We are assuming here that $contexts is actually an array, whereas
     * ctools_context_required::select permits ctools_context objects as well.
     */
    $this->add_empty($contexts);
    if (empty($context)) {
      return $contexts['empty'];
    }

    $result = parent::select($contexts, $context);

    // Don't flip out if it can't find the context; this is optional, put
    // in an empty.
    if ($result === FALSE) {
      $result = $contexts['empty'];
    }
    return $result;
  }

}

/**
 * Return a keyed array of context that match the given 'required context'
 * filters.
 *
 * Functions or systems that require contexts of a particular type provide a
 * ctools_context_required or ctools_context_optional object. This function
 * examines that object and an array of contexts to determine which contexts
 * match the filter.
 *
 * Since multiple contexts can be required, this function will accept either
 * an array of all required contexts, or just a single required context object.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param array|ctools_context_required|ctools_context_optional $required
 *   A *_required or *_optional object, or an array of such objects, which
 *   define the selection condition.
 *
 * @return array
 *   A keyed array of contexts that match the filter.
 */
function ctools_context_filter($contexts, $required) {
  if (is_array($required)) {
    $result = array();
    foreach ($required as $item) {
      $result = array_merge($result, _ctools_context_filter($contexts, $item));
    }
    return $result;
  }

  return _ctools_context_filter($contexts, $required);
}

/**
 * Helper function for ctools_context_filter().
 *
 * Used to transform the required context during the merge into the final array.
 *
 * @internal This function DOES NOT form part of the CTools API.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param ctools_context_required|ctools_context_optional $required
 *   A ctools_context_required or ctools_context_optional object, although if
 *   given something else will return an empty array.
 *
 * @return array
 */
function _ctools_context_filter($contexts, $required) {
  $result = array();

  if (is_object($required)) {
    $result = $required->filter($contexts);
  }

  return $result;
}

/**
 * Create a select box to choose possible contexts.
 *
 * This only creates a selector if there is actually a choice; if there
 * is only one possible context, that one is silently assigned.
 *
 * If an array of required contexts is provided, one selector will be
 * provided for each context.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param array|ctools_context_required|ctools_context_optional $required
 *   The required context object or array of objects.
 * @param array|string $default
 *   The default value for the select object, suitable for a #default_value
 *   render key. Where $required is an array, this is an array keyed by the
 *   same key values as $required for all keys where an empty string is not a
 *   suitable default. Otherwise it is just the default value.
 *
 * @return array
 *   A form element, or NULL if there are no contexts that satisfy the
 *   requirements.
 */
function ctools_context_selector($contexts, $required, $default) {
  if (is_array($required)) {
    $result = array('#tree' => TRUE);
    $count = 1;
    foreach ($required as $id => $item) {
      $result[] = _ctools_context_selector(
        $contexts, $item, isset($default[$id]) ? $default[$id] : '', $count++
      );
    }
    return $result;
  }

  return _ctools_context_selector($contexts, $required, $default);
}

/**
 * Helper function for ctools_context_selector().
 *
 * @internal This function DOES NOT form part of the CTools API. Use the API
 * function ctools_context_selector() instead.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param ctools_context_required|ctools_context_optional $required
 *   The required context object.
 * @param $default
 *   The default value for the select object, suitable for a #default_value
 *   render key.
 * @param int $num
 *   If supplied and non-zero, the title of the select form element will be
 *   "Context $num", otherwise it will be "Context".
 *
 * @return array
 *   A form element, or NULL if there are no contexts that satisfy the
 *   requirements.
 */
function _ctools_context_selector($contexts, $required, $default, $num = 0) {
  $filtered = ctools_context_filter($contexts, $required);
  $count = count($filtered);

  $form = array();

  if ($count >= 1) {
    // If there's more than one to choose from, create a select widget.
    foreach ($filtered as $cid => $context) {
      $options[$cid] = $context->get_identifier();
    }
    if (!empty($required->title)) {
      $title = $required->title;
    }
    else {
      $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
    }

    $form = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => $title,
      '#default_value' => $default,
    );
  }

  return $form;
}

/**
 * Are there enough contexts for a plugin?
 *
 * Some plugins can have a 'required contexts' item which can either
 * be a context requirement object or an array of them. When contexts
 * are required, items that do not have enough contexts should not
 * appear. This tests an item to see if it has enough contexts
 * to actually appear.
 *
 * @param $contexts
 *   A keyed array of all available contexts.
 * @param $required
 *   The required context object or array of objects.
 *
 * @return bool
 *   True if there are enough contexts, otherwise False.
 */
function ctools_context_match_requirements($contexts, $required) {
  if (!is_array($required)) {
    $required = array($required);
  }

  // Get the keys to avoid bugs in PHP 5.0.8 with keys and loops.
  // And use it to remove optional contexts.
  $keys = array_keys($required);
  foreach ($keys as $key) {
    if (empty($required[$key]->required)) {
      unset($required[$key]);
    }
  }

  $count = count($required);
  return (count(ctools_context_filter($contexts, $required)) >= $count);
}

/**
 * Create a select box to choose possible contexts.
 *
 * This only creates a selector if there is actually a choice; if there
 * is only one possible context, that one is silently assigned.
 *
 * If an array of required contexts is provided, one selector will be
 * provided for each context.
 *
 * @param $contexts
 *   A keyed array of all available contexts.
 * @param $required
 *   The required context object or array of objects.
 * @param array|string $default
 *   The default value for the select object, suitable for a #default_value
 *   render key. Where $required is an array, this is an array keyed by the
 *   same key values as $required for all keys where an empty string is not a
 *   suitable default. Otherwise it is just the default value.
 *
 * @return array
 *   A form element, or NULL if there are no contexts that satisfy the
 *   requirements.
 */
function ctools_context_converter_selector($contexts, $required, $default) {
  if (is_array($required)) {
    $result = array('#tree' => TRUE);
    $count = 1;
    foreach ($required as $id => $dependency) {
      $default_id = isset($default[$id]) ? $default[$id] : '';
      $result[] = _ctools_context_converter_selector(
        $contexts, $dependency, $default_id, $count++
      );
    }
    return $result;
  }

  return _ctools_context_converter_selector($contexts, $required, $default);
}

/**
 * Helper function for ctools_context_converter_selector().
 *
 * @internal This function DOES NOT form part of the CTools API. Use the API
 * function ctools_context_converter_selector() instead.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param ctools_context $required
 *   The required context object.
 * @param $default
 *   The default value for the select object, suitable for a #default_value
 *   render key.
 * @param int $num
 *   If supplied and non-zero, the title of the select form element will be
 *   "Context $num", otherwise it will be "Context".
 *
 * @return array|null
 *   A form element, or NULL if there are no contexts that satisfy the
 *   requirements.
 */
function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) {
  $filtered = ctools_context_filter($contexts, $required);
  $count = count($filtered);

  if ($count > 1) {
    // If there's more than one to choose from, create a select widget.
    $options = array();
    foreach ($filtered as $cid => $context) {
      if ($context->type === 'any') {
        $options[''] = t('No context');
        continue;
      }
      $key = $context->get_identifier();
      if ($converters = ctools_context_get_converters($cid . '.', $context)) {
        $options[$key] = $converters;
      }
    }
    if (empty($options)) {
      return array(
        '#type' => 'value',
        '#value' => 'any',
      );
    }
    if (!empty($required->title)) {
      $title = $required->title;
    }
    else {
      $title = $num ? t('Context %count', array('%count' => $num)) : t('Context');
    }

    return array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => $title,
      '#description' => t('Please choose which context and how you would like it converted.'),
      '#default_value' => $default,
    );
  }
  else {
    // Not enough choices to need a selector, so don't make one.
    return NULL;
  }
}

/**
 * Get a list of converters available for a given context.
 *
 * @param string $cid
 *   A context ID.
 * @param ctools_context $context
 *   The context for which converters are needed.
 *
 * @return array
 *   A list of context converters.
 */
function ctools_context_get_converters($cid, $context) {
  if (empty($context->plugin)) {
    return array();
  }

  return _ctools_context_get_converters($cid, $context->plugin);
}

/**
 * Get a list of converters available for a given context.
 *
 * @internal This function DOES NOT form part of the CTools API. Use the API
 * function ctools_context_get_converters() instead.
 *
 * @param string $id
 *   A context ID.
 * @param string $plugin_name
 *   The name of the context plugin.
 *
 * @return array
 *   A list of context converters.
 */
function _ctools_context_get_converters($id, $plugin_name) {
  $plugin = ctools_get_context($plugin_name);
  if (empty($plugin['convert list'])) {
    return array();
  }

  $converters = array();
  if (is_array($plugin['convert list'])) {
    $converters = $plugin['convert list'];
  }
  elseif ($function = ctools_plugin_get_function($plugin, 'convert list')) {
    $converters = (array) $function($plugin);
  }

  foreach (module_implements('ctools_context_convert_list_alter') as $module) {
    $function = $module . '_ctools_context_convert_list_alter';
    $function($plugin, $converters);
  }

  // Now, change them all to include the plugin:
  $return = array();
  foreach ($converters as $key => $title) {
    $return[$id . $key] = $title;
  }

  natcasesort($return);
  return $return;
}

/**
 * Get a list of all contexts converters available.
 *
 * For all contexts returned by ctools_get_contexts(), return the converter
 * for all contexts that have one.
 *
 * @return array
 *   A list of context converters, keyed by the title of the converter.
 */
function ctools_context_get_all_converters() {
  $contexts = ctools_get_contexts();
  $converters = array();
  foreach ($contexts as $name => $context) {
    if (empty($context['no required context ui'])) {
      $context_converters = _ctools_context_get_converters($name . '.', $name);
      if ($context_converters) {
        $converters[$context['title']] = $context_converters;
      }
    }
  }

  return $converters;
}

/**
 * Let the context convert an argument based upon the converter that was given.
 *
 * @param ctools_context $context
 *   The context object.
 * @param string $converter
 *   The type of converter to use, which should be a string provided by the
 *   converter list function.
 * @param array $converter_options
 *   An array of options to pass on to the generation function. For contexts
 *   that use token module, of particular use is 'sanitize' => FALSE which can
 *   get raw tokens. This should ONLY be used in values that will later be
 *   treated as unsafe user input since these values are by themselves unsafe.
 *   It is particularly useful to get raw values from Field API.
 *
 * @return string|null
 */
function ctools_context_convert_context($context, $converter, $converter_options = array()) {
  // Contexts without plugins might be optional placeholders.
  if (empty($context->plugin)) {
    return NULL;
  }

  $value = $context->argument;
  $plugin = ctools_get_context($context->plugin);
  if ($function = ctools_plugin_get_function($plugin, 'convert')) {
    $value = $function($context, $converter, $converter_options);
  }

  foreach (module_implements('ctools_context_converter_alter') as $module) {
    $function = $module . '_ctools_context_converter_alter';
    $function($context, $converter, $value, $converter_options);
  }

  return $value;
}

/**
 * Choose a context or contexts based upon the selection made via
 * ctools_context_filter.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param array|ctools_context_required $required
 *   The required context object(s) provided by the plugin.
 * @param $context
 *   The selection made using ctools_context_selector().
 *
 * @return ctools_context|array|false
 *   Returns FALSE if $required is not an object, or array of objects, or
 *   the value of $required->select() for the context, or an array of those (if
 *   passed an array in $required).
 */
function ctools_context_select($contexts, $required, $context) {
  if (is_array($required)) {

    /**
     * @var array $required
     *   Array of required context objects.
     * @var ctools_context_required $item
     *   A required context object.
     */
    $result = array();
    foreach ($required as $id => $item) {
      // @todo What's the difference between the following and "empty($item)" ?
      if (empty($required[$id])) {
        continue;
      }

      if (($result[] = _ctools_context_select($contexts, $item, $context[$id])) === FALSE) {
        return FALSE;
      }
    }
    return $result;
  }

  return _ctools_context_select($contexts, $required, $context);
}

/**
 * Helper function for calling the required context object's selection function.
 *
 * This function DOES NOT form part of the CTools API.
 *
 * @param array $contexts
 *   A keyed array of all available contexts.
 * @param ctools_context_required $required
 *   The required context object provided by the plugin.
 * @param $context
 *   The selection made using ctools_context_selector().
 *
 * @return ctools_context|bool
 *   FALSE if the $required is not an object. A ctools_context object if one
 *   matched.
 */
function _ctools_context_select($contexts, $required, $context) {
  if (!is_object($required)) {
    return FALSE;
  }

  return $required->select($contexts, $context);
}

/**
 * Create a new context object.
 *
 * @param string $type
 *   The type of context to create; this loads a plugin.
 * @param mixed $data
 *   The data to put into the context.
 * @param $conf
 *   A configuration structure if this context was created via UI.
 *
 * @return ctools_context
 *   A $context or NULL if one could not be created.
 */
function ctools_context_create($type, $data = NULL, $conf = FALSE) {
  ctools_include('plugins');
  $plugin = ctools_get_context($type);

  if ($function = ctools_plugin_get_function($plugin, 'context')) {
    return $function(FALSE, $data, $conf, $plugin);
  }
}

/**
 * Create an empty context object.
 *
 * Empty context objects are primarily used as placeholders in the UI where
 * the actual contents of a context object may not be known. It may have
 * additional text embedded to give the user clues as to how the context
 * is used.
 *
 * @param $type
 *   The type of context to create; this loads a plugin.
 *
 * @return ctools_context
 *   A $context or NULL if one could not be created.
 */
function ctools_context_create_empty($type) {
  $plugin = ctools_get_context($type);
  if ($function = ctools_plugin_get_function($plugin, 'context')) {
    $context = $function(TRUE, NULL, FALSE, $plugin);
    if (is_object($context)) {
      $context->empty = TRUE;
    }

    return $context;
  }
}

/**
 * Perform keyword and context substitutions.
 *
 * @param string $string
 *   The string in which to replace keywords.
 * @param array $keywords
 *   Array of keyword-replacement pairs.
 * @param array $contexts
 *
 * @param array $converter_options
 *   Options to pass on to ctools_context_convert_context(), defaults to an
 *   empty array.
 *
 * @return string
 *   The returned string, with substitutions performed.
 */
function ctools_context_keyword_substitute($string, $keywords, $contexts, array $converter_options = array()) {
  // Ensure a default keyword exists:
  $keywords['%%'] = '%';

  // Match contexts to the base keywords:
  $context_keywords = array();
  foreach ($contexts as $context) {
    if (isset($context->keyword)) {
      $context_keywords[$context->keyword] = $context;
    }
  }

  // Look for context matches we we only have to convert known matches.
  $matches = array();
  if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) {
    foreach ($matches[1] as $keyword) {
      // Ignore anything it finds with %%.
      if ($keyword[0] == '%') {
        continue;
      }

      // If the keyword is already set by something passed in, don't try to
      // overwrite it.
      if (array_key_exists('%' . $keyword, $keywords)) {
        continue;
      }

      // Figure out our keyword and converter, if specified.
      if (strpos($keyword, ':')) {
        list($context, $converter) = explode(':', $keyword, 2);
      }
      else {
        $context = $keyword;
        if (isset($context_keywords[$keyword])) {
          $plugin = ctools_get_context($context_keywords[$context]->plugin);

          // Fall back to a default converter, if specified.
          if ($plugin && !empty($plugin['convert default'])) {
            $converter = $plugin['convert default'];
          }
        }
      }

      if (!isset($context_keywords[$context])) {
        $keywords['%' . $keyword] = '%' . $keyword;
      }
      else {
        if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
          $keywords['%' . $keyword] = '';
        }
        else {
          if (!empty($converter)) {
            $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options);
          }
          else {
            $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
          }
        }
      }
    }
  }
  return strtr($string, $keywords);
}

/**
 * Determine a unique context ID for a context.
 *
 * Often contexts of many different types will be placed into a list. This
 * ensures that even though contexts of multiple types may share IDs, they
 * are unique in the final list.
 */
function ctools_context_id($context, $type = 'context') {
  // If not set, FALSE or empty.
  if (!isset($context['id']) || !$context['id']) {
    $context['id'] = 1;
  }

  // @todo is '' the appropriate default value?
  $name = isset($context['name']) ? $context['name'] : '';

  return $type . '_' . $name . '_' . $context['id'];
}

/**
 * Get the next id available given a list of already existing objects.
 *
 * This finds the next id available for the named object.
 *
 * @param array $objects
 *   A list of context descriptor objects, i.e, arguments, relationships,
 *   contexts, etc.
 * @param string $name
 *   The name being used.
 *
 * @return int
 *   The next integer id available.
 */
function ctools_context_next_id($objects, $name) {
  $id = 0;
  // Figure out which instance of this argument we're creating.
  if (!$objects) {
    return $id + 1;
  }

  foreach ($objects as $object) {
    if (isset($object['name']) && $object['name'] === $name) {
      if (isset($object['id']) && $object['id'] > $id) {
        $id = (int) $object['id'];
      }
      // @todo If obj has no 'id', should we increment local id? $id = $id + 1;
    }
  }

  return $id + 1;
}

// ---------------------------------------------------------------------------
// Functions related to contexts from arguments.
/**
 * Fetch metadata for a specific argument plugin.
 *
 * @param $argument
 *   Name of an argument plugin.
 *
 * @return array
 *   An array with information about the requested argument plugin.
 */

function ctools_get_argument($argument) {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'arguments', $argument);
}

/**
 * Fetch metadata for all argument plugins.
 *
 * @return array
 *   An array of arrays with information about all available argument plugins.
 */
function ctools_get_arguments() {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'arguments');
}

/**
 * Get a context from an argument.
 *
 * @param $argument
 *   The configuration of an argument. It must contain the following data:
 *   - name: The name of the argument plugin being used.
 *   - argument_settings: The configuration based upon the plugin forms.
 *   - identifier: The human readable identifier for this argument, usually
 *     defined by the UI.
 *   - keyword: The keyword used for this argument for substitutions.
 *
 * @param $arg
 *   The actual argument received. This is expected to be a string from a URL
 *   but this does not have to be the only source of arguments.
 * @param $empty
 *   If true, the $arg will not be used to load the context. Instead, an empty
 *   placeholder context will be loaded.
 *
 * @return ctools_context
 *   A context object if one can be loaded.
 */
function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) {
  ctools_include('plugins');
  if (empty($argument['name'])) {
    return NULL;
  }

  $function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context');
  if ($function) {
    // Backward compatibility: Merge old style settings into new style:
    if (!empty($argument['settings'])) {
      $argument += $argument['settings'];
      unset($argument['settings']);
    }

    $context = $function($arg, $argument, $empty);

    if (is_object($context)) {
      $context->identifier = $argument['identifier'];
      $context->page_title = isset($argument['title']) ? $argument['title'] : '';
      $context->keyword = $argument['keyword'];
      $context->id = ctools_context_id($argument, 'argument');
      $context->original_argument = $arg;

      if (!empty($context->empty)) {
        $context->placeholder = array(
          'type' => 'argument',
          'conf' => $argument,
        );
      }
    }
    return $context;
  }
}

/**
 * Retrieve a list of empty contexts for all arguments.
 *
 * @param array $arguments
 *
 * @return array
 *
 * @see ctools_context_get_context_from_arguments()
 */
function ctools_context_get_placeholders_from_argument($arguments) {
  $contexts = array();
  foreach ($arguments as $argument) {
    $context = ctools_context_get_context_from_argument($argument, NULL, TRUE);
    if ($context) {
      $contexts[ctools_context_id($argument, 'argument')] = $context;
    }
  }
  return $contexts;
}

/**
 * Load the contexts for a given list of arguments.
 *
 * @param array $arguments
 *   The array of argument definitions.
 * @param array &$contexts
 *   The array of existing contexts. New contexts will be added to this array.
 * @param array $args
 *   The arguments to load.
 *
 * @return bool
 *   TRUE if all is well, FALSE if an argument wants to 404.
 *
 * @see ctools_context_get_context_from_argument()
 */
function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) {
  foreach ($arguments as $argument) {
    // Pull the argument off the list.
    $arg = array_shift($args);
    $id = ctools_context_id($argument, 'argument');

    // For % arguments embedded in the URL, our context is already loaded.
    // There is no need to go and load it again.
    if (empty($contexts[$id])) {
      if ($context = ctools_context_get_context_from_argument($argument, $arg)) {
        $contexts[$id] = $context;
      }
    }
    else {
      $context = $contexts[$id];
    }

    if ((empty($context) || empty($context->data))
      && !empty($argument['default'])
      && $argument['default'] === '404'
    ) {
      return FALSE;
    }
  }
  return TRUE;
}

// ---------------------------------------------------------------------------
// Functions related to contexts from relationships.
/**
 * Fetch plugin metadata for a specific relationship plugin.
 *
 * @param $relationship
 *   Name of a panel content type.
 *
 * @return array
 *   An array with information about the requested relationship.
 *
 * @see ctools_get_relationships()
 */

function ctools_get_relationship($relationship) {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'relationships', $relationship);
}

/**
 * Fetch metadata for all relationship plugins.
 *
 * @return array
 *   An array of arrays with information about all available relationships.
 *
 * @see ctools_get_relationship()
 */
function ctools_get_relationships() {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'relationships');
}

/**
 * Return a context from a relationship.
 *
 * @param array $relationship
 *   The configuration of a relationship. It must contain the following data:
 *   - name: The name of the relationship plugin being used.
 *   - relationship_settings: The configuration based upon the plugin forms.
 *   - identifier: The human readable identifier for this relationship, usually
 *     defined by the UI.
 *   - keyword: The keyword used for this relationship for substitutions.
 *
 * @param ctools_context $source_context
 *   The context this relationship is based upon.
 * @param bool $placeholders
 *   If TRUE, placeholders are acceptable.
 *
 * @return ctools_context|null
 *   A context object if one can be loaded, otherwise NULL.
 *
 * @see ctools_context_get_relevant_relationships()
 * @see ctools_context_get_context_from_relationships()
 */
function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) {
  ctools_include('plugins');
  $function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context');
  if ($function) {
    // Backward compatibility: Merge old style settings into new style:
    if (!empty($relationship['relationship_settings'])) {
      $relationship += $relationship['relationship_settings'];
      unset($relationship['relationship_settings']);
    }

    $context = $function($source_context, $relationship, $placeholders);
    if ($context) {
      $context->identifier = $relationship['identifier'];
      $context->page_title = isset($relationship['title']) ? $relationship['title'] : '';
      $context->keyword = $relationship['keyword'];
      if (!empty($context->empty)) {
        $context->placeholder = array(
          'type' => 'relationship',
          'conf' => $relationship,
        );
      }
      return $context;
    }
  }
  return NULL;
}

/**
 * Fetch all relevant relationships.
 *
 * Relevant relationships are any relationship that can be created based upon
 * the list of existing contexts. For example, the 'node author' relationship
 * is relevant if there is a 'node' context, but makes no sense if there is
 * not one.
 *
 * @param $contexts
 *   An array of contexts used to figure out which relationships are relevant.
 *
 * @return array
 *   An array of relationship keys that are relevant for the given set of
 *   contexts.
 *
 * @see ctools_context_filter()
 * @see ctools_context_get_context_from_relationship()
 * @see ctools_context_get_context_from_relationships()
 */
function ctools_context_get_relevant_relationships($contexts) {
  $relevant = array();
  $relationships = ctools_get_relationships();

  // Go through each relationship.
  foreach ($relationships as $rid => $relationship) {
    // For each relationship, see if there is a context that satisfies it.
    if (empty($relationship['no ui'])
      && ctools_context_filter($contexts, $relationship['required context'])
    ) {
      $relevant[$rid] = $relationship['title'];
    }
  }

  return $relevant;
}

/**
 * Fetch all active relationships.
 *
 * @param $relationships
 *   An keyed array of relationship data including:
 *   - name: name of relationship
 *   - context: context id relationship belongs to. This will be used to
 *     identify which context in the $contexts array to use to create the
 *     relationship context.
 *
 * @param $contexts
 *   A keyed array of contexts used to figure out which relationships
 *   are relevant. New contexts will be added to this.
 *
 * @param $placeholders
 *   If TRUE, placeholders are acceptable.
 *
 * @see ctools_context_get_context_from_relationship()
 * @see ctools_context_get_relevant_relationships()
 */
function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) {
  foreach ($relationships as $rdata) {
    if (!isset($rdata['context'])) {
      continue;
    }

    if (is_array($rdata['context'])) {
      $rcontexts = array();
      foreach ($rdata['context'] as $cid) {
        if (!empty($contexts[$cid])) {
          $rcontexts[] = $contexts[$cid];
        }
      }
    }
    elseif (!empty($contexts[$rdata['context']])) {
      $rcontexts = $contexts[$rdata['context']];
    }

    $cid = ctools_context_id($rdata, 'relationship');
    if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) {
      $contexts[$cid] = $context;
    }
  }
}

// ---------------------------------------------------------------------------
// Functions related to loading contexts from simple context definitions.
/**
 * Fetch metadata on a specific context plugin.
 *
 * @param string $context
 *   Name of a context.
 *
 * @return array
 *   An array with information about the requested panel context.
 */

function ctools_get_context($context) {
  static $gate = array();
  ctools_include('plugins');
  $plugin = ctools_get_plugins('ctools', 'contexts', $context);
  if (empty($gate['context']) && !empty($plugin['superceded by'])) {
    // This gate prevents infinite loops.
    $gate[$context] = TRUE;
    $new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']);
    $gate[$context] = FALSE;

    // If a new plugin was returned, return it. Otherwise fall through and
    // return the original we fetched.
    if ($new_plugin) {
      return $new_plugin;
    }
  }

  return $plugin;
}

/**
 * Fetch metadata for all context plugins.
 *
 * @return array
 *   An array of arrays with information about all available panel contexts.
 */
function ctools_get_contexts() {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'contexts');
}

/**
 * Return a context object from a context definition array.
 *
 * The input $context contains the information needed to identify and invoke
 * the context plugin and create the plugin context from that.
 *
 * @param array $context
 *   The configuration of a context. It must contain the following data:
 *   - name: The name of the context plugin being used.
 *   - context_settings: The configuration based upon the plugin forms.
 *   - identifier: The human readable identifier for this context, usually
 *     defined by the UI.
 *   - keyword: The keyword used for this context for substitutions.
 * @param string $type
 *   This is either 'context' which indicates the context will be loaded
 *   from data in the settings, or 'requiredcontext' which means the
 *   context must be acquired from an external source. This is the method
 *   used to pass pure contexts from one system to another.
 * @param mixed $argument
 *   Optional information passed to the plugin context via the arg defined in
 *   the plugin's "placeholder name" field.
 *
 * @return ctools_context|null
 *   A context object if one can be loaded.
 *
 * @see ctools_get_context()
 * @see ctools_plugin_get_function()
 */
function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) {
  ctools_include('plugins');
  $plugin = ctools_get_context($context['name']);
  $function = ctools_plugin_get_function($plugin, 'context');
  if ($function) {
    // Backward compatibility: Merge old style settings into new style:
    if (!empty($context['context_settings'])) {
      $context += $context['context_settings'];
      unset($context['context_settings']);
    }

    if (isset($argument) && isset($plugin['placeholder name'])) {
      $context[$plugin['placeholder name']] = $argument;
    }

    $return = $function($type == 'requiredcontext', $context, TRUE, $plugin);
    if ($return) {
      $return->identifier = $context['identifier'];
      $return->page_title = isset($context['title']) ? $context['title'] : '';
      $return->keyword = $context['keyword'];

      if (!empty($context->empty)) {
        $context->placeholder = array(
          'type' => 'context',
          'conf' => $context,
        );
      }

      return $return;
    }
  }

  return NULL;
}

/**
 * Retrieve a list of base contexts based upon a simple 'contexts' definition.
 *
 * For required contexts this will always retrieve placeholders.
 *
 * @param $contexts
 *   The list of contexts defined in the UI.
 * @param $type
 *   Either 'context' or 'requiredcontext', which indicates whether the contexts
 *   are loaded from internal data or copied from an external source.
 * @param $placeholders
 *   If True, placeholders are acceptable.
 *
 * @return array
 *   Array of contexts, keyed by context ID.
 */
function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) {
  $return = array();
  foreach ($contexts as $context) {
    $ctext = ctools_context_get_context_from_context($context, $type);
    if ($ctext) {
      if ($placeholders) {
        $ctext->placeholder = TRUE;
      }
      $return[ctools_context_id($context, $type)] = $ctext;
    }
  }
  return $return;
}

/**
 * Match up external contexts to our required contexts.
 *
 * This function is used to create a list of contexts with proper IDs based
 * upon a list of required contexts.
 *
 * These contexts passed in should match the numeric positions of the required
 * contexts. The caller must ensure this has already happened correctly as this
 * function will not detect errors here.
 *
 * @param $required
 *   A list of required contexts as defined by the UI.
 * @param $contexts
 *   A list of matching contexts as passed in from the calling system.
 *
 * @return array
 *   Array of contexts, keyed by context ID.
 */
function ctools_context_match_required_contexts($required, $contexts) {
  $return = array();
  if (!is_array($required)) {
    return $return;
  }

  foreach ($required as $r) {
    $context = clone array_shift($contexts);
    $context->identifier = $r['identifier'];
    $context->page_title = isset($r['title']) ? $r['title'] : '';
    $context->keyword = $r['keyword'];
    $return[ctools_context_id($r, 'requiredcontext')] = $context;
  }

  return $return;
}

/**
 * Load a full array of contexts for an object.
 *
 * Not all of the types need to be supported by this object.
 *
 * This function is not used to load contexts from external data, but may be
 * used to load internal contexts and relationships. Otherwise it can also be
 * used to generate a full set of placeholders for UI purposes.
 *
 * @param object $object
 *   An object that contains some or all of the following variables:
 *
 *   - requiredcontexts: A list of UI configured contexts that are required
 *     from an external source. Since these require external data, they will
 *     only be added if $placeholders is set to TRUE, and empty contexts will
 *     be created.
 *   - arguments: A list of UI configured arguments that will create contexts.
 *     As these require external data, they will only be added if $placeholders
 *     is set to TRUE.
 *   - contexts: A list of UI configured contexts that have no external source,
 *     and are essentially hardcoded. For example, these might configure a
 *     particular node or a particular taxonomy term.
 *   - relationships: A list of UI configured contexts to be derived from other
 *     contexts that already exist from other sources. For example, these might
 *     be used to get a user object from a node via the node author
 *     relationship.
 * @param bool $placeholders
 *   If True, this will generate placeholder objects for any types this function
 *   cannot load.
 * @param array $contexts
 *   An array of pre-existing contexts that will be part of the return value.
 *
 * @return array
 *   Merged output of all results of ctools_context_get_context_from_contexts().
 */
function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) {
  if (!empty($object->base_contexts)) {
    $contexts += $object->base_contexts;
  }

  if ($placeholders) {
    // This will load empty contexts as placeholders for arguments that come
    // from external sources. If this isn't set, it's assumed these context
    // will already have been matched up and loaded.
    if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) {
      $contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders);
    }

    if (!empty($object->arguments) && is_array($object->arguments)) {
      $contexts += ctools_context_get_placeholders_from_argument($object->arguments);
    }
  }

  if (!empty($object->contexts) && is_array($object->contexts)) {
    $contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders);
  }

  // Add contexts from relationships.
  if (!empty($object->relationships) && is_array($object->relationships)) {
    ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders);
  }

  return $contexts;
}

/**
 * Return the first context with a form id from a list of contexts.
 *
 * This function is used to figure out which contexts represents 'the form'
 * from a list of contexts. Only one contexts can actually be 'the form' for
 * a given page, since the @code
 * {<form>}
 * @endcode tag can not be embedded within
 * itself.
 */
function ctools_context_get_form($contexts) {
  if (!empty($contexts)) {
    foreach ($contexts as $id => $context) {
      // If a form shows its id as being a 'required context' that means the
      // the context is external to this display and does not count.
      if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') {
        return $context;
      }
    }
  }
}

/**
 * Replace placeholders with real contexts using data extracted from a form
 * for the purposes of previews.
 *
 * @param $contexts
 *   All of the contexts, including the placeholders.
 * @param $arguments
 *   The arguments. These will be acquired from $form_state['values'] and the
 *   keys must match the context IDs.
 *
 * @return array
 *   A new $contexts array containing the replaced contexts. Not all contexts
 *   may be replaced if, for example, an argument was unable to be converted
 *   into a context.
 */
function ctools_context_replace_placeholders($contexts, $arguments) {
  foreach ($contexts as $cid => $context) {
    if (empty($context->empty)) {
      continue;
    }

    $new_context = NULL;
    switch ($context->placeholder['type']) {
      case 'relationship':
        $relationship = $context->placeholder['conf'];
        if (isset($contexts[$relationship['context']])) {
          $new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]);
        }
        break;

      case 'argument':
        if (isset($arguments[$cid]) && $arguments[$cid] !== '') {
          $argument = $context->placeholder['conf'];
          $new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]);
        }
        break;

      case 'context':
        if (!empty($arguments[$cid])) {
          $context_info = $context->placeholder['conf'];
          $new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]);
        }
        break;
    }

    if ($new_context && empty($new_context->empty)) {
      $contexts[$cid] = $new_context;
    }
  }

  return $contexts;
}

/**
 * Provide a form array for getting data to replace placeholder contexts
 * with real data.
 */
function ctools_context_replace_form(&$form, $contexts) {
  foreach ($contexts as $cid => $context) {
    if (empty($context->empty)) {
      continue;
    }

    // Get plugin info from the context which should have been set when the
    // empty context was created.
    $info = NULL;
    $plugin = NULL;
    $settings = NULL;
    switch ($context->placeholder['type']) {
      case 'argument':
        $info = $context->placeholder['conf'];
        $plugin = ctools_get_argument($info['name']);
        break;

      case 'context':
        $info = $context->placeholder['conf'];
        $plugin = ctools_get_context($info['name']);
        break;
    }

    // Ask the plugin where the form is.
    if ($plugin && isset($plugin['placeholder form'])) {
      if (is_array($plugin['placeholder form'])) {
        $form[$cid] = $plugin['placeholder form'];
      }
      else {
        if (function_exists($plugin['placeholder form'])) {
          $widget = $plugin['placeholder form']($info);
          if ($widget) {
            $form[$cid] = $widget;
          }
        }
      }

      if (!empty($form[$cid])) {
        $form[$cid]['#title'] = t('@identifier (@keyword)', array(
          '@keyword' => '%' . $context->keyword,
          '@identifier' => $context->identifier,
        ));
      }
    }
  }
}

// ---------------------------------------------------------------------------
// Functions related to loading access control plugins.
/**
 * Fetch metadata on a specific access control plugin.
 *
 * @param $name
 *   Name of a plugin.
 *
 * @return array
 *   An array with information about the requested access control plugin.
 */

function ctools_get_access_plugin($name) {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'access', $name);
}

/**
 * Fetch metadata for all access control plugins.
 *
 * @return array
 *   An array of arrays with information about all available access control plugins.
 */
function ctools_get_access_plugins() {
  ctools_include('plugins');
  return ctools_get_plugins('ctools', 'access');
}

/**
 * Fetch a list of access plugins that are available for a given list of
 * contexts.
 *
 * If 'logged-in-user' is not in the list of contexts, it will be added as
 * this is required.
 *
 * @param array $contexts
 *   Array of ctools_context objects with which to select access plugins.
 *
 * @return array
 *   Array of applicable access plugins. Can be empty.
 */
function ctools_get_relevant_access_plugins($contexts) {
  if (!isset($contexts['logged-in-user'])) {
    $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  }

  $all_plugins = ctools_get_access_plugins();
  $plugins = array();
  foreach ($all_plugins as $id => $plugin) {
    if (!empty($plugin['required context'])
      && !ctools_context_match_requirements($contexts, $plugin['required context'])
    ) {
      continue;
    }
    $plugins[$id] = $plugin;
  }

  return $plugins;
}

/**
 * Create a context for the logged in user.
 */
function ctools_access_get_loggedin_context() {
  $context = ctools_context_create('entity:user', array('type' => 'current'), TRUE);
  $context->identifier = t('Logged in user');
  $context->keyword = 'viewer';
  $context->id = 0;

  return $context;
}

/**
 * Get a summary of an access plugin's settings.
 *
 * @return string
 *   The summary text.
 */
function ctools_access_summary($plugin, $contexts, $test) {
  if (!isset($contexts['logged-in-user'])) {
    $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  }

  $description = '';
  if ($function = ctools_plugin_get_function($plugin, 'summary')) {
    $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
    $context = isset($test['context']) ? $test['context'] : array();
    $selected_context = ctools_context_select($contexts, $required_context, $context);
    $description = $function($test['settings'], $selected_context, $plugin);
  }

  if (!empty($test['not'])) {
    $description = "NOT ($description)";
  }

  return $description;
}

/**
 * Get a summary of a group of access plugin's settings.
 *
 * @param $access
 *   An array of settings theoretically set by the user, including the array
 *   of plugins to check:
 *    - 'plugins': the array of plugin metadata info to check
 *    - 'logic': (optional) either 'and' or 'or', indicating how to combine
 *      restrictions. Defaults to 'or'.
 * @param array $contexts
 *   An array of zero or more contexts that may be used to determine if
 *   the user has access.
 *
 * @return string
 *   The summary text. Can be NULL if there are no plugins defined.
 */
function ctools_access_group_summary($access, $contexts) {
  if (empty($access['plugins']) || !is_array($access['plugins'])) {
    return NULL;
  }

  $descriptions = array();
  foreach ($access['plugins'] as $id => $test) {
    $plugin = ctools_get_access_plugin($test['name']);
    $descriptions[] = ctools_access_summary($plugin, $contexts, $test);
  }

  $separator =
    (isset($access['logic']) && $access['logic'] === 'and')
      ? t(', and ') : t(', or ');
  return implode($separator, $descriptions);
}

/**
 * Determine if the current user has access via a plugin.
 *
 * @param array $settings
 *   An array of settings theoretically set by the user, including the array
 *   of plugins to check:
 *    - 'plugins': the array of plugin metadata info to check
 *    - 'logic': (optional) either 'and' or 'or', indicating how to combine
 *      restrictions. The 'or' case is not fully implemented and returns the
 *      input contexts unchanged.
 *
 * @param array $contexts
 *   An array of zero or more contexts that may be used to determine if
 *   the user has access.
 *
 * @return bool
 *   TRUE if access is granted, FALSE if otherwise.
 */
function ctools_access($settings, $contexts = array()) {
  if (empty($settings['plugins'])) {
    return TRUE;
  }

  if (!isset($settings['logic'])) {
    $settings['logic'] = 'and';
  }

  if (!isset($contexts['logged-in-user'])) {
    $contexts['logged-in-user'] = ctools_access_get_loggedin_context();
  }

  foreach ($settings['plugins'] as $test) {
    $pass = FALSE;
    $plugin = ctools_get_access_plugin($test['name']);
    if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) {
      // Do we need just some contexts or all of them?
      if (!empty($plugin['all contexts'])) {
        $test_contexts = $contexts;
      }
      else {
        $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
        $context = isset($test['context']) ? $test['context'] : array();
        $test_contexts = ctools_context_select($contexts, $required_context, $context);
      }

      $pass = $function($test['settings'], $test_contexts, $plugin);
      if (!empty($test['not'])) {
        $pass = !$pass;
      }
    }

    if ($pass && $settings['logic'] == 'or') {
      // Pass if 'or' and this rule passed.
      return TRUE;
    }
    elseif (!$pass && $settings['logic'] == 'and') {
      // Fail if 'and' and this rule failed.
      return FALSE;
    }
  }

  // Return TRUE if logic was and, meaning all rules passed.
  // Return FALSE if logic was or, meaning no rule passed.
  return ($settings['logic'] === 'and');
}

/**
 * Create default settings for a new access plugin.
 *
 * @param $plugin
 *   The access plugin being used.
 *
 * @return array
 *   A default configured test that should be placed in $access['plugins'];
 */
function ctools_access_new_test($plugin) {
  $test = array(
    'name' => $plugin['name'],
    'settings' => array(),
  );

  // Set up required context defaults.
  if (isset($plugin['required context'])) {
    if (is_object($plugin['required context'])) {
      $test['context'] = '';
    }
    else {
      $test['context'] = array();
      foreach ($plugin['required context'] as $required) {
        $test['context'][] = '';
      }
    }
  }

  $default = NULL;
  if (isset($plugin['default'])) {
    $default = $plugin['default'];
  }
  elseif (isset($plugin['defaults'])) {
    $default = $plugin['defaults'];
  }

  // Setup plugin defaults.
  if (isset($default)) {
    if (is_array($default)) {
      $test['settings'] = $default;
    }
    elseif (function_exists($default)) {
      $test['settings'] = $default();
    }
    else {
      $test['settings'] = array();
    }
  }

  return $test;
}

/**
 * Apply restrictions to contexts based upon the access control configured.
 *
 * These restrictions allow the UI to not show content that may not be relevant
 * to all types of a particular context.
 *
 * @param array $settings
 *   Array of keys specifying the settings:
 *    - 'plugins': the array of plugin metadata info to check. If not set, or
 *      not an array, the function returns with no action.
 *    - 'logic': (optional) either 'and' or 'or', indicating how to combine
 *      restrictions. Defaults to 'and'.
 *      The 'or' case is not fully implemented and returns with no action if
 *      there is more than one plugin.
 *
 * @param array $contexts
 *   Array of available contexts.
 */
function ctools_access_add_restrictions($settings, $contexts) {
  if (empty($settings['plugins']) || !is_array($settings['plugins'])) {
    return;
  }

  if (!isset($settings['logic'])) {
    $settings['logic'] = 'and';
  }

  // We're not going to try to figure out restrictions on the or.
  if ($settings['logic'] === 'or' && count($settings['plugins']) > 1) {
    return;
  }

  foreach ($settings['plugins'] as $test) {
    $plugin = ctools_get_access_plugin($test['name']);
    // $plugin is 'array()' on error.
    if ($plugin
      && $function = ctools_plugin_get_function($plugin, 'restrictions')
    ) {
      $required_context = isset($plugin['required context']) ? $plugin['required context'] : array();
      $context = isset($test['context']) ? $test['context'] : array();
      $contexts = ctools_context_select($contexts, $required_context, $context);

      if ($contexts !== FALSE) {
        $function($test['settings'], $contexts);
      }
    }
  }
}
