e2a7ca75f8292a0d136dd85e60a7cff4773bb76b
[yaffs-website] / web / modules / custom / menu_views / menu_views.install
1 <?php
2
3 /**
4  * @file
5  * Install and update functions for the menu_views module.
6  */
7
8 /**
9  * Implements hook_install().
10  */
11 function menu_views_install() {
12   // @FIXME
13 // db_update('system')
14 //     ->fields(array(
15 //       'weight' => 100,
16 //     ))
17 //     ->condition('type', 'module')
18 //     ->condition('name', 'menu_views')
19 //     ->execute();
20
21 }
22
23 /**
24  * Updates module weight in system table.
25  * Grants the "administer menu views" permission to roles that currently have the "administer menu" permission.
26  */
27 function menu_views_update_7100() {
28   // @FIXME
29 // db_update('system')
30 //     ->fields(array(
31 //       'weight' => 100,
32 //     ))
33 //     ->condition('type', 'module')
34 //     ->condition('name', 'menu_views')
35 //     ->execute();
36
37   $roles = user_roles(FALSE, 'administer menu');
38   foreach ($roles as $rid => $role) {
39     user_role_grant_permissions($rid, array('administer menu views'));
40   }
41   return t("Every role with the 'administer menu' permission has also received the 'administer menu views' permission.");
42 }
43
44 /**
45  * FIXED UPDATE: Replaces attached view arguments (%N and %P) of menu link items with Token API equivalents.
46  */
47 function menu_views_update_7102(&$sandbox) {
48   if (!isset($sandbox['progress'])) {
49     $sandbox['progress'] = 0;
50     $sandbox['current_mlid'] = 0;
51     // Only count links that possibly have a key class with a string value in
52     // its serialized options array.
53     $sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links}")->fetchField();
54   }
55   // Process twenty links at a time.
56   $limit = 20;
57   // Fetch links
58   $links = db_query_range("SELECT mlid, options FROM {menu_links} WHERE mlid > :mlid ORDER BY mlid", 0, $limit, array(':mlid' => $sandbox['current_mlid']))->fetchAll();
59   foreach ($links as $link) {
60     $options = unserialize($link->options);
61     if (isset($options['menu_views'])) {
62       $arguments = array();
63       if (isset($options['menu_views']['arguments'])) {
64         $arguments = $options['menu_views']['arguments'];
65       }
66       $options['menu_views']['arguments'] = str_replace(array('%N', '%P'), array('[menu-link:node:nid]', '[menu-link:parent:node:nid]'), $arguments);
67       db_update('menu_links')
68         ->fields(array(
69           'options' => serialize($options),
70         ))
71         ->condition('mlid', $link->mlid)
72         ->execute();
73     }
74     $sandbox['progress']++;
75     $sandbox['current_mlid'] = $link->mlid;
76   }
77   $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
78   // To display a message to the user when the update is completed, return it.
79   // If you do not want to display a completion message, simply return nothing.
80   return t('All menu link items with attached views have had their arguments converted for use with Drupal tokens.');
81 }
82
83 /**
84  * Updates existing menu views to reference the proper mlid. When creating a new menu item, even if the attach view form was present, caused menu views to not save the mlid as it didn't yet exist. This update converts all existing menu views that have a referenced mlid of 0.
85  */
86 function menu_views_update_7103(&$sandbox) {
87   if (!isset($sandbox['progress'])) {
88     $sandbox['progress'] = 0;
89     $sandbox['current_mlid'] = 0;
90     // Only count links that possibly have a key class with a string value in
91     // its serialized options array.
92     $sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links}")->fetchField();
93   }
94   // Process twenty links at a time.
95   $limit = 20;
96   // Fetch links
97   $links = db_query_range("SELECT mlid, options FROM {menu_links} WHERE mlid > :mlid ORDER BY mlid", 0, $limit, array(':mlid' => $sandbox['current_mlid']))->fetchAll();
98   foreach ($links as $link) {
99     $options = unserialize($link->options);
100     if (isset($options['menu_views'])) {
101       $options['menu_views']['mlid'] = $link->mlid;
102       db_update('menu_links')
103         ->fields(array(
104           'options' => serialize($options),
105         ))
106         ->condition('mlid', $link->mlid)
107         ->execute();
108     }
109     $sandbox['progress']++;
110     $sandbox['current_mlid'] = $link->mlid;
111   }
112   $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
113   // To display a message to the user when the update is completed, return it.
114   // If you do not want to display a completion message, simply return nothing.
115   return t('All menu link items with attached views have had their arguments converted for use with Drupal tokens.');
116 }
117
118 /**
119  * Upgrades existing menu items that have attached views. This is a major update for the 7.x-2.x branch.
120  */
121 function menu_views_update_7200(&$sandbox) {
122   if (!isset($sandbox['progress'])) {
123     $sandbox['progress'] = 0;
124     $sandbox['current_mlid'] = 0;
125     // Only count links that possibly have a key class with a string value in
126     // its serialized options array.
127     $sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links}")->fetchField();
128   }
129   // Process twenty links at a time.
130   $limit = 20;
131   // Fetch links
132   $links = db_query_range("SELECT * FROM {menu_links} WHERE mlid > :mlid ORDER BY mlid", 0, $limit, array(':mlid' => $sandbox['current_mlid']))->fetchAll();
133   foreach ($links as $link) {
134     $options = unserialize($link->options);
135     if (isset($options['menu_views'])) {
136       $item = _menu_views_default_values();
137       $item['mlid'] = $link->mlid;
138       $item['type'] = (isset($options['menu_views']['name']) && !empty($options['menu_views']['name']))
139                       && (isset($options['menu_views']['display']) && !empty($options['menu_views']['display'])) ? 'view' : 'link';
140       $item['original_path'] = $link->link_path != '<view>' ? $link->link_path : '';
141       $item['view']['name'] = isset($options['menu_views']['name']) ? $options['menu_views']['name'] : '';;
142       $item['view']['display'] = isset($options['menu_views']['display']) ? $options['menu_views']['display'] : '';
143       $item['view']['arguments'] = isset($options['menu_views']['arguments']) ? $options['menu_views']['arguments'] : '';
144       $options['menu_views'] = $item;
145       db_update('menu_links')
146         ->fields(array(
147           'options' => serialize($options),
148         ))
149         ->condition('mlid', $link->mlid)
150         ->execute();
151     }
152     $sandbox['progress']++;
153     $sandbox['current_mlid'] = $link->mlid;
154   }
155   $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
156   // To display a message to the user when the update is completed, return it.
157   // If you do not want to display a completion message, simply return nothing.
158   return t('All menu items that have attached views have been upgraded. This is a major update for the 7.x-2.x branch.');
159 }
160