ui.data.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. * Interface for data UI classes providing an options list.
  26. */
  27. interface RulesDataInputOptionsListInterface extends RulesDataDirectInputFormInterface {
  28. /**
  29. * Returns the options list for the data type.
  30. *
  31. * @param RulesPlugin $element
  32. * The rules element to get the options for.
  33. * @param string $name
  34. * The name of the parameter for which to get options.
  35. *
  36. * For retrieving information about the used data type and parameter, the
  37. * helper RulesDataUI::getTypeInfo() may be used as following:
  38. * @code
  39. * list($type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
  40. * @endcode
  41. *
  42. * @return
  43. * An array of options as used by hook_options_list().
  44. */
  45. public static function optionsList(RulesPlugin $element, $name);
  46. }
  47. /**
  48. * Default UI related class for data types.
  49. */
  50. class RulesDataUI {
  51. /**
  52. * Specifies the default input mode per data type.
  53. */
  54. public static function getDefaultMode() {
  55. return 'selector';
  56. }
  57. /**
  58. * Provides the selection form for a parameter.
  59. */
  60. public static function selectionForm($name, $info, $settings, RulesPlugin $element) {
  61. if (!isset($settings[$name . ':select'])) {
  62. $settings[$name . ':select'] = '';
  63. $vars = $element->availableVariables();
  64. // Default to variables with the same name as the parameter.
  65. if (isset($vars[$name])) {
  66. $settings[$name . ':select'] = $name;
  67. }
  68. // If there is only one match, use it by default.
  69. elseif (count($matches = RulesData::matchingDataSelector($vars, $info, '', 1, FALSE)) == 1) {
  70. $settings[$name . ':select'] = rules_array_key($matches);
  71. }
  72. }
  73. $form[$name . ':select'] = array(
  74. '#type' => 'rules_data_selection',
  75. '#title' => t('Data selector'),
  76. '#default_value' => $settings[$name . ':select'],
  77. '#required' => empty($info['optional']),
  78. '#autocomplete_path' => RulesPluginUI::path($element->root()->name, 'autocomplete' . '/' . $name),
  79. // Make the autocomplete textfield big enough so that it can display
  80. // descriptions without word wraps.
  81. '#size' => 75,
  82. '#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>.",
  83. array('@url' => rules_external_help('data-selection'))),
  84. );
  85. $cache = rules_get_cache();
  86. $form['types_help'] = array(
  87. '#theme' => 'rules_settings_help',
  88. '#heading' => t('Data types'),
  89. );
  90. if ($info['type'] == '*') {
  91. $type_labels[] = t('any');
  92. }
  93. else {
  94. $types = is_array($info['type']) ? $info['type'] : array($info['type']);
  95. $type_labels = array();
  96. foreach ($types as $type) {
  97. $type_labels[] = drupal_ucfirst(isset($cache['data_info'][$type]['label']) ? $cache['data_info'][$type]['label'] : $type);
  98. }
  99. }
  100. $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)));
  101. if (!empty($info['translatable'])) {
  102. if (empty($info['custom translation language'])) {
  103. $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the current interface language.');
  104. }
  105. else {
  106. $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the configured language.');
  107. }
  108. $form['translation'] = array(
  109. '#theme' => 'rules_settings_help',
  110. '#text' => $text,
  111. '#heading' => t('Translation'),
  112. );
  113. }
  114. $form['help'] = array(
  115. '#theme' => 'rules_data_selector_help',
  116. '#variables' => $element->availableVariables(),
  117. '#parameter' => $info,
  118. );
  119. // Add data processor.
  120. $settings += array($name . ':process' => array());
  121. $form[$name . ':process'] = array();
  122. RulesDataProcessor::attachForm($form[$name . ':process'], $settings[$name . ':process'], $info, $element->availableVariables());
  123. return $form;
  124. }
  125. /**
  126. * Renders the value by making use of the label if an options list is available.
  127. *
  128. * Used for data UI classes implementing the
  129. * RulesDataDirectInputFormInterface.
  130. *
  131. * In case an options list is available, the the usual render() method won't
  132. * be invoked, instead the selected entry is rendered via this method.
  133. *
  134. * @todo for Drupal 8: Refactor to avoid implementations have to care about
  135. * option lists when generating the form, but not when rendering values.
  136. */
  137. public static function renderOptionsLabel($value, $name, $info, RulesPlugin $element) {
  138. if (!empty($info['options list'])) {
  139. $element->call('loadBasicInclude');
  140. $options = entity_property_options_flatten(call_user_func($info['options list'], $element, $name));
  141. if (!is_array($value) && isset($options[$value])) {
  142. $value = $options[$value];
  143. }
  144. elseif (is_array($value)) {
  145. foreach ($value as $key => $single_value) {
  146. if (isset($options[$single_value])) {
  147. $value[$key] = $options[$single_value];
  148. }
  149. }
  150. $value = implode(', ', $value);
  151. }
  152. return array(
  153. 'content' => array('#markup' => check_plain($value)),
  154. '#attributes' => array('class' => array('rules-parameter-options-entry')),
  155. );
  156. }
  157. }
  158. /**
  159. * Returns the data type and parameter information for the given arguments.
  160. *
  161. * This helper may be used by options list callbacks operation at data-type
  162. * level, see RulesDataInputOptionsListInterface.
  163. */
  164. public static function getTypeInfo(RulesPlugin $element, $name) {
  165. $parameters = $element->pluginParameterInfo();
  166. return array($parameters[$name]['type'], $parameters[$name]);
  167. }
  168. }
  169. /**
  170. * UI for textual data.
  171. */
  172. class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInterface {
  173. public static function getDefaultMode() {
  174. return 'input';
  175. }
  176. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  177. if (!empty($info['options list'])) {
  178. // Make sure the .rules.inc of the providing module is included as the
  179. // options list callback may reside there.
  180. $element->call('loadBasicInclude');
  181. $form[$name] = array(
  182. '#type' => 'select',
  183. '#options' => call_user_func($info['options list'], $element, $name),
  184. );
  185. }
  186. else {
  187. $form[$name] = array(
  188. '#type' => 'textarea',
  189. );
  190. RulesDataInputEvaluator::attachForm($form, $settings, $info, $element->availableVariables());
  191. }
  192. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  193. $form[$name] += array(
  194. '#title' => t('Value'),
  195. '#default_value' => $settings[$name],
  196. '#required' => empty($info['optional']),
  197. '#after_build' => array('rules_ui_element_fix_empty_after_build'),
  198. '#rows' => 3,
  199. );
  200. return $form;
  201. }
  202. public static function render($value) {
  203. return array(
  204. 'content' => array('#markup' => check_plain($value)),
  205. '#attributes' => array('class' => array('rules-parameter-text')),
  206. );
  207. }
  208. }
  209. /**
  210. * UI for text tokens.
  211. */
  212. class RulesDataUITextToken extends RulesDataUIText {
  213. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  214. $form = parent::inputForm($name, $info, $settings, $element);
  215. if ($form[$name]['#type'] == 'textarea') {
  216. $form[$name]['#element_validate'][] = 'rules_ui_element_token_validate';
  217. $form[$name]['#description'] = t('May only contain lowercase letters, numbers, and underscores and has to start with a letter.');
  218. $form[$name]['#rows'] = 1;
  219. }
  220. return $form;
  221. }
  222. }
  223. /**
  224. * UI for formatted text.
  225. */
  226. class RulesDataUITextFormatted extends RulesDataUIText {
  227. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  228. $form = parent::inputForm($name, $info, $settings, $element);
  229. $settings += array($name => isset($info['default value']) ? $info['default value'] : array('value' => NULL, 'format' => NULL));
  230. $form[$name]['#type'] = 'text_format';
  231. $form[$name]['#base_type'] = 'textarea';
  232. $form[$name]['#default_value'] = $settings[$name]['value'];
  233. $form[$name]['#format'] = $settings[$name]['format'];
  234. return $form;
  235. }
  236. public static function render($value) {
  237. return array(
  238. 'content' => array('#markup' => check_plain($value['value'])),
  239. '#attributes' => array('class' => array('rules-parameter-text-formatted')),
  240. );
  241. }
  242. }
  243. /**
  244. * UI for decimal data.
  245. */
  246. class RulesDataUIDecimal extends RulesDataUIText {
  247. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  248. $form = parent::inputForm($name, $info, $settings, $element);
  249. if (empty($info['options list'])) {
  250. $form[$name]['#type'] = 'textfield';
  251. }
  252. $form[$name]['#element_validate'][] = 'rules_ui_element_decimal_validate';
  253. $form[$name]['#rows'] = 1;
  254. return $form;
  255. }
  256. }
  257. /**
  258. * UI for integers.
  259. */
  260. class RulesDataUIInteger extends RulesDataUIText {
  261. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  262. $form = parent::inputForm($name, $info, $settings, $element);
  263. if (empty($info['options list'])) {
  264. $form[$name]['#type'] = 'textfield';
  265. }
  266. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
  267. return $form;
  268. }
  269. }
  270. /**
  271. * UI for IP addresses.
  272. */
  273. class RulesDataUIIPAddress extends RulesDataUIText {
  274. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  275. $form = parent::inputForm($name, $info, $settings, $element);
  276. if (empty($info['options list'])) {
  277. $form[$name]['#type'] = 'textfield';
  278. $form[$name]['#description'] = t('If not provided, the IP address of the current user will be used.');
  279. }
  280. $form[$name]['#element_validate'][] = 'rules_ui_element_ip_address_validate';
  281. $form[$name]['#rows'] = 1;
  282. return $form;
  283. }
  284. }
  285. /**
  286. * UI for boolean data.
  287. */
  288. class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputFormInterface {
  289. public static function getDefaultMode() {
  290. return 'input';
  291. }
  292. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  293. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  294. // Note: Due to the checkbox even optional parameter always receive a value.
  295. $form[$name] = array(
  296. '#type' => 'radios',
  297. '#default_value' => $settings[$name],
  298. '#options' => array(
  299. TRUE => t('@label: True.', array('@label' => $info['label'])),
  300. FALSE => t('@label: False.', array('@label' => $info['label'])),
  301. ),
  302. );
  303. return $form;
  304. }
  305. public static function render($value) {
  306. return array(
  307. 'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
  308. '#attributes' => array('class' => array('rules-parameter-boolean')),
  309. );
  310. }
  311. }
  312. /**
  313. * UI for dates.
  314. */
  315. class RulesDataUIDate extends RulesDataUIText {
  316. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  317. $settings += array($name => isset($info['default value']) ? $info['default value'] : (empty($info['optional']) ? gmdate('Y-m-d H:i:s', time()) : NULL));
  318. // Convert any configured timestamp into a readable format.
  319. if (is_numeric($settings[$name])) {
  320. $settings[$name] = gmdate('Y-m-d H:i:s', $settings[$name]);
  321. }
  322. $form = parent::inputForm($name, $info, $settings, $element);
  323. $form[$name]['#type'] = 'textfield';
  324. $form[$name]['#element_validate'][] = 'rules_ui_element_date_validate';
  325. // Note that the date input evaluator takes care for parsing dates using
  326. // strtotime() into a timestamp, which is the internal date format.
  327. $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.',
  328. array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
  329. '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
  330. //TODO: Leverage the jquery datepicker+timepicker once a module providing
  331. //the timpeicker is available.
  332. return $form;
  333. }
  334. public static function render($value) {
  335. $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
  336. return array(
  337. 'content' => array('#markup' => $value),
  338. '#attributes' => array('class' => array('rules-parameter-date')),
  339. );
  340. }
  341. }
  342. /**
  343. * UI for duration type parameter.
  344. */
  345. class RulesDataUIDuration extends RulesDataUIText {
  346. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  347. $form = parent::inputForm($name, $info, $settings, $element);
  348. $form[$name]['#type'] = 'rules_duration';
  349. $form[$name]['#after_build'][] = 'rules_ui_element_duration_after_build';
  350. return $form;
  351. }
  352. public static function render($value) {
  353. $value = is_numeric($value) ? format_interval($value) : check_plain($value);
  354. return array(
  355. 'content' => array('#markup' => $value),
  356. '#attributes' => array('class' => array('rules-parameter-duration')),
  357. );
  358. }
  359. }
  360. /**
  361. * UI for the URI type parameter.
  362. */
  363. class RulesDataUIURI extends RulesDataUIText {
  364. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  365. $form = parent::inputForm($name, $info, $settings, $element);
  366. $form[$name]['#rows'] = 1;
  367. $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'));
  368. return $form;
  369. }
  370. }
  371. /**
  372. * UI for lists of textual data.
  373. */
  374. class RulesDataUIListText extends RulesDataUIText {
  375. public static function getDefaultMode() {
  376. return 'input';
  377. }
  378. /**
  379. * @todo This does not work for inputting textual values including "\n".
  380. */
  381. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  382. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  383. $form = parent::inputForm($name, $info, $settings, $element);
  384. if ($form[$name]['#type'] == 'textarea') {
  385. // Fix up the value to be an array during after build.
  386. $form[$name]['#delimiter'] = "\n";
  387. $form[$name]['#after_build'][] = 'rules_ui_list_textarea_after_build';
  388. $form[$name]['#pre_render'][] = 'rules_ui_list_textarea_pre_render';
  389. $form[$name]['#default_value'] = !empty($settings[$name]) ? implode("\n", $settings[$name]) : NULL;
  390. $form[$name]['#description'] = t('A list of values, one on each line.');
  391. }
  392. else {
  393. $form[$name]['#multiple'] = TRUE;
  394. }
  395. return $form;
  396. }
  397. public static function render($value) {
  398. return array(
  399. 'content' => array('#markup' => check_plain(implode(', ', $value))),
  400. '#attributes' => array('class' => array('rules-parameter-list')),
  401. );
  402. }
  403. }
  404. /**
  405. * UI for lists of integers.
  406. */
  407. class RulesDataUIListInteger extends RulesDataUIListText {
  408. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  409. $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
  410. $form = parent::inputForm($name, $info, $settings, $element);
  411. if ($form[$name]['#type'] == 'textarea') {
  412. $form[$name]['#description'] = t('A list of integers, separated by commas. E.g. enter "1, 2, 3".');
  413. $form[$name]['#delimiter'] = ',';
  414. $form[$name]['#default_value'] = !empty($settings[$name]) ? implode(", ", $settings[$name]) : NULL;
  415. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_list_validate';
  416. $form[$name]['#rows'] = 1;
  417. }
  418. return $form;
  419. }
  420. }
  421. /**
  422. * UI for lists of tokens.
  423. */
  424. class RulesDataUIListToken extends RulesDataUIListInteger {
  425. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  426. $form = parent::inputForm($name, $info, $settings, $element);
  427. if ($form[$name]['#type'] == 'textarea') {
  428. $form[$name]['#description'] = t('A list of text tokens, separated by commas. E.g. enter "one, two, three".');
  429. $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
  430. }
  431. return $form;
  432. }
  433. }
  434. /**
  435. * UI for entity-based data types.
  436. */
  437. class RulesDataUIEntity extends RulesDataUIText {
  438. public static function getDefaultMode() {
  439. return 'selector';
  440. }
  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. $form[$name]['#type'] = 'textfield';
  445. $entity_info = entity_get_info($info['type']);
  446. if (empty($entity_info['entity keys']['name'])) {
  447. $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
  448. }
  449. $form[$name]['#title'] = t('@entity identifier', array('@entity' => $entity_info['label']));
  450. $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
  451. $form[$name]['#description'] = t('Specify an identifier of a @entity.', array('@entity' => $entity_label));
  452. }
  453. return $form;
  454. }
  455. }
  456. /**
  457. * UI for exportable entity-based data types.
  458. */
  459. class RulesDataUIEntityExportable extends RulesDataUIEntity {
  460. public static function getDefaultMode() {
  461. return 'input';
  462. }
  463. }
  464. /**
  465. * Data UI variant displaying a select list of available bundle entities.
  466. *
  467. * This is used for "bundle entities" implemented via the 'bundle of' feature
  468. * of entity.module.
  469. */
  470. class RulesDataUIBundleEntity extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
  471. public static function getDefaultMode() {
  472. return 'input';
  473. }
  474. /**
  475. * Implements RulesDataInputOptionsListInterface::optionsList().
  476. */
  477. public static function optionsList(RulesPlugin $element, $name) {
  478. list($data_type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
  479. $bundles = array();
  480. $entity_info = entity_get_info();
  481. $bundle_of_type = $entity_info[$data_type]['bundle of'];
  482. if (isset($entity_info[$bundle_of_type]['bundles'])) {
  483. foreach ($entity_info[$bundle_of_type]['bundles'] as $bundle_name => $bundle_info) {
  484. $bundles[$bundle_name] = $bundle_info['label'];
  485. }
  486. }
  487. return $bundles;
  488. }
  489. }
  490. /**
  491. * UI for taxonomy vocabularies.
  492. *
  493. * @see RulesTaxonomyVocabularyWrapper
  494. */
  495. class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
  496. public static function getDefaultMode() {
  497. return 'input';
  498. }
  499. /**
  500. * Implements RulesDataInputOptionsListInterface::optionsList().
  501. */
  502. public static function optionsList(RulesPlugin $element, $name) {
  503. $options = array();
  504. foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
  505. $options[$machine_name] = $vocab->name;
  506. }
  507. return $options;
  508. }
  509. }
  510. /**
  511. * UI for lists of entity-based data types.
  512. */
  513. class RulesDataUIListEntity extends RulesDataUIListInteger {
  514. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  515. $form = parent::inputForm($name, $info, $settings, $element);
  516. if (empty($info['options list'])) {
  517. $entity_info = entity_get_info(entity_property_list_extract_type($info['type']));
  518. if (!empty($entity_info['entity keys']['name'])) {
  519. $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
  520. }
  521. $form[$name]['#title'] = t('@entity identifiers', array('@entity' => $entity_info['label']));
  522. $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
  523. $form[$name]['#description'] = t('Specify a comma-separated list of identifiers of @entity entities.', array('@entity' => $entity_label));
  524. }
  525. return $form;
  526. }
  527. }