strongarm.admin.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Variable management strongarm form.
  4. */
  5. function strongarm_admin_form($form_state) {
  6. global $conf;
  7. $vars = strongarm_vars_load(TRUE, TRUE);
  8. $form = array('#theme' => 'strongarm_admin_form',);
  9. foreach ($vars as $name => $variable) {
  10. if ($variable->export_type & EXPORT_IN_CODE) {
  11. $default = ctools_get_default_object('variable', $name);
  12. // If variable value does not match global $conf, this value has been
  13. // hardcoded (e.g. in settings.php) and has been allowed to pass
  14. // through. It cannot be reverted.
  15. $hardcoded = FALSE;
  16. $restorable = FALSE;
  17. if (isset($conf[$name]) && $conf[$name] !== $variable->value) {
  18. $storage = t('Hardcoded');
  19. $hardcoded = TRUE;
  20. }
  21. elseif (!empty($variable->in_code_only)) {
  22. $storage = t('In code');
  23. $restorable = TRUE;
  24. }
  25. elseif ($variable->value != $default->value) {
  26. $storage = t('Overridden');
  27. $restorable = TRUE;
  28. }
  29. else {
  30. $storage = t('Saved to DB');
  31. }
  32. $value = $hardcoded ? $conf[$name] : $variable->value;
  33. // If the variable is in the database and differs from its code value,
  34. // allow administrator to revert its value.
  35. if ($restorable) {
  36. $form['revert']['#tree'] = TRUE;
  37. $form['revert'][$name]['revert'] = array('#type' => 'checkbox');
  38. $form['revert'][$name]['value'] = array('#type' => 'value', '#value' => $default->value);
  39. }
  40. if (module_exists('variable') && ($info = variable_get_info($name))) {
  41. $form['name'][$name] = array('#markup' => $info['title'] . '<br/>' . $name);
  42. $form['storage'][$name] = array('#markup' => $storage);
  43. $info['value'] = $variable->value;
  44. $form['value'][$name] = array('#markup' => variable_format_value($info));
  45. }
  46. else {
  47. $form['name'][$name] = array('#markup' => $name);
  48. $form['storage'][$name] = array('#markup' => $storage);
  49. $form['value'][$name] = array('#markup' => check_plain(_strongarm_readable($value)));
  50. }
  51. }
  52. }
  53. if (!empty($form['revert'])) {
  54. $form['submit'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Restore values to DB'),
  57. '#submit' => array('strongarm_admin_revert_submit'),
  58. );
  59. }
  60. return $form;
  61. }
  62. /**
  63. * Revert form submit handler.
  64. */
  65. function strongarm_admin_revert_submit(&$form, &$form_state) {
  66. if (!empty($form_state['values']['revert'])) {
  67. foreach ($form_state['values']['revert'] as $name => $revert) {
  68. if ($revert['revert']) {
  69. variable_set($name, $revert['value']);
  70. }
  71. }
  72. strongarm_flush_caches();
  73. }
  74. }
  75. /**
  76. * Display variables in a nicer way.
  77. */
  78. function _strongarm_readable($var) {
  79. if (is_string($var) || is_numeric($var)) {
  80. return truncate_utf8($var, 30, TRUE, TRUE);
  81. }
  82. else if (is_bool($var)) {
  83. return $var ? 'TRUE' : 'FALSE';
  84. }
  85. else if (is_array($var)) {
  86. $test = $detected = array();
  87. $test['keys'] = array_keys($var);
  88. $test['values'] = array_values($var);
  89. foreach ($test as $type => $values) {
  90. $numeric = TRUE;
  91. $sequential = 0;
  92. $boolean = TRUE;
  93. foreach ($values as $v) {
  94. $numeric = is_numeric($v) && $numeric;
  95. $sequential = is_numeric($v) && ($sequential == $v) && $sequential !== FALSE ? $sequential + 1 : FALSE;
  96. $boolean = $boolean && ($v === 0 || $v === 1 || $v === '1' || $v === '0' || $v === TRUE || $v === FALSE);
  97. }
  98. $detected[$type]['numeric'] = $numeric;
  99. $detected[$type]['sequential'] = $sequential !== FALSE;
  100. $detected[$type]['boolean'] = $boolean;
  101. }
  102. // List of things
  103. if (!empty($var) && $detected['keys']['numeric'] && $detected['keys']['sequential']) {
  104. return truncate_utf8(implode(', ', $var), 30, TRUE, TRUE);
  105. }
  106. return '-';
  107. }
  108. }
  109. /**
  110. * Theme function for the strongarm admin form.
  111. */
  112. function theme_strongarm_admin_form(&$vars) {
  113. $form = $vars['form'];
  114. drupal_add_js('misc/tableselect.js');
  115. $rows = $headers = array();
  116. $headers[] = array('class' => array('select-all'));
  117. $headers[] = t('Variable');
  118. $headers[] = t('Storage');
  119. $headers[] = t('Value');
  120. foreach (element_children($form['name']) as $name) {
  121. $row = array();
  122. $row[] = isset($form['revert'][$name]) ? drupal_render($form['revert'][$name]) : '';
  123. $row[] = drupal_render($form['name'][$name]);
  124. $row[] = drupal_render($form['storage'][$name]);
  125. $row[] = drupal_render($form['value'][$name]);
  126. $rows[] = $row;
  127. }
  128. $output = theme('table', array('header' => $headers, 'rows' => $rows));
  129. $output .= drupal_render_children($form);
  130. return $output;
  131. }