Projet

Général

Profil

Paste
Télécharger (10,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.install @ 599a39cd

1
<?php
2

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

    
8
/**
9
 * Implements hook_field_schema().
10
 */
11
function date_field_schema($field) {
12
  $db_columns = array();
13
  switch ($field['type']) {
14
    case 'datestamp':
15
      $db_columns['value'] = array(
16
        'type' => 'int',
17
        'size' => 'big',
18
        'not null' => FALSE,
19
        'sortable' => TRUE,
20
        'views' => TRUE,
21
      );
22
      break;
23

    
24
    case 'datetime':
25
      $db_columns['value'] = array(
26
        'type' => 'datetime',
27
        'mysql_type' => 'datetime',
28
        'pgsql_type' => 'timestamp without time zone',
29
        'sqlite_type' => 'varchar',
30
        'sqlsrv_type' => 'smalldatetime',
31
        'not null' => FALSE,
32
        'sortable' => TRUE,
33
        'views' => TRUE,
34
      );
35
      break;
36

    
37
    default:
38
      $db_columns['value'] = array(
39
        'type' => 'varchar',
40
        'length' => 20,
41
        'not null' => FALSE,
42
        'sortable' => TRUE,
43
        'views' => TRUE,
44
      );
45
  }
46

    
47
  // If a second date is needed for 'End date', make a copy of the first one.
48
  if (!empty($field['settings']['todate'])) {
49
    $db_columns['value2'] = $db_columns['value'];
50

    
51
    // We don't want Field API to create additional columns, just the first.
52
    // We modify them our own way in views data.
53
    $db_columns['value2']['views'] = FALSE;
54

    
55
    // Create a compound index on value and value2 and an index on value2.
56
    $indexes = array(
57
      'value' => array('value', 'value2'),
58
      'value2' => array('value2'),
59
    );
60
  }
61
  else {
62
    // Otherwise just an index on value.
63
    $indexes = array('value' => array('value'));
64
  }
65
  // Timezone and offset columns are used only if date-specific dates are used.
66
  if (isset($field['settings']['tz_handling']) && $field['settings']['tz_handling'] == 'date') {
67
    $db_columns['timezone'] = array(
68
      'type' => 'varchar',
69
      'length' => 50,
70
      'not null' => FALSE,
71
      'sortable' => TRUE,
72
      'views' => FALSE,
73
    );
74
    $db_columns['offset'] = array(
75
      'type' => 'int',
76
      'not null' => FALSE,
77
      'sortable' => TRUE,
78
      'views' => FALSE,
79
    );
80
    if (!empty($field['settings']['todate'])) {
81
      $db_columns['offset2'] = array(
82
        'type' => 'int',
83
        'not null' => FALSE,
84
        'sortable' => TRUE,
85
        'views' => FALSE,
86
      );
87
    }
88
  }
89
  if (isset($field['settings']['repeat']) && $field['settings']['repeat'] == 1) {
90
    $db_columns['rrule'] = array(
91
      'type' => 'text',
92
      'not null' => FALSE,
93
      'sortable' => FALSE,
94
      'views' => FALSE,
95
    );
96
  }
97
  return array('columns' => $db_columns, 'indexes' => $indexes);
98
}
99

    
100
/**
101
 * Implements hook_update_last_removed().
102
 */
103
function date_update_last_removed() {
104
  return 6005;
105
}
106

    
107
/**
108
 * Get rid of the individual formatters for each format type.
109
 *
110
 * These are now settings in the default formatter.
111
 */
112
function date_update_7000() {
113
  $instances = field_info_instances();
114
  foreach ($instances as $entity_type => $entities) {
115
    foreach ($entities as $bundle => $fields) {
116
      foreach ($fields as $field_name => $instance) {
117
        if (in_array($instance['widget']['type'], array('date_popup'))) {
118
          $changed = FALSE;
119
          foreach ($instance['display'] as $context => $display) {
120
            if ($display['type'] != 'date_default' && $display['type'] != 'date_interval' && $display['type'] != 'hidden') {
121
              $instance['display'][$context]['type'] = 'date_default';
122
              $instance['display'][$context]['settings']['format_type'] = str_replace('date_', '', $display['type']);
123
              $changed = TRUE;
124
            }
125
          }
126
          if ($changed) {
127
            field_update_instance($instance);
128
          }
129
        }
130
      }
131
    }
132
  }
133
}
134

    
135
/**
136
 * Get rid of the separate widgets for repeating dates.
137
 *
138
 * The code now handles repeating dates correctly using the regular widgets.
139
 */
140
function date_update_7001() {
141
  $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC));
142
  $query->join('field_config', 'fc', 'fc.id = fci.field_id');
143
  $query->fields('fci');
144
  $query->condition(db_or()->condition('fc.type', 'date')->condition('fc.type', 'datestamp')->condition('fc.type', 'datetime'));
145
  $results = $query->execute();
146

    
147
  $allowed_types = array(
148
    'date_popup_repeat',
149
    'date_text_repeat',
150
    'date_select_repeat',
151
  );
152
  foreach ($results as $record) {
153
    $instance = unserialize($record['data']);
154
    if (in_array($instance['widget']['type'], $allowed_types)) {
155
      $instance['widget']['type'] = str_replace('_repeat', '', $instance['widget']['type']);
156
      db_update('field_config_instance')
157
        ->fields(array(
158
          'data' => serialize($instance),
159
        ))
160
        ->condition('field_name', $record['field_name'])
161
        ->condition('entity_type', $record['entity_type'])
162
        ->condition('bundle', $record['bundle'])
163
        ->execute();
164
    }
165
  }
166
  field_cache_clear();
167
  drupal_set_message(t('The widgets for repeating dates have changed. Please check the Display Fields page for each content type that has repeating date fields and confirm that the right widget has been selected.'), 'warning');
168
}
169

    
170
/**
171
 * Add a notification about the new Date All Day module, and enable it.
172
 */
173
function date_update_7002() {
174
  drupal_set_message(t("The <em>All Day</em> functionality has been moved into a separate module. This new module provides the option to add an <em>All Day</em> checkbox to toggle time on and off for date fields. It also contains the theme that displays the <em>All Day</em> text on fields that have no time. For consistency with prior code, it has been automatically enabled. If you don't want the <em>All Day</em> functionality you can disable this module."));
175
  module_enable(array('date_all_day'));
176
}
177

    
178
/**
179
 * Adds a notification about the new Date Repeat Field module, and enable it.
180
 */
181
function date_update_7003() {
182
  drupal_set_message(t("The <em>Date Repeat</em> integration for Date fields is being moved into a separate module. For consistency with prior code, it has been automatically enabled if the Date Repeat API module is enabled. If you don't use <em>Date Repeat</em> functionality in your fields, you can disable this module."));
183
  if (module_exists('date_repeat')) {
184
    module_enable(array('date_repeat_field'));
185
  }
186
}
187

    
188
/**
189
 * Date text widgets should always use an increment of 1.
190
 */
191
function date_update_7004() {
192

    
193
  // Select date fields.
194
  $query = db_select('field_config_instance', 'fci', array('fetch' => PDO::FETCH_ASSOC));
195
  $query->join('field_config', 'fc', 'fc.id = fci.field_id');
196
  $query->fields('fci');
197
  $query->condition(db_or()->condition('fc.type', 'date')->condition('fc.type', 'datestamp')->condition('fc.type', 'datetime'));
198
  $results = $query->execute();
199

    
200
  // Find the ones that use the date_text widget.
201
  foreach ($results as $record) {
202
    $instance = unserialize($record['data']);
203
    if (in_array($instance['widget']['type'], array('date_text'))) {
204
      $instance['widget']['settings']['increment'] = 1;
205
      db_update('field_config_instance')
206
        ->fields(array(
207
          'data' => serialize($instance),
208
        ))
209
        ->condition('field_name', $record['field_name'])
210
        ->condition('entity_type', $record['entity_type'])
211
        ->condition('bundle', $record['bundle'])
212
        ->execute();
213
    }
214
  }
215
  field_cache_clear();
216
  drupal_set_message(t('Date text widgets have been updated to use an increment of 1.'));
217
}
218

    
219
/**
220
 * Revisited: Date text widgets should always use an increment of 1.
221
 */
222
function date_update_7005() {
223
  // @see https://www.drupal.org/node/1355256
224
  date_update_7004();
225
}
226

    
227
/**
228
 * Add date value indexes to existed field tables.
229
 */
230
function date_update_7006() {
231
  // Get all fields provided by the Data module.
232
  $query = db_select('field_config', 'f')
233
    ->fields('f', array('id', 'field_name', 'data'))
234
    ->condition('f.type', array('datetime', 'date', 'datestamp'));
235

    
236
  // Special handling for the Encrypt module.
237
  if (module_exists('field_encrypt')) {
238
    $query->condition('f.data', '%' . db_like(' field_encrypt";a:1:{s:7:"encrypt";i:0 ') . '%', 'LIKE');
239
  }
240

    
241
  // Complete the query.
242
  $fields = $query->execute()->fetchAllAssoc('id');
243

    
244
  foreach ($fields as $id => $info) {
245
    $field_info = field_info_field($info->field_name);
246

    
247
    // Add indexes only for SQL storage fields.
248
    if ($field_info['storage']['type'] != 'field_sql_storage') {
249
      continue;
250
    }
251

    
252
    $tables = array(
253
      key($field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT']),
254
      key($field_info['storage']['details']['sql']['FIELD_LOAD_REVISION']),
255
    );
256

    
257
    $data = unserialize($info->data);
258
    $field_name = $info->field_name . '_value';
259
    $field_name2 = $info->field_name . '_value2';
260

    
261
    // Build indexes.
262
    $indexes = $data['indexes'] = array();
263
    if (!empty($data['settings']['todate'])) {
264
      $indexes[$field_name] = array($field_name, $field_name2);
265
      $indexes[$field_name2] = array($field_name2);
266
      $data['indexes']['value'] = array('value', 'value2');
267
      $data['indexes']['value2'] = array('value2');
268
    }
269
    else {
270
      $indexes[$field_name] = array($field_name);
271
      $data['indexes']['value'] = array('value');
272
    }
273

    
274
    // Add missed indexes to tables.
275
    foreach ($indexes as $name => $index) {
276
      foreach ($tables as $table) {
277
        if (!db_index_exists($table, $name)) {
278
          db_add_index($table, $name, $index);
279
        }
280
      }
281
    }
282

    
283
    // Fix date fields storage 'field_config' indexes.
284
    db_update('field_config')
285
      ->fields(array('data' => serialize($data)))
286
      ->condition('id', $id)
287
      ->execute();
288
  }
289
}
290

    
291
/**
292
 * Update datestamp field schema to use 'big' integers.
293
 */
294
function date_update_7007() {
295
  $fields = field_read_fields(array('type' => 'datestamp'));
296
  foreach ($fields as $field_name => $field) {
297
    foreach (array_intersect_key($field['columns'], drupal_map_assoc(array('value', 'value2'))) as $column_name => $schema) {
298
      $schema['size'] = 'big';
299
      $column = $field_name . '_' . $column_name;
300
      if (db_table_exists('field_data_' . $field_name)) {
301
        db_change_field('field_data_' . $field_name, $column, $column, $schema);
302
      }
303
      if (db_table_exists('field_revision_' . $field_name)) {
304
        db_change_field('field_revision_' . $field_name, $column, $column, $schema);
305
      }
306
    }
307
  }
308
}
309

    
310
/**
311
 * The date_migrate_example module was renamed.
312
 */
313
function date_update_7200() {
314
  db_delete('system')->condition('name', 'date_migrate_example')->execute();
315
}