menu_attributes.install 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the menu_attributes module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function menu_attributes_install() {
  10. db_update('system')
  11. ->fields(array(
  12. 'weight' => 10,
  13. ))
  14. ->condition('type', 'module')
  15. ->condition('name', 'menu_attributes')
  16. ->execute();
  17. }
  18. /**
  19. * Implements hook_uninstall().
  20. */
  21. function menu_attributes_uninstall() {
  22. drupal_load('module', 'menu_attributes');
  23. $attributes = menu_attributes_menu_attribute_info();
  24. foreach (array_keys($attributes) as $attribute) {
  25. variable_del("menu_attributes_{$attribute}_enable");
  26. variable_del("menu_attributes_{$attribute}_default");
  27. }
  28. }
  29. /**
  30. * Update the module weight.
  31. */
  32. function menu_attributes_update_1() {
  33. db_update('system')
  34. ->fields(array(
  35. 'weight' => 10,
  36. ))
  37. ->condition('type', 'module')
  38. ->condition('name', 'menu_attributes')
  39. ->execute();
  40. }
  41. /**
  42. * Fix any menu links that had the class attribute saved as an string.
  43. */
  44. function menu_attributes_update_7000(&$sandbox) {
  45. if (!isset($sandbox['progress'])) {
  46. $sandbox['progress'] = 0;
  47. $sandbox['current_mlid'] = 0;
  48. // Only count links that possibly have a key class with a string value in
  49. // its serialized options array.
  50. $sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links} WHERE options LIKE '%s:5:\"class\";s:%'")->fetchField();
  51. }
  52. // Process twenty links at a time.
  53. $limit = 20;
  54. $links = db_query_range("SELECT mlid, options FROM {menu_links} WHERE mlid > :mlid AND options LIKE '%s:5:\"class\";s:%' ORDER BY mlid", 0, $limit, array(':mlid' => $sandbox['current_mlid']))->fetchAllKeyed();
  55. foreach ($links as $mlid => $options) {
  56. $options = unserialize($options);
  57. if (isset($options['attributes']['class']) && is_string($options['attributes']['class'])) {
  58. // Convert classes to an array.
  59. $options['attributes']['class'] = explode(' ', $options['attributes']['class']);
  60. db_update('menu_links')
  61. ->fields(array(
  62. 'options' => serialize($options),
  63. ))
  64. ->condition('mlid', $mlid)
  65. ->execute();
  66. }
  67. $sandbox['progress']++;
  68. $sandbox['current_mlid'] = $mlid;
  69. }
  70. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  71. // To display a message to the user when the update is completed, return it.
  72. // If you do not want to display a completion message, simply return nothing.
  73. return t('All menu links with non-array value for class attribute have been fixed.');
  74. }
  75. /**
  76. * Grant the 'administer menu attributes' permission to roles that currently
  77. * have the 'administer menu' permission.
  78. */
  79. function menu_attributes_update_7001() {
  80. $roles = user_roles(FALSE, 'administer menu');
  81. foreach ($roles as $rid => $role) {
  82. user_role_grant_permissions($rid, array('administer menu attributes'));
  83. }
  84. return t("Every role with the 'administer menu' permission has also received the 'administer menu attributes' permission.");
  85. }