ui.plugins.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @file Contains UI for diverse plugins provided by Rules.
  4. */
  5. /**
  6. * Rule specific UI.
  7. */
  8. class RulesRuleUI extends RulesActionContainerUI {
  9. protected $rule, $conditions;
  10. public function __construct(FacesExtendable $object) {
  11. parent::__construct($object);
  12. $this->rule = $object;
  13. $this->conditions = $this->rule->conditionContainer();
  14. }
  15. public function form(&$form, &$form_state, $options = array()) {
  16. $form_state['rules_element'] = $this->rule;
  17. $label = $this->element->label();
  18. // Automatically add a counter to unlabelled rules.
  19. if ($label == t('unlabeled') && !$this->element->isRoot() && !empty($options['init'])) {
  20. $parent = $this->element->parentElement();
  21. $label .= ' ' . count($parent->getIterator());
  22. }
  23. // Components have already a label. If used inside a rule-set add a label
  24. // though. It's called 'Name' in the UI though.
  25. if (!$this->element->isRoot()) {
  26. $form['label'] = array(
  27. '#type' => 'textfield',
  28. '#title' => t('Name'),
  29. '#default_value' => empty($options['init']) ? $label : '',
  30. '#required' => TRUE,
  31. '#weight' => 5,
  32. '#description' => t('A human-readable name shortly describing the rule.'),
  33. );
  34. }
  35. $form += array('conditions' => array('#weight' => -5, '#tree' => TRUE));
  36. $this->conditions->form($form['conditions'], $form_state);
  37. unset($form['conditions']['negate']);
  38. // Add actions form.
  39. $iterator = new RecursiveIteratorIterator($this->rule->actions(), RecursiveIteratorIterator::SELF_FIRST);
  40. parent::form($form, $form_state, $options, $iterator);
  41. // Hide nested elements during creation.
  42. $form['elements']['#access'] = empty($options['init']);
  43. $form['conditions']['elements']['#access'] = empty($options['init']);
  44. $form_state['redirect'] = RulesPluginUI::path($this->element->root()->name, 'edit', $this->element);
  45. if (!empty($options['button'])) {
  46. $form['submit']['#value'] = t('Save changes');
  47. }
  48. }
  49. /**
  50. * Applies the values of the form to the rule configuration.
  51. */
  52. function form_extract_values($form, &$form_state) {
  53. $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
  54. // Run condition and action container value extraction.
  55. if (isset($form['conditions'])) {
  56. $this->conditions->extender('RulesConditionContainerUI')->form_extract_values($form['conditions'], $form_state);
  57. }
  58. if (!empty($form_values['label'])) {
  59. $this->element->label = $form_values['label'];
  60. }
  61. parent::form_extract_values($form, $form_state);
  62. }
  63. public function operations() {
  64. // When rules are listed only show the edit and delete operations.
  65. $ops = parent::operations();
  66. $ops['#links'] = array_intersect_key($ops['#links'], array_flip(array('edit', 'delete')));
  67. return $ops;
  68. }
  69. }
  70. /**
  71. * Reaction rule specific UI.
  72. */
  73. class RulesReactionRuleUI extends RulesRuleUI {
  74. public function form(&$form, &$form_state, $options = array()) {
  75. $form['events'] = array(
  76. '#type' => 'container',
  77. '#weight' => -10,
  78. '#access' => empty($options['init']),
  79. );
  80. $event_info = rules_fetch_data('event_info');
  81. $form['events']['table'] = array(
  82. '#theme' => 'table',
  83. '#caption' => 'Events',
  84. '#header' => array('Event', 'Operations'),
  85. '#empty' => t('None'),
  86. );
  87. $form['events']['table']['#attributes']['class'][] = 'rules-elements-table';
  88. foreach ($this->rule->events() as $event_name) {
  89. $event_info += array($event_name => array('label' => t('Unknown event "!event_name"', array('!event_name' => $event_name))));
  90. $form['events']['table']['#rows'][$event_name] = array(
  91. check_plain($event_info[$event_name]['label']),
  92. '<span class="rules_rule_event">' . l(t('delete'), RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name)) . '</span>',
  93. );
  94. }
  95. // Add the "add event" row.
  96. $cell['colspan'] = 3;
  97. $cell['data']['#theme'] = 'links__rules';
  98. $cell['data']['#attributes']['class'][] = 'rules-operations-add';
  99. $cell['data']['#attributes']['class'][] = 'action-links';
  100. $cell['data']['#links']['add_event'] = array(
  101. 'title' => t('Add event'),
  102. 'href' => RulesPluginUI::path($this->rule->name, 'add/event'),
  103. 'query' => drupal_get_destination(),
  104. );
  105. $form['events']['table']['#rows'][] = array('data' => array($cell), 'class' => array('rules-elements-add'));
  106. parent::form($form, $form_state, $options);
  107. unset($form['label']);
  108. }
  109. /**
  110. * Adds the configuration settings form (label, tags, description, ..).
  111. */
  112. public function settingsForm(&$form, &$form_state) {
  113. parent::settingsForm($form, $form_state);
  114. $form['settings']['active'] = array(
  115. '#type' => 'checkbox',
  116. '#title' => t('Active'),
  117. '#default_value' => !isset($this->rule->active) || $this->rule->active,
  118. );
  119. $form['settings']['weight'] = array(
  120. '#type' => 'weight',
  121. '#title' => t('Weight'),
  122. '#default_value' => $this->element->weight,
  123. '#weight' => 5,
  124. '#delta' => 10,
  125. '#description' => t('Order rules that react on the same event. Rules with a higher weight are evaluated after rules with less weight.'),
  126. );
  127. unset($form['settings']['component_provides']);
  128. }
  129. public function settingsFormExtractValues($form, &$form_state) {
  130. $form_values = RulesPluginUI::getFormStateValues($form['settings'], $form_state);
  131. parent::settingsFormExtractValues($form, $form_state);
  132. $this->rule->active = $form_values['active'];
  133. $this->rule->weight = $form_values['weight'];
  134. }
  135. }
  136. /**
  137. * Rule set specific UI.
  138. */
  139. class RulesRuleSetUI extends RulesActionContainerUI {
  140. public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
  141. // Pass an iterator just iterating over the rules, thus no further child
  142. // elements will be displayed.
  143. parent::form($form, $form_state, $options, $this->element->getIterator());
  144. // Only show the add rule link.
  145. $form['elements']['#add']['#links'] = array_intersect_key($form['elements']['#add']['#links'], array('add_rule' => 1));
  146. $form['elements']['#attributes']['class'][] = 'rules-rule-set';
  147. $form['elements']['#caption'] = t('Rules');
  148. }
  149. }
  150. /**
  151. * UI for Rules loops.
  152. */
  153. class RulesLoopUI extends RulesActionContainerUI {
  154. public function form(&$form, &$form_state, $options = array()) {
  155. parent::form($form, $form_state, $options);
  156. $settings = $this->element->settings;
  157. $form['item'] = array(
  158. '#type' => 'fieldset',
  159. '#title' => t('Current list item'),
  160. '#description' => t('The variable used for holding each list item in the loop. This variable will be available inside the loop only.'),
  161. '#tree' => TRUE,
  162. );
  163. $form['item']['label'] = array(
  164. '#type' => 'textfield',
  165. '#title' => t('Variable label'),
  166. '#default_value' => $settings['item:label'],
  167. '#required' => TRUE,
  168. );
  169. $form['item']['var'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Variable name'),
  172. '#default_value' => $settings['item:var'],
  173. '#description' => t('The variable name must contain only lowercase letters, numbers, and underscores and must be unique in the current scope.'),
  174. '#element_validate' => array('rules_ui_element_machine_name_validate'),
  175. '#required' => TRUE,
  176. );
  177. }
  178. function form_extract_values($form, &$form_state) {
  179. parent::form_extract_values($form, $form_state);
  180. $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
  181. $this->element->settings['item:var'] = $form_values['item']['var'];
  182. $this->element->settings['item:label'] = $form_values['item']['label'];
  183. }
  184. public function form_validate($form, &$form_state) {
  185. parent::form_validate($form, $form_state);
  186. $vars = $this->element->availableVariables();
  187. $name = $this->element->settings['item:var'];
  188. if (isset($vars[$name])) {
  189. form_error($form['item']['var'], t('The variable name %name is already taken.', array('%name' => $name)));
  190. }
  191. }
  192. public function buildContent() {
  193. $content = parent::buildContent();
  194. $content['description']['item'] = array(
  195. '#caption' => t('List item'),
  196. '#theme' => 'rules_content_group',
  197. );
  198. $content['description']['item']['var'] = array(
  199. '#theme' => 'rules_variable_view',
  200. '#info' => $this->element->listItemInfo(),
  201. '#name' => $this->element->settings['item:var'],
  202. );
  203. return $content;
  204. }
  205. }