menu_token.install 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Install file for menu_token module.
  5. */
  6. /**
  7. * Implements hook_update_N().
  8. */
  9. function menu_token_update_7000(&$sandbox) {
  10. $schema['menu_token'] = array(
  11. 'description' => t('Menu token properties'),
  12. 'fields' => array (
  13. 'mlid' => array (
  14. 'description' => t('The menu link {menu_links}.mlid'),
  15. 'type' => 'int',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'link_path' => array (
  20. 'description' => t('The actual path with tokens'),
  21. 'type' => 'varchar',
  22. 'length' => 255,
  23. 'not null' => TRUE,
  24. 'default' => '',
  25. ),
  26. ),
  27. 'primary key' => array('mlid'),
  28. );
  29. db_create_table('menu_token', $schema['menu_token']);
  30. }
  31. /**
  32. * Implements hook_update_N().
  33. */
  34. function menu_token_update_7001(&$sandbox) {
  35. // Initializing sandbox variables.
  36. if (!isset($sandbox['progress'])) {
  37. // Preparing array of menu items for batch insert.
  38. foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
  39. $sandbox['items'][] = array('mlid' => $mlid, 'link_path' => $link_path);
  40. }
  41. $sandbox['progress'] = 0;
  42. $sandbox['max'] = count($sandbox['items']);
  43. }
  44. // Insert current record.
  45. if (!empty($sandbox['max'])) {
  46. db_merge('menu_token')
  47. ->key(array('mlid' => $sandbox['items'][$sandbox['progress']]['mlid']))
  48. ->fields(array('link_path' => $sandbox['items'][$sandbox['progress']]['link_path']))
  49. ->execute();
  50. }
  51. $sandbox['progress']++;
  52. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  53. // Delete variable in case of all queries were executed
  54. if ($sandbox['#finished']) {
  55. variable_del('menu_token_enabled');
  56. }
  57. }
  58. /**
  59. * Implements hook_update_N().
  60. */
  61. function menu_token_update_7002(&$sandbox) {
  62. if (!isset($sandbox['progress'])) {
  63. $sandbox['progress'] = 0;
  64. if (db_table_exists('menu_token')) {
  65. $sandbox['max'] = db_select('menu_token', 'mt')->countQuery()->execute()->fetchField();
  66. }
  67. }
  68. if (!empty($sandbox['max'])) {
  69. $tokens = db_select('menu_token', 'mt')
  70. ->fields('mt', array('mlid', 'link_path'))
  71. ->orderBy('mlid')
  72. ->range($sandbox['progress'], 10)
  73. ->execute()
  74. ->fetchAllKeyed();
  75. if (!empty($tokens)) {
  76. $links = db_select('menu_links', 'ml')
  77. ->fields('ml', array('mlid', 'options'))
  78. ->condition('mlid', array_keys($tokens))
  79. ->execute()
  80. ->fetchAllKeyed();
  81. foreach ($links as $mlid => $options) {
  82. $options = unserialize($options);
  83. $options['menu_token_link_path'] = $tokens[$mlid];
  84. $options['menu_token_link_data'] = array();
  85. db_update('menu_links')
  86. ->fields(array('options' => serialize($options)))
  87. ->condition('mlid', $mlid)
  88. ->execute();
  89. }
  90. }
  91. $sandbox['progress'] += 10;
  92. }
  93. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  94. if ($sandbox['#finished'] >= 1) {
  95. $sandbox['#finished'] = 1;
  96. // Drop the deprecated menu_token table if it exists.
  97. if (db_table_exists('menu_token')) {
  98. db_drop_table('menu_token');
  99. }
  100. return t('The Menu Token module has been updated successfully.');
  101. }
  102. }
  103. /**
  104. * Drop the deprecated menu_token table if it exists.
  105. */
  106. function menu_token_update_7003() {
  107. if (db_table_exists('menu_token')) {
  108. db_drop_table('menu_token');
  109. }
  110. }
  111. /**
  112. * Migrate menu items link path, from '<front>' to 'menutoken/[uuid]'.
  113. */
  114. function menu_token_update_7004() {
  115. $result = db_select('menu_links', 'm')
  116. ->fields('m', array('mlid', 'options'))
  117. ->condition('link_path', '<front>')
  118. ->execute();
  119. foreach ($result as $menu_link) {
  120. $options = unserialize($menu_link->options);
  121. if (isset($options['menu_token_data'])) {
  122. db_update('menu_links')
  123. ->fields(array(
  124. 'link_path' => 'menutoken/' . uniqid(),
  125. 'router_path' => 'menutoken/%',
  126. ))
  127. ->condition('mlid', $menu_link->mlid)
  128. ->execute();
  129. }
  130. }
  131. }
  132. /**
  133. * Implements hook_uninstall().
  134. */
  135. function menu_token_uninstall() {
  136. $result = db_select('variable', 'v')
  137. ->fields('v', array('name'))
  138. ->condition('name', 'menu_token_%', '=')
  139. ->execute();
  140. foreach ($result as $row) {
  141. variable_del($row->name);
  142. }
  143. }