login_destination.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Login Destination module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function login_destination_schema() {
  10. $schema['login_destination'] = array(
  11. 'description' => 'Login Destination rules.',
  12. 'fields' => array(
  13. 'id' => array(
  14. 'type' => 'serial',
  15. 'unsigned' => TRUE,
  16. 'not null' => TRUE,
  17. 'description' => 'Primary Key: Unique ID.',
  18. ),
  19. 'triggers' => array(
  20. 'type' => 'text',
  21. 'not null' => TRUE,
  22. 'description' => 'Triggers on which to perform redirect',
  23. ),
  24. 'roles' => array(
  25. 'type' => 'text',
  26. 'not null' => TRUE,
  27. 'description' => 'Roles to perform redirect for',
  28. ),
  29. 'pages_type' => array(
  30. 'type' => 'int',
  31. 'not null' => TRUE,
  32. 'default' => 0,
  33. 'size' => 'tiny',
  34. 'description' => 'Flag to indicate from which pages to redirect. (0 = all pages except listed pages, 1 = only listed pages, 2 = Use custom PHP code)',
  35. ),
  36. 'pages' => array(
  37. 'type' => 'text',
  38. 'not null' => TRUE,
  39. 'description' => 'Pages from which to redirect',
  40. ),
  41. 'destination_type' => array(
  42. 'type' => 'int',
  43. 'not null' => TRUE,
  44. 'default' => 0,
  45. 'size' => 'tiny',
  46. 'description' => 'Flag to indicate the destination type. (0 = static URL, 1 = PHP code)',
  47. ),
  48. 'destination' => array(
  49. 'type' => 'text',
  50. 'not null' => TRUE,
  51. 'description' => 'Redirect destination',
  52. ),
  53. 'weight' => array(
  54. 'type' => 'int',
  55. 'not null' => TRUE,
  56. 'default' => 0,
  57. 'description' => "The rule's weight.",
  58. ),
  59. ),
  60. 'primary key' => array('id'),
  61. 'indexes' => array(
  62. 'list' => array('weight'),
  63. ),
  64. );
  65. return $schema;
  66. }
  67. /**
  68. * Implementation of hook_install().
  69. */
  70. function login_destination_install() {
  71. // update the alter option of 'user/logout' to TRUE (menu_save invokes necessary hooks)
  72. $result = db_query("SELECT mlid, menu_name FROM {menu_links} WHERE link_path = 'user/logout' OR link_path = 'user/login' OR link_path = 'user' ORDER BY mlid ASC");
  73. foreach($result as $res) {
  74. $item = menu_link_load($res->mlid);
  75. $item['options']['alter'] = TRUE;
  76. db_update('menu_links')
  77. ->fields(array(
  78. 'options' => serialize($item['options']),
  79. ))
  80. ->condition('mlid', $item['mlid'])
  81. ->execute();
  82. }
  83. }
  84. /**
  85. * Implementation of hook_uninstall().
  86. */
  87. function login_destination_uninstall() {
  88. variable_del('login_destination_preserve_destination');
  89. variable_del('login_destination_profile_redirect');
  90. }
  91. function login_destination_update_7000() {
  92. $type = variable_get('ld_condition_type', 'always');
  93. $snippet = variable_get('ld_condition_snippet', '');
  94. if ($type == 'snippet') {
  95. $form_state['values']['pages_type'] = 2;
  96. // We introduced php tags.
  97. $form_state['values']['pages'] = '<?php ' . $snippet . '?>';
  98. }
  99. elseif ($type == 'pages') {
  100. $form_state['values']['pages_type'] = 1;
  101. $form_state['values']['pages'] = $snippet;
  102. }
  103. else {
  104. $form_state['values']['pages_type'] = 0;
  105. $form_state['values']['pages'] = $snippet;
  106. }
  107. $type = variable_get('ld_url_type', 'static');
  108. $snippet = variable_get('ld_url_destination', '');
  109. if ($type == 'snippet') {
  110. $form_state['values']['destination_type'] = 1;
  111. // syntax for return value has changed.
  112. $form_state['values']['destination'] = '<?php /* ' . $snippet . ' */ ?>';
  113. }
  114. else {
  115. $form_state['values']['destination_type'] = 0;
  116. $form_state['values']['destination'] = $snippet;
  117. }
  118. $form_state['values']['triggers'] = serialize(array('login'));
  119. $form_state['values']['roles'] = serialize(array());
  120. drupal_write_record('login_destination', $form_state['values']);
  121. variable_set('login_destination_preserve_destination', variable_get('ld_destination', 0));
  122. variable_del('ld_condition_type');
  123. variable_del('ld_condition_snippet');
  124. variable_del('ld_destination');
  125. variable_del('ld_url_type');
  126. variable_del('ld_url_destination');
  127. }