fivestar.install
9.05 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* @file
* Installation file for fivestar module.
*/
function fivestar_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE 'fivestar_%'");
}
/**
* hook_field_schema().
*/
function fivestar_field_schema() {
return array(
'columns' => array(
'rating' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'sortable' => TRUE
),
'target' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE
),
),
);
}
/**
* Fixes the axis value stored for fivestar fields
*/
function fivestar_update_7201() {
drupal_load('module', 'fivestar');
$fields = field_read_fields(array('module' => 'fivestar'));
$tags_numeric = array_values(fivestar_get_tags());
foreach ($fields as $field) {
if (is_numeric($field['settings']['axis'])) {
$field['settings']['axis'] = $tags_numeric[$field['settings']['axis']];
}
}
}
/**
* Moves the field settings to field instance settings
*/
function fivestar_update_7202() {
$fields = field_read_fields(array('module' => 'fivestar'));
foreach ($fields as $field) {
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
$instance['settings'] = $field['settings'];
field_update_instance($instance);
}
}
}
/**
* Convert all existing fivestar/node_type settings into fields with exposed fivestar formatters
*/
function fivestar_update_7203() {
// Gather the node types
$query = db_select('node_type', 'n');
$query->addField('n', 'type');
$result = $query->execute();
$types = $result->fetchCol();
// Gather the tags. In the case that fivestar_get_tags() is ever removed from
// the module, this update still needs to run.
$tags_txt = variable_get('fivestar_tags', 'vote');
$tags_exploded = explode(',', $tags_txt);
$tags = array();
$got_vote = FALSE;
foreach ($tags_exploded as $tag) {
$tag_trimmed = trim($tag);
if ($tag_trimmed) {
$tags[] = $tag_trimmed;
if ($tag_trimmed == 'vote') {
$got_vote = TRUE;
}
}
}
if (!$got_vote) {
$tags[] = 'vote';
}
$tags;
foreach ($tags as $tag) {
$suffix = '';
foreach ($types as $type) {
$var_suffix = $type . ($tag == 'vote' ? '' : '_' . $tag);
$settings = array(
'stars' => variable_get('fivestar_stars_' . $var_suffix, 6),
'allow_clear' => variable_get('fivestar_unvote_' . $var_suffix, 0),
'feedback_enable' => variable_get('fivestar_feedback_' . $var_suffix, 1),
'style' => variable_get('fivestar_style_' . $var_suffix, 'average'),
'text' => variable_get('fivestar_text_' . $var_suffix, 'dual'),
'title' => variable_get('fivestar_title_' . $var_suffix, 1),
);
if (variable_get('fivestar_' . $var_suffix, FALSE)) {
// Check to see if a field for this tag exists and create one if needed
$field_name = 'field_' . $tag;
$field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
$i = 0;
while (!empty($field) && $field['type'] != 'fivestar') {
$suffix = '_' . $i;
$field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
$i++;
}
if (empty($field)) {
$field_values = array(
'field_name' => $field_name . $suffix,
'type' => 'fivestar',
'settings' => array(
'axis' => $tag,
),
);
$field = field_create_field($field_values);
}
// Create an instance of the field in this bundle
$instance = field_read_instance('node', $field['field_name'], $type, array('include_deleted' => TRUE));
if (empty($instance)) {
$instance_info = array(
'field_name' => $field['field_name'],
'entity_type' => 'node',
'bundle' => $type,
'widget' => array(
'type' => 'stars',
),
'display' => array(
'default' => array(
'type' => 'fivestar_formatter_exposed_stars',
'settings' => $settings,
),
),
'settings' => array(
'stars' => $settings['stars'],
'target' => 'self',
),
);
if (variable_get('fivestar_position_teaser_' . $var_suffix, 'hidden') != 'hidden') {
$instance_info['display']['teaser'] = array(
'type' => 'fivestar_formatter_exposed_stars',
'settings' => $settings,
);
}
// Set the widget.
$widget = variable_get('fivestar_widget' . $var_suffix, 'default');
$instance_info['widget']['settings']['widget']['fivestar_widget'] = $widget;
field_create_instance($instance_info);
}
}
}
}
// Rebuild the menu to remove the node type tag form paths
menu_rebuild();
_field_info_collate_fields(TRUE);
}
/**
* Preserve settings from fivestar_formatter_exposed_stars and convert to
* fivestar_formatter_default.
*/
function fivestar_update_7204() {
$fields = field_read_fields(array('type' => 'fivestar'));
foreach ($fields as $field) {
// Iterate through the instances of the field.
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
// The default should be to not allow clearing.
$instance['settings']['allow_clear'] = FALSE;
// Check each of the displays on the field instance an convert the formatter
// from fivestar_formatter_exposed_stars to fivestar_formatter_default.
foreach ($instance['display'] as $key => $display) {
if ($display['type'] == 'fivestar_formatter_exposed_stars') {
// Convert the formatter and set the exposed settings.
$instance['display'][$key]['type'] == 'fivestar_formatter_default';
$instance['display'][$key]['settings']['expose'] = TRUE;
// The widget type needs to be exposed for the widget to be exposed.
$instance['widget']['type'] = 'exposed';
// If one of the displays allowed clearing change the field settings
// to allow clearing.
if ($display['settings']['allow_clear'] == TRUE) {
$instance['settings']['allow_clear'] = TRUE;
}
}
}
// Update the instance
field_update_instance($instance);
}
}
}
/**
* Rename fivestar 'select' widget to 'fivestar_select'
* @see http://drupal.org/node/1285456
*/
function fivestar_update_7205() {
$fields = field_read_fields(array('type' => 'fivestar'));
foreach ($fields as $field) {
// Iterate through the instances of the field.
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
// If the widget type is select, lets change it.
if ($instance['widget']['type'] == 'select') {
$instance['widget']['type'] = 'fivestar_select';
// Update the instance
field_update_instance($instance);
}
}
}
}
/**
* Preserve setting after new feature preventing re-votes
* @see http://drupal.org/node/356605
*/
function fivestar_update_7206() {
$fields = field_read_fields(array('type' => 'fivestar'));
foreach ($fields as $field) {
// Iterate through the instances of the field.
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
// The default should be to allow re-voting.
$instance['settings']['allow_revote'] = TRUE;
// Update the instance
field_update_instance($instance);
}
}
}
/**
* Preserve setting after new feature preventing own votes
* @see http://drupal.org/node/189527
*/
function fivestar_update_7207() {
$fields = field_read_fields(array('type' => 'fivestar'));
foreach ($fields as $field) {
// Iterate through the instances of the field.
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
// The default should be to allow own votes.
$instance['settings']['allow_ownvote'] = TRUE;
// Update the instance
field_update_instance($instance);
}
}
}
/**
* Change field formatters to ensure unique.
* @see http://drupal.org/node/1063754
*/
function fivestar_update_7208() {
$fields = field_read_fields(array('type' => 'fivestar'));
foreach ($fields as $field) {
// Iterate through the instances of the field.
$instances = field_read_instances(array('field_name' => $field['field_name']));
foreach ($instances as $instance) {
$updated = FALSE;
foreach ($instance['display'] as &$display) {
if (in_array($display['type'], array('default', 'percentage', 'rating'))) {
$updated = TRUE;
$display['type'] = 'fivestar_formatter_' . $display['type'];
}
}
if ($updated) {
// Only trigger instance update if we actually changed anything.
field_update_instance($instance);
}
}
}
}