Projet

Général

Profil

Paste
Télécharger (7,01 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / fivestar / fivestar.install @ a5572547

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
      );
108

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

    
114
        $i = 0;
115
        while(!empty($field) && $field['type'] != 'fivestar') {
116
          $suffix = '_' . $i;
117
          $field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
118
          $i++;
119
        }
120
        if (empty($field)) {
121
          $field_values = array(
122
            'field_name' => $field_name . $suffix,
123
            'type' => 'fivestar',
124
            'settings' => array(
125
              'axis' => $tag,
126
            ),
127
          );
128
          $field = field_create_field($field_values);
129
        }
130
        
131
        // Create an instance of the field in this bundle
132
        $instance = field_read_instance('node', $field['field_name'], $type, array('include_deleted' => TRUE));
133
        if (empty($instance)) {
134
          $settings['allow_clear'] = $settings['unvote_enable'];
135
          $settings['style'] = $settings['star_display'];
136
          $settings['text'] = $settings['text_display'];
137

    
138
          $instance_info = array(
139
            'field_name' => $field['field_name'],
140
            'entity_type' => 'node',
141
            'bundle' => $type,
142
            'widget' => array(
143
              'type' => 'stars',
144
            ),
145
            'display' => array(
146
              'default' => array(
147
                'type' => 'fivestar_formatter_exposed_stars',
148
                'settings' => $settings,
149
              ),
150
            ),
151
            'settings' => array(
152
              'stars' => $settings['stars'],
153
              'target' => 'self',
154
            ),
155
          );
156

    
157
          if (variable_get('fivestar_position_teaser_' . $var_suffix, 'hidden') != 'hidden') {
158
            $instance_info['display']['teaser'] = array(
159
              'type' => 'fivestar_formatter_exposed_stars',
160
              'settings' => $settings,
161
            );
162
          }
163
          
164
          field_create_instance($instance_info);
165
        }
166
      }
167
    }
168
  }
169
  
170
  // Rebuild the menu to remove the node type tag form paths
171
  menu_rebuild();
172
  _field_info_collate_fields(TRUE);
173
}
174

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

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

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

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