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