Projet

Général

Profil

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

root / drupal7 / sites / all / modules / fivestar / fivestar.install @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Installation file for fivestar module.
6
 */
7

    
8
function fivestar_uninstall() {
9
  db_query("DELETE FROM {variable} WHERE name LIKE 'fivestar_%'");
10
}
11

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

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

    
41
  foreach ($fields as $field) {
42
    if (is_numeric($field['settings']['axis'])) {
43
      $field['settings']['axis'] = $tags_numeric[$field['settings']['axis']];
44
    }
45
  }
46
}
47

    
48
/**
49
 * Moves the field settings to field instance settings
50
 */
51
function fivestar_update_7202() {
52
  $fields = field_read_fields(array('module' => 'fivestar'));
53

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

    
63

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

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

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

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

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

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

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

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

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

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

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

    
165
          field_create_instance($instance_info);
166
        }
167
      }
168
    }
169
  }
170

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

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

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

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

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

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

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

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