1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Install file for menu_token module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_schema().
|
10
|
*/
|
11
|
function menu_token_schema() {
|
12
|
$schema['menu_token'] = array(
|
13
|
'description' => t('Menu token properties'),
|
14
|
'fields' => array (
|
15
|
'mlid' => array (
|
16
|
'description' => t('The menu link {menu_links}.mlid'),
|
17
|
'type' => 'int',
|
18
|
'unsigned' => TRUE,
|
19
|
'not null' => TRUE,
|
20
|
),
|
21
|
'link_path' => array (
|
22
|
'description' => t('The actual path with tokens'),
|
23
|
'type' => 'varchar',
|
24
|
'length' => 255,
|
25
|
'not null' => TRUE,
|
26
|
'default' => '',
|
27
|
),
|
28
|
),
|
29
|
|
30
|
'primary key' => array('mlid'),
|
31
|
);
|
32
|
|
33
|
return $schema;
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Implements hook_install().
|
38
|
*/
|
39
|
function menu_token_install() {
|
40
|
db_update('system')
|
41
|
->fields(array('weight' => 10))
|
42
|
->condition('type', 'module', '=')
|
43
|
->condition('name', 'menu_token', '=')
|
44
|
->execute();
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Implements hook_update_N().
|
49
|
*/
|
50
|
function menu_token_update_7000(&$sandbox) {
|
51
|
if (!db_table_exists('menu_token')) {
|
52
|
$schema['menu_token'] = array(
|
53
|
'description' => t('Menu token properties'),
|
54
|
'fields' => array (
|
55
|
'mlid' => array (
|
56
|
'description' => t('The menu link {menu_links}.mlid'),
|
57
|
'type' => 'int',
|
58
|
'unsigned' => TRUE,
|
59
|
'not null' => TRUE,
|
60
|
),
|
61
|
'link_path' => array (
|
62
|
'description' => t('The actual path with tokens'),
|
63
|
'type' => 'varchar',
|
64
|
'length' => 255,
|
65
|
'not null' => TRUE,
|
66
|
'default' => '',
|
67
|
),
|
68
|
),
|
69
|
|
70
|
'primary key' => array('mlid'),
|
71
|
);
|
72
|
|
73
|
db_create_table('menu_token', $schema['menu_token']);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Implements hook_update_N().
|
79
|
*/
|
80
|
function menu_token_update_7001(&$sandbox) {
|
81
|
|
82
|
// Initializing sandbox variables.
|
83
|
if (!isset($sandbox['progress'])) {
|
84
|
// Preparing array of menu items for batch insert.
|
85
|
foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
|
86
|
$sandbox['items'][] = array('mlid' => $mlid, 'link_path' => $link_path);
|
87
|
}
|
88
|
$sandbox['progress'] = 0;
|
89
|
$sandbox['max'] = count($sandbox['items']);
|
90
|
}
|
91
|
|
92
|
// Insert current record.
|
93
|
if (!empty($sandbox['max'])) {
|
94
|
db_merge('menu_token')
|
95
|
->key(array('mlid' => $sandbox['items'][$sandbox['progress']]['mlid']))
|
96
|
->fields(array('link_path' => $sandbox['items'][$sandbox['progress']]['link_path']))
|
97
|
->execute();
|
98
|
}
|
99
|
|
100
|
$sandbox['progress']++;
|
101
|
|
102
|
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
|
103
|
|
104
|
// Delete variable in case of all queries were executed
|
105
|
if ($sandbox['#finished']) {
|
106
|
variable_del('menu_token_enabled');
|
107
|
}
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Implements hook_update_N().
|
112
|
*/
|
113
|
function menu_token_update_7002(&$sandbox) {
|
114
|
if (!isset($sandbox['progress'])) {
|
115
|
$sandbox['progress'] = 0;
|
116
|
if (db_table_exists('menu_token')) {
|
117
|
$sandbox['max'] = db_select('menu_token', 'mt')->countQuery()->execute()->fetchField();
|
118
|
}
|
119
|
}
|
120
|
|
121
|
if (!empty($sandbox['max'])) {
|
122
|
$tokens = db_select('menu_token', 'mt')
|
123
|
->fields('mt', array('mlid', 'link_path'))
|
124
|
->orderBy('mlid')
|
125
|
->range($sandbox['progress'], 10)
|
126
|
->execute()
|
127
|
->fetchAllKeyed();
|
128
|
|
129
|
if (!empty($tokens)) {
|
130
|
$links = db_select('menu_links', 'ml')
|
131
|
->fields('ml', array('mlid', 'options'))
|
132
|
->condition('mlid', array_keys($tokens))
|
133
|
->execute()
|
134
|
->fetchAllKeyed();
|
135
|
|
136
|
foreach ($links as $mlid => $options) {
|
137
|
$options = unserialize($options);
|
138
|
$options['menu_token_link_path'] = $tokens[$mlid];
|
139
|
$options['menu_token_link_data'] = array();
|
140
|
|
141
|
db_update('menu_links')
|
142
|
->fields(array('options' => serialize($options)))
|
143
|
->condition('mlid', $mlid)
|
144
|
->execute();
|
145
|
}
|
146
|
}
|
147
|
|
148
|
$sandbox['progress'] += 10;
|
149
|
}
|
150
|
|
151
|
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
|
152
|
|
153
|
if ($sandbox['#finished'] >= 1) {
|
154
|
$sandbox['#finished'] = 1;
|
155
|
|
156
|
// Drop the deprecated menu_token table if it exists.
|
157
|
if (db_table_exists('menu_token')) {
|
158
|
db_drop_table('menu_token');
|
159
|
}
|
160
|
|
161
|
return t('The Menu Token module has been updated successfully.');
|
162
|
}
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Drop the deprecated menu_token table if it exists.
|
167
|
*/
|
168
|
function menu_token_update_7003() {
|
169
|
if (db_table_exists('menu_token')) {
|
170
|
db_drop_table('menu_token');
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Migrate menu items link path, from '<front>' to 'menutoken/[uuid]'.
|
176
|
*/
|
177
|
function menu_token_update_7004() {
|
178
|
$result = db_select('menu_links', 'm')
|
179
|
->fields('m', array('mlid', 'options'))
|
180
|
->condition('link_path', '<front>')
|
181
|
->execute();
|
182
|
|
183
|
foreach ($result as $menu_link) {
|
184
|
$options = unserialize($menu_link->options);
|
185
|
if (isset($options['menu_token_data'])) {
|
186
|
db_update('menu_links')
|
187
|
->fields(array(
|
188
|
'link_path' => 'menutoken/' . uniqid(),
|
189
|
'router_path' => 'menutoken/%',
|
190
|
))
|
191
|
->condition('mlid', $menu_link->mlid)
|
192
|
->execute();
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Implements hook_update_N().
|
199
|
* Update Weight to 10
|
200
|
*/
|
201
|
function menu_token_update_7005() {
|
202
|
db_update('system')
|
203
|
->fields(array('weight' => 10))
|
204
|
->condition('type', 'module', '=')
|
205
|
->condition('name', 'menu_token', '=')
|
206
|
->execute();
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Implements hook_uninstall().
|
211
|
*/
|
212
|
function menu_token_uninstall() {
|
213
|
$result = db_select('variable', 'v')
|
214
|
->fields('v', array('name'))
|
215
|
->condition('name', 'menu_token_%', '=')
|
216
|
->execute();
|
217
|
|
218
|
foreach ($result as $row) {
|
219
|
variable_del($row->name);
|
220
|
}
|
221
|
}
|