/
mobile_codes.install
executable file
·224 lines (209 loc) · 6.19 KB
/
mobile_codes.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* @file
*/
/**
* Implements hook_schema().
*/
function mobile_codes_schema() {
$schema['mobile_codes_presets'] = array(
'export' => array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'name',
'identifier' => 'preset',
'default hook' => 'default_mobile_codes_preset',
'api' => array(
'owner' => 'mobile_codes',
'api' => 'default_mobile_codes_presets',
'minimum_version' => 2,
'current_version' => 2,
),
),
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => '', // @TODO
),
'provider' => array(
'type' => 'varchar',
'length' => 255,
'description' => '', // @TODO
),
'defaults' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => '', // @TODO
),
),
'unique keys' => array(
'name' => array('name')
),
'primary key' => array('name'),
);
$schema['mobile_codes_providers'] = array(
'export' => array(
'key' => 'name',
'key name' => 'Name',
'primary key' => 'name',
'identifier' => 'provider',
'default hook' => 'default_mobile_codes_provider',
'api' => array(
'owner' => 'mobile_codes',
'api' => 'default_mobile_codes_providers',
'minimum_version' => 2,
'current_version' => 2,
),
),
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 255,
'description' => '', // @TODO
),
'url' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'description' => '', // @TODO
),
'parameters' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => '', // @TODO
)
),
'primary key' => array('name'),
'unique keys' => array(
'name' => array('name')
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function mobile_codes_install() {
drupal_install_schema('mobile_codes');
}
/**
* Implements hook_uninstall().
*/
function mobile_codes_uninstall() {
drupal_uninstall_schema('mobile_codes');
variable_del('mobile_codes_block_node_url_preset');
variable_del('mobile_codes_block_user_vcard_preset');
variable_del('mobile_codes_settings');
}
/**
* Implements hook_update_N().
*/
function mobile_codes_update_6200() {
$ret = array();
// Remove 'mobile_codes' table.
db_drop_table($ret, 'mobile_codes');
// Modify 'mobile_codes_presets' table and data.
db_drop_field($ret, 'mobile_codes_presets', 'pid');
db_add_field($ret, 'mobile_codes_presets', 'provider', array('type' => 'varchar', 'length' => 255, 'description' => ''));
db_change_field($ret, 'mobile_codes_presets', 'data', 'defaults', array('type' => 'text', 'size' => 'medium', 'not null' => TRUE, 'serialize' => TRUE, 'description' => ''));
$results = db_query('SELECT * FROM {mobile_codes_presets} WHERE provider IS NULL');
while ($result = db_fetch_object($results)) {
$old = unserialize($result->defaults);
$provider = "nokia_{$old['type']}";
$name = drupal_strtolower($result->name);
$size = $old['type'] = 'qr'
? array('s' => 2, 'm' => 4, 'l' => 6)
: array('s' => 0.12, 'm' => 0.18, 'l' => 0.25);
$defaults = serialize(array('size' => $size[strtolower($old['size'])]));
$ret[] = _mobile_codes_update_sql("UPDATE {mobile_codes_presets} SET name = '{$name}', provider = '{$provider}', defaults = '%s' WHERE name = '{$result->name}'", $defaults);
}
// Add 'mobile_codes_providers' table.
db_create_table($ret, 'mobile_codes_providers', array(
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 255,
'description' => '', // @TODO
),
'url' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'description' => '', // @TODO
),
'parameters' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => '', // @TODO
)
),
'primary key' => array('name'),
'unique keys' => array(
'name' => array('name')
),
));
// Delete variables.
variable_del('mobile_codes_flush');
return $ret;
}
/**
* Perform an SQL query and return success or failure.
*
* @see http://api.drupal.org/api/function/update_sql/6
*
* @param $sql
* A string containing a complete SQL query.
* @return
* An array containing the keys:
* success: a boolean indicating whether the query succeeded
* query: the SQL query executed, passed through check_plain()
* rows: number of rows effected
*/
function _mobile_codes_update_sql($sql) {
$args = func_get_args();
array_shift($args);
$result = db_query($sql, $args);
$sql = _mobile_codes_return_query_string($sql, $args);
return array('success' => $result !== FALSE, 'query' => check_plain($sql), 'rows' => db_affected_rows());
}
/**
* Builds a basic query, returns string.
*
* @see http://api.drupal.org/api/function/db_query/6
*
* @param $query
* A string containing an SQL query.
* @param ...
* A variable number of arguments which are substituted into the query
* using printf() syntax. Instead of a variable number of query arguments,
* you may also pass a single array containing the query arguments.
*
* Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
* in '') and %%.
*
* NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
* and TRUE values to decimal 1.
*
* @return
* String of query that would have been run
*/
function _mobile_codes_return_query_string($query) {
$args = func_get_args();
array_shift($args);
$query = db_prefix_tables($query);
if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}
_db_query_callback($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
return $query;
}