variable.variable.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * @file
  4. * Variable module hook implementations
  5. */
  6. /**
  7. * Implements hook_variable_group_info().
  8. */
  9. function variable_variable_group_info() {
  10. // Group for variable that have no group
  11. $groups['default'] = array(
  12. 'title' => t('Other'),
  13. 'description' => t("Variables that don't belong to any other group."),
  14. );
  15. $groups['debug'] = array(
  16. 'title' => t('Debug'),
  17. 'description' => t('Debug and development options.'),
  18. );
  19. $groups['variable'] = array(
  20. 'title' => t('Variable'),
  21. 'description' => t('Variables that contain metadata about the variable system.'),
  22. );
  23. return $groups;
  24. }
  25. /**
  26. * Implements hook_variable_type_info().
  27. */
  28. function variable_variable_type_info() {
  29. // Array of values
  30. $type['array'] = array(
  31. 'title' => t('Array'),
  32. 'element' => array('#type' => 'fieldset', '#tree' => TRUE),
  33. // Properties for each array item
  34. 'repeat' => array(
  35. 'element' => array('#type' => 'textfield'),
  36. ),
  37. 'format callback' => 'variable_format_array',
  38. 'element callback' => 'variable_form_element_array',
  39. 'default' => array(),
  40. );
  41. // Array whose keys are named properties.
  42. $type['properties'] = array(
  43. 'title' => t('Properties'),
  44. 'format callback' => 'variable_format_properties',
  45. 'type' => 'array',
  46. );
  47. // TRUE / FALSE value, checkbox
  48. $type['boolean'] = array(
  49. 'title' => t('Boolean'),
  50. 'element' => array('#type' => 'checkbox'),
  51. 'format callback' => 'variable_format_boolean',
  52. );
  53. // Default type for variables with no other type
  54. $type['default'] = array(
  55. 'title' => t('Default'),
  56. 'element' => array('#type' => 'textfield'),
  57. 'access' => 'administer site configuration',
  58. );
  59. // Enable/Disable
  60. $type['enable'] = array(
  61. 'title' => t('Enable'),
  62. 'options' => array(t('Disabled'), t('Enabled')),
  63. 'default' => 0,
  64. 'element' => array('#type' => 'radios'),
  65. 'format callback' => 'variable_format_selection',
  66. );
  67. // Multiple variable that will spawn into multiple elements
  68. $type['multiple'] = array(
  69. 'title' => t('Multiple'),
  70. 'element' => array('#type' => 'fieldset'),
  71. 'build callback' => 'variable_build_multiple',
  72. 'format callback' => 'variable_format_multiple',
  73. 'element callback' => 'variable_form_element_multiple',
  74. 'value callback' => 'variable_multiple_get_value',
  75. 'default callback' => 'variable_multiple_get_default',
  76. );
  77. $type['mail_address'] = array(
  78. 'title' => t('E-mail address'),
  79. 'element' => array('#type' => 'textfield'),
  80. 'token' => TRUE,
  81. );
  82. $type['mail_text'] = array(
  83. 'title' => t('Mail text'),
  84. 'multiple' => array('subject' => t('Subject'), 'body' => t('Body')),
  85. 'build callback' => 'variable_build_mail_text',
  86. 'localize' => TRUE,
  87. 'type' => 'multiple',
  88. );
  89. $type['number'] = array(
  90. 'title' => t('Number'),
  91. 'element' => array('#type' => 'textfield', '#size' => 15, '#maxlength' => 10),
  92. 'token' => TRUE,
  93. 'validate callback' => 'variable_validate_number',
  94. 'format callback' => 'variable_format_number',
  95. );
  96. // Select multiple options from multiple choices
  97. $type['options'] = array(
  98. 'title' => t('Options'),
  99. 'options' => TRUE,
  100. 'element' => array('#type' => 'checkboxes'),
  101. 'element callback' => 'variable_form_element_options',
  102. 'format callback' => 'variable_format_options',
  103. );
  104. // Select single option from multiple choices
  105. $type['select'] = array(
  106. 'title' => t('Select'),
  107. 'options' => TRUE,
  108. // This will become radios or drop-down depending on the number of options
  109. 'element callback' => 'variable_form_element_options',
  110. 'format callback' => 'variable_format_selection',
  111. );
  112. // Select number from array of values. Options array that can be list of numbers will be converted to a value => value
  113. $type['select_number'] = array(
  114. 'title' => t('Select'),
  115. 'options' => TRUE,
  116. 'element callback' => 'variable_form_element_options',
  117. 'options callback' => 'variable_options_select_number',
  118. );
  119. $type['string'] = array(
  120. 'title' => t('String'),
  121. 'element' => array('#type' => 'textfield'),
  122. 'localize' => TRUE,
  123. 'format callback' => 'variable_format_string',
  124. 'token' => TRUE,
  125. // This type may have an 'allowed tags' attribute.
  126. // If empty it will be formatted as plain text
  127. 'allowed tags' => array(),
  128. );
  129. $type['text'] = array(
  130. 'title' => t('Text'),
  131. 'element' => array('#type' => 'textarea'),
  132. 'localize' => TRUE,
  133. 'format callback' => 'variable_format_text',
  134. 'token' => TRUE,
  135. // This type may have an 'allowed tags' attribute.
  136. // If empty it will be formatted with filter_xss_admin.
  137. 'allowed tags' => array(),
  138. );
  139. // Default type for variables with no other type
  140. $type['unknown'] = array(
  141. 'title' => t('Unknown'),
  142. 'access' => 'administer site configuration',
  143. 'format' => 'variable_format_unknown',
  144. 'element callback' => 'variable_form_element_unknown',
  145. 'element' => array('#type' => 'item'),
  146. );
  147. $type['url'] = array(
  148. 'title' => t('URL'),
  149. 'element' => array('#type' => 'textfield', '#size' => 80, '#maxlength' => 255),
  150. 'token' => TRUE,
  151. );
  152. $type['mail_part'] = array(
  153. 'title' => t('Mail parts'),
  154. 'options' => array('subject' => t('Subject'), 'body' => t('Body')),
  155. );
  156. $type['text_format'] = array(
  157. 'title' => t('Formatted text'),
  158. 'element' => array('#type' => 'text_format'),
  159. 'element callback' => 'variable_form_element_text_format',
  160. 'format callback' => 'variable_format_text_format',
  161. 'default callback' => 'variable_text_format_default',
  162. 'localize' => TRUE,
  163. );
  164. return $type;
  165. }
  166. /**
  167. * Build multiple mail variable
  168. */
  169. function variable_build_mail_text($variable, $options = array()) {
  170. $name = str_replace('[mail_part]', '', $variable['name']);
  171. // For mail text, children have different types
  172. $variable['children'][$name . 'subject']['type'] = 'string';
  173. $variable['children'][$name . 'body']['type'] = 'text';
  174. $variable = variable_build_multiple($variable, $options);
  175. return $variable;
  176. }
  177. /**
  178. * Format select variable
  179. */
  180. function variable_format_selection($variable, $options = array()) {
  181. $variable = variable_build_options($variable, $options);
  182. if (isset($variable['value'])) {
  183. return isset($variable['options'][$variable['value']]) ? $variable['options'][$variable['value']] : '<' . t('Invalid option') . '>';
  184. }
  185. else {
  186. return variable_format_empty($variable);
  187. }
  188. }
  189. /**
  190. * Format options variable. Value is an array of options.
  191. */
  192. function variable_format_options($variable, $options = array()) {
  193. $variable = variable_build_options($variable, $options);
  194. $names = array();
  195. if (isset($variable['value']) && $variable['value']) {
  196. if (is_array($variable['value'])) {
  197. foreach ($variable['value'] as $index => $value) {
  198. $names[$index] = isset($variable['options'][$value]) ? $variable['options'][$value] : '<' . t('Invalid option') . '>';
  199. }
  200. return implode(', ', $names);
  201. }
  202. else {
  203. return '<' . t('Invalid value') . '>';
  204. }
  205. }
  206. else {
  207. return variable_format_empty($variable);
  208. }
  209. }
  210. /**
  211. * Format array variable, handling nested arrays
  212. */
  213. function variable_format_array($variable = NULL, $options = array()) {
  214. if (empty($variable['value'])) {
  215. return variable_format_empty($variable);
  216. }
  217. else {
  218. $list = array();
  219. foreach ($variable['value'] as $index => $item) {
  220. if (is_array($item) || is_object($item)) {
  221. $list[$index] = variable_format_array(array('value' => (array)$item), $options);
  222. }
  223. else {
  224. $list[$index] = check_plain((string)$item);
  225. }
  226. }
  227. return theme('item_list', array('items' => $list));
  228. }
  229. }
  230. /**
  231. * Format array variable with known keys, handling nested arrays
  232. */
  233. function variable_format_properties($variable = NULL, $options = array()) {
  234. if (empty($variable['value'])) {
  235. return variable_format_empty($variable);
  236. }
  237. else {
  238. $rows = array();
  239. foreach ($variable['value'] as $name => $item) {
  240. $title = check_plain((string)$name);
  241. if (is_array($item) || is_object($item)) {
  242. $value = variable_format_array(array('value' => (array)$item), $options);
  243. }
  244. else {
  245. $value = check_plain((string)$item);
  246. }
  247. $rows[] = array('<em>' . $title . '</em>', $value);
  248. }
  249. return theme('table', array('rows' => $rows));
  250. }
  251. }
  252. /**
  253. * Format boolean variable
  254. */
  255. function variable_format_boolean($variable, $options = array()) {
  256. if (isset($variable['value'])) {
  257. return $variable['value'] ? t('True') : t('False');
  258. }
  259. else {
  260. return t('Undefined');
  261. }
  262. }
  263. /**
  264. * Format variable empty value
  265. */
  266. function variable_format_empty($variable) {
  267. return isset($variable['empty']) ? $variable['empty'] : t('Empty');
  268. }
  269. /**
  270. * Format variable as number.
  271. */
  272. function variable_format_number($variable, $options = array()) {
  273. if (is_numeric($variable['value'])) {
  274. return (string)$variable['value'];
  275. }
  276. elseif (empty($variable['value'])) {
  277. return '';
  278. }
  279. else {
  280. return check_plain($variable['value']);
  281. }
  282. }
  283. /**
  284. * Format variable as string. Either check plain for filter_xss.
  285. */
  286. function variable_format_string($variable, $options = array()) {
  287. if (empty($variable['value'])) {
  288. return '';
  289. }
  290. elseif (!empty($variable['allowed tags'])) {
  291. return filter_xss($variable['value'], $variable['allowed tags']);
  292. }
  293. else {
  294. return check_plain($variable['value']);
  295. }
  296. }
  297. /**
  298. * Format text variable
  299. */
  300. function variable_format_text($variable, $options = array()) {
  301. if (empty($variable['value'])) {
  302. return '';
  303. }
  304. elseif (!empty($variable['allowed tags'])) {
  305. return filter_xss($variable['value'], $variable['allowed tags']);
  306. }
  307. else {
  308. return filter_xss_admin($variable['value']);
  309. }
  310. }
  311. /**
  312. * Options callback for numeric select
  313. */
  314. function variable_options_select_number($variable, $options = array()) {
  315. return drupal_map_assoc($variable['options']);
  316. }
  317. /**
  318. * Default callback for text_format.
  319. */
  320. function variable_text_format_default($variable, $options = array()) {
  321. $out = array(
  322. 'value' => '',
  323. 'format' => filter_default_format(),
  324. );
  325. if (!empty($variable['default'])) {
  326. if (is_string($variable['default'])) {
  327. $out['value'] = $variable['default'];
  328. }
  329. elseif (is_array($variable['default'])) {
  330. if (isset($variable['default']['value'])) {
  331. $out['value'] = $variable['default']['value'];
  332. }
  333. if (isset($variable['default']['format'])) {
  334. $out['format'] = $variable['default']['format'];
  335. }
  336. }
  337. }
  338. return $out;
  339. }
  340. /**
  341. * Format text_format.
  342. */
  343. function variable_format_text_format($variable, $options = array()) {
  344. return check_markup($variable['value']['value'], $variable['value']['format']);
  345. }
  346. /**
  347. * Format multiple variable.
  348. */
  349. function variable_format_multiple($variable, $options = array()) {
  350. $rows = array();
  351. foreach ($variable['children'] as $name => $child) {
  352. $rows[$name] = array(
  353. array('data' => check_plain($child['title']), 'header' => TRUE),
  354. variable_format_value($child)
  355. );
  356. }
  357. return theme('table', array('rows' => $rows));
  358. }
  359. /**
  360. * Validate numeric variable.
  361. */
  362. function variable_validate_number($variable) {
  363. if (!is_numeric($variable['value'])) {
  364. return t('The value is not a number.');
  365. }
  366. }