You are here

function ca_load_trigger_conditions in Ubercart 6.2

Return an array of conditions available for the specified trigger.

Parameters

$trigger: The name of a trigger to find conditions for; if left empty, the function returns conditions for the previously specified trigger.

Return value

A nested array of names/titles for the conditions available for the trigger; in the format required by select elements in FAPI.

2 calls to ca_load_trigger_conditions()
ca_conditions_form in ca/ca.admin.inc
Build a form for adding and editing conditions on a predicate.
_ca_conditions_form_tree in ca/ca.admin.inc
Recursively add logical groups to the conditions form as fieldsets.

File

ca/ca.module, line 624
This is a demonstration module for the new conditional actions API.

Code

function ca_load_trigger_conditions($trigger = '') {
  static $options = array();
  if (!empty($trigger)) {

    // Load the specified trigger.
    $trigger = ca_load_trigger($trigger);
    $trigger_entities = array();

    // Organize trigger arguments by entity.
    foreach ($trigger['#arguments'] as $argument) {
      $trigger_entities[$argument['#entity']] = $argument;
    }

    // Load and loop through all the conditions defined by modules.
    $conditions = ca_load_condition();
    foreach ($conditions as $name => $condition) {

      // Check through each argument needed for the condition.
      // Cast to an array to accommodate conditions that need no arguments.
      foreach ((array) $condition['#arguments'] as $argument) {
        $entity = $argument['#entity'];

        // If the condition requires an entity the trigger doesn't provide,
        // then skip to the next condition.
        if ($entity != 'arguments' && !isset($trigger_entities[$entity])) {
          continue 2;
        }
      }

      // Getting this far means that all of the condition's arguments have
      // the same entity types as the trigger's. Add it to the options,
      // and group them by category for usability.
      $options[$condition['#category']][$name] = $condition['#title'];
    }

    // Alphabetically sort the groups and their options.
    foreach ($options as $group => $conditions) {
      asort($conditions);
      $options[$group] = $conditions;
    }
    ksort($options);
  }
  return $options;
}