Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date_api / date_api.install @ db9ffd17

1
<?php
2

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

    
8
/**
9
 * Helper function for setting Date variables.
10
 */
11
function date_api_set_variables() {
12
  // Set absolute minimum and maximum year for dates on this site.
13
  // There is actually no maximum and minimum year in PHP 5, but a date with
14
  // a year less than 0 would result in negative ISO and DATETIME dates,
15
  // like -1250-01-01T00:00:00, which probably won't make sense or work
16
  // correctly anywhere.
17
  // The odd construct of using variable_get() instead of variable_set()
18
  // is so we don't accidentally write over an existing value. If
19
  // no value is set, variable_get() will set it.
20
  variable_get('date_max_year', 4000);
21
  variable_get('date_min_year', 1);
22
  variable_get('date_php_min_year', 1901);
23

    
24
  // Set an API version in a way that other modules can test for compatibility.
25
  variable_set('date_api_version', '7.2');
26
}
27

    
28
/**
29
 * Implements hook_requirements().
30
 */
31
function date_api_requirements($phase) {
32
  $requirements = array();
33

    
34
  if ($phase == 'runtime') {
35
    $t = get_t();
36
    module_load_include('module', 'date_api');
37
    $messages = date_api_status();
38
    $error_messages = !empty($messages['errors']) ? $messages['errors'] : array();
39
    $success_messages = !empty($messages['success']) ? $messages['success'] : array();
40

    
41
    if (!empty($error_messages)) {
42
      $requirements['date'] = array(
43
        'title' => $t('Date API'),
44
        'value' => $t('Missing system date settings'),
45
        'description' => implode(' ', array_merge($error_messages, $success_messages)),
46
        'severity' => REQUIREMENT_ERROR,
47
      );
48
    }
49
    else {
50
      $requirements['date'] = array(
51
        'title' => $t('Date API'),
52
        'value' => $t('System date settings'),
53
        'description' => implode(' ', $success_messages),
54
      );
55
    }
56
  }
57
  return $requirements;
58
}
59

    
60
/**
61
 * Implements hook_install().
62
 */
63
function date_api_install() {
64
  // Only set the message if Drupal itself is already installed.
65
  if (variable_get('install_task') == 'done') {
66
    // Ensure translations don't break at install time.
67
    $t = get_t();
68

    
69
    // date_api_set_variables can install date_timezone. The
70
    // date_timezone_install() function does a module_enable('date_api'). This
71
    // means that date_api_enable() can be called before date_api_install()
72
    // finishes! So the date_api schema needs to be installed before this line!
73
    date_api_set_variables();
74

    
75
    $message = $t('The Date API requires that you set up the <a href="@regional_settings">site timezone and first day of week settings</a> and the <a href="@regional_date_time">date format settings</a> to function correctly.', array('@regional_settings' => url('admin/config/regional/settings'), '@regional_date_time' => url('admin/config/regional/date-time')));
76
    drupal_set_message(filter_xss_admin($message), 'warning');
77
  }
78
}
79

    
80
/**
81
 * Implements hook_enable().
82
 */
83
function date_api_enable() {
84
  date_api_set_variables();
85
}
86

    
87
/**
88
 * Implements hook_uninstall().
89
 */
90
function date_api_uninstall() {
91
  cache_clear_all('date_timezone_identifiers_list', 'cache');
92
  $variables = array(
93
    'date_api_version',
94
    'date_min_year',
95
    'date_max_year',
96
    'date_php_min_year',
97
    'date_db_tz_support',
98
    'date_api_use_iso8601',
99
    );
100
  foreach ($variables as $variable) {
101
    variable_del($variable);
102
  }
103

    
104
  if (db_table_exists('views_display')) {
105
    $displays = array(
106
      'date_nav',
107
    );
108
    db_query("DELETE FROM {views_display} WHERE display_plugin IN ('" . implode("','", $displays) . "')");
109
    db_query("DELETE FROM {cache_views}");
110
  }
111
}
112

    
113
/**
114
 * Implements hook_update_last_removed().
115
 */
116
function date_api_update_last_removed() {
117
  return 6005;
118
}
119

    
120
/**
121
 * Move old date format data to new date format tables, and delete the old
122
 * tables. Insert only values that don't already exist in the new tables, in
123
 * case new version of those custom values have already been created.
124
 */
125
function date_api_update_7000() {
126
  // Move format data from the old 'date_format_types' table to the new
127
  // 'date_format_type' table.
128
  if (db_table_exists('date_format_types')) {
129
    // Find all the custom entries in the D6 table.
130
    $result = db_select('date_format_types', 'old')
131
      ->fields('old', array('type', 'title', 'locked'))
132
      ->condition('locked', 0)
133
      ->execute()
134
      ->fetchAll(PDO::FETCH_ASSOC);
135

    
136
    // Iterate over all the old values.
137
    foreach ($result as $row) {
138
      // See if this value already exists in the new table
139
      // (it might have been added manually before this update got run).
140
      $count = db_select('date_format_type', 'new')
141
        ->condition('type', $row['type'])
142
        ->countQuery()
143
        ->execute()
144
        ->fetchField();
145

    
146
      // If the value is missing, insert it.
147
      // Do nothing if it already exists, assume the value in the
148
      // new table trumps the old values.
149
      if (empty($count)) {
150
        db_insert('date_format_type')
151
          ->fields(array(
152
            'type' => $row['type'],
153
            'title' => $row['title'],
154
            'locked' => $row['locked'],
155
          ))
156
          ->execute();
157
      }
158
    }
159

    
160
    // Drop the old table.
161
    db_drop_table('date_format_types');
162

    
163
  }
164

    
165
  // Move format data from the old 'date_formats' table (which was renamed to
166
  // 'd6_date_formats') to the new 'date_formats' table.
167
  if (db_table_exists('d6_date_formats')) {
168
    // Find all the custom entries in the D6 table.
169
    $result = db_select('d6_date_formats', 'old')
170
      ->fields('old', array('format', 'type', 'locked'))
171
      ->condition('type', 'custom')
172
      ->execute()
173
      ->fetchAll(PDO::FETCH_ASSOC);
174

    
175
    // Iterate over all the old values.
176
    foreach ($result as $row) {
177
      // See if this value already exists in the new table (it might have been
178
      // added manually before this update got run).
179
      $count = db_select('date_formats', 'new')
180
        ->condition('format', $row['format'])
181
        ->condition('type', $row['type'])
182
        ->countQuery()
183
        ->execute()
184
        ->fetchField();
185

    
186
      // If the value is missing, insert it. Do nothing if it already exists,
187
      // assume the value in the new table trumps the old values.
188
      if (empty($count)) {
189
        db_insert('date_formats')
190
          ->fields(array(
191
            'format' => $row['format'],
192
            'type' => $row['type'],
193
            'locked' => $row['locked'],
194
          ))
195
          ->execute();
196
      }
197
    }
198

    
199
    // Drop the old table.
200
    db_drop_table('d6_date_formats');
201
  }
202

    
203
  // Move format data from the old 'date_format_locale' table (which was renamed
204
  // to 'd6_date_format_locale') to the new 'date_format_locale' table.
205
  if (db_table_exists('d6_date_format_locale')) {
206
    // Find all the custom entries in the D6 table.
207
    $result = db_select('d6_date_format_locale', 'old')
208
      ->fields('old', array('format', 'type', 'language'))
209
      ->condition('type', 'custom')
210
      ->execute()
211
      ->fetchAll(PDO::FETCH_ASSOC);
212

    
213
    // Iterate over all the old values.
214
    foreach ($result as $row) {
215
      // See if this value already exists in the new table (it might have been
216
      // added manually before this update got run).
217
      $count = db_select('date_format_locale', 'new')
218
        ->condition('format', $row['format'])
219
        ->condition('type', $row['type'])
220
        ->condition('language', $row['language'])
221
        ->countQuery()
222
        ->execute()
223
        ->fetchField();
224

    
225
      // If the value is missing, insert it.
226
      // Do nothing if it already exists, assume the value in the
227
      // new table trumps the old values.
228
      if (empty($count)) {
229
        db_insert('date_format_locale')
230
          ->fields(array(
231
            'format' => $row['format'],
232
            'type' => $row['type'],
233
            'language' => $row['language'],
234
          ))
235
          ->execute();
236
      }
237
    }
238

    
239
    // Drop the old table.
240
    db_drop_table('d6_date_format_locale');
241
  }
242
}
243

    
244

    
245
/**
246
 * Drop D6 timezone_name field on {users} after upgrading to D7.
247
 */
248
function date_api_update_7001() {
249
  if (db_field_exists('users', 'timezone_name')) {
250
    db_drop_field('users', 'timezone_name');
251
  }
252
}