uw_virtual_site_homepage.install 15.3 KB
Newer Older
1
2
<?php

3
4
5
6
/*
 * Implements hook_update_dependencies().
 * Setting the order of update hooks so that uw_virtual_site_homepage_update_7100 runs first.
 */
7
function uw_virtual_site_homepage_update_dependencies() {
8
  // Indicate that the uw_virtual_site_homepage_update_7100() function provided by this module
9
10
  // must run before the one of the os_profiles_update_N() function provided by the 'os_profiles' module.

11
12
13
  $dependencies['os_profiles'][7045] = array(
    'uw_virtual_site_homepage' => 7100,
  );
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  $dependencies['os_profiles'][7048] = array(
    'uw_virtual_site_homepage' => 7100,
  );
  $dependencies['os_profiles'][7049] = array(
    'uw_virtual_site_homepage' => 7100,
  );
  $dependencies['os_profiles'][7050] = array(
    'uw_virtual_site_homepage' => 7100,
  );
  $dependencies['os_profiles'][7051] = array(
    'uw_virtual_site_homepage' => 7100,
  );
  $dependencies['os_profiles'][7052] = array(
    'uw_virtual_site_homepage' => 7100,
  );
29
30
31
  return $dependencies;
}

32
/**
33
 * Implements hook_update_N().
34
35
 * Changing Office Hours field name and instance for virtual homepages.
 */
Lily Yan's avatar
Lily Yan committed
36
function uw_virtual_site_homepage_update_7100() {
Eric Bremner's avatar
Eric Bremner committed
37
38
39
40
  // There is a conflict with os_profiles and uw_virtual_site_homepage for the field: field_office_hours.
  // We are going to change our custom module: uw_virtual_site_homepage, to use a field with name: field_uw_office_hours.
  // New field and instances will have to be created, all content migrated, all old instances removed.

41
42
  // Set variables, changing the below fields for uw homepage virtual sites.
  // Profile image, Site description, Faculty, Office location, Phone number, Extension, Email and Office hours.
43

44
45
  $entity_type = 'node';
  $bundle = 'uw_virtual_site_homepage';
46

47
48
49
50
51
52
  $old_names = array(
    'field_faculty',
	'field_office_location',
	'field_phone_number',
	'field_extension',
	'field_email_homepage',
53
54
55
	'field_office_hours',
    'field_profile_image',
    'body',
56
  );
57
58

  // Step through each of the old names and convert to new names.
59
  foreach ($old_names as $old_name) {
60
61

    // If this is the body, special new name, else insert uw into old field name.
62
63
64
65
66
67
68
    if ($old_name == 'body') {
      $new_name = 'field_uw_site_description';  
    }
    else {
      $new_name =  str_replace("field", "field_uw", $old_name);
    }

69
    // Get old fields' data.
70
71
72
73
    $field_data = db_select('field_data_' . $old_name, 'old')
      ->fields('old')
      ->condition('entity_type', $entity_type)
      ->condition('bundle', $bundle)
74
      ->execute();
75

76
77
78
    // Store old fields data.
    $counter = 0;
    $data_new = array();
79
    while ($data = $field_data->fetchAssoc()) {
80
81
82
83
84
      foreach ($data as $column => $value) {
        $column = str_replace($old_name, $new_name, $column);
        $data_new[$counter][$column] = $value;
      }
      $counter++;
85
    }
86
87

    // Get old fields' revisions.
88
89
90
91
    $field_revision = db_select('field_revision_' . $old_name, 'old')
      ->fields('old')
      ->condition('entity_type', $entity_type)
      ->condition('bundle', $bundle)
92
93
      ->execute();

94
95
96
    // Store old fields' revisions.
    $counter = 0;
    $revision_new = array();
97
98
99
    while ($revision = $field_revision->fetchAssoc()) {
      foreach ($revision as $column => $value) {
        $column = str_replace($old_name, $new_name, $column);
100
        $revision_new[$counter][$column] = $value;
101
      }
102
      $counter++;
103
104
    }

105
106
107
108
    // As long as not body, delete the old field.
    if($old_name !== 'body') {
      field_delete_field($old_name);
    }
109

110
111
112
113
114
    // If there is no new field, create the new field.
    if(!db_table_exists('field_data_' . $new_name)) {
      $new_field = _get_new_field($new_name);
      field_create_field($new_field);
    }
115

116
117
118
119
120
121
122
123
124
125
126
127
128
    // Get the old instance.
    $check_old_instance = db_select('field_config_instance', 'fc')
                            ->fields('fc')
                            ->condition('field_name', $old_name)
                            ->condition('entity_type', $entity_type)
                            ->condition('bundle', $bundle)
                            ->execute()->fetchAll();

    // If there is an old instance, delete it.
    if(count($check_old_instance) > 0) {
      $instance['field_name'] = $old_name;
      $instance['entity_type'] = $entity_type;
      $instance['bundle'] = $bundle;
129
130
      field_delete_instance($instance);
    }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    // Step through each of the old fields data and set it to the new fields data.
    foreach($data_new as $data) {

      // If there is a new field, insert the old data.
      if(db_table_exists('field_data_' . $new_name)) {

        // Get the data from the new field.
        $check_for_field = db_select('field_data_' . $new_name, 'data')
                            ->fields('data');
        foreach($data as $key => $value) {
          $check_for_field->condition($key, $value);
        }
        $results = $check_for_field->execute();

        // Check if there is data from the old field in the new field.
        $insert = true;
Eric Bremner's avatar
Eric Bremner committed
148
        while($record = $results->fetchAssoc()) {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
          $insert = false;
          break;
        }

        // If there is no data from old field in new field, insert the old field data into the new field.
        if($insert) {
          db_insert('field_data_' . $new_name)
            ->fields($data)
            ->execute();
        }
      }
    }

    // Step through each old revision and set it to the new revision.
    foreach($revision_new as $revision) {

      // If there is a new revision, insert the old revision.
      if(db_table_exists('field_revision_' . $new_name)) {

        // Get the data from the new revision.
        $check_for_revision = db_select('field_revision_' . $new_name, 'data')
                            ->fields('data');
        foreach($revision as $key => $value) {
          $check_for_field->condition($key, $value);
        }
        $results = $check_for_field->execute();

        // Check if there is data from the old revision in the new revision.
        $insert = true;
        if($record = $results->fetchAssoc()) {
          $insert = false;
          break;
        }

        // If there is no old revision in the new revision, insert the old revision in the new revision.
        if($insert) {
          db_insert('field_revision_' . $new_name)
            ->fields($revision)
            ->execute();
        }
      }
    }
  }

  // Perform feature revert to place new instances of fields.
  features_revert_module('uw_virtual_site_homepage');
}

/**
 * Helper function to get the new field information
 */
200
201
202
203
function _get_new_field($name) {
  switch($name) {
    case "field_uw_email_homepage":
      $new_field = array(
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_email_homepage',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
      return $new_field;

    case "field_uw_email_homepage":
      $new_field = array(
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_email_homepage',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

      return $new_field;

    case "field_uw_extension":
      $new_field = array(
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_extension',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

293
294
295
296
      return $new_field;

    case "field_uw_faculty":
      $new_field = array(
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_faculty',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

324
325
326
327
      return $new_field;

    case "field_uw_office_hours":
      $new_field = array(
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_office_hours',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(),
                  'translatable' => 0,
                  'type' => 'text_long',
                );

353
354
355
356
      return $new_field;

    case "field_uw_office_location":
      $new_field = array(
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_office_location',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

384
385
386
387
      return $new_field;

    case "field_uw_phone_number":
      $new_field = array(
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_phone_number',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(
                    'max_length' => 255,
                  ),
                  'translatable' => 0,
                  'type' => 'text',
                );

415
416
417
418
      return $new_field;

    case "field_uw_profile_image":
      $new_field = array(
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(),
                  'field_name' => 'field_uw_profile_image',
                  'foreign keys' => array(
                    'fid' => array(
                      'columns' => array(
                        'fid' => 'fid',
                      ),
                      'table' => 'file_managed',
                    ),
                  ),
                  'indexes' => array(
                    'fid' => array(
                      0 => 'fid',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'image',
                  'settings' => array(
                    'default_image' => 0,
                    'uri_scheme' => 'public',
                  ),
                  'translatable' => 0,
                  'type' => 'image',
                );

447
448
449
450
      return $new_field;

    case "field_uw_site_description":
      $new_field = array(
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
                  'active' => 1,
                  'cardinality' => 1,
                  'deleted' => 0,
                  'entity_types' => array(
                    0 => 'node',
                  ),
                  'field_name' => 'field_uw_site_description',
                  'foreign keys' => array(
                    'format' => array(
                      'columns' => array(
                        'format' => 'format',
                      ),
                      'table' => 'filter_format',
                    ),
                  ),
                  'indexes' => array(
                    'format' => array(
                      0 => 'format',
                    ),
                  ),
                  'locked' => 0,
                  'module' => 'text',
                  'settings' => array(),
                  'translatable' => 0,
                  'type' => 'text_with_summary',
                );

478
479
      return $new_field;
  }
480
}