login_destination.admin.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callback file for the Login Destination module.
  5. */
  6. /**
  7. * Form for editing an entire login destination at once.
  8. *
  9. * Shows list of all login destination.
  10. */
  11. function login_destination_overview_form($form, &$form_state) {
  12. // Get all login destination rules from the database.
  13. $result = db_select('login_destination', 'l')
  14. ->fields('l', array(
  15. 'id',
  16. 'triggers',
  17. 'roles',
  18. 'pages_type',
  19. 'pages',
  20. 'destination',
  21. 'weight',
  22. 'enabled',
  23. )
  24. )
  25. ->orderBy('weight')
  26. ->execute()
  27. ->fetchAll();
  28. $form['#tree'] = TRUE;
  29. // Loop through the categories and add them to the table.
  30. foreach ($result as $data) {
  31. $triggers = array_map('check_plain', unserialize($data->triggers));
  32. if (empty($triggers)) {
  33. $triggers = array();
  34. }
  35. $roles = array_map('check_plain', unserialize($data->roles));
  36. if (empty($roles)) {
  37. $roles = array();
  38. }
  39. $form[$data->id]['destination']['#markup'] = theme('login_destination_destination', array('destination' => $data->destination));
  40. $form[$data->id]['triggers']['#markup'] = theme('login_destination_triggers', array('items' => $triggers));
  41. $form[$data->id]['pages']['#markup'] = theme('login_destination_pages', array('pages' => $data->pages, 'pages_type' => $data->pages_type));
  42. $form[$data->id]['roles']['#markup'] = theme('login_destination_roles', array('items' => $roles));
  43. $form[$data->id]['weight'] = array(
  44. '#type' => 'weight',
  45. '#title' => t('Weight'),
  46. '#delta' => 50,
  47. '#default_value' => $data->weight,
  48. '#title_display' => 'invisible',
  49. );
  50. $form[$data->id]['enabled'] = array(
  51. '#type' => 'checkbox',
  52. '#title' => t('Enabled'),
  53. '#default_value' => $data->enabled,
  54. '#title_display' => 'invisible',
  55. );
  56. // Build a list of operations.
  57. $operations = array();
  58. $operations['edit'] = array(
  59. '#type' => 'link',
  60. '#title' => t('edit'),
  61. '#href' => 'admin/config/people/login-destination/edit/' . $data->id,
  62. );
  63. $operations['delete'] = array(
  64. '#type' => 'link',
  65. '#title' => t('delete'),
  66. '#href' => 'admin/config/people/login-destination/delete/' . $data->id,
  67. );
  68. $form[$data->id]['operations'] = $operations;
  69. }
  70. if (element_children($form)) {
  71. $form['actions'] = array('#type' => 'actions');
  72. $form['actions']['submit'] = array(
  73. '#type' => 'submit',
  74. '#value' => t('Save configuration'),
  75. );
  76. }
  77. else {
  78. $form['#empty_text'] = t('There is no Login Destination Rule.');
  79. }
  80. return $form;
  81. }
  82. /**
  83. * Returns HTML for a login destination list.
  84. *
  85. * @param array $variables
  86. * An associative array containing:
  87. * - form: A render element representing the form.
  88. *
  89. * @ingroup themeable
  90. */
  91. function theme_login_destination_overview_form($variables) {
  92. $form = $variables['form'];
  93. drupal_add_tabledrag('login-destination-overview', 'order', 'sibling', 'login-destination-weight');
  94. $header = array(
  95. t('Destination'),
  96. t('Triggers'),
  97. t('Pages'),
  98. t('Roles'),
  99. array('data' => t('Enabled'), 'class' => array('checkbox')),
  100. t('Weight'),
  101. array('data' => t('Operations'), 'colspan' => '2'),
  102. );
  103. $rows = array();
  104. foreach (element_children($form) as $ldid) {
  105. if (isset($form[$ldid]['enabled'])) {
  106. $element = &$form[$ldid];
  107. $operations = array();
  108. foreach (element_children($element['operations']) as $op) {
  109. $operations[] = array('data' => drupal_render($element['operations'][$op]), 'class' => array('login-destination-operations'));
  110. }
  111. while (count($operations) < 2) {
  112. $operations[] = '';
  113. }
  114. $row = array();
  115. $row[] = drupal_render($element['destination']);
  116. $row[] = drupal_render($element['triggers']);
  117. $row[] = drupal_render($element['pages']);
  118. $row[] = drupal_render($element['roles']);
  119. $row[] = array(
  120. 'data' => drupal_render($element['enabled']),
  121. 'class' => array(
  122. 'checkbox', 'login-destination-enabled',
  123. ),
  124. );
  125. $form[$ldid]['weight']['#attributes']['class'] = array('login-destination-weight');
  126. $row[] = drupal_render($element['weight']);
  127. $row = array_merge($row, $operations);
  128. $row = array_merge(array('data' => $row), array());
  129. $row['class'][] = 'draggable';
  130. $rows[] = $row;
  131. }
  132. }
  133. $output = '';
  134. if (empty($rows)) {
  135. $rows[] = array(
  136. array(
  137. 'data' => $form['#empty_text'],
  138. 'colspan' => '7',
  139. ),
  140. );
  141. }
  142. $table_arguments = array(
  143. 'header' => $header,
  144. 'rows' => $rows,
  145. 'attributes' => array(
  146. 'id' => 'login-destination-overview',
  147. ),
  148. );
  149. $output .= theme('table', $table_arguments);
  150. $output .= drupal_render_children($form);
  151. return $output;
  152. }
  153. /**
  154. * Submit handler for the login destination overview form.
  155. *
  156. * This function update the login destination rule attribute
  157. * like rules are enabled/disabled or its weight.
  158. *
  159. * @see login_destination_overview_form()
  160. */
  161. function login_destination_overview_form_submit($form, &$form_state) {
  162. $element = &$form_state['values'];
  163. foreach (element_children($element) as $ldid) {
  164. if (isset($form[$ldid]['enabled'])) {
  165. $login_destination_rules[$ldid] = $element[$ldid];
  166. $login_destination_rules[$ldid]['ldid'] = $ldid;
  167. }
  168. }
  169. foreach ($login_destination_rules as $ldid => $login_destination_rule) {
  170. _login_destination_update_rules($login_destination_rule);
  171. }
  172. drupal_set_message(t('Your configuration has been saved.'), 'status');
  173. }
  174. /**
  175. * Save all our changed items to the database.
  176. *
  177. * @param array $login_destination_rule
  178. * An associative array representing a login destination item:
  179. * - enabled: (required) can contain 0 or 1, if rule is enabled then
  180. * it should be 1 else 0.
  181. * - weight: (required) can contain any integer value.
  182. *
  183. * @return bool
  184. * The ldid of the saved login destination rule, or FALSE
  185. * if the login destination rule could not be saved.
  186. */
  187. function _login_destination_update_rules($login_destination_rule) {
  188. if (!(isset($login_destination_rule['enabled']) && isset($login_destination_rule['weight']) && isset($login_destination_rule['ldid']))) {
  189. return FALSE;
  190. }
  191. if ($login_destination_rule['enabled'] != 0 && $login_destination_rule['enabled'] != 1) {
  192. return FALSE;
  193. }
  194. $login_destination_rule['weight'] = intval($login_destination_rule['weight']);
  195. if (!is_int($login_destination_rule['weight'])) {
  196. return FALSE;
  197. }
  198. db_update('login_destination')
  199. ->fields(array(
  200. 'enabled' => $login_destination_rule['enabled'],
  201. 'weight' => $login_destination_rule['weight'],
  202. ))
  203. ->condition('id', $login_destination_rule['ldid'])
  204. ->execute();
  205. return $login_destination_rule['ldid'];
  206. }
  207. /**
  208. * Render a destination of login destination rule.
  209. */
  210. function theme_login_destination_destination($variables) {
  211. $output = nl2br(check_plain($variables['destination']));
  212. if (empty($output)) {
  213. $output = '<i>' . t('Empty') . '</i>';
  214. }
  215. return $output;
  216. }
  217. /**
  218. * Render a trigger of login destination rule.
  219. */
  220. function theme_login_destination_triggers($variables) {
  221. $items = array_map('check_plain', $variables['items']);
  222. if (empty($items)) {
  223. return '<i>' . t('All triggers') . '</i>';
  224. }
  225. $output = '';
  226. foreach ($items as &$item) {
  227. switch ($item) {
  228. case 'login':
  229. $item = t('Login');
  230. break;
  231. case 'logout':
  232. $item = t('Logout');
  233. break;
  234. }
  235. $output .= $item . "<br/>";
  236. }
  237. return $output;
  238. }
  239. /**
  240. * Render a page type of login destination rule.
  241. */
  242. function theme_login_destination_pages($variables) {
  243. $type = $variables['pages_type'];
  244. if ($type == LOGIN_DESTINATION_REDIRECT_PHP) {
  245. return nl2br(check_plain($variables['pages']));
  246. }
  247. $pages = trim($variables['pages']);
  248. if (empty($pages)) {
  249. if ($type == LOGIN_DESTINATION_REDIRECT_NOTLISTED) {
  250. return '<i>' . t('All pages') . '</i>';
  251. }
  252. else {
  253. return '<i>' . t('No pages') . '</i>';
  254. }
  255. }
  256. $pages = explode("\n", preg_replace('/\r/', '', check_plain($variables['pages'])));
  257. $output = '';
  258. foreach ($pages as &$page) {
  259. if ($type == LOGIN_DESTINATION_REDIRECT_NOTLISTED) {
  260. $output .= "~ ";
  261. }
  262. $output .= $page . "<br/>";
  263. }
  264. return $output;
  265. }
  266. /**
  267. * Render a roles of login destination rule.
  268. */
  269. function theme_login_destination_roles($variables) {
  270. $items = array_values(array_intersect_key(_login_destination_role_options(), $variables['items']));
  271. if (empty($items)) {
  272. return '<i>' . t('All roles') . '</i>';
  273. }
  274. return theme('item_list', array('items' => $items));
  275. }
  276. /**
  277. * Category edit page.
  278. */
  279. function login_destination_edit_form($form, &$form_state, array $rule = array()) {
  280. // Default values.
  281. $rule += array(
  282. 'triggers' => array(),
  283. 'roles' => array(),
  284. 'pages_type' => LOGIN_DESTINATION_REDIRECT_NOTLISTED,
  285. 'pages' => '',
  286. 'destination_type' => LOGIN_DESTINATION_STATIC,
  287. 'destination' => '<front>',
  288. 'id' => NULL,
  289. 'weight' => 0,
  290. );
  291. $access = user_access('use PHP for settings');
  292. $type = $rule['destination_type'];
  293. if ($type == LOGIN_DESTINATION_SNIPPET && !$access) {
  294. $form['destination_type'] = array(
  295. '#type' => 'value',
  296. '#value' => LOGIN_DESTINATION_SNIPPET,
  297. );
  298. $form['destination'] = array(
  299. '#type' => 'value',
  300. '#value' => $rule['destination'],
  301. );
  302. }
  303. else {
  304. $options = array(
  305. LOGIN_DESTINATION_STATIC => t('Internal page or external URL'),
  306. );
  307. $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(
  308. '%blog' => 'blog',
  309. '%front' => '<front>',
  310. '%current' => '<current>',
  311. )
  312. );
  313. if (module_exists('php') && $access) {
  314. $options += array(LOGIN_DESTINATION_SNIPPET => t('Page returned by this PHP code (experts only)'));
  315. $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, for example. %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(
  316. '%php' => '<?php ?>',
  317. '%function' => 'url($path = \'\', array $options = array())',
  318. '%example' => '<?php return array(\'blog\', array(\'fragment\' => \'overlay=admin/config\', ), ); ?>',
  319. '@url' => 'http://api.drupal.org/api/drupal/includes--common.inc/function/url/7',
  320. )
  321. );
  322. }
  323. $form['destination_type'] = array(
  324. '#type' => 'radios',
  325. '#title' => t('Redirect to page'),
  326. '#default_value' => $type,
  327. '#options' => $options,
  328. );
  329. $form['destination'] = array(
  330. '#type' => 'textarea',
  331. '#default_value' => $rule['destination'],
  332. '#description' => $description,
  333. );
  334. }
  335. $triggers = array_map('check_plain', $rule['triggers']);
  336. if (empty($triggers)) {
  337. $triggers = array();
  338. }
  339. $form['triggers'] = array(
  340. '#type' => 'checkboxes',
  341. '#title' => t('Redirect upon triggers'),
  342. '#options' => array('login' => t('Login, registration, one-time login link'), 'logout' => t('Logout')),
  343. '#default_value' => $triggers,
  344. '#description' => t('Redirect only upon selected trigger(s). If you select no triggers, all of them will be used.'),
  345. );
  346. $type = $rule['pages_type'];
  347. if ($type == LOGIN_DESTINATION_REDIRECT_PHP && !$access) {
  348. $form['pages_type'] = array(
  349. '#type' => 'value',
  350. '#value' => LOGIN_DESTINATION_REDIRECT_PHP,
  351. );
  352. $form['pages'] = array(
  353. '#type' => 'value',
  354. '#value' => $rule['destination'],
  355. );
  356. }
  357. else {
  358. $options = array(
  359. LOGIN_DESTINATION_REDIRECT_NOTLISTED => t('All pages except those listed'),
  360. LOGIN_DESTINATION_REDIRECT_LISTED => t('Only the listed pages'),
  361. );
  362. $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(
  363. '%blog' => 'blog',
  364. '%blog-wildcard' => 'blog/*',
  365. '%front' => '<front>',
  366. '%login' => 'user',
  367. '%register' => 'user/register',
  368. '%reset' => 'user/*/edit',
  369. )
  370. );
  371. if (module_exists('php') && $access) {
  372. $options += array(LOGIN_DESTINATION_REDIRECT_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
  373. $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 ?>'));
  374. }
  375. $form['pages_type'] = array(
  376. '#type' => 'radios',
  377. '#title' => t('Redirect from specific pages'),
  378. '#default_value' => $type,
  379. '#options' => $options,
  380. );
  381. $form['pages'] = array(
  382. '#type' => 'textarea',
  383. '#default_value' => $rule['pages'],
  384. '#description' => $description,
  385. );
  386. }
  387. $default_role_options = array_map('check_plain', $rule['roles']);
  388. if (empty($default_role_options)) {
  389. $default_role_options = array();
  390. }
  391. $form['roles'] = array(
  392. '#type' => 'checkboxes',
  393. '#title' => t('Redirect users with roles'),
  394. '#options' => _login_destination_role_options(),
  395. '#default_value' => $default_role_options,
  396. '#description' => t('Redirect only the selected role(s). If you select no roles, all users will be redirected.'),
  397. );
  398. $form['actions'] = array('#type' => 'actions');
  399. $form['actions']['submit'] = array(
  400. '#type' => 'submit',
  401. '#value' => t('Save'),
  402. );
  403. if ($rule['id']) {
  404. $form['id'] = array(
  405. '#type' => 'hidden',
  406. '#value' => $rule['id'],
  407. );
  408. }
  409. return $form;
  410. }
  411. /**
  412. * Validate the contact category edit page form submission.
  413. */
  414. function login_destination_edit_form_validate($form, &$form_state) {
  415. $destination = $form_state['values']['destination'];
  416. $destination_type = $form_state['values']['destination_type'];
  417. // Check user has enter any path.
  418. if (!empty($destination) && $destination_type == 0) {
  419. $destination = preg_replace("/\?.+/", "", $destination);
  420. if (!drupal_valid_path($destination)) {
  421. form_set_error('destination', t('Incorrect path, Please Enter valid Path'));
  422. }
  423. }
  424. }
  425. /**
  426. * Process the contact category edit page form submission.
  427. */
  428. function login_destination_edit_form_submit($form, &$form_state) {
  429. $form_state['values']['triggers'] = serialize(array_filter($form_state['values']['triggers']));
  430. $form_state['values']['roles'] = serialize(array_filter($form_state['values']['roles']));
  431. if (empty($form_state['values']['id'])) {
  432. drupal_write_record('login_destination', $form_state['values']);
  433. }
  434. else {
  435. drupal_write_record('login_destination', $form_state['values'], array('id'));
  436. }
  437. drupal_set_message(t('Login destination to %destination has been saved.', array('%destination' => $form_state['values']['destination'])));
  438. $form_state['redirect'] = 'admin/config/people/login-destination';
  439. }
  440. /**
  441. * Form builder for deleting a login destination.
  442. */
  443. function login_destination_delete_form($form, &$form_state, array $rule) {
  444. $form['login_destination'] = array(
  445. '#type' => 'value',
  446. '#value' => $rule,
  447. );
  448. return confirm_form(
  449. $form,
  450. t('Are you sure you want to delete the login destination %destination ?', array('%destination' => $rule['destination'])),
  451. 'admin/config/people/login-destination',
  452. t('This action cannot be undone.'),
  453. t('Delete'),
  454. t('Cancel')
  455. );
  456. }
  457. /**
  458. * Submit handler for the confirm delete login destination form.
  459. */
  460. function login_destination_delete_form_submit($form, &$form_state) {
  461. $rule = $form['login_destination']['#value'];
  462. db_delete('login_destination')
  463. ->condition('id', $rule['id'])
  464. ->execute();
  465. drupal_set_message(t('The login destination %destination has been deleted.', array('%destination' => $rule['destination'])));
  466. $form_state['redirect'] = 'admin/config/people/login-destination';
  467. }
  468. /**
  469. * Settings page.
  470. */
  471. function login_destination_settings() {
  472. $form = array();
  473. $form['settings']['login_destination_preserve_destination'] = array(
  474. '#type' => 'checkbox',
  475. '#default_value' => variable_get('login_destination_preserve_destination', FALSE),
  476. '#title' => t('Preserve the destination parameter'),
  477. '#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."),
  478. );
  479. $form['settings']['login_destination_immediate_redirect'] = array(
  480. '#type' => 'checkbox',
  481. '#default_value' => variable_get('login_destination_immediate_redirect', FALSE),
  482. '#title' => t('Redirect immediately after using one-time login link'),
  483. '#description' => t("User will be redirected before given the possibility to change their password."),
  484. );
  485. return system_settings_form($form);
  486. }