login_destination.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callback file for the Login Destination module.
  5. */
  6. /**
  7. * List destination rules.
  8. */
  9. function login_destination_overview() {
  10. $header = array(
  11. t('Destination'),
  12. t('Triggers'),
  13. t('Pages'),
  14. t('Roles'),
  15. array('data' => t('Operations'), 'colspan' => 2),
  16. );
  17. $rows = array();
  18. // Get all login destination rules from the database.
  19. $result = db_select('login_destination', 'l')
  20. ->fields('l', array('id', 'triggers', 'roles', 'pages_type', 'pages', 'destination'))
  21. ->orderBy('weight')
  22. ->execute()
  23. ->fetchAll();
  24. // Loop through the categories and add them to the table.
  25. foreach ($result as $data) {
  26. $triggers = array_map('check_plain', unserialize($data->triggers));
  27. if (empty($triggers))
  28. $triggers = array();
  29. $roles = array_map('check_plain', unserialize($data->roles));
  30. if (empty($roles))
  31. $roles = array();
  32. $rows[] = array(
  33. theme('login_destination_destination', array('destination' => $data->destination)),
  34. theme('login_destination_triggers', array('items' => $triggers)),
  35. theme('login_destination_pages', array('pages' => $data->pages, 'pages_type' => $data->pages_type)),
  36. theme('login_destination_roles', array('items' => $roles)),
  37. l(t('Edit'), 'admin/config/people/login-destination/edit/' . $data->id),
  38. l(t('Delete'), 'admin/config/people/login-destination/delete/' . $data->id),
  39. );
  40. }
  41. if (!$rows) {
  42. $rows[] = array(array(
  43. 'data' => t('No rules available.'),
  44. 'colspan' => 6,
  45. ));
  46. }
  47. $build['login-destination_table'] = array(
  48. '#theme' => 'table',
  49. '#header' => $header,
  50. '#rows' => $rows,
  51. );
  52. return $build;
  53. }
  54. function theme_login_destination_destination($variables) {
  55. $output = nl2br(check_plain($variables['destination']));
  56. if (empty($output)) {
  57. $output = '<i>' . t('Empty') . '</i>';
  58. }
  59. return $output;
  60. }
  61. function theme_login_destination_triggers($variables) {
  62. $items = array_map('check_plain', $variables['items']);
  63. if (empty($items)) {
  64. return '<i>' . t('All triggers') . '</i>';
  65. }
  66. $output = '';
  67. foreach ($items as &$item) {
  68. switch ($item) {
  69. case 'login':
  70. $item = 'Login';
  71. break;
  72. case 'logout':
  73. $item = 'Logout';
  74. break;
  75. }
  76. $output .= $item . "<br/>";
  77. }
  78. return $output;
  79. }
  80. function theme_login_destination_pages($variables) {
  81. $type = $variables['pages_type'];
  82. if ($type == LOGIN_DESTINATION_REDIRECT_PHP) {
  83. return nl2br(check_plain($variables['pages']));
  84. }
  85. $pages = trim($variables['pages']);
  86. if (empty($pages)) {
  87. if ($type == LOGIN_DESTINATION_REDIRECT_NOTLISTED) {
  88. return '<i>' . t('All pages') . '</i>';
  89. }
  90. else {
  91. return '<i>' . t('No pages') . '</i>';
  92. }
  93. }
  94. $pages = explode("\n", preg_replace('/\r/', '', check_plain($variables['pages'])));
  95. $output = '';
  96. foreach ($pages as &$page) {
  97. if ($type == LOGIN_DESTINATION_REDIRECT_NOTLISTED) {
  98. $output .= "~ ";
  99. }
  100. $output .= $page . "<br/>";
  101. }
  102. return $output;
  103. }
  104. function theme_login_destination_roles($variables) {
  105. $items = array_values(array_intersect_key(_login_destination_role_options(), $variables['items']));
  106. if (empty($items)) {
  107. return '<i>' . t('All roles') . '</i>';
  108. }
  109. return theme('item_list', array('items' => $items));
  110. }
  111. /**
  112. * Category edit page.
  113. */
  114. function login_destination_edit_form($form, &$form_state, array $rule = array()) {
  115. // default values
  116. $rule += array(
  117. 'triggers' => array(),
  118. 'roles' => array(),
  119. 'pages_type' => LOGIN_DESTINATION_REDIRECT_NOTLISTED,
  120. 'pages' => '',
  121. 'destination_type' => LOGIN_DESTINATION_STATIC,
  122. 'destination' => '<front>',
  123. 'id' => NULL,
  124. 'weight' => 0,
  125. );
  126. $access = user_access('use PHP for settings');
  127. $type = $rule['destination_type'];
  128. if ($type == LOGIN_DESTINATION_SNIPPET && !$access) {
  129. $form['destination_type'] = array(
  130. '#type' => 'value',
  131. '#value' => LOGIN_DESTINATION_SNIPPET,
  132. );
  133. $form['destination'] = array(
  134. '#type' => 'value',
  135. '#value' => $rule['destination'],
  136. );
  137. }
  138. else {
  139. $options = array(
  140. LOGIN_DESTINATION_STATIC => t('Internal page or external URL'),
  141. );
  142. $description = t("Specify page by using its path. Example path is %blog for the blog page. %front is the front page. %current is the current page. Precede with http:// for an external URL. Leave empty to redirect to a default page.", array('%blog' => 'blog', '%front' => '<front>', '%current' => '<current>'));
  143. if (module_exists('php') && $access) {
  144. $options += array(LOGIN_DESTINATION_SNIPPET => t('Page returned by this PHP code (experts only)'));
  145. $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. It should return either a string value or an array of params that the %function function will understand, e.g. %example. For more information, see the online API entry for <a href="@url">url function</a>. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>', '%function' => 'url($path = \'\', array $options = array())', '%example' => '<?php return array(\'blog\', array(\'fragment\' => \'overlay=admin/config\', ), ); ?>', '@url' => 'http://api.drupal.org/api/drupal/includes--common.inc/function/url/7'));
  146. }
  147. $form['destination_type'] = array(
  148. '#type' => 'radios',
  149. '#title' => 'Redirect to page',
  150. '#default_value' => $type,
  151. '#options' => $options,
  152. );
  153. $form['destination'] = array(
  154. '#type' => 'textarea',
  155. '#default_value' => $rule['destination'],
  156. '#description' => $description,
  157. );
  158. }
  159. $triggers = array_map('check_plain', $rule['triggers']);
  160. if (empty($triggers)) {
  161. $triggers = array();
  162. }
  163. $form['triggers'] = array(
  164. '#type' => 'checkboxes',
  165. '#title' => t('Redirect upon triggers'),
  166. '#options' => array('login' => 'Login, registration, one-time login link', 'logout' => 'Logout'),
  167. '#default_value' => $triggers,
  168. '#description' => 'Redirect only upon selected trigger(s). If you select no triggers, all of them will be used.',
  169. );
  170. $type = $rule['pages_type'];
  171. if ($type == LOGIN_DESTINATION_REDIRECT_PHP && !$access) {
  172. $form['pages_type'] = array(
  173. '#type' => 'value',
  174. '#value' => LOGIN_DESTINATION_REDIRECT_PHP,
  175. );
  176. $form['pages'] = array(
  177. '#type' => 'value',
  178. '#value' => $rule['destination'],
  179. );
  180. }
  181. else {
  182. $options = array(
  183. LOGIN_DESTINATION_REDIRECT_NOTLISTED => t('All pages except those listed'),
  184. LOGIN_DESTINATION_REDIRECT_LISTED => t('Only the listed pages'),
  185. );
  186. $description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. %login is the login form. %register is the registration form. %reset is the one-time login (e-mail validation).", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>', '%login' => 'user', '%register' => 'user/register', '%reset' => 'user/*/edit'));
  187. if (module_exists('php') && $access) {
  188. $options += array(LOGIN_DESTINATION_REDIRECT_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
  189. $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
  190. }
  191. $form['pages_type'] = array(
  192. '#type' => 'radios',
  193. '#title' => t('Redirect from specific pages'),
  194. '#default_value' => $type,
  195. '#options' => $options,
  196. );
  197. $form['pages'] = array(
  198. '#type' => 'textarea',
  199. '#default_value' => $rule['pages'],
  200. '#description' => $description,
  201. );
  202. }
  203. $default_role_options = array_map('check_plain', $rule['roles']);
  204. if (empty($default_role_options)) {
  205. $default_role_options = array();
  206. }
  207. $form['roles'] = array(
  208. '#type' => 'checkboxes',
  209. '#title' => t('Redirect users with roles'),
  210. '#options' => _login_destination_role_options(),
  211. '#default_value' => $default_role_options,
  212. '#description' => 'Redirect only the selected role(s). If you select no roles, all users will be redirected.',
  213. );
  214. $form['weight'] = array(
  215. '#type' => 'weight',
  216. '#title' => t('Weight'),
  217. '#default_value' => $rule['weight'],
  218. '#description' => t('When evaluating login destination rules, those with lighter (smaller) weights get evaluated before rules with heavier (larger) weights.'),
  219. );
  220. $form['actions'] = array('#type' => 'actions');
  221. $form['actions']['submit'] = array(
  222. '#type' => 'submit',
  223. '#value' => t('Save'),
  224. );
  225. if ($rule['id']) {
  226. $form['id'] = array(
  227. '#type' => 'hidden',
  228. '#value' => $rule['id'],
  229. );
  230. }
  231. return $form;
  232. }
  233. /**
  234. * Validate the contact category edit page form submission.
  235. */
  236. function login_destination_edit_form_validate($form, &$form_state) {
  237. }
  238. /**
  239. * Process the contact category edit page form submission.
  240. */
  241. function login_destination_edit_form_submit($form, &$form_state) {
  242. $form_state['values']['triggers'] = serialize(array_filter($form_state['values']['triggers']));
  243. $form_state['values']['roles'] = serialize(array_filter($form_state['values']['roles']));
  244. if (empty($form_state['values']['id'])) {
  245. drupal_write_record('login_destination', $form_state['values']);
  246. }
  247. else {
  248. drupal_write_record('login_destination', $form_state['values'], array('id'));
  249. }
  250. drupal_set_message(t('Login destination to %destination has been saved.', array('%destination' => $form_state['values']['destination'])));
  251. $form_state['redirect'] = 'admin/config/people/login-destination';
  252. }
  253. /**
  254. * Form builder for deleting a login destination.
  255. */
  256. function login_destination_delete_form($form, &$form_state, array $rule) {
  257. $form['login_destination'] = array(
  258. '#type' => 'value',
  259. '#value' => $rule,
  260. );
  261. return confirm_form(
  262. $form,
  263. t('Are you sure you want to delete the login destination %destination ?', array('%destination' => $rule['destination'])),
  264. 'admin/config/people/login-destination',
  265. t('This action cannot be undone.'),
  266. t('Delete'),
  267. t('Cancel')
  268. );
  269. }
  270. /**
  271. * Submit handler for the confirm delete login destination form.
  272. */
  273. function login_destination_delete_form_submit($form, &$form_state) {
  274. $rule = $form['login_destination']['#value'];
  275. db_delete('login_destination')
  276. ->condition('id', $rule['id'])
  277. ->execute();
  278. drupal_set_message(t('The login destination %destination has been deleted.', array('%destination' => $rule['destination'])));
  279. $form_state['redirect'] = 'admin/config/people/login-destination';
  280. }
  281. /**
  282. * Settings page.
  283. */
  284. function login_destination_settings() {
  285. $form = array();
  286. $form['settings']['login_destination_preserve_destination'] = array(
  287. '#type' => 'checkbox',
  288. '#default_value' => variable_get('login_destination_preserve_destination', FALSE),
  289. '#title' => t('Preserve the destination parameter'),
  290. '#description' => t("The 'destination' GET parameter will have priority over the settings of this module. With this setting enabled, redirect from the user login block will not work."),
  291. );
  292. $form['settings']['login_destination_immediate_redirect'] = array(
  293. '#type' => 'checkbox',
  294. '#default_value' => variable_get('login_destination_immediate_redirect', FALSE),
  295. '#title' => t('Redirect immediately after using one-time login link'),
  296. '#description' => t("User will be redirected before given the possibility to change their password."),
  297. );
  298. return system_settings_form($form);
  299. }