rules.drush.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @file
  4. * Rules module drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. *
  9. * @return
  10. * An associative array describing your command(s).
  11. *
  12. * @see drush_parse_command()
  13. */
  14. function rules_drush_command() {
  15. $items = array();
  16. $items['rules-list'] = array(
  17. 'description' => "List all the active and inactive rules for your site.",
  18. 'drupal dependencies' => array('rules'),
  19. 'aliases' => array('rules'),
  20. );
  21. $items['rules-enable'] = array(
  22. 'description' => "Enable a rule on your site.",
  23. 'arguments' => array(
  24. 'rule' => 'Rule name to enable.',
  25. ),
  26. 'drupal dependencies' => array('rules'),
  27. 'aliases' => array('re'),
  28. );
  29. $items['rules-disable'] = array(
  30. 'description' => "Disable a rule on your site.",
  31. 'arguments' => array(
  32. 'rule' => 'Rule name to export.',
  33. ),
  34. 'drupal dependencies' => array('rules'),
  35. 'aliases' => array('rd'),
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Implements hook_drush_help().
  41. */
  42. function rules_drush_help($section) {
  43. switch ($section) {
  44. case 'drush:rules':
  45. return dt("List all the rules on your site.");
  46. case 'drush:rules-enable':
  47. return dt("Enable/activate a rule on your site.");
  48. case 'drush:rules-disable':
  49. return dt("Disable/deactivate a rule on your site.");
  50. }
  51. }
  52. /**
  53. * Get a list of all rules.
  54. */
  55. function drush_rules_list() {
  56. $rules = rules_config_load_multiple(FALSE);
  57. $rows = array(array(dt('Rule'), dt('Label'), dt('Event'), dt('Active'), dt('Status')));
  58. foreach ($rules as $rule) {
  59. if (!empty($rule->name) && !empty($rule->label)) {
  60. $events = array();
  61. $event_info = rules_fetch_data('event_info');
  62. if ($rule instanceof RulesTriggerableInterface) {
  63. foreach ($rule->events() as $event_name) {
  64. $event_info += array($event_name => array('label' => dt('Unknown event "!event_name"', array('!event_name' => $event_name))));
  65. $events[] = check_plain($event_info[$event_name]['label']);
  66. }
  67. }
  68. $rows[] = array(
  69. $rule->name,
  70. $rule->label,
  71. implode(', ', $events),
  72. $rule->active ? dt('Enabled') : dt('Disabled'),
  73. $rule->status ? theme('entity_status', array('status' => $rule->status, 'html' => FALSE)) : '',
  74. );
  75. }
  76. }
  77. drush_print_table($rows, TRUE);
  78. }
  79. /**
  80. * Enable a rule on the site.
  81. */
  82. function drush_rules_enable() {
  83. $args = func_get_args();
  84. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  85. if (empty($rule_name)) {
  86. return drush_set_error('', 'No rule name given.');
  87. }
  88. $rule = rules_config_load($rule_name);
  89. if (empty($rule)) {
  90. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  91. }
  92. if (empty($rule->active)) {
  93. $rule->active = TRUE;
  94. $rule->save();
  95. drush_log(dt('The rule "!name" has been enabled.', array('!name' => $rule_name)), 'success');
  96. }
  97. else {
  98. drush_log(dt('The rule "!name" is already enabled.', array('!name' => $rule_name)), 'warning');
  99. }
  100. }
  101. /**
  102. * Disable a rule on the site.
  103. */
  104. function drush_rules_disable() {
  105. $args = func_get_args();
  106. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  107. if (empty($rule_name)) {
  108. return drush_set_error('', 'No rule name given.');
  109. }
  110. $rule = rules_config_load($rule_name);
  111. if (empty($rule)) {
  112. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  113. }
  114. if (!empty($rule->active)) {
  115. $rule->active = FALSE;
  116. $rule->save();
  117. drush_log(dt('The rule "!name" has been disabled.', array('!name' => $rule_name)), 'success');
  118. }
  119. else {
  120. drush_log(dt('The rule "!name" is already disabled.', array('!name' => $rule_name)), 'warning');
  121. }
  122. }