[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/rules/rules_admin/ -> rules_admin.inc (source)

   1  <?php
   2  // $Id: rules_admin.inc,v 1.1.2.14 2010/11/25 11:14:53 fago Exp $
   3  
   4  
   5  /**
   6   * @file Rules Admin UI - Utility functions
   7   */
   8  require_once drupal_get_path('module', 'rules_admin') .'/rules_admin.rule_proxy.inc';
   9  
  10  /**
  11   * Gets the variable, which is assigned (mapped) to the given argument.
  12   */
  13  function rules_admin_element_map_variable_name($name, $element) {
  14    if (!isset($element['#settings']['#argument map'])) {
  15      //per default try the the same name
  16      return $name;
  17    }
  18    $map = $element['#settings']['#argument map'];
  19    return isset($map[$name]) ? $map[$name] : $name;
  20  }
  21  
  22  /**
  23   * Shows the element help
  24   */
  25  function rules_admin_element_help(&$form, $element) {
  26    $info = rules_retrieve_element_info($element);
  27    if (isset($info['help']) || isset($element['help'])) {
  28      $help = isset($element['help']) ? $element['help'] : $info['help'];
  29    }
  30    else if (isset($element['#type']) && in_array($element['#type'], array('action', 'condition'))) {
  31      rules_include('rules_forms');
  32      $help = rules_element_invoke($element, 'help', array());
  33    }
  34    if (isset($help) && $help) {
  35      $form['help'] = array(
  36        '#type' => 'fieldset',
  37        '#title' => t('Description'),
  38        '#description' => $help,
  39      );
  40    }
  41  }
  42  
  43  /**
  44   * Gets all compatible sets, which means that all arguments are available in the other set too
  45   */
  46  function rules_admin_get_compatible_sets($rule, $sets) {
  47    if (isset($rule['#set']) && $rule['#set']) {
  48      $set = rules_get_rule_sets($rule['#set']);
  49      foreach ($sets as $key => $this_set) {
  50        if (array_diff(array_keys($set['arguments']), array_keys($this_set['arguments']))) {
  51          unset($sets[$key]);
  52        }
  53      }
  54    }
  55    return $sets;
  56  }
  57  
  58  /**
  59   * Gets an array of element labels grouped by modules
  60   *
  61   * @param $elements
  62   *   An array of info elements, e.g. as returned from rules_get_events().
  63   * @param $key
  64   *   The key for which the value is used for grouping the elements.
  65   */
  66  function rules_admin_get_grouped_labels($elements, $key = 'module') {
  67    $grouped = array();
  68    $ungrouped = array();
  69  
  70    foreach (array_filter($elements, 'rules_admin_element_filter') as $name => $element) {
  71      if (isset($element[$key])) {
  72        $grouped[$element[$key]][$name] = rules_get_element_label($element);
  73      }
  74      else {
  75        $ungrouped[$name] = rules_get_element_label($element);
  76      }
  77    }
  78    foreach ($grouped as $name => $group) {
  79      asort($grouped[$name]);
  80    }
  81    ksort($grouped);
  82    asort($ungrouped);
  83    return $ungrouped + $grouped;
  84  }
  85  
  86  /**
  87   * This is used for filtering the rules, sets, action and conditions that should be shown.
  88   * Rules and sets are filtered when they are fixed and the variable 'rules_show_fixed'
  89   * is  not set to on. Actions and conditions are filtered out if they are marked as
  90   * being hidden.
  91   *
  92   * @return TRUE, when the element should be kept. Else FALSE.
  93   */
  94  function rules_admin_element_filter($element) {
  95    if (!variable_get('rules_show_fixed', FALSE)) {
  96      if (isset($element['#status']) && $element['#status'] == 'fixed') {
  97        return FALSE;
  98      }
  99      if (isset($element['status']) && $element['status'] == 'fixed') {
 100        return FALSE;
 101      }
 102    }
 103    if (isset($element['hidden']) && $element['hidden']) {
 104      return FALSE;
 105    }
 106    return TRUE;
 107  }
 108  
 109  /**
 110   * Determines if the given data type is valid for the given argument.
 111   *
 112   * @param $data_type
 113   *   The data type to check.
 114   * @param $arg_type
 115   *   The arguments needed data type.
 116   *
 117   * @return
 118   *   True or false.
 119   */
 120  function rules_admin_is_valid_data_type($data_type, $arg_type) {
 121    if (is_array($arg_type)) {
 122      foreach ($arg_type as $type) {
 123        // We have multiple options to check for.
 124        if (rules_admin_is_valid_data_type($data_type, $type)) {
 125          return TRUE;
 126        }
 127      }
 128      return FALSE;
 129    }
 130    if ($arg_type == '*') {
 131      return TRUE;
 132    }
 133    return $arg_type == $data_type;
 134  }
 135  
 136  /**
 137   * Filters the info about elements (actions, conditions), so that only elements
 138   * compatible with the given variables are kept, which means suiting variables
 139   * for all arguments are available, so that the action/condition can be
 140   * configured.
 141   * Additionally, arguments having no suiting variables are recorded
 142   * in the 'unsatisfied arguments' slot of the element in $infos.
 143   */
 144  function rules_admin_filter_info($variables, &$infos) {
 145    $filtered = array();
 146    foreach ($infos as $key => $info) {
 147      if (isset($info['arguments']) && is_array($info['arguments'])) {
 148        foreach ($info['arguments'] as $name => $argument_info) {
 149          if (!rules_admin_argument_satisifable($argument_info, $variables)) {
 150            // Get the unsatisfied arguments.
 151            $infos[$key] += array('unsatisfied arguments' => array());
 152            $infos[$key]['unsatisfied arguments'][$name] = $argument_info['label'];
 153          }
 154        }
 155        if (empty($infos[$key]['unsatisfied arguments'])) {
 156          $filtered[$key] = $info;
 157        }
 158      }
 159      else {
 160        $filtered[$key] = $info;
 161      }
 162    }
 163    return $filtered;
 164  }
 165  
 166  /**
 167   * Determines whether the argument can be configured properly, so whether
 168   * there are matching variables available or needed.
 169   */
 170  function rules_admin_argument_satisifable($info, $variables) {
 171    if ($info['type'] == '*') {
 172      return TRUE;
 173    }
 174    if (($type_info = rules_get_data_types($info['type']))) {
 175      // For backward compatibility use_input_form is interpreted as !identifiable if it's unset.
 176      if (!empty($type_info['use_input_form']) || (!isset($type_info['use_input_form']) && empty($type_info['identifiable']))) {
 177        // Argument value will be set on configuration time.
 178        return TRUE;
 179      }
 180    }
 181    if (count(rules_admin_map_get_possible_arguments($info, $variables))) {
 182      return TRUE;
 183    }
 184    return FALSE;
 185  }
 186  
 187  /**
 188   * Gets the possible variables (= of the same entity) for an argument
 189   */
 190  function rules_admin_map_get_possible_arguments($arg_info, $variables) {
 191    $matches = array();
 192    foreach ($variables as $name => $info) {
 193      if (rules_admin_is_valid_data_type($info['type'], $arg_info)) {
 194        $matches[$name] = $info['label'];
 195      }
 196    }
 197    return $matches;
 198  }
 199  
 200  /**
 201   * Returns an array of all existing categories
 202   *
 203   * @param $item_type
 204   *   Either 'rules' or 'rule_sets'
 205   *
 206   * @return
 207   *   An array of categories to use.
 208   */
 209  function rules_admin_get_categories($item_type) {
 210    $categories = array();
 211    foreach (array_filter(rules_get_configured_items($item_type), 'rules_admin_element_filter') as $item) {
 212      if ($item_type == 'rules' && isset($item['#categories']) && is_array($item['#categories'])) {
 213        $categories = $categories + drupal_map_assoc($item['#categories']);
 214      }
 215      if ($item_type == 'rule_sets' && isset($item['categories']) && is_array($item['categories'])) {
 216        $categories = $categories + drupal_map_assoc($item['categories']);
 217      }
 218    }
 219    return array_filter($categories);
 220  }
 221  
 222  /**
 223   * Gets the label for the info of the given element by apply label callbacks.
 224   * Note that this is also used for argument infos.
 225   *
 226   * @param $cfirst
 227   *   If TRUE, capitalize the first letter, if FALSE lower case the first letter.
 228   * @param $element
 229   *   The element having or containing this label. Pass by referenec to allow
 230   *   callbacks to alter some info, used by rules_core_node_label_callback().
 231   *
 232   * @return An array of changed properties of the given info
 233   */
 234  function _rules_admin_get_label(&$form_state, $info, &$element, $value, $cfirst = NULL) {
 235    $info_changes = array();
 236  
 237    if (drupal_strtolower($info['label']) != drupal_strtolower($value)) {
 238      //label has been customized
 239      $info_changes['label'] = $value;
 240      $info_changes['label callback'] = FALSE;
 241    }
 242    else if (isset($info['label callback']) && $info['label callback'] && function_exists($info['label callback'])) {
 243      $argument_labels = rules_admin_get_argument_labels($form_state['proxy'], $element);
 244      $info_changes['label'] = $info['label callback']($element['#settings'], $argument_labels, $element);
 245      // Labels are treated as user input, so we decode any entites.
 246      $info_changes['label'] = decode_entities($info_changes['label']);
 247      if (isset($cfirst)) {
 248        $function = $cfirst ? 'drupal_strtoupper' : 'drupal_strtolower';
 249        $info_changes['label'] = $function(drupal_substr($info_changes['label'], 0, 1)) . drupal_substr($info_changes['label'], 1);
 250      }
 251    }
 252    return $info_changes;
 253  }
 254  
 255  /**
 256   * Gets the labels of the variables, which are configured to be passed as arguments
 257   * to the given element.
 258   */
 259  function rules_admin_get_argument_labels($proxy, $element, $prefix = '@') {
 260    $labels = array();
 261    $names  = rules_get_mapped_argument_names($element);
 262    $vars   = $proxy->get_available_variables($element['#id']);
 263    $labels = array();
 264    foreach ($names as $argument_name => $variable_name) {
 265      if (isset($vars[$variable_name]['label'])) {
 266        $labels[$prefix . $argument_name] = $vars[$variable_name]['label'];
 267      }
 268    }
 269    return $labels;
 270  }
 271  
 272  /**
 273   * Returns a table of rules filtered by the given parameters
 274   *
 275   * Supported parameters: fixed, category, set, active.
 276   * Category and set may be set to NULL to hide their columns.
 277   */
 278  function rules_admin_overview_table($params) {
 279    $rules = rules_get_configured_items('rules');
 280    _rules_element_defaults($rules);
 281    rules_sort_children($rules);
 282  
 283    //set parameter defaults
 284    $params += array('category' => NULL, 'set' => NULL, 'active' => TRUE, 'fixed' => FALSE);
 285    extract($params);
 286  
 287    $is_set = $set && strpos($set, 'event_') !== 0;
 288  
 289    $header = array(t('Label'), $is_set ? t('Set') : t('Event'), t('Category'), t('Status'), t('Operations'));
 290    $rows = array();
 291  
 292    foreach (element_children($rules) as $name) {
 293      $rule = $rules[$name];
 294      _rules_element_defaults($rule);
 295  
 296      if ((!$category || in_array($category, $rule['#categories'])) && (!$set && strpos($rule['#set'], 'event_') === 0 || $rule['#set'] == $set) && $rule['#active'] == $active && ($rule['#status'] == 'fixed') == $fixed) {
 297        $path = RULES_ADMIN_RULE_PATH .'/'. $name;
 298        $ops = array();
 299        if ($rule['#status'] == 'custom') {
 300          $ops[] = l(t('delete'), $path .'/delete', array('query' => drupal_get_destination()));
 301        }
 302        else if ($rule['#status'] == 'altered') {
 303          $ops[] = l(t('revert'), $path .'/revert', array('query' => drupal_get_destination()));
 304        }
 305        $ops[] = l(t('clone'), $path .'/clone', array(), drupal_get_destination());
 306        $categories = array_map('check_plain', $rule['#categories']);
 307  
 308        $set_info = rules_get_rule_sets($rule['#set']);
 309        // If the set info is missing, the module providing the event is gone and the
 310        // rule can't be edited.
 311        $rows[] = array(
 312          $set_info ? l($rule['#label'], $path .'/edit') : check_plain($rule['#label']),
 313          $set_info ? check_plain(rules_get_element_label($set_info)) : array('class' => 'error', 'data' => check_plain($rule['#set'])),
 314          implode(', ', $categories),
 315          theme('rules_admin_configuration_status', $rule['#status']),
 316          implode(' ', $ops),
 317        );
 318        if (!$set_info) {
 319          rules_handle_error_msg("%set can't be found. Probably the providing module has been deactivated.", array('%set' => $rule['#set']));
 320        }
 321      }
 322    }
 323  
 324    if (count($rows)) {
 325      return array('#value' => theme('table', $header, $rows, array('class' => 'rules-rule-configurations')));
 326    }
 327    return array('#value' => '<p>'. t('None') .'</p>');
 328  }
 329  
 330  /**
 331   * Returns the given status rendered as html.
 332   */
 333  function theme_rules_admin_configuration_status($status) {
 334    if ($status == 'altered') {
 335      $help = t('This rule has been provided by a module, but has been modified.');
 336      return '<span title="'. $help .'">'. t('Modified') .'</span>';
 337    }
 338    else if ($status == 'fixed') {
 339      $help = t("This rule has been provided by a module and can't be customized.");
 340      return '<span title="'. $help .'">'. t('Fixed') .'</span>';
 341    }
 342    else if ($status == 'custom') {
 343      $help = t('A custom defined rule.');
 344      return '<span title="'. $help .'">'. t('Custom') .'</span>';
 345    }
 346    $help = t('This rule has been provided by a module.');
 347    return '<span title="'. $help .'">'. t('Default') .'</span>';
 348  }
 349  
 350  /**
 351   * Fixes the breadcrumb on the rule edit/delete/revert
 352   */
 353  function rules_admin_fix_breadcrumb($rule, $name) {
 354    $crumbs = array();
 355    if (strpos($rule['#set'], 'event_') !== 0) {
 356      $crumbs[] = l(t('Rule sets'), RULES_ADMIN_SET_PATH);
 357      $set_info = rules_get_rule_sets($rule['#set']);
 358      $crumbs[] = l($set_info['label'], RULES_ADMIN_SET_PATH .'/'. $rule['#set']);
 359    }
 360    else {
 361      $crumbs[] = l(t('Triggered rules'), RULES_ADMIN_TRIGGER_PATH);
 362    }
 363    if (arg(5)) {
 364      // Also add a crumb to the rule itself.
 365      array_unshift($crumbs, l(t('Rules'), RULES_ADMIN_PATH));
 366      $crumbs[] = l($rule['#label'], RULES_ADMIN_RULE_PATH .'/'. $name);
 367    }
 368    rules_admin_add_crumbs($crumbs);
 369  }
 370  
 371  /**
 372   * Adds some further crumbs to the breadcrumb.
 373   * Due to the bug http://drupal.org/node/430304, some pages might have no breadcrumb.
 374   */
 375  function rules_admin_add_crumbs($crumbs) {
 376    if ($breadcrumb = drupal_get_breadcrumb()) {
 377      $breadcrumb = array_merge($breadcrumb, $crumbs);
 378      drupal_set_breadcrumb($breadcrumb);
 379    }
 380  }
 381  
 382  function rules_admin_is_event_rule($rule) {
 383    return !empty($rule['#set']) && strpos($rule['#set'], 'event_') === 0;
 384  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7