ui.plugins.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. $form['events']['table'] = array(
  81. '#theme' => 'table',
  82. '#caption' => 'Events',
  83. '#header' => array('Event', 'Operations'),
  84. '#empty' => t('None'),
  85. );
  86. $form['events']['table']['#attributes']['class'][] = 'rules-elements-table';
  87. foreach ($this->rule->events() as $event_name) {
  88. $event_handler = rules_get_event_handler($event_name, $this->rule->getEventSettings($event_name));
  89. $event_operations = array(
  90. '#theme' => 'links__rules',
  91. '#attributes' => array(
  92. 'class' => array(
  93. 'rules-operations',
  94. 'action-links',
  95. 'rules_rule_event',
  96. ),
  97. ),
  98. '#links' => array(
  99. 'delete_event' => array(
  100. 'title' => t('delete'),
  101. 'href' => RulesPluginUI::path($this->rule->name, 'delete/event/' . $event_name),
  102. 'query' => drupal_get_destination(),
  103. ),
  104. ),
  105. );
  106. $form['events']['table']['#rows'][$event_name] = array(
  107. 'data' => array(
  108. $event_handler->summary(),
  109. array('data' => $event_operations),
  110. ),
  111. );
  112. }
  113. // Add the "add event" row.
  114. $cell['colspan'] = 3;
  115. $cell['data']['#theme'] = 'links__rules';
  116. $cell['data']['#attributes']['class'][] = 'rules-operations-add';
  117. $cell['data']['#attributes']['class'][] = 'action-links';
  118. $cell['data']['#links']['add_event'] = array(
  119. 'title' => t('Add event'),
  120. 'href' => RulesPluginUI::path($this->rule->name, 'add/event'),
  121. 'query' => drupal_get_destination(),
  122. );
  123. $form['events']['table']['#rows'][] = array('data' => array($cell), 'class' => array('rules-elements-add'));
  124. parent::form($form, $form_state, $options);
  125. unset($form['label']);
  126. }
  127. /**
  128. * Adds the configuration settings form (label, tags, description, ..).
  129. */
  130. public function settingsForm(&$form, &$form_state) {
  131. parent::settingsForm($form, $form_state);
  132. $form['settings']['active'] = array(
  133. '#type' => 'checkbox',
  134. '#title' => t('Active'),
  135. '#default_value' => !isset($this->rule->active) || $this->rule->active,
  136. );
  137. $form['settings']['weight'] = array(
  138. '#type' => 'weight',
  139. '#title' => t('Weight'),
  140. '#default_value' => $this->element->weight,
  141. '#weight' => 5,
  142. '#delta' => 10,
  143. '#description' => t('Order rules that react on the same event. Rules with a higher weight are evaluated after rules with less weight.'),
  144. );
  145. unset($form['settings']['component_provides']);
  146. }
  147. public function settingsFormExtractValues($form, &$form_state) {
  148. $form_values = RulesPluginUI::getFormStateValues($form['settings'], $form_state);
  149. parent::settingsFormExtractValues($form, $form_state);
  150. $this->rule->active = $form_values['active'];
  151. $this->rule->weight = $form_values['weight'];
  152. }
  153. }
  154. /**
  155. * Rule set specific UI.
  156. */
  157. class RulesRuleSetUI extends RulesActionContainerUI {
  158. public function form(&$form, &$form_state, $options = array(), $iterator = NULL) {
  159. // Pass an iterator just iterating over the rules, thus no further child
  160. // elements will be displayed.
  161. parent::form($form, $form_state, $options, $this->element->getIterator());
  162. // Only show the add rule link.
  163. $form['elements']['#add']['#links'] = array_intersect_key($form['elements']['#add']['#links'], array('add_rule' => 1));
  164. $form['elements']['#attributes']['class'][] = 'rules-rule-set';
  165. $form['elements']['#caption'] = t('Rules');
  166. }
  167. }
  168. /**
  169. * UI for Rules loops.
  170. */
  171. class RulesLoopUI extends RulesActionContainerUI {
  172. public function form(&$form, &$form_state, $options = array()) {
  173. parent::form($form, $form_state, $options);
  174. $settings = $this->element->settings;
  175. $form['item'] = array(
  176. '#type' => 'fieldset',
  177. '#title' => t('Current list item'),
  178. '#description' => t('The variable used for holding each list item in the loop. This variable will be available inside the loop only.'),
  179. '#tree' => TRUE,
  180. );
  181. $form['item']['label'] = array(
  182. '#type' => 'textfield',
  183. '#title' => t('Variable label'),
  184. '#default_value' => $settings['item:label'],
  185. '#required' => TRUE,
  186. );
  187. $form['item']['var'] = array(
  188. '#type' => 'textfield',
  189. '#title' => t('Variable name'),
  190. '#default_value' => $settings['item:var'],
  191. '#description' => t('The variable name must contain only lowercase letters, numbers, and underscores and must be unique in the current scope.'),
  192. '#element_validate' => array('rules_ui_element_machine_name_validate'),
  193. '#required' => TRUE,
  194. );
  195. }
  196. function form_extract_values($form, &$form_state) {
  197. parent::form_extract_values($form, $form_state);
  198. $form_values = RulesPluginUI::getFormStateValues($form, $form_state);
  199. $this->element->settings['item:var'] = $form_values['item']['var'];
  200. $this->element->settings['item:label'] = $form_values['item']['label'];
  201. }
  202. public function form_validate($form, &$form_state) {
  203. parent::form_validate($form, $form_state);
  204. $vars = $this->element->availableVariables();
  205. $name = $this->element->settings['item:var'];
  206. if (isset($vars[$name])) {
  207. form_error($form['item']['var'], t('The variable name %name is already taken.', array('%name' => $name)));
  208. }
  209. }
  210. public function buildContent() {
  211. $content = parent::buildContent();
  212. $content['description']['item'] = array(
  213. '#caption' => t('List item'),
  214. '#theme' => 'rules_content_group',
  215. );
  216. $content['description']['item']['var'] = array(
  217. '#theme' => 'rules_variable_view',
  218. '#info' => $this->element->listItemInfo(),
  219. '#name' => $this->element->settings['item:var'],
  220. );
  221. return $content;
  222. }
  223. }