Projet

Général

Profil

Paste
Télécharger (3,72 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules.drush.inc @ d719f12f

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules module drush integration.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 *
11
 * @return
12
 *   An associative array describing your command(s).
13
 *
14
 * @see drush_parse_command()
15
 */
16
function rules_drush_command() {
17
  $items = array();
18

    
19
  $items['rules-list'] = array(
20
    'description' => "List all the active and inactive rules for your site.",
21
    'drupal dependencies' => array('rules'),
22
    'aliases' => array('rules'),
23
  );
24
  $items['rules-enable'] = array(
25
    'description' => "Enable a rule on your site.",
26
    'arguments' => array(
27
      'rule' => 'Rule name to enable.',
28
    ),
29
    'drupal dependencies' => array('rules'),
30
    'aliases' => array('re'),
31
  );
32
  $items['rules-disable'] = array(
33
    'description' => "Disable a rule on your site.",
34
    'arguments' => array(
35
      'rule' => 'Rule name to export.',
36
    ),
37
    'drupal dependencies' => array('rules'),
38
    'aliases' => array('rd'),
39
  );
40

    
41
  return $items;
42
}
43

    
44
/**
45
 * Implements hook_drush_help().
46
 */
47
function rules_drush_help($section) {
48
  switch ($section) {
49
    case 'drush:rules':
50
      return dt("List all the rules on your site.");
51
    case 'drush:rules-enable':
52
      return dt("Enable/activate a rule on your site.");
53
    case 'drush:rules-disable':
54
      return dt("Disable/deactivate a rule on your site.");
55
  }
56
}
57

    
58
/**
59
 * Get a list of all rules.
60
 */
61
function drush_rules_list() {
62
  $rules = rules_config_load_multiple(FALSE);
63
  $rows = array(array(dt('Rule'), dt('Label'), dt('Event'), dt('Active'), dt('Status')));
64
  foreach ($rules as $rule) {
65
    if (!empty($rule->name) && !empty($rule->label)) {
66
      $events = array();
67
      $event_info = rules_fetch_data('event_info');
68
      if ($rule instanceof RulesTriggerableInterface) {
69
        foreach ($rule->events() as $event_name) {
70
          $event_info += array($event_name => array('label' => dt('Unknown event "!event_name"', array('!event_name' => $event_name))));
71
          $events[] = check_plain($event_info[$event_name]['label']);
72
        }
73
      }
74
      $rows[] = array(
75
        $rule->name,
76
        $rule->label,
77
        implode(', ', $events),
78
        $rule->active ? dt('Enabled') : dt('Disabled'),
79
        $rule->status ? theme('entity_status', array('status' => $rule->status, 'html' => FALSE)) : '',
80
      );
81
    }
82
  }
83
  drush_print_table($rows, TRUE);
84
}
85

    
86
/**
87
 * Enable a rule on the site.
88
 */
89
function drush_rules_enable() {
90
  $args = func_get_args();
91
  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
92
  if (empty($rule_name)) {
93
    return drush_set_error('', 'No rule name given.');
94
  }
95

    
96
  $rule = rules_config_load($rule_name);
97
  if (empty($rule)) {
98
    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
99
  }
100

    
101
  if (empty($rule->active)) {
102
    $rule->active = TRUE;
103
    $rule->save();
104
    drush_log(dt('The rule "!name" has been enabled.', array('!name' => $rule_name)), 'success');
105
  }
106
  else {
107
    drush_log(dt('The rule "!name" is already enabled.', array('!name' => $rule_name)), 'warning');
108
  }
109
}
110

    
111
/**
112
 * Disable a rule on the site.
113
 */
114
function drush_rules_disable() {
115
  $args = func_get_args();
116
  $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
117
  if (empty($rule_name)) {
118
    return drush_set_error('', 'No rule name given.');
119
  }
120

    
121
  $rule = rules_config_load($rule_name);
122
  if (empty($rule)) {
123
    return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
124
  }
125

    
126
  if (!empty($rule->active)) {
127
    $rule->active = FALSE;
128
    $rule->save();
129
    drush_log(dt('The rule "!name" has been disabled.', array('!name' => $rule_name)), 'success');
130
  }
131
  else {
132
    drush_log(dt('The rule "!name" is already disabled.', array('!name' => $rule_name)), 'warning');
133
  }
134
}