login_destination.install 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. 'enabled' => array(
  60. 'type' => 'int',
  61. 'not null' => TRUE,
  62. 'unsigned' => TRUE,
  63. 'default' => 1,
  64. 'description' => "The rule enabled/disabled status.",
  65. ),
  66. ),
  67. 'primary key' => array('id'),
  68. 'indexes' => array(
  69. 'list' => array('weight'),
  70. ),
  71. );
  72. return $schema;
  73. }
  74. /**
  75. * Implements hook_install().
  76. */
  77. function login_destination_install() {
  78. // Update the alter option of 'user/logout' to TRUE,
  79. // (menu_save invokes necessary hooks).
  80. $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");
  81. foreach ($result as $res) {
  82. $item = menu_link_load($res->mlid);
  83. $item['options']['alter'] = TRUE;
  84. db_update('menu_links')
  85. ->fields(array(
  86. 'options' => serialize($item['options']),
  87. ))
  88. ->condition('mlid', $item['mlid'])
  89. ->execute();
  90. }
  91. }
  92. /**
  93. * Implements hook_uninstall().
  94. */
  95. function login_destination_uninstall() {
  96. variable_del('login_destination_preserve_destination');
  97. variable_del('login_destination_profile_redirect');
  98. }
  99. /**
  100. * Implements hook_update_N().
  101. */
  102. function login_destination_update_7000() {
  103. $type = variable_get('ld_condition_type', 'always');
  104. $snippet = variable_get('ld_condition_snippet', '');
  105. if ($type == 'snippet') {
  106. $form_state['values']['pages_type'] = 2;
  107. // We introduced php tags.
  108. $form_state['values']['pages'] = '<?php ' . $snippet . '?>';
  109. }
  110. elseif ($type == 'pages') {
  111. $form_state['values']['pages_type'] = 1;
  112. $form_state['values']['pages'] = $snippet;
  113. }
  114. else {
  115. $form_state['values']['pages_type'] = 0;
  116. $form_state['values']['pages'] = $snippet;
  117. }
  118. $type = variable_get('ld_url_type', 'static');
  119. $snippet = variable_get('ld_url_destination', '');
  120. if ($type == 'snippet') {
  121. $form_state['values']['destination_type'] = 1;
  122. // Syntax for return value has changed.
  123. $form_state['values']['destination'] = '<?php /* ' . $snippet . ' */ ?>';
  124. }
  125. else {
  126. $form_state['values']['destination_type'] = 0;
  127. $form_state['values']['destination'] = $snippet;
  128. }
  129. $form_state['values']['triggers'] = serialize(array('login'));
  130. $form_state['values']['roles'] = serialize(array());
  131. drupal_write_record('login_destination', $form_state['values']);
  132. variable_set('login_destination_preserve_destination', variable_get('ld_destination', 0));
  133. variable_del('ld_condition_type');
  134. variable_del('ld_condition_snippet');
  135. variable_del('ld_destination');
  136. variable_del('ld_url_type');
  137. variable_del('ld_url_destination');
  138. }
  139. /**
  140. * Implements hook_update_N().
  141. */
  142. function login_destination_update_7001() {
  143. $spec = array(
  144. 'type' => 'int',
  145. 'unsigned' => TRUE,
  146. 'not null' => TRUE,
  147. 'default' => 1,
  148. );
  149. db_add_field('login_destination', 'enabled', $spec);
  150. }