ui.data.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * @file Contains data type related forms.
  4. */
  5. /**
  6. * Interface for data types providing a direct input form.
  7. */
  8. interface RulesDataDirectInputFormInterface {
  9. /**
  10. * Constructs the direct input form.
  11. *
  12. * @return Array
  13. * The direct input form.
  14. */
  15. public static function inputForm($name, $info, $settings, RulesPlugin $element);
  16. /**
  17. * Render the configured value.
  18. *
  19. * @return Array
  20. * A renderable array.
  21. */
  22. public static function render($value);
  23. }
  24. /**
  25. * Default UI related class for data types.
  26. */
  27. class RulesDataUI {
  28. /**
  29. * Specifies the default input mode per data type.
  30. */
  31. public static function getDefaultMode() {
  32. return 'selector';
  33. }
  34. /**
  35. * Provides the selection form for a parameter.
  36. */
  37. public static function selectionForm($name, $info, $settings, RulesPlugin $element) {
  38. if (!isset($settings[$name . ':select'])) {
  39. $settings[$name . ':select'] = '';
  40. $vars = $element->availableVariables();
  41. // Default to variables with the same name as the parameter.
  42. if (isset($vars[$name])) {
  43. $settings[$name . ':select'] = $name;
  44. }
  45. // If there is only one match, use it by default.
  46. elseif (count($matches = RulesData::matchingDataSelector($vars, $info, '', 1, FALSE)) == 1) {
  47. $settings[$name . ':select'] = rules_array_key($matches);
  48. }
  49. }
  50. $form[$name . ':select'] = array(
  51. '#type' => 'rules_data_selection',
  52. '#title' => t('Data selector'),
  53. '#default_value' => $settings[$name . ':select'],
  54. '#required' => empty($info['optional']),
  55. '#autocomplete_path' => RulesPluginUI::path($element->root()->name, 'autocomplete' . '/' . $name),
  56. // Make the autocomplete textfield big enough so that it can display
  57. // descriptions without word wraps.
  58. '#size' => 75,
  59. '#description' => t("The data selector helps you drill down into the data available to Rules. <em>To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type').</em> More useful tips about data selection is available in <a href='@url'>the online documentation</a>.",
  60. array('@url' => rules_external_help('data-selection'))),
  61. );
  62. $cache = rules_get_cache();
  63. $form['types_help'] = array(
  64. '#theme' => 'rules_settings_help',
  65. '#heading' => t('Data types'),
  66. );
  67. if ($info['type'] == '*') {
  68. $type_labels[] = t('any');
  69. }
  70. else {
  71. $types = is_array($info['type']) ? $info['type'] : array($info['type']);
  72. $type_labels = array();
  73. foreach ($types as $type) {
  74. $type_labels[] = drupal_ucfirst(isset($cache['data_info'][$type]['label']) ? $cache['data_info'][$type]['label'] : $type);
  75. }
  76. }
  77. $form['types_help']['#text'] = format_plural(count($type_labels), 'Select data of the type %types.', 'Select data of the types %types.', array('%types' => implode(', ', $type_labels)));
  78. if (!empty($info['translatable'])) {
  79. if (empty($info['custom translation language'])) {
  80. $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the current interface language.');
  81. }
  82. else {
  83. $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the configured language.');
  84. }
  85. $form['translation'] = array(
  86. '#theme' => 'rules_settings_help',
  87. '#text' => $text,
  88. '#heading' => t('Translation'),
  89. );
  90. }
  91. $form['help'] = array(
  92. '#theme' => 'rules_data_selector_help',
  93. '#variables' => $element->availableVariables(),
  94. '#parameter' => $info,
  95. );
  96. // Add data processor.
  97. $settings += array($name . ':process' => array());
  98. $form[$name . ':process'] = array();
  99. RulesDataProcessor::attachForm($form[$name . ':process'], $settings[$name . ':process'], $info, $element->availableVariables());
  100. return $form;
  101. }
  102. /**
  103. * Renders the value by making use of the label if an options list is available.
  104. *
  105. * Used for data UI classes implementing the
  106. * RulesDataDirectInputFormInterface.
  107. *
  108. * In case an options list is available, the the usual render() method won't
  109. * be invoked, instead the selected entry is rendered via this method.
  110. *
  111. * @todo for Drupal 8: Refactor to avoid implementations have to care about
  112. * option lists when generating the form, but not when rendering values.
  113. */
  114. public static function renderOptionsLabel($value, $name, $info, RulesPlugin $element) {
  115. if (!empty($info['options list'])) {
  116. $element->call('loadBasicInclude');
  117. $options = entity_property_options_flatten($info['options list']($element, $name));
  118. if (!is_array($value) && isset($options[$value])) {
  119. $value = $options[$value];
  120. }
  121. elseif (is_array($value)) {
  122. foreach ($value as $key => $single_value) {
  123. if (isset($options[$single_value])) {
  124. $value[$key] = $options[$single_value];
  125. }
  126. }
  127. $value = implode(', ', $value);
  128. }
  129. return array(
  130. 'content' => array('#markup' => check_plain($value)),
  131. '#attributes' => array('class' => array('rules-parameter-options-entry')),
  132. );
  133. }
  134. }
  135. }
  136. /**
  137. * UI for textual data.
  138. */
  139. class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInterface {
  140. public static function getDefaultMode() {
  141. return 'input';
  142. }
  143. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  144. if (!empty($info['options list'])) {
  145. // Make sure the .rules.inc of the providing module is included as the
  146. // options list callback may reside there.
  147. $element->call('loadBasicInclude');
  148. $form[$name] = array(
  149. '#type' => 'select',
  150. '#options' => call_user_func($info['options list'], $element, $name),
  151. );
  152. }
  153. else {
  154. $form[$name] = array(
  155. '#type' => 'textarea',
  156. );
  157. RulesDataInputEvaluator::attachForm($form, $settings, $info, $element->availableVariables());
  158. }
  159. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  160. $form[$name] += array(
  161. '#title' => t('Value'),
  162. '#default_value' => $settings[$name],
  163. '#required' => empty($info['optional']),
  164. '#after_build' => array('rules_ui_element_fix_empty_after_build'),
  165. '#rows' => 3,
  166. );
  167. return $form;
  168. }
  169. public static function render($value) {
  170. return array(
  171. 'content' => array('#markup' => check_plain($value)),
  172. '#attributes' => array('class' => array('rules-parameter-text')),
  173. );
  174. }
  175. }
  176. /**
  177. * UI for text tokens.
  178. */
  179. class RulesDataUITextToken extends RulesDataUIText {
  180. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  181. $form = parent::inputForm($name, $info, $settings, $element);
  182. if ($form[$name]['#type'] == 'textarea') {
  183. $form[$name]['#element_validate'][] = 'rules_ui_element_token_validate';
  184. $form[$name]['#description'] = t('May only contain lowercase letters, numbers, and underscores and has to start with a letter.');
  185. $form[$name]['#rows'] = 1;
  186. }
  187. return $form;
  188. }
  189. }
  190. /**
  191. * UI for formatted text.
  192. */
  193. class RulesDataUITextFormatted extends RulesDataUIText {
  194. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  195. $form = parent::inputForm($name, $info, $settings, $element);
  196. $settings += array($name => isset($info['default value']) ? $info['default value'] : array('value' => NULL, 'format' => NULL));
  197. $form[$name]['#type'] = 'text_format';
  198. $form[$name]['#base_type'] = 'textarea';
  199. $form[$name]['#default_value'] = $settings[$name]['value'];
  200. $form[$name]['#format'] = $settings[$name]['format'];
  201. return $form;
  202. }
  203. public static function render($value) {
  204. return array(
  205. 'content' => array('#markup' => check_plain($value['value'])),
  206. '#attributes' => array('class' => array('rules-parameter-text-formatted')),
  207. );
  208. }
  209. }
  210. /**
  211. * UI for decimal data.
  212. */
  213. class RulesDataUIDecimal extends RulesDataUIText {
  214. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  215. $form = parent::inputForm($name, $info, $settings, $element);
  216. if (empty($info['options list'])) {
  217. $form[$name]['#type'] = 'textfield';
  218. }
  219. $form[$name]['#element_validate'][] = 'rules_ui_element_decimal_validate';
  220. $form[$name]['#rows'] = 1;
  221. return $form;
  222. }
  223. }
  224. /**
  225. * UI for integers.
  226. */
  227. class RulesDataUIInteger extends RulesDataUIText {
  228. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  229. $form = parent::inputForm($name, $info, $settings, $element);
  230. if (empty($info['options list'])) {
  231. $form[$name]['#type'] = 'textfield';
  232. }
  233. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
  234. return $form;
  235. }
  236. }
  237. /**
  238. * UI for boolean data.
  239. */
  240. class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputFormInterface {
  241. public static function getDefaultMode() {
  242. return 'input';
  243. }
  244. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  245. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  246. // Note: Due to the checkbox even optional parameter always receive a value.
  247. $form[$name] = array(
  248. '#type' => 'checkbox',
  249. '#title' => check_plain($info['label']),
  250. '#default_value' => $settings[$name],
  251. );
  252. return $form;
  253. }
  254. public static function render($value) {
  255. return array(
  256. 'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
  257. '#attributes' => array('class' => array('rules-parameter-boolean')),
  258. );
  259. }
  260. }
  261. /**
  262. * UI for dates.
  263. */
  264. class RulesDataUIDate extends RulesDataUIText {
  265. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  266. $settings += array($name => isset($info['default value']) ? $info['default value'] : (empty($info['optional']) ? gmdate('Y-m-d H:i:s', time()) : NULL));
  267. // Convert any configured timestamp into a readable format.
  268. if (is_numeric($settings[$name])) {
  269. $settings[$name] = gmdate('Y-m-d H:i:s', $settings[$name]);
  270. }
  271. $form = parent::inputForm($name, $info, $settings, $element);
  272. $form[$name]['#type'] = 'textfield';
  273. $form[$name]['#element_validate'][] = 'rules_ui_element_date_validate';
  274. // Note that the date input evaluator takes care for parsing dates using
  275. // strtotime() into a timestamp, which is the internal date format.
  276. $form[$name]['#description'] = t('The date in GMT. You may enter a fixed time (like %format) or any other values in GMT known by the PHP !strtotime function (like "+1 day"). Relative dates like "+1 day" or "now" relate to the evaluation time.',
  277. array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
  278. '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
  279. //TODO: Leverage the jquery datepicker+timepicker once a module providing
  280. //the timpeicker is available.
  281. return $form;
  282. }
  283. public static function render($value) {
  284. $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
  285. return array(
  286. 'content' => array('#markup' => $value),
  287. '#attributes' => array('class' => array('rules-parameter-date')),
  288. );
  289. }
  290. }
  291. /**
  292. * UI for duration type parameter.
  293. */
  294. class RulesDataUIDuration extends RulesDataUIText {
  295. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  296. $form = parent::inputForm($name, $info, $settings, $element);
  297. $form[$name]['#type'] = 'rules_duration';
  298. $form[$name]['#after_build'][] = 'rules_ui_element_duration_after_build';
  299. return $form;
  300. }
  301. public static function render($value) {
  302. $value = is_numeric($value) ? format_interval($value) : check_plain($value);
  303. return array(
  304. 'content' => array('#markup' => $value),
  305. '#attributes' => array('class' => array('rules-parameter-duration')),
  306. );
  307. }
  308. }
  309. /**
  310. * UI for the URI type parameter.
  311. */
  312. class RulesDataUIURI extends RulesDataUIText {
  313. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  314. $form = parent::inputForm($name, $info, $settings, $element);
  315. $form[$name]['#rows'] = 1;
  316. $form[$name]['#description'] = t('You may enter relative URLs like %url as well as absolute URLs like %absolute-url.', array('%url' => 'user/login?destination=node', '%absolute-url' => 'http://drupal.org'));
  317. return $form;
  318. }
  319. }
  320. /**
  321. * UI for lists of textual data.
  322. */
  323. class RulesDataUIListText extends RulesDataUIText {
  324. public static function getDefaultMode() {
  325. return 'input';
  326. }
  327. /**
  328. * @todo This does not work for inputting textual values including "\n".
  329. */
  330. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  331. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  332. $form = parent::inputForm($name, $info, $settings, $element);
  333. if ($form[$name]['#type'] == 'textarea') {
  334. // Fix up the value to be an array during after build.
  335. $form[$name]['#delimiter'] = "\n";
  336. $form[$name]['#after_build'][] = 'rules_ui_list_textarea_after_build';
  337. $form[$name]['#pre_render'][] = 'rules_ui_list_textarea_pre_render';
  338. $form[$name]['#default_value'] = !empty($settings[$name]) ? implode("\n", $settings[$name]) : NULL;
  339. $form[$name]['#description'] = t('A list of values, one on each line.');
  340. }
  341. else {
  342. $form[$name]['#multiple'] = TRUE;
  343. }
  344. return $form;
  345. }
  346. public static function render($value) {
  347. return array(
  348. 'content' => array('#markup' => check_plain(implode(', ', $value))),
  349. '#attributes' => array('class' => array('rules-parameter-list')),
  350. );
  351. }
  352. }
  353. /**
  354. * UI for lists of integers.
  355. */
  356. class RulesDataUIListInteger extends RulesDataUIListText {
  357. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  358. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  359. $form = parent::inputForm($name, $info, $settings, $element);
  360. if ($form[$name]['#type'] == 'textarea') {
  361. $form[$name]['#description'] = t('A list of integers, separated by commas. E.g. enter "1, 2, 3".');
  362. $form[$name]['#delimiter'] = ',';
  363. $form[$name]['#default_value'] = !empty($settings[$name]) ? implode(", ", $settings[$name]) : NULL;
  364. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_list_validate';
  365. $form[$name]['#rows'] = 1;
  366. }
  367. return $form;
  368. }
  369. }
  370. /**
  371. * UI for lists of tokens.
  372. */
  373. class RulesDataUIListToken extends RulesDataUIListInteger {
  374. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  375. $form = parent::inputForm($name, $info, $settings, $element);
  376. if ($form[$name]['#type'] == 'textarea') {
  377. $form[$name]['#description'] = t('A list of text tokens, separated by commas. E.g. enter "one, two, three".');
  378. $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
  379. }
  380. return $form;
  381. }
  382. }
  383. /**
  384. * UI for entity-based data types.
  385. */
  386. class RulesDataUIEntity extends RulesDataUIText {
  387. public static function getDefaultMode() {
  388. return 'selector';
  389. }
  390. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  391. $form = parent::inputForm($name, $info, $settings, $element);
  392. if (empty($info['options list'])) {
  393. $form[$name]['#type'] = 'textfield';
  394. $entity_info = entity_get_info($info['type']);
  395. if (empty($entity_info['entity keys']['name'])) {
  396. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
  397. }
  398. $form[$name]['#title'] = t('@entity identifier', array('@entity' => $entity_info['label']));
  399. $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
  400. $form[$name]['#description'] = t('Specify an identifier of a @entity.', array('@entity' => $entity_label));
  401. }
  402. return $form;
  403. }
  404. }
  405. /**
  406. * UI for exportable entity-based data types.
  407. */
  408. class RulesDataUIEntityExportable extends RulesDataUIEntity {
  409. public static function getDefaultMode() {
  410. return 'input';
  411. }
  412. }
  413. /**
  414. * UI for taxonomy vocabularies.
  415. *
  416. * @see RulesTaxonomyVocabularyWrapper
  417. */
  418. class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity {
  419. public static function getDefaultMode() {
  420. return 'input';
  421. }
  422. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  423. // Add an options list of all vocabularies if there is none yet.
  424. if (!isset($info['options list'])) {
  425. $info['options list'] = array('RulesDataUITaxonomyVocabulary', 'optionsList');
  426. }
  427. return parent::inputForm($name, $info, $settings, $element);
  428. }
  429. public static function optionsList() {
  430. $options = array();
  431. foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
  432. $options[$machine_name] = $vocab->name;
  433. }
  434. return $options;
  435. }
  436. }
  437. /**
  438. * UI for lists of entity-based data types.
  439. */
  440. class RulesDataUIListEntity extends RulesDataUIListInteger {
  441. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  442. $form = parent::inputForm($name, $info, $settings, $element);
  443. if (empty($info['options list'])) {
  444. $entity_info = entity_get_info(entity_property_list_extract_type($info['type']));
  445. if (!empty($entity_info['entity keys']['name'])) {
  446. $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
  447. }
  448. $form[$name]['#title'] = t('@entity identifiers', array('@entity' => $entity_info['label']));
  449. $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
  450. $form[$name]['#description'] = t('Specify a comma-separated list of identifiers of @entity entities.', array('@entity' => $entity_label));
  451. }
  452. return $form;
  453. }
  454. }