You are here

class RulesUICategory in Rules 7.2

Class holding category related methods.

Hierarchy

Expanded class hierarchy of RulesUICategory

File

ui/ui.core.inc, line 1238
Contains core Rules UI functions.

View source
class RulesUICategory {

  /**
   * Gets info about all available categories, or about a specific category.
   *
   * @return array
   */
  public static function getInfo($category = NULL) {
    $data = rules_fetch_data('category_info');
    if (isset($category)) {
      return $data[$category];
    }
    return $data;
  }

  /**
   * Returns a group label, e.g. as usable for opt-groups in a select list.
   *
   * @param array $item_info
   *   The info-array of an item, e.g. an entry of hook_rules_action_info().
   * @param bool $in_category
   *   (optional) Whether group labels for grouping inside a category should be
   *   return. Defaults to FALSE.
   *
   * @return string|bool
   *   The group label to use, or FALSE if none can be found.
   */
  public static function getItemGroup($item_info, $in_category = FALSE) {
    if (isset($item_info['category']) && !$in_category) {
      return self::getCategory($item_info, 'label');
    }
    elseif (!empty($item_info['group'])) {
      return $item_info['group'];
    }
    return FALSE;
  }

  /**
   * Gets the category for the given item info array.
   *
   * @param array $item_info
   *   The info-array of an item, e.g. an entry of hook_rules_action_info().
   * @param string|null $key
   *   (optional) The key of the category info to return, e.g. 'label'. If none
   *   is given the whole info array is returned.
   *
   * @return array|mixed|false
   *   Either the whole category info array or the value of the given key. If
   *   no category can be found, FALSE is returned.
   */
  public static function getCategory($item_info, $key = NULL) {
    if (isset($item_info['category'])) {
      $info = self::getInfo($item_info['category']);
      return isset($key) ? $info[$key] : $info;
    }
    return FALSE;
  }

  /**
   * Returns an array of options to use with a select.
   *
   * Returns an array of options to use with a selectfor the items specified
   * in the given hook.
   *
   * @param string $item_type
   *   The item type to get options for. One of 'data', 'event', 'condition' and
   *   'action'.
   * @param array|null $items
   *   (optional) An array of items to restrict the options to.
   *
   * @return array
   *   An array of options.
   */
  public static function getOptions($item_type, $items = NULL) {
    $sorted_data = array();
    $ungrouped = array();
    $data = $items ? $items : rules_fetch_data($item_type . '_info');
    foreach ($data as $name => $info) {

      // Verify the current user has access to use it.
      if (!user_access('bypass rules access') && !empty($info['access callback']) && !call_user_func($info['access callback'], $item_type, $name)) {
        continue;
      }
      if ($group = RulesUICategory::getItemGroup($info)) {
        $sorted_data[drupal_ucfirst($group)][$name] = drupal_ucfirst($info['label']);
      }
      else {
        $ungrouped[$name] = drupal_ucfirst($info['label']);
      }
    }
    asort($ungrouped);
    foreach ($sorted_data as $key => $choices) {
      asort($choices);
      $sorted_data[$key] = $choices;
    }

    // Sort the grouped data by category weights, defaulting to weight 0 for
    // groups without a respective category.
    $sorted_groups = array();
    foreach (array_keys($sorted_data) as $label) {
      $sorted_groups[$label] = array(
        'weight' => 0,
        'label' => $label,
      );
    }

    // Add in category weights.
    foreach (RulesUICategory::getInfo() as $info) {
      if (isset($sorted_groups[$info['label']])) {
        $sorted_groups[$info['label']] = $info;
      }
    }
    uasort($sorted_groups, '_rules_ui_sort_categories');

    // Now replace weights with group content.
    foreach ($sorted_groups as $group => $weight) {
      $sorted_groups[$group] = $sorted_data[$group];
    }
    return $ungrouped + $sorted_groups;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesUICategory::getCategory public static function Gets the category for the given item info array.
RulesUICategory::getInfo public static function Gets info about all available categories, or about a specific category.
RulesUICategory::getItemGroup public static function Returns a group label, e.g. as usable for opt-groups in a select list.
RulesUICategory::getOptions public static function Returns an array of options to use with a select.