ui.plugins.inc 8.9 KB

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