Projet

Général

Profil

Paste
Télécharger (9,13 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / fivestar / fivestar.install @ d50a36e0

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update, and uninstall functions the Fivestar module.
6
 */
7

    
8
/**
9
 * Implements hook_uninstall().
10
 */
11
function fivestar_uninstall() {
12
  db_query("DELETE FROM {variable} WHERE name LIKE 'fivestar_%'");
13
}
14

    
15
/**
16
 * Hook_field_schema().
17
 */
18
function fivestar_field_schema() {
19
  return array(
20
    'columns' => array(
21
      'rating' => array(
22
        'type' => 'int',
23
        'unsigned' => TRUE,
24
        'not null' => FALSE,
25
        'sortable' => TRUE,
26
      ),
27
      'target' => array(
28
        'type' => 'int',
29
        'unsigned' => TRUE,
30
        'not null' => FALSE,
31
      ),
32
    ),
33
  );
34
}
35

    
36
/**
37
 * Fixes the axis value stored for fivestar fields.
38
 */
39
function fivestar_update_7201() {
40
  drupal_load('module', 'fivestar');
41
  $fields = field_read_fields(array('module' => 'fivestar'));
42
  $tags_numeric = array_values(fivestar_get_tags());
43

    
44
  foreach ($fields as $field) {
45
    if (is_numeric($field['settings']['axis'])) {
46
      $field['settings']['axis'] = $tags_numeric[$field['settings']['axis']];
47
    }
48
  }
49
}
50

    
51
/**
52
 * Moves the field settings to field instance settings.
53
 */
54
function fivestar_update_7202() {
55
  $fields = field_read_fields(array('module' => 'fivestar'));
56

    
57
  foreach ($fields as $field) {
58
    $instances = field_read_instances(array('field_name' => $field['field_name']));
59
    foreach ($instances as $instance) {
60
      $instance['settings'] = $field['settings'];
61
      field_update_instance($instance);
62
    }
63
  }
64
}
65

    
66
/**
67
 * Converts all existing fivestar/node_type settings into fields with exposed fivestar formatters.
68
 */
69
function fivestar_update_7203() {
70
  // Gather the node types.
71
  $query = db_select('node_type', 'n');
72
  $query->addField('n', 'type');
73
  $result = $query->execute();
74
  $types = $result->fetchCol();
75

    
76
  // Gather the tags.  In the case that fivestar_get_tags() is ever removed from
77
  // the module, this update still needs to run.
78
  $tags_txt = variable_get('fivestar_tags', 'vote');
79
  $tags_exploded = explode(',', $tags_txt);
80

    
81
  $tags = array();
82
  $got_vote = FALSE;
83
  foreach ($tags_exploded as $tag) {
84
    $tag_trimmed = trim($tag);
85
    if ($tag_trimmed) {
86
      $tags[] = $tag_trimmed;
87
      if ($tag_trimmed == 'vote') {
88
        $got_vote = TRUE;
89
      }
90
    }
91
  }
92

    
93
  if (!$got_vote) {
94
    $tags[] = 'vote';
95
  }
96
  $tags;
97

    
98
  foreach ($tags as $tag) {
99
    $suffix = '';
100
    foreach ($types as $type) {
101
      $var_suffix = $type . ($tag == 'vote' ? '' : '_' . $tag);
102

    
103
      $settings = array(
104
        'stars' => variable_get('fivestar_stars_' . $var_suffix, 6),
105
        'allow_clear' => variable_get('fivestar_unvote_' . $var_suffix, 0),
106
        'feedback_enable' => variable_get('fivestar_feedback_' . $var_suffix, 1),
107
        'style' => variable_get('fivestar_style_' . $var_suffix, 'average'),
108
        'text' => variable_get('fivestar_text_' . $var_suffix, 'dual'),
109
        'title' => variable_get('fivestar_title_' . $var_suffix, 1),
110
      );
111

    
112
      if (variable_get('fivestar_' . $var_suffix, FALSE)) {
113
        // Check to see if a field for this tag exists and create one if needed.
114
        $field_name = 'field_' . $tag;
115
        $field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
116

    
117
        $i = 0;
118
        while (!empty($field) && $field['type'] != 'fivestar') {
119
          $suffix = '_' . $i;
120
          $field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
121
          $i++;
122
        }
123
        if (empty($field)) {
124
          $field_values = array(
125
            'field_name' => $field_name . $suffix,
126
            'type' => 'fivestar',
127
            'settings' => array(
128
              'axis' => $tag,
129
            ),
130
          );
131
          $field = field_create_field($field_values);
132
        }
133

    
134
        // Create an instance of the field in this bundle.
135
        $instance = field_read_instance('node', $field['field_name'], $type, array('include_deleted' => TRUE));
136
        if (empty($instance)) {
137
          $instance_info = array(
138
            'field_name' => $field['field_name'],
139
            'entity_type' => 'node',
140
            'bundle' => $type,
141
            'widget' => array(
142
              'type' => 'stars',
143
            ),
144
            'display' => array(
145
              'default' => array(
146
                'type' => 'fivestar_formatter_exposed_stars',
147
                'settings' => $settings,
148
              ),
149
            ),
150
            'settings' => array(
151
              'stars' => $settings['stars'],
152
              'target' => 'self',
153
            ),
154
          );
155

    
156
          if (variable_get('fivestar_position_teaser_' . $var_suffix, 'hidden') != 'hidden') {
157
            $instance_info['display']['teaser'] = array(
158
              'type' => 'fivestar_formatter_exposed_stars',
159
              'settings' => $settings,
160
            );
161
          }
162

    
163
          // Set the widget.
164
          $widget = variable_get('fivestar_widget' . $var_suffix, 'default');
165
          $instance_info['widget']['settings']['widget']['fivestar_widget'] = $widget;
166

    
167
          field_create_instance($instance_info);
168
        }
169
      }
170
    }
171
  }
172

    
173
  // Rebuild the menu to remove the node type tag form paths.
174
  menu_rebuild();
175
  _field_info_collate_fields(TRUE);
176
}
177

    
178
/**
179
 * Preserve settings from fivestar_formatter_exposed_stars and convert to fivestar_formatter_default.
180
 */
181
function fivestar_update_7204() {
182
  $fields = field_read_fields(array('type' => 'fivestar'));
183
  foreach ($fields as $field) {
184
    // Iterate through the instances of the field.
185
    $instances = field_read_instances(array('field_name' => $field['field_name']));
186
    foreach ($instances as $instance) {
187
      // The default should be to not allow clearing.
188
      $instance['settings']['allow_clear'] = FALSE;
189
      // Check each of the displays on the field instance an convert the formatter
190
      // from fivestar_formatter_exposed_stars to fivestar_formatter_default.
191
      foreach ($instance['display'] as $key => $display) {
192
        if ($display['type'] == 'fivestar_formatter_exposed_stars') {
193
          // Convert the formatter and set the exposed settings.
194
          $instance['display'][$key]['type'] == 'fivestar_formatter_default';
195
          $instance['display'][$key]['settings']['expose'] = TRUE;
196

    
197
          // The widget type needs to be exposed for the widget to be exposed.
198
          $instance['widget']['type'] = 'exposed';
199

    
200
          // If one of the displays allowed clearing change the field settings
201
          // to allow clearing.
202
          if ($display['settings']['allow_clear'] == TRUE) {
203
            $instance['settings']['allow_clear'] = TRUE;
204
          }
205
        }
206
      }
207
      // Update the instance.
208
      field_update_instance($instance);
209
    }
210
  }
211
}
212

    
213
/**
214
 * Rename fivestar 'select' widget to 'fivestar_select'.
215
 *
216
 * @see http://drupal.org/node/1285456
217
 */
218
function fivestar_update_7205() {
219
  $fields = field_read_fields(array('type' => 'fivestar'));
220
  foreach ($fields as $field) {
221
    // Iterate through the instances of the field.
222
    $instances = field_read_instances(array('field_name' => $field['field_name']));
223
    foreach ($instances as $instance) {
224
      // If the widget type is select, lets change it.
225
      if ($instance['widget']['type'] == 'select') {
226
        $instance['widget']['type'] = 'fivestar_select';
227
        // Update the instance.
228
        field_update_instance($instance);
229
      }
230
    }
231
  }
232
}
233

    
234
/**
235
 * Preserve setting after new feature preventing re-votes.
236
 *
237
 * @see http://drupal.org/node/356605
238
 */
239
function fivestar_update_7206() {
240
  $fields = field_read_fields(array('type' => 'fivestar'));
241
  foreach ($fields as $field) {
242
    // Iterate through the instances of the field.
243
    $instances = field_read_instances(array('field_name' => $field['field_name']));
244
    foreach ($instances as $instance) {
245
      // The default should be to allow re-voting.
246
      $instance['settings']['allow_revote'] = TRUE;
247
      // Update the instance.
248
      field_update_instance($instance);
249
    }
250
  }
251
}
252

    
253
/**
254
 * Preserve setting after new feature preventing own votes.
255
 *
256
 * @see http://drupal.org/node/189527
257
 */
258
function fivestar_update_7207() {
259
  $fields = field_read_fields(array('type' => 'fivestar'));
260
  foreach ($fields as $field) {
261
    // Iterate through the instances of the field.
262
    $instances = field_read_instances(array('field_name' => $field['field_name']));
263
    foreach ($instances as $instance) {
264
      // The default should be to allow own votes.
265
      $instance['settings']['allow_ownvote'] = TRUE;
266
      // Update the instance.
267
      field_update_instance($instance);
268
    }
269
  }
270
}
271

    
272
/**
273
 * Change field formatters to ensure unique.
274
 *
275
 * @see http://drupal.org/node/1063754
276
 */
277
function fivestar_update_7208() {
278
  $fields = field_read_fields(array('type' => 'fivestar'));
279
  foreach ($fields as $field) {
280
    // Iterate through the instances of the field.
281
    $instances = field_read_instances(array('field_name' => $field['field_name']));
282
    foreach ($instances as $instance) {
283
      $updated = FALSE;
284
      foreach ($instance['display'] as &$display) {
285
        if (in_array($display['type'], array('default', 'percentage', 'rating'))) {
286
          $updated = TRUE;
287
          $display['type'] = 'fivestar_formatter_' . $display['type'];
288
        }
289
      }
290
      if ($updated) {
291
        // Only trigger instance update if we actually changed anything.
292
        field_update_instance($instance);
293
      }
294
    }
295
  }
296
}