front_page.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the front page module.
  5. */
  6. /**
  7. * Form for configuring front page settings.
  8. */
  9. function front_page_admin($form, &$form_state) {
  10. $form['front_page_enable'] = array(
  11. '#type' => 'checkbox',
  12. '#title' => t('Front Page Override'),
  13. '#description' => t('Enable this if you want the front page module to manage the home page.'),
  14. '#default_value' => variable_get('front_page_enable', 0),
  15. );
  16. // Load any existing settings and build the by redirect by role form
  17. $form['roles'] = array(
  18. '#tree' => TRUE,
  19. '#type' => 'fieldset',
  20. '#title' => t('Roles'),
  21. '#description' => t('These are the settings for each role. If Front Page Override is enabled when a user reaches the home page the site will iterate through the roles below from top to bottom until firstly the user has the role and secondly the role is not set to SKIP. If no roles get selected the site front page will be shown. To rearrange the order in which the roles are processed you may do this at the !link.', array('!link' => l(t('Arrange tab'), 'admin/config/front/arrange'))),
  22. '#collapsible' => FALSE,
  23. );
  24. // build the form for roles
  25. $roles = user_roles();
  26. $front_page_data = front_page_get_all();
  27. // Map the available modes common for all roles.
  28. $modes = array(
  29. '' => t('Skip'),
  30. 'themed' => t('Themed'),
  31. 'full' => t('Full'),
  32. 'redirect' => t('Redirect'),
  33. 'alias' => t('Alias'),
  34. );
  35. // Set the description common for all roles.
  36. $descriptions = array(
  37. '' => t('Disable functionality for this role.'),
  38. 'themed' => t('Means your default layout, theme and stylesheet will be loaded with your custom front_page.'),
  39. 'full' => t('Allows you to have a completely different layout, style sheet etc.'),
  40. 'redirect' => t('Will automatically redirect visitors to a specific page specified in the REDIRECT TO box.'),
  41. 'alias' => t('Will display the page listed in path as if it were the home page. This option does not redirect.'),
  42. );
  43. // Format the options to use as radio buttons
  44. $options = array();
  45. foreach ($modes as $key => $mode) {
  46. $options[$key] = "<strong>{$mode}:</strong> {$descriptions[$key]}";
  47. }
  48. // Iterate each role
  49. foreach ($roles as $rid => $role) {
  50. // Determine the mode for this role
  51. $mode = isset($front_page_data[$rid]['mode']) ? $front_page_data[$rid]['mode'] : '';
  52. $form['roles'][$rid] = array(
  53. '#type' => 'fieldset',
  54. '#collapsible' => TRUE,
  55. '#collapsed' => TRUE,
  56. '#title' => t('Front Page for !rolename (%mode)', array('!rolename' => $role, '%mode' => $modes[$mode])),
  57. '#weight' => isset($front_page_data[$rid]['weight']) ? $front_page_data[$rid]['weight'] : 0,
  58. );
  59. $form['roles'][$rid]['mode'] = array(
  60. '#type' => 'radios',
  61. '#title' => t('Select mode'),
  62. '#default_value' => $mode,
  63. '#options' => $options,
  64. );
  65. // We need a wrapper because of a core bug that prevents the visible
  66. // #state from properly hiding text_format widgets:
  67. // @see: https://drupal.org/node/997826
  68. $form['roles'][$rid]['data_wrapper'] = array(
  69. '#type' => 'container',
  70. '#states' => array(
  71. 'visible' => array(
  72. ':input[name="roles[' . $rid . '][mode]"]' => array(
  73. array('value' => 'full'),
  74. array('value' => 'themed')
  75. ),
  76. ),
  77. ),
  78. );
  79. $form['roles'][$rid]['data_wrapper']['data'] = array(
  80. '#type' => 'text_format',
  81. '#title' => t('Data'),
  82. '#default_value' => (isset($front_page_data[$rid]['data']) && isset($front_page_data[$rid]['mode']) && ($front_page_data[$rid]['mode'] == 'themed' || $front_page_data[$rid]['mode'] == 'full')) ? $front_page_data[$rid]['data'] : NULL,
  83. '#format' => !empty($front_page_data[$rid]['filter_format']) ? $front_page_data[$rid]['filter_format'] : NULL,
  84. '#description' => t('Paste your HTML or TEXT here.') . '<br /><br />' . t('You can paste in the full HTML code for a complete page and include a different style sheet in the HEAD of the document if you want a completely different layout and style to the rest of your site.'),
  85. );
  86. $form['roles'][$rid]['path'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Path'),
  89. '#default_value' => (isset($front_page_data[$rid]['data']) && isset($front_page_data[$rid]['mode']) && ($front_page_data[$rid]['mode'] == 'redirect' || $front_page_data[$rid]['mode'] == 'alias')) ? $front_page_data[$rid]['data'] : NULL,
  90. '#cols' => 20,
  91. '#rows' => 1,
  92. '#description' => t('If you are using <strong>Redirect</strong> or <strong>Alias</strong> you need to specify the path. An alias path should only include the URL part of a URL (eg "node/51"). A redirect path can contain a full URL including get parameters and fragment string (eg "node/51?page=5#anchor").'),
  93. '#states' => array(
  94. 'visible' => array(
  95. ':input[name="roles[' . $rid . '][mode]"]' => array(
  96. array('value' => 'redirect'),
  97. array('value' => 'alias')
  98. ),
  99. ),
  100. ),
  101. );
  102. }
  103. $form['submit'] = array(
  104. '#type' => 'submit',
  105. '#value' => t('Save Settings'),
  106. );
  107. return $form;
  108. }
  109. /**
  110. * Validation hook for front_page_admin.
  111. */
  112. function front_page_admin_validate($form, &$form_state) {
  113. if (is_array($form_state['values']['roles'])) {
  114. foreach ($form_state['values']['roles'] as $rid => $role) {
  115. switch ($role['mode']) {
  116. case 'themed':
  117. case 'full':
  118. if (empty($role['data_wrapper']['data']['value'])) {
  119. form_set_error('roles][' . $rid . '][data][value', 'You must set the data field for ' . $role['mode'] . ' mode.');
  120. }
  121. break;
  122. case 'redirect':
  123. if (empty($role['path'])) {
  124. form_set_error('roles][' . $rid . '][path', 'You must set the path field for redirect mode.');
  125. }
  126. break;
  127. case 'alias':
  128. if (empty($role['path'])) {
  129. form_set_error('roles][' . $rid . '][path', 'You must set the path field for alias mode.');
  130. }
  131. elseif (!preg_match('@^[^?#]+$@', $role['path'])) {
  132. form_set_error('roles][' . $rid . '][path', 'You must set only the URI part of a URL in alias mode.');
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * Submit hook for front_page_admin.
  141. */
  142. function front_page_admin_submit($form, &$form_state) {
  143. variable_set('front_page_enable', $form_state['values']['front_page_enable']);
  144. db_query("UPDATE {front_page} SET mode=''");
  145. if (is_array($form_state['values']['roles'])) {
  146. foreach ($form_state['values']['roles'] as $rid => $role) {
  147. switch ($role['mode']) {
  148. case 'themed':
  149. case 'full':
  150. db_merge('front_page')
  151. ->key(array('rid' => $rid))
  152. ->fields(array(
  153. 'mode' => $role['mode'],
  154. 'data' => $role['data_wrapper']['data']['value'],
  155. 'filter_format' => $role['data_wrapper']['data']['format'],
  156. ))
  157. ->execute();
  158. break;
  159. case 'redirect':
  160. case 'alias':
  161. db_merge('front_page')
  162. ->key(array('rid' => $rid))
  163. ->fields(array(
  164. 'mode' => $role['mode'],
  165. 'data' => $role['path'],
  166. 'filter_format' => '',
  167. ))
  168. ->execute();
  169. break;
  170. default:
  171. db_merge('front_page')
  172. ->key(array('rid' => $rid))
  173. ->fields(array(
  174. 'mode' => '',
  175. 'data' => '',
  176. 'filter_format' => '',
  177. ))
  178. ->execute();
  179. break;
  180. }
  181. }
  182. }
  183. drupal_set_message(t('Your settings have been saved.'));
  184. }
  185. /**
  186. * Form for arranging the roles according to what order the roles should be processed.
  187. */
  188. function front_page_admin_arrange_form($form, &$form_state) {
  189. $roles = user_roles();
  190. $front_page_data = front_page_get_all();
  191. foreach ($roles as $rid => $role) {
  192. $front_page_data[$rid]['name'] = $role;
  193. }
  194. $form['roles'] = array(
  195. '#tree' => TRUE,
  196. );
  197. foreach ($front_page_data as $role_id => $role) {
  198. $form['roles'][$role_id]['title']['#markup'] = $role['name'];
  199. $form['roles'][$role_id]['mode']['#markup'] = !empty($role['mode']) ? $role['mode'] : 'skip';
  200. $form['roles'][$role_id]['preview']['#markup'] = !empty($role['mode']) ? l(t('preview'), 'front_page/preview/' . $role_id, array('attributes' => array('target' => '_blank'))) : '';
  201. if (!empty($role['mode'])) {
  202. $form['roles'][$role_id]['enabled'] = array(
  203. '#type' => 'checkbox',
  204. '#title' => t('Enable'),
  205. '#title_display' => 'invisible',
  206. '#default_value' => TRUE,
  207. );
  208. }
  209. else {
  210. $form['roles'][$role_id]['enabled']['#markup'] = 'disabled';
  211. }
  212. $form['roles'][$role_id]['weight'] = array(
  213. '#type' => 'weight',
  214. '#title' => t('Weight'),
  215. '#title_display' => 'invisible',
  216. '#delta' => 10,
  217. '#default_value' => isset($role['weight']) ? $role['weight'] : 0,
  218. );
  219. }
  220. $form['submit'] = array(
  221. '#type' => 'submit',
  222. '#value' => t('Save Order'),
  223. );
  224. return $form;
  225. }
  226. /**
  227. * Submit hook for front_page_admin_arrange_form.
  228. */
  229. function front_page_admin_arrange_form_submit($form, &$form_state) {
  230. $front_page_data = front_page_get_all();
  231. foreach ($form_state['values']['roles'] as $rid => $role) {
  232. if (isset($role['mode']) && !$role['mode'] || !isset($front_page_data[$rid])) {
  233. db_merge('front_page')
  234. ->key(array('rid' => $rid))
  235. ->fields(array(
  236. 'mode' => '',
  237. 'data' => '',
  238. 'filter_format' => '',
  239. 'weight' => $role['weight'],
  240. ))
  241. ->execute();
  242. }
  243. else {
  244. db_merge('front_page')
  245. ->key(array('rid' => $rid))
  246. ->fields(array(
  247. 'weight' => $role['weight'],
  248. ))
  249. ->execute();
  250. }
  251. }
  252. }
  253. /**
  254. * Form for setting the <front> placeholder to another path.
  255. */
  256. function front_page_admin_home_links($form, &$form_state) {
  257. $form['front_page_home_link_path'] = array(
  258. '#type' => 'textfield',
  259. '#title' => t('Redirect your site HOME links to'),
  260. '#default_value' => variable_get('front_page_home_link_path', ''),
  261. '#cols' => 20, '#rows' => 1,
  262. '#description' => t('Specify where the user should be redirected to. An example would be <em>node/12</em>. Leave blank when you\'re not using HOME redirect.'),
  263. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=')
  264. );
  265. return system_settings_form($form);
  266. }
  267. /**
  268. * Returns HTML for the front page arrange form into a table.
  269. *
  270. * @param $variables
  271. * An associative array containing:
  272. * - form: A render element representing the form.
  273. */
  274. function theme_front_page_admin_arrange_form($variables) {
  275. $form = $variables['form'];
  276. drupal_add_tabledrag('front-page-arrange', 'order', 'sibling', 'front-page-weight');
  277. $header = array(
  278. t('Role'),
  279. t('Mode'),
  280. t('Preview'),
  281. t('Enabled'),
  282. t('Weight'),
  283. );
  284. $rows = array();
  285. foreach (element_children($form['roles']) as $rid) {
  286. $element = &$form['roles'][$rid];
  287. // Add special classes to be used for tabledrag.js.
  288. $element['weight']['#attributes']['class'] = array('front-page-weight');
  289. $row = array();
  290. $row[] = drupal_render($element['title']);
  291. $row[] = drupal_render($element['mode']);
  292. $row[] = drupal_render($element['preview']);
  293. $row[] = drupal_render($element['enabled']);
  294. $row[] = drupal_render($element['weight']);
  295. $row = array_merge(array('data' => $row), $element['#attributes']);
  296. $row['class'][] = 'draggable';
  297. $rows[] = $row;
  298. }
  299. $output = '';
  300. if (empty($rows)) {
  301. $rows[] = array(array('data' => 'no roles', 'colspan' => '5'));
  302. }
  303. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'front-page-arrange')));
  304. $output .= drupal_render_children($form);
  305. return $output;
  306. }