variable_realm.form.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @file
  4. * Administrative forms for variable realms.
  5. */
  6. /**
  7. * Select variables for realm.
  8. */
  9. function variable_realm_select_variables_form($form, &$form_state, $realm_name) {
  10. $controller = variable_realm_controller($realm_name);
  11. $current = $controller->getEnabledVariables();
  12. $optional = $controller->getAvailableVariables();
  13. // The final list will be the sum of both lists. We may have unknown variables
  14. // we want to preserve.
  15. $list = array_unique(array_merge($optional, $current));
  16. $form['realm_name'] = array('#type' => 'value', '#value' => $realm_name);
  17. $form['message']['select']['#markup'] = '<h3>' . t('Select variables to be set for this realm.') . '</h3>';
  18. if ($current) {
  19. $form['message']['current']['#markup'] = '<p>' . t('Currently selected variables are: <em>!variables</em>', array('!variables' => variable_list_name($current))) . '</p>';
  20. }
  21. $form['variables'] = array(
  22. '#type' => 'vertical_tabs',
  23. '#tree' => TRUE,
  24. );
  25. $variable_groups = variable_group_variables($list);
  26. foreach ($variable_groups as $group => $group_list) {
  27. $group_info = variable_get_group($group);
  28. $group_current = array_intersect($group_list, $current);
  29. $form['variables'][$group] = array(
  30. '#type' => 'fieldset',
  31. '#title' => $group_info['title'],
  32. '#theme' => 'variable_table_select',
  33. '#collapsible' => TRUE, '#collapsed' => TRUE,
  34. );
  35. foreach ($group_list as $name) {
  36. // Variable names may clash with form element names, so we need to replace '[' and ']'
  37. $safename = str_replace(array('[', ']'), array('<', '>'), $name);
  38. $form['variables'][$group][$safename] = array(
  39. '#type' => 'checkbox',
  40. '#default_value' => in_array($name, $group_current),
  41. '#variable_name' => $name,
  42. '#parents' => array('variables', $safename),
  43. );
  44. }
  45. }
  46. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  47. return $form;
  48. }
  49. /**
  50. * Select variables for realm.
  51. */
  52. function variable_realm_select_variables_form_submit($form, &$form_state) {
  53. // Get realm name and current list of variables.
  54. $realm_name = $form_state['values']['realm_name'];
  55. $controller = variable_realm_controller($realm_name);
  56. $old_variables = $controller->getEnabledVariables();
  57. // Get main variable names
  58. $variables = $form_state['values']['variables'];
  59. unset($variables['variables__active_tab']);
  60. $variables = array_keys(array_filter($variables));
  61. // Translate variable names
  62. foreach ($variables as $index => $name) {
  63. $variables[$index] = str_replace(array('<', '>'), array('[', ']'), $name);
  64. }
  65. // Hook for modules to alter this variable list.
  66. drupal_alter('variable_realm_variable_list', $variables, $realm_name);
  67. // And save the list to a variable.
  68. $controller->setRealmVariable('list', $variables);
  69. // Spawn multiple variables and translate into actual variables
  70. $new_list = variable_children($variables);
  71. // Delete variables from realm that are not in the new list.
  72. $old_list = variable_children($old_variables);
  73. foreach (array_diff($old_list, $new_list) as $name) {
  74. $controller->deleteVariable($name);
  75. drupal_set_message(t('Deleted existing values of %name from realm variables.', array('%name' => $name)));
  76. }
  77. }
  78. /**
  79. * Edit variables for realm.
  80. */
  81. function variable_realm_edit_variables_form($form, &$form_state, $realm_name, $realm_key) {
  82. $controller = variable_realm_controller($realm_name);
  83. $form['realm_name'] = array('#type' => 'value', '#value' => $realm_name);
  84. $form['realm_key'] = array('#type' => 'value', '#value' => $realm_key);
  85. $options['realm'] = variable_realm($realm_name, $realm_key);
  86. if ($variable_list = $controller->getEnabledVariables()) {
  87. // Group variables by variable group for vertical tabls
  88. $group_list = array();
  89. foreach ($variable_list as $variable_name) {
  90. $variable_info = variable_get_info($variable_name, $options);
  91. $group = $variable_info['group'];
  92. $group_list[$group][] = $variable_name;
  93. }
  94. $form['variables'] = array(
  95. '#type' => 'vertical_tabs',
  96. '#tree' => TRUE,
  97. );
  98. foreach ($group_list as $group => $group_variables) {
  99. $group_info = variable_get_group($group);
  100. $form['variables'][$group] = array(
  101. '#type' => 'fieldset',
  102. '#title' => $group_info['title'],
  103. '#collapsible' => TRUE, '#collapsed' => TRUE,
  104. );
  105. // Set form parents for this variable / group.
  106. $options['form parents'] = array('variables', $group);
  107. $form['variables'][$group] += variable_edit_subform($group_variables, $options);
  108. }
  109. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  110. }
  111. else {
  112. $form['message']['#markup'] = '<h3>' . t('No variables have been selected as %realm_name specific.', array('%realm_name' => $controller->getTitle())) . '</h3>';
  113. }
  114. if (!empty($realm_info['select path'])) {
  115. $form['select']['#markup'] = t('To select variables to be configured, see <a href="!select-url">%realm_name variable settings</a>.', array(
  116. '%realm_name' => $realm_info['title'],
  117. '!select-url' => url($realm_info['select path'])
  118. ));
  119. }
  120. return $form;
  121. }
  122. /**
  123. * Edit variables for realm.
  124. */
  125. function variable_realm_edit_variables_form_submit($form, &$form_state) {
  126. $realm_name = $form_state['values']['realm_name'];
  127. $realm_key = $form_state['values']['realm_key'];
  128. foreach ($form_state['values']['variables'] as $group => $group_variables) {
  129. if (is_array($group_variables)) {
  130. foreach ($group_variables as $name => $value) {
  131. $current = variable_realm_get($realm_name, $realm_key, $name);
  132. if ($current !== $value) {
  133. variable_realm_set($realm_name, $realm_key, $name, $value);
  134. }
  135. }
  136. }
  137. }
  138. // Redirect later depending on query string parameters.
  139. _variable_realm_form_submit_redirect($form, $form_state);
  140. }
  141. /**
  142. * Key selector for realm forms.
  143. */
  144. function variable_realm_form_key_selector($realm_name, $current_key) {
  145. $element_name = VARIABLE_REALM_FORM_SWITCHER . $realm_name;
  146. $query_name = 'variable_realm_' . $realm_name . '_key';
  147. $controller = variable_realm_controller($realm_name);
  148. $form[$element_name] = array(
  149. '#type' => 'fieldset',
  150. '#title' => t('There are %name variables in this form', array('%name' => $controller->getVariableName())),
  151. '#weight' => -100,
  152. '#description' => t('Check you are editing the variables for the right %realm value or select the desired %realm.', array('%realm' => $controller->getTitle())),
  153. );
  154. // Replace only this element on current query string, there may be others.
  155. $current_query = $_GET;
  156. unset($current_query['q']);
  157. foreach ($controller->getAllKeys() as $realm_key => $key_name) {
  158. $query[VARIABLE_REALM_QUERY_STRING . $realm_name] = $realm_key;
  159. $link = l($key_name, $_GET['q'], array('query' => $query + $current_query));
  160. $items[] = $current_key == $realm_key ? '<strong>' . $link . '</strong>' : $link;
  161. }
  162. $form[$element_name]['select_key'] = array(
  163. '#type' => 'item',
  164. '#markup' => implode(' | ', $items),
  165. );
  166. return $form;
  167. }
  168. /**
  169. * Get current realm key from query string or from current realm value.
  170. */
  171. function variable_realm_form_key_current($realm_name) {
  172. $realm_controller = variable_realm_controller($realm_name);
  173. if ($key = variable_realm_params($realm_name)) {
  174. return $key;
  175. }
  176. elseif ($key = $realm_controller->getKey()) {
  177. return $key;
  178. }
  179. elseif ($key = $realm_controller->getRequestKey()) {
  180. return $key;
  181. }
  182. else {
  183. return $realm_controller->getDefaultKey();
  184. }
  185. }
  186. /**
  187. * Alter settings form and return list of found variables.
  188. */
  189. function _variable_realm_variable_settings_form_alter(&$form, $realm_name, $variables) {
  190. $result = array();
  191. foreach (element_children($form) as $field) {
  192. if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
  193. // Rewrite fieldsets recursively.
  194. $result += _variable_realm_variable_settings_form_alter($form[$field], $realm_name, $variables);
  195. }
  196. elseif (in_array($field, $variables)) {
  197. if (isset($form[$field]['#variable_realm'])) {
  198. // Oh-oh, variable already taken by another realm.
  199. _variable_realm_variable_settings_form_conflict($field);
  200. $form[$field]['#disabled'] = TRUE;
  201. }
  202. else {
  203. // Mark variable as already taken by a realm
  204. $form[$field]['#variable_realm'] = $realm_name;
  205. }
  206. _variable_realm_variable_settings_form_mark($realm_name, $form[$field]);
  207. // Addd field => name to result
  208. $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
  209. }
  210. }
  211. return $result;
  212. }
  213. /**
  214. * Mark variable as belonging to a realm.
  215. */
  216. function _variable_realm_variable_settings_form_mark($realm_name, &$element) {
  217. $realm_info = variable_realm_info($realm_name);
  218. // Add form field class (i18n-variable) and description text.
  219. if (!empty($realm_info['variable class'])) {
  220. $element['#attributes']['class'][] = $realm_info['variable class'];
  221. }
  222. $element['#description'] = !empty($element['#description']) ? $element['#description'] : '';
  223. $element['#description'] .= ' <strong>' . t('This is a @name variable.', array('@name' => $realm_info['variable name'])) . '</strong> ';
  224. }
  225. /**
  226. * Warning about variable conflict.
  227. */
  228. function _variable_realm_variable_settings_form_conflict($variable) {
  229. static $warnings;
  230. if (!isset($warnings[$variable])) {
  231. $warnings[$variable] = TRUE;
  232. drupal_set_message(t('There are conflicting realm variables in the form. The variable %name is enabled for more than one realm. Review your realm settings', array('%name' => variable_name($variable))), 'warning');
  233. }
  234. }
  235. /**
  236. * Save realm variables and remove them from form.
  237. */
  238. function variable_realm_variable_settings_form_submit($form, &$form_state) {
  239. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  240. foreach ($form['#realm_keys'] as $realm_name => $realm_key) {
  241. $realm_controller = variable_realm_controller($realm_name);
  242. //$language = i18n_language($form_state['values']['i18n_variable_language']);
  243. //unset($form_state['values']['i18n_variable_language']);
  244. $variables = array_keys($form['#realm_variables'][$realm_name]);
  245. foreach ($variables as $variable_name) {
  246. if (isset($form_state['values'][$variable_name])) {
  247. if ($op == t('Reset to defaults')) {
  248. variable_realm_del($realm_name, $realm_key, $variable_name);
  249. }
  250. else {
  251. $value = $form_state['values'][$variable_name];
  252. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  253. $value = array_keys(array_filter($value));
  254. }
  255. variable_realm_set($realm_name, $realm_key, $variable_name, $value);
  256. }
  257. // If current is not default realm key, we don't set any global variable (without realm)
  258. if ($realm_key != $realm_controller->getDefaultKey()) {
  259. unset($form_state['values'][$variable_name]);
  260. }
  261. }
  262. }
  263. }
  264. // Redirect later depending on query string parameters.
  265. _variable_realm_form_submit_redirect($form, $form_state);
  266. // The form will go now through system_settings_form_submit()
  267. }
  268. /**
  269. * Process system_theme_settings form submissions.
  270. *
  271. * @see system_theme_settings_submit()
  272. */
  273. function variable_realm_variable_theme_form_submit($form, &$form_state) {
  274. // Regular theme submission without variable set.
  275. $values = $form_state['values'];
  276. // If the user uploaded a new logo or favicon, save it to a permanent location
  277. // and use it in place of the default theme-provided file.
  278. if ($file = $values['logo_upload']) {
  279. unset($values['logo_upload']);
  280. $filename = file_unmanaged_copy($file->uri);
  281. $values['default_logo'] = 0;
  282. $values['logo_path'] = $filename;
  283. $values['toggle_logo'] = 1;
  284. }
  285. if ($file = $values['favicon_upload']) {
  286. unset($values['favicon_upload']);
  287. $filename = file_unmanaged_copy($file->uri);
  288. $values['default_favicon'] = 0;
  289. $values['favicon_path'] = $filename;
  290. $values['toggle_favicon'] = 1;
  291. }
  292. // If the user entered a path relative to the system files directory for
  293. // a logo or favicon, store a public:// URI so the theme system can handle it.
  294. if (!empty($values['logo_path'])) {
  295. $values['logo_path'] = _system_theme_settings_validate_path($values['logo_path']);
  296. }
  297. if (!empty($values['favicon_path'])) {
  298. $values['favicon_path'] = _system_theme_settings_validate_path($values['favicon_path']);
  299. }
  300. if (empty($values['default_favicon']) && !empty($values['favicon_path'])) {
  301. $values['favicon_mimetype'] = file_get_mimetype($values['favicon_path']);
  302. }
  303. $key = $values['var'];
  304. // Exclude unnecessary elements before saving.
  305. unset($values['var'], $values['submit'], $values['reset'], $values['form_id'], $values['op'], $values['form_build_id'], $values['form_token']);
  306. // Set realm variable.
  307. $variable_name = $key;
  308. $realm_name = $form['#realm_theme'];
  309. $realm_key = $form['#realm_keys'][$realm_name];
  310. variable_realm_set($realm_name, $realm_key, $variable_name, $values);
  311. // If current is default realm key, set global variable too.
  312. $realm_controller = variable_realm_controller($realm_name);
  313. if ($realm_key == $realm_controller->getDefaultKey()) {
  314. variable_set($variable_name, $values);
  315. }
  316. // Confirmation, clear cache, taken from system_theme_settings_submit()
  317. drupal_set_message(t('The configuration options have been saved.'));
  318. cache_clear_all();
  319. // Redirect later depending on query string parameters.
  320. _variable_realm_form_submit_redirect($form, $form_state);
  321. }
  322. /**
  323. * Get variable list for settings forms handling multiple realms.
  324. *
  325. * Variables available for more than one realm, will be kept only in the list
  326. * for the realm with the higher weight.
  327. */
  328. function _variable_realm_variable_settings_form_list() {
  329. $list = array();
  330. foreach (variable_realm_list_all() as $realm_name => $controller) {
  331. if ($controller->getInfo('form settings') && ($realm_list = $controller->getEnabledVariables())) {
  332. // Remove from previous realms with lower weight.
  333. foreach ($list as $name => $variables) {
  334. $list[$name] = array_diff($variables, $realm_list);
  335. }
  336. $list[$realm_name] = $realm_list;
  337. }
  338. }
  339. return $list;
  340. }
  341. /**
  342. * Redirect to current page after form submission, using query string parameters.
  343. */
  344. function _variable_realm_form_submit_redirect($form, &$form_state) {
  345. if ($query_params = variable_realm_params()) {
  346. foreach ($query_params as $realm => $value) {
  347. $query[VARIABLE_REALM_QUERY_STRING . $realm] = $value;
  348. }
  349. // Parameters to be passed to drupal_goto().
  350. $form_state['redirect'] = array(current_path(), array('query' => $query));
  351. }
  352. }
  353. /**
  354. * Add realm switcher to the form.
  355. */
  356. function _variable_realm_variable_settings_form_switcher(&$form) {
  357. // Add switchers for current realms and current key.
  358. // Add realm values and subform realm / key selector.
  359. foreach (array_keys($form['#realm_variables']) as $realm_name) {
  360. $current_key = variable_realm_form_key_current($realm_name);
  361. $info = variable_realm_info($realm_name);
  362. if (!empty($info['form switcher'])) {
  363. $form += variable_realm_form_key_selector($realm_name, $current_key);
  364. }
  365. $form['#realm_keys'][$realm_name] = $current_key;
  366. }
  367. // Make sure realm switchers are added for all parent realms of current ones.
  368. foreach (variable_realm_list_all() as $realm_name => $realm_controller) {
  369. if (($parent_realms = $realm_controller->getParentRealms()) && !empty($form['#realm_variables'][$realm_name]) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm_name])) {
  370. // Check we have selectors for the other realms.
  371. foreach ($parent_realms as $realm) {
  372. $info = variable_realm_info($realm);
  373. if (!empty($info['form switcher']) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm])) {
  374. $current_key = variable_realm_form_key_current($realm);
  375. $form += variable_realm_form_key_selector($realm, $current_key);
  376. }
  377. }
  378. }
  379. }
  380. }