login_destination.install 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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("
  81. SELECT mlid, menu_name
  82. FROM {menu_links}
  83. WHERE link_path = 'user/logout' OR link_path = 'user/login' OR link_path = 'user'
  84. ORDER BY mlid ASC");
  85. foreach ($result as $res) {
  86. $item = menu_link_load($res->mlid);
  87. $item['options']['alter'] = TRUE;
  88. db_update('menu_links')
  89. ->fields(array(
  90. 'options' => serialize($item['options']),
  91. ))
  92. ->condition('mlid', $item['mlid'])
  93. ->execute();
  94. }
  95. }
  96. /**
  97. * Implements hook_uninstall().
  98. */
  99. function login_destination_uninstall() {
  100. variable_del('login_destination_preserve_destination');
  101. variable_del('login_destination_profile_redirect');
  102. }
  103. /**
  104. * Implements hook_update_N().
  105. */
  106. function login_destination_update_7000() {
  107. $type = variable_get('ld_condition_type', 'always');
  108. $snippet = variable_get('ld_condition_snippet', '');
  109. if ($type == 'snippet') {
  110. $form_state['values']['pages_type'] = 2;
  111. // We introduced php tags.
  112. $form_state['values']['pages'] = '<?php ' . $snippet . '?>';
  113. }
  114. elseif ($type == 'pages') {
  115. $form_state['values']['pages_type'] = 1;
  116. $form_state['values']['pages'] = $snippet;
  117. }
  118. else {
  119. $form_state['values']['pages_type'] = 0;
  120. $form_state['values']['pages'] = $snippet;
  121. }
  122. $type = variable_get('ld_url_type', 'static');
  123. $snippet = variable_get('ld_url_destination', '');
  124. if ($type == 'snippet') {
  125. $form_state['values']['destination_type'] = 1;
  126. // Syntax for return value has changed.
  127. $form_state['values']['destination'] = '<?php /* ' . $snippet . ' */ ?>';
  128. }
  129. else {
  130. $form_state['values']['destination_type'] = 0;
  131. $form_state['values']['destination'] = $snippet;
  132. }
  133. $form_state['values']['triggers'] = serialize(array('login'));
  134. $form_state['values']['roles'] = serialize(array());
  135. drupal_write_record('login_destination', $form_state['values']);
  136. variable_set('login_destination_preserve_destination', variable_get('ld_destination', 0));
  137. variable_del('ld_condition_type');
  138. variable_del('ld_condition_snippet');
  139. variable_del('ld_destination');
  140. variable_del('ld_url_type');
  141. variable_del('ld_url_destination');
  142. }
  143. /**
  144. * Implements hook_update_N().
  145. */
  146. function login_destination_update_7001() {
  147. $spec = array(
  148. 'type' => 'int',
  149. 'unsigned' => TRUE,
  150. 'not null' => TRUE,
  151. 'default' => 1,
  152. );
  153. db_add_field('login_destination', 'enabled', $spec);
  154. }
  155. /**
  156. * Clear hooks cache.
  157. */
  158. function login_destination_update_7002() {
  159. cache_clear_all('hook_info', 'cache_bootstrap');
  160. }
  161. /**
  162. * Automatically give all roles with permission "Administer Users" the new dedicated permission
  163. * "Administer Login Destination settings".
  164. */
  165. function login_destination_update_7003() {
  166. drupal_set_message(t('The Login Destination module has just been updated.<br>
  167. A new permission called "Administer Login Destination settings" has now been
  168. added.<br>Previously the access to the Login Destination\'s settings page was
  169. managed by the "Administer Users" permission.<br>That\'s why all roles with
  170. that old permission have been just automatically given the new dedicated
  171. "Administer Login Destination settings" permission.<br>If you want to
  172. duoble-check things, you can go to the
  173. <a href="/admin/people/permissions" title="Permissions page" >Permissions page</a> now.'));
  174. $roles = user_roles(TRUE, 'administer users');
  175. foreach ($roles as $rid => $role_name) {
  176. user_role_grant_permissions($rid, array('administer login destination settings'));
  177. }
  178. }