variable_realm.form.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. $form = variable_base_form($form, $form_state, $variable_list, $options);
  88. // variable_base_form() adds its own submit handler overriding the default,
  89. // so we need to add it as a explicit submit callback here.
  90. $form['#submit'][] = 'variable_realm_edit_variables_form_submit';
  91. // Group variables by variable group for vertical tabls
  92. $group_list = array();
  93. foreach ($variable_list as $variable_name) {
  94. $variable_info = variable_get_info($variable_name, $options);
  95. $group = $variable_info['group'];
  96. $group_list[$group][] = $variable_name;
  97. }
  98. $form['variables'] = array(
  99. '#type' => 'vertical_tabs',
  100. '#tree' => TRUE,
  101. );
  102. foreach ($group_list as $group => $group_variables) {
  103. $group_info = variable_get_group($group);
  104. $form['variables'][$group] = array(
  105. '#type' => 'fieldset',
  106. '#title' => $group_info['title'],
  107. '#collapsible' => TRUE, '#collapsed' => TRUE,
  108. );
  109. // Set form parents for this variable / group.
  110. $options['form parents'] = array('variables', $group);
  111. $form['variables'][$group] += variable_edit_subform($group_variables, $options);
  112. }
  113. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  114. }
  115. else {
  116. $form['message']['#markup'] = '<h3>' . t('No variables have been selected as %realm_name specific.', array('%realm_name' => $controller->getTitle())) . '</h3>';
  117. }
  118. if (!empty($realm_info['select path'])) {
  119. $form['select']['#markup'] = t('To select variables to be configured, see <a href="!select-url">%realm_name variable settings</a>.', array(
  120. '%realm_name' => $realm_info['title'],
  121. '!select-url' => url($realm_info['select path'])
  122. ));
  123. }
  124. return $form;
  125. }
  126. /**
  127. * Edit variables for realm.
  128. */
  129. function variable_realm_edit_variables_form_submit($form, &$form_state) {
  130. $realm_name = $form_state['values']['realm_name'];
  131. $realm_key = $form_state['values']['realm_key'];
  132. foreach ($form_state['values']['variables'] as $group => $group_variables) {
  133. if (is_array($group_variables)) {
  134. foreach ($group_variables as $name => $value) {
  135. $current = variable_realm_get($realm_name, $realm_key, $name);
  136. if ($current !== $value) {
  137. variable_realm_set($realm_name, $realm_key, $name, $value);
  138. }
  139. }
  140. }
  141. }
  142. // Redirect later depending on query string parameters.
  143. _variable_realm_form_submit_redirect($form, $form_state);
  144. }
  145. /**
  146. * Key selector for realm forms.
  147. */
  148. function variable_realm_form_key_selector($realm_name, $current_key) {
  149. $element_name = VARIABLE_REALM_FORM_SWITCHER . $realm_name;
  150. $query_name = 'variable_realm_' . $realm_name . '_key';
  151. $controller = variable_realm_controller($realm_name);
  152. $keys = $controller->getAllKeys();
  153. // Don't show selector if there aren't any keys to select.
  154. if (empty($keys)) {
  155. return array();
  156. }
  157. $form[$element_name] = array(
  158. '#type' => 'fieldset',
  159. '#title' => t('There are %name variables in this form', array('%name' => $controller->getVariableName())),
  160. '#weight' => -100,
  161. '#description' => t('Check you are editing the variables for the right %realm value or select the desired %realm.', array('%realm' => $controller->getTitle())),
  162. );
  163. // Replace only this element on current query string, there may be others.
  164. $current_query = $_GET;
  165. unset($current_query['q']);
  166. foreach ($keys as $realm_key => $key_name) {
  167. $query[VARIABLE_REALM_QUERY_STRING . $realm_name] = $realm_key;
  168. $link = l($key_name, $_GET['q'], array('query' => $query + $current_query));
  169. $items[] = $current_key == $realm_key ? '<strong>' . $link . '</strong>' : $link;
  170. }
  171. $form[$element_name]['select_key'] = array(
  172. '#type' => 'item',
  173. '#markup' => implode(' | ', $items),
  174. );
  175. return $form;
  176. }
  177. /**
  178. * Get current realm key from query string or from current realm value.
  179. */
  180. function variable_realm_form_key_current($realm_name) {
  181. $realm_controller = variable_realm_controller($realm_name);
  182. if ($key = variable_realm_params($realm_name)) {
  183. return $key;
  184. }
  185. elseif ($key = $realm_controller->getKey()) {
  186. return $key;
  187. }
  188. elseif ($key = $realm_controller->getRequestKey()) {
  189. return $key;
  190. }
  191. else {
  192. return $realm_controller->getDefaultKey();
  193. }
  194. }
  195. /**
  196. * Alter settings form and return list of found variables.
  197. */
  198. function _variable_realm_variable_settings_form_alter(&$form, $realm_name, $variables) {
  199. $result = array();
  200. foreach (element_children($form) as $field) {
  201. if (count(element_children($form[$field])) && empty($form[$field]['#tree'])) {
  202. // Rewrite fieldsets recursively.
  203. $result += _variable_realm_variable_settings_form_alter($form[$field], $realm_name, $variables);
  204. }
  205. elseif (in_array($field, $variables)) {
  206. if (isset($form[$field]['#variable_realm'])) {
  207. // Oh-oh, variable already taken by another realm.
  208. _variable_realm_variable_settings_form_conflict($field);
  209. $form[$field]['#disabled'] = TRUE;
  210. }
  211. else {
  212. // Mark variable as already taken by a realm
  213. $form[$field]['#variable_realm'] = $realm_name;
  214. }
  215. _variable_realm_variable_settings_form_mark($realm_name, $form[$field]);
  216. // Addd field => name to result
  217. $result[$field] = !empty($form[$field]['#title']) ? $form[$field]['#title'] : $field;
  218. }
  219. }
  220. return $result;
  221. }
  222. /**
  223. * Mark variable as belonging to a realm.
  224. */
  225. function _variable_realm_variable_settings_form_mark($realm_name, &$element) {
  226. $realm_info = variable_realm_info($realm_name);
  227. // Add form field class (i18n-variable) and description text.
  228. if (!empty($realm_info['variable class'])) {
  229. $element['#attributes']['class'][] = $realm_info['variable class'];
  230. }
  231. $element['#description'] = !empty($element['#description']) ? $element['#description'] : '';
  232. $element['#description'] .= ' <strong>' . t('This is a @name variable.', array('@name' => $realm_info['variable name'])) . '</strong> ';
  233. }
  234. /**
  235. * Warning about variable conflict.
  236. */
  237. function _variable_realm_variable_settings_form_conflict($variable) {
  238. static $warnings;
  239. if (!isset($warnings[$variable])) {
  240. $warnings[$variable] = TRUE;
  241. 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');
  242. }
  243. }
  244. /**
  245. * Save realm variables and remove them from form.
  246. */
  247. function variable_realm_variable_settings_form_submit($form, &$form_state) {
  248. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  249. foreach ($form['#realm_keys'] as $realm_name => $realm_key) {
  250. $realm_controller = variable_realm_controller($realm_name);
  251. //$language = i18n_language($form_state['values']['i18n_variable_language']);
  252. //unset($form_state['values']['i18n_variable_language']);
  253. $variables = array_keys($form['#realm_variables'][$realm_name]);
  254. foreach ($variables as $variable_name) {
  255. if (isset($form_state['values'][$variable_name])) {
  256. if ($op == t('Reset to defaults')) {
  257. variable_realm_del($realm_name, $realm_key, $variable_name);
  258. }
  259. else {
  260. $value = $form_state['values'][$variable_name];
  261. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  262. $value = array_keys(array_filter($value));
  263. }
  264. variable_realm_set($realm_name, $realm_key, $variable_name, $value);
  265. }
  266. // If current is not default realm key, we don't set any global variable (without realm)
  267. if ($realm_key != $realm_controller->getDefaultKey()) {
  268. unset($form_state['values'][$variable_name]);
  269. }
  270. }
  271. }
  272. }
  273. // Redirect later depending on query string parameters.
  274. _variable_realm_form_submit_redirect($form, $form_state);
  275. // The form will go now through system_settings_form_submit()
  276. }
  277. /**
  278. * Process system_theme_settings form submissions.
  279. *
  280. * @see system_theme_settings_submit()
  281. */
  282. function variable_realm_variable_theme_form_submit($form, &$form_state) {
  283. // Regular theme submission without variable set.
  284. $values = $form_state['values'];
  285. // If the user uploaded a new logo or favicon, save it to a permanent location
  286. // and use it in place of the default theme-provided file.
  287. if ($file = $values['logo_upload']) {
  288. unset($values['logo_upload']);
  289. $filename = file_unmanaged_copy($file->uri);
  290. $values['default_logo'] = 0;
  291. $values['logo_path'] = $filename;
  292. $values['toggle_logo'] = 1;
  293. }
  294. if ($file = $values['favicon_upload']) {
  295. unset($values['favicon_upload']);
  296. $filename = file_unmanaged_copy($file->uri);
  297. $values['default_favicon'] = 0;
  298. $values['favicon_path'] = $filename;
  299. $values['toggle_favicon'] = 1;
  300. }
  301. // If the user entered a path relative to the system files directory for
  302. // a logo or favicon, store a public:// URI so the theme system can handle it.
  303. if (!empty($values['logo_path'])) {
  304. $values['logo_path'] = _system_theme_settings_validate_path($values['logo_path']);
  305. }
  306. if (!empty($values['favicon_path'])) {
  307. $values['favicon_path'] = _system_theme_settings_validate_path($values['favicon_path']);
  308. }
  309. if (empty($values['default_favicon']) && !empty($values['favicon_path'])) {
  310. $values['favicon_mimetype'] = file_get_mimetype($values['favicon_path']);
  311. }
  312. $key = $values['var'];
  313. // Exclude unnecessary elements before saving.
  314. unset($values['var'], $values['submit'], $values['reset'], $values['form_id'], $values['op'], $values['form_build_id'], $values['form_token']);
  315. // Set realm variable.
  316. $variable_name = $key;
  317. $realm_name = $form['#realm_theme'];
  318. $realm_key = $form['#realm_keys'][$realm_name];
  319. variable_realm_set($realm_name, $realm_key, $variable_name, $values);
  320. // If current is default realm key, set global variable too.
  321. $realm_controller = variable_realm_controller($realm_name);
  322. if ($realm_key == $realm_controller->getDefaultKey()) {
  323. variable_set($variable_name, $values);
  324. }
  325. // Confirmation, clear cache, taken from system_theme_settings_submit()
  326. drupal_set_message(t('The configuration options have been saved.'));
  327. cache_clear_all();
  328. // Redirect later depending on query string parameters.
  329. _variable_realm_form_submit_redirect($form, $form_state);
  330. }
  331. /**
  332. * Get variable list for settings forms handling multiple realms.
  333. *
  334. * Variables available for more than one realm, will be kept only in the list
  335. * for the realm with the higher weight.
  336. */
  337. function _variable_realm_variable_settings_form_list() {
  338. $list = array();
  339. foreach (variable_realm_list_all() as $realm_name => $controller) {
  340. if ($controller->getInfo('form settings') && ($realm_list = $controller->getEnabledVariables())) {
  341. // Remove from previous realms with lower weight.
  342. foreach ($list as $name => $variables) {
  343. $list[$name] = array_diff($variables, $realm_list);
  344. }
  345. $list[$realm_name] = $realm_list;
  346. }
  347. }
  348. return $list;
  349. }
  350. /**
  351. * Redirect to current page after form submission, using query string parameters.
  352. */
  353. function _variable_realm_form_submit_redirect($form, &$form_state) {
  354. if ($query_params = variable_realm_params()) {
  355. foreach ($query_params as $realm => $value) {
  356. $query[VARIABLE_REALM_QUERY_STRING . $realm] = $value;
  357. }
  358. // Parameters to be passed to drupal_goto().
  359. $form_state['redirect'] = array(current_path(), array('query' => $query));
  360. }
  361. }
  362. /**
  363. * Add realm switcher to the form.
  364. */
  365. function _variable_realm_variable_settings_form_switcher(&$form) {
  366. // Add switchers for current realms and current key.
  367. // Add realm values and subform realm / key selector.
  368. foreach (array_keys($form['#realm_variables']) as $realm_name) {
  369. $current_key = variable_realm_form_key_current($realm_name);
  370. $info = variable_realm_info($realm_name);
  371. if (!empty($info['form switcher'])) {
  372. $form += variable_realm_form_key_selector($realm_name, $current_key);
  373. }
  374. $form['#realm_keys'][$realm_name] = $current_key;
  375. }
  376. // Make sure realm switchers are added for all parent realms of current ones.
  377. foreach (variable_realm_list_all() as $realm_name => $realm_controller) {
  378. if (($parent_realms = $realm_controller->getParentRealms()) && !empty($form['#realm_variables'][$realm_name]) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm_name])) {
  379. // Check we have selectors for the other realms.
  380. foreach ($parent_realms as $realm) {
  381. $info = variable_realm_info($realm);
  382. if (!empty($info['form switcher']) && empty($form[VARIABLE_REALM_FORM_SWITCHER . $realm])) {
  383. $current_key = variable_realm_form_key_current($realm);
  384. $form += variable_realm_form_key_selector($realm, $current_key);
  385. }
  386. }
  387. }
  388. }
  389. }