Project

General

Profile

Download (18.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Install, update and uninstall functions for the cdm_dataportal module.
5
 */
6

    
7
/**
8
 * Implements hook_install().
9
 */
10
function cdm_dataportal_install() {
11
  db_update('system')
12
    ->fields(array(
13
      'weight' => 20,
14
    ))
15
    ->condition('name', 'cdm_dataportal')
16
    ->execute();
17
}
18

    
19
/**
20
 * Implements hook_uninstall().
21
 */
22
function cdm_dataportal_uninstall() {
23
  // Delete all nodes with a cdm content type from the node table.
24
  // Comment @WA: you also may want to delete these content types from the
25
  // node_type table.
26
  db_delete('node')
27
    ->condition('type', 'cdm_%')
28
    ->execute();
29
}
30

    
31
/*
32
 * update functions:
33
 *
34
 * - 1 digit for Drupal core compatibility.
35
 * - 1 digit for your module's major release version (e.g., is this the 7.x-1.* (1) or 7.x-2.* (2) series of your module?). This digit should be 0 for initial porting of your module to a new Drupal core API.
36
 * - 2 digits for sequential counting, starting with 00.
37
 *
38
 * @see http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_update_N/7
39
 */
40

    
41
/**
42
 * update for RELEASE 3.1.3:
43
 *  - reset edit_map_server variable to default
44
 */
45
function cdm_dataportal_update_7301() {
46
  // reset edit_map_server variable to default
47
   return _remove_variable('edit_map_server');
48
}
49

    
50
/**
51
 * update for RELEASE 3.1.4:
52
 *  - reset edit_map_server variable to default
53
 */
54
function cdm_dataportal_update_7302() {
55

    
56
  return
57
  _remove_variable('edit_map_server') . // once again reset edit_map_server variable to default
58
  _rename_variable('cdm_dataportal_show_media', 'cdm_images_include_children');
59
}
60

    
61
/**
62
 * update for RELEASE 3.2.1:
63
 *  - adding missing permissions for role CDM Admin
64
 */
65
function cdm_dataportal_update_7303() {
66
  $role = user_role_load_by_name('CDM admin');
67
  if(!$role){
68
    return "Role CDM admin not found, so the update is skipped.";
69
  }
70

    
71
  $tasks_performed = array();
72

    
73
  // permissions for node types
74
  $node_type_names = node_type_get_names();
75
  $types_to_update = array('page', 'story', 'article');
76
  foreach ($types_to_update as $name){
77
    if(in_array($name, $node_type_names)) {
78
      $node_type_permissions = array(
79
          'create ' . $name . ' content',
80
          'edit any ' . $name . ' content',
81
          'edit own ' . $name . ' content',
82
          'delete any ' . $name . ' content',
83
          'delete own ' . $name . ' content',
84
      );
85
      user_role_grant_permissions($role->rid, $node_type_permissions);
86
      $tasks_performed[] = "node type " . $name;
87
    }
88
  }
89

    
90
  foreach (filter_formats() as $key=>$format){
91
    if($key == 'full_html' || $format->format == "Full HTML"){
92
      $formats_permissions = array(
93
          'use text format full_html'
94
      );
95
      user_role_grant_permissions($role->rid, $formats_permissions);
96
      $tasks_performed[] = 'use text format full_html';
97
    }
98
  }
99

    
100
  // assure  'create url aliases' can be set
101
  if(!module_exists('path')){
102
    module_enable(array('path'), TRUE);
103
    $tasks_performed[] = 'module path enabled';
104
  }
105
  $other_permissions = array(
106
      'create url aliases',
107
      'delete revisions',
108
      'revert revisions',
109
      'view own unpublished content',
110
      'flush caches'
111
  );
112
  user_role_grant_permissions($role->rid, $other_permissions);
113
  $tasks_performed[] = 'and other permissions';
114

    
115
  return "adding missing permissions for role CDM Admin: " . join(', ', $tasks_performed);
116
}
117

    
118

    
119
/**
120
 * update for RELEASE 3.2.2:
121
 *  - migrating variable cdm_dataportal_show_default_image to cdm_taxon_profile_image['show']
122
 *  - enabling required module file
123
 *  - enable new debug block for all dataportals
124
 *  - migrating variables for map settings
125
 */
126
function cdm_dataportal_update_7304() {
127

    
128
    module_enable(array('file'), TRUE);
129
    $tasks_performed[] = 'module file enabled';
130

    
131
    if(!variable_get(CDM_TAXON_PROFILE_IMAGE, FALSE)){
132
      $cdm_taxon_profile_image_settings = unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT);
133
      $cdm_taxon_profile_image_settings['show'] = variable_get('cdm_dataportal_show_default_image', 0);
134
      variable_set(CDM_TAXON_PROFILE_IMAGE, $cdm_taxon_profile_image_settings);
135
      variable_del('cdm_dataportal_show_default_image');
136
      $tasks_performed[] = 'migrating variable cdm_dataportal_show_default_image to cdm_taxon_profile_image[\'show\']';
137
    }
138

    
139

    
140
    // CDM web service debug block
141
    $cdm_ws_debug_block = array(
142
        'block' => array(
143
            'module' => 'cdm_api',
144
            'delta' => 'cdm_ws_debug',
145
            'theme' => NULL,
146
            'status' => 1,
147
            'weight' => -50,
148
            'region' => 'sidebar_first',
149
            'pages' => '',
150
            'cache' => -1,
151
        ),
152
        'block_role' =>
153
            array(
154

    
155
        )
156
    );
157

    
158
    $admin_user = user_load(1);
159

    
160
    // harmonize cdm admin role names if needed
161
    $cdm_admin_role = user_role_load_by_name('CDM Admin');
162
    if(is_object($cdm_admin_role) && $cdm_admin_role->name !== 'CDM admin'){
163
      $cdm_admin_role->name = 'CDM admin';
164
      user_role_save($cdm_admin_role);
165
      $tasks_performed[] = $cdm_admin_role->name . ' role name harmonized';
166
    }
167
    $cdm_admin_role = user_role_load_by_name('CDM admin');
168

    
169
    if(is_object($cdm_admin_role)) {
170
      // make sure admin is member of role CDM admin
171
      if (!isset($admin_user->roles[$cdm_admin_role->rid])) {
172
        user_multiple_role_edit(array(1), 'add_role', $cdm_admin_role->rid);
173
        $tasks_performed[] = 'admin user (uid=1) added to role ' . $cdm_admin_role->name;
174
      }
175
      $cdm_ws_debug_block['block_role'][] =  array(
176
        'module' => 'cdm_api',
177
        'delta' => 'cdm_ws_debug',
178
        'rid' => $cdm_admin_role->rid
179
      );
180
    }
181

    
182
    $admin_role = user_role_load_by_name("admin"); // Drupal 5
183

    
184
    if(is_object($admin_role)){
185
      $admin_role->name = 'administrator';
186
      user_role_save($admin_role);
187
      $tasks_performed[] = 'administrator role name harmonized';
188
    } else {
189
      $admin_role = user_role_load_by_name("administrator"); // Drupal 7
190
    }
191
    if(is_object($admin_role)){
192
      // make sure admin is member of role admin
193
      if (!isset($admin_user->roles[$admin_role->rid])) {
194
        user_multiple_role_edit(array(1), 'add_role', $admin_role->rid);
195
        $tasks_performed[] = 'admin user (uid=1) added to role ' . $admin_role->name;
196
      }
197
      $cdm_ws_debug_block['block_role'][] =  array(
198
          'module' => 'cdm_api',
199
          'delta' => 'cdm_ws_debug',
200
          'rid' => $admin_role->rid
201
      );
202
    }
203

    
204
    // clean up existing block configuration which might be set by drupal automatically, we need to override this
205
    db_delete('block')->condition('module', 'cdm_api')->execute();
206
    db_delete('block_role')->condition('module', 'cdm_api')->execute();
207
    // insert
208
    $block_insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
209
    $block_role_insert = db_insert('block_role')->fields(array('module', 'delta', 'rid'));
210

    
211
    $themes = system_list('theme');
212
    foreach ($themes as $theme) {
213
      if($theme->status == "1"){ // only for enabled themes
214
        $cdm_ws_debug_block['block']['theme'] = $theme->name;
215
        $block_insert->values($cdm_ws_debug_block['block']);
216
      }
217
    }
218
    foreach($cdm_ws_debug_block['block_role'] as $block_role){
219
      $block_role_insert->values($block_role);
220
    }
221
    $block_insert->execute();
222
    $block_role_insert->execute();
223
    $tasks_performed[] = 'CDM web service debug block enabled for \'CDM admin\' users in all themes';
224
    $tasks_performed[] = _remove_variable('cdm_webservice_debug');
225
    $tasks_performed[] = _remove_variable('cdm_webservice_isStub');
226

    
227
    /*
228
     *  migrating variables for map settings
229
     */
230
    $cdm_map_distribution = array();
231

    
232
    $cdm_map_distribution['width'] = variable_get('cdm_dataportal_geoservice_display_width', 390);
233
    $cdm_map_distribution['height'] = $cdm_map_distribution['width'] / 2;
234
    _remove_variable('cdm_dataportal_geoservice_display_width');
235

    
236
    $cdm_map_distribution['bbox'] = variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90');
237
    _remove_variable('cdm_dataportal_geoservice_bounding_box');
238

    
239
    $cdm_map_distribution['show_labels'] = variable_get('cdm_dataportal_geoservice_labels_on', FALSE);
240
    _remove_variable('cdm_dataportal_geoservice_labels_on');
241

    
242
    $cdm_map_distribution['caption'] = variable_get('cdm_dataportal_geoservice_map_caption', '');
243
    _remove_variable('cdm_dataportal_geoservice_map_caption');
244

    
245
    $cdm_map_distribution['distribution_opacity'] = variable_get('cdm_dataportal_geoservice_distributionOpacity', '0.5');
246
    _remove_variable('cdm_dataportal_geoservice_distributionOpacity');
247

    
248
    $cdm_map_distribution['mapType'] = variable_get('cdm_dataportal_map_openlayers', 1);
249
    _remove_variable('cdm_dataportal_map_openlayers');
250

    
251
    // imageMap
252
    $cdm_map_distribution['image_map'] = array();
253
    $cdm_map_distribution['image_map']['base_layer']= variable_get('map_base_layer', 'cyprusdivs');
254
    _remove_variable('map_base_layer');
255

    
256
    $cdm_map_distribution['image_map']['bg_color'] = variable_get('map_bg_color', '1874CD');
257
    _remove_variable('map_bg_color');
258

    
259
    $cdm_map_distribution['image_map']['layer_style'] = variable_get('map_base_layer_style', 'ffffff,606060,,');
260
    _remove_variable('map_base_layer_style');
261

    
262
    // OpenLayers
263
    $cdm_map_distribution['openlayers'] = array();
264
    $cdm_map_distribution['openlayers']['base_layers'] = variable_get('baselayers', array('mapproxy_vmap0' => 'mapproxy_vmap0', 'PREFERRED' => 'mapproxy_vmap0'));
265
    _remove_variable('baselayers');
266

    
267
    $cdm_map_distribution['openlayers']['show_layer_switcher'] = variable_get('cdm_dataportal_geoservice_showLayerSwitcher', TRUE);
268
    _remove_variable('cdm_dataportal_geoservice_showLayerSwitcher');
269

    
270
    $cdm_map_distribution['legend'] = array();
271
    $cdm_map_distribution['legend']['show'] = variable_get('cdm_dataportal_geoservice_legend_on', TRUE);
272
    _remove_variable('cdm_dataportal_geoservice_legend_on');
273

    
274
    $cdm_map_distribution['legend']['opacity'] = variable_get('cdm_dataportal_geoservice_legendOpacity', '0.5');
275
    _remove_variable('cdm_dataportal_geoservice_legendOpacity');
276

    
277
    $cdm_map_distribution['legend']['font_size'] = variable_get('cdm_dataportal_geoservice_legend_font_size', 10);
278
    _remove_variable('cdm_dataportal_geoservice_legend_font_size');
279

    
280
    $cdm_map_distribution['legend']['font_style'] = variable_get('cdm_dataportal_geoservice_legend_font_style', FALSE);
281
    _remove_variable('cdm_dataportal_geoservice_legend_font_style');
282

    
283
    $cdm_map_distribution['legend']['icon_width'] = variable_get('cdm_dataportal_geoservice_legend_icon_width', 35);
284
    _remove_variable('cdm_dataportal_geoservice_legend_icon_width');
285

    
286
    $cdm_map_distribution['legend']['icon_height'] = variable_get('cdm_dataportal_geoservice_legend_icon_height', 15);
287
    _remove_variable('cdm_dataportal_geoservice_legend_icon_height');
288

    
289
    variable_set('cdm_map_distribution', $cdm_map_distribution);
290
    $tasks_performed[] =  'variables for map settings migrated to new variable "cdm_map_distribution"';
291

    
292
    return  join(', ',$tasks_performed);
293
}
294

    
295

    
296
/**
297
 * update for RELEASE 3.2.3:
298
 *  - remove variable 'cdm_search_use_default_values'
299
 */
300
function cdm_dataportal_update_7305() {
301
  $tasks_performed = array();
302
  $tasks_performed[] = _remove_variable('cdm_search_use_default_values');
303

    
304
  return join(', ',$tasks_performed);
305
}
306

    
307
  /**
308
   * update for RELEASE 3.7.0:
309
   *  - add 'custom_placeholder_enabled' to  'cdm_taxon_profile_image'
310
   */
311
  function cdm_dataportal_update_7306() {
312
    $tasks_performed = array();
313

    
314
    $cdm_taxon_profile_image = variable_get('cdm_taxon_profile_image');
315
    if(!isset($cdm_taxon_profile_image['custom_placeholder_enabled'])){
316
      $cdm_taxon_profile_image['custom_placeholder_enabled'] = 1;
317
    }
318

    
319
    $tasks_performed[] = _create_variable('cdm_taxon_profile_image', $cdm_taxon_profile_image);
320

    
321
    return join(', ',$tasks_performed);
322
  }
323

    
324
  /**
325
   * update for RELEASE 3.8.0:
326
   *  - replace variable 'distribution_sort' with new 'distribution_order_mode'
327
   *  - add variable distribution_tree_omit_levels and set TDWG2 if required
328
   */
329
  function cdm_dataportal_update_7307() {
330
    $tasks_performed = array();
331

    
332
    // replace distribution_sort with new variable
333
    $distribution_sort = variable_get('distribution_sort');
334
    $distribution_order_mode = 'FLAT_ALPHA';
335
    $distribution_tree_omit_levels = array();
336
    if($distribution_sort == 'HIDE_TDWG2'){
337
      $distribution_order_mode = 'TREE';
338
      $distribution_tree_omit_levels[UUID_NAMEDAREALEVEL_TDWGLEVEL_2] = UUID_NAMEDAREALEVEL_TDWGLEVEL_2;
339

    
340
    }
341
    $tasks_performed[] = _remove_variable('distribution_sort');
342
    $tasks_performed[] = _create_variable(DISTRIBUTION_ORDER_MODE, $distribution_order_mode);
343

    
344
    // add variable distribution_tree_omit_levels and set TDWG2 if required
345
    $tasks_performed[] = _create_variable(DISTRIBUTION_TREE_OMIT_LEVELS, $distribution_tree_omit_levels);
346

    
347
    return join(', ',$tasks_performed);
348
  }
349

    
350
/**
351
 * update for RELEASE 3.12:
352
 *  - move values for map width and height into 'image_map' settings
353
 *  - set map aspect ratio based on the old width and height values
354
 */
355
function cdm_dataportal_update_7308() {
356

    
357
  $tasks_performed = array();
358
  //  move values for map width and height into 'image_map' settings
359
  //  and
360
  //  set map aspect ratio based on the old width and height values
361
  $map_distribution = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
362

    
363
  $w = 512 * 2;
364
  $h = 512;
365
  if(isset($map_distribution['width'])) {
366
    $w = $map_distribution['width'];
367
    unset($map_distribution['width']);
368
  }
369
  if(isset($map_distribution['height'])) {
370
    $h = $map_distribution['height'];
371
    unset($map_distribution['height']);
372
  }
373
  $map_distribution['image_map']['height'] = $w;
374
  $map_distribution['image_map']['width'] = $h;
375
  $map_distribution['aspect_ratio'] = $w / $h;
376

    
377
  $tasks_performed[] = _modify_variable(CDM_MAP_DISTRIBUTION, $map_distribution);
378

    
379
  return join(', ',$tasks_performed);
380
}
381

    
382
/**
383
 * update for RELEASE 4.2:
384
 *  -  rename variable taxontree_ranklimit to cdm_taxontree_ranklimit
385
 */
386
function cdm_dataportal_update_7309() {
387

    
388
  $tasks_performed = array();
389
  $tasks_performed[] = _rename_variable('taxontree_ranklimit', 'cdm_taxontree_ranklimit');
390
  return join(', ',$tasks_performed);
391
}
392

    
393
/**
394
 * update for RELEASE 4.4:
395
 *  - grant new permission 'access cdm content' to default roles
396
 *  - enable back_to_search_results block if show_back_to_search_results was enabled
397
 *  - remove the variable 'cdm_dataportal_show_back_to_search_results'
398
 */
399
function cdm_dataportal_update_7400() {
400

    
401
  $tasks_performed = array();
402

    
403
  // additional permission for the anonymous user	authenticated user roles
404
  $additional_default_permissions = array(
405
    'access cdm content'
406
  );
407
  user_role_grant_permissions(1, $additional_default_permissions); // anonymous user
408
  user_role_grant_permissions(2, $additional_default_permissions); // authenticated user
409
  $tasks_performed[] = "grant new permission 'access cdm content' to default roles";
410

    
411
  // enable the back_to_search_results block
412
  $show_button = variable_get('cdm_dataportal_show_back_to_search_results', 1);
413
  if($show_button == 1 || $show_button == "1" ) {
414
    $default_theme = variable_get('theme_default', 'zen_dataportal');
415
    _block_rehash($default_theme); // ensure the block table is up to date
416
    db_update('block')
417
      ->fields(array(
418
        'weight' => 20,
419
        'region' => 'navigation',
420
        'status' => 1,
421
      ))
422
      ->condition('delta', 'back_to_search_results')
423
      ->condition('theme', 'zen_dataportal')
424
      ->execute();
425
      db_update('block')
426
        ->fields(array(
427
          'weight' => -20,
428
          'region' => 'content',
429
          'status' => 1,
430
        ))
431
        ->condition('delta', 'back_to_search_results')
432
        ->condition('theme', 'zen_dataportal', '<>')
433
        ->execute();
434
    $tasks_performed[] = "back_to_search_results block enabled";
435
  }
436
  $tasks_performed[] = _remove_variable('cdm_dataportal_show_back_to_search_results');
437

    
438
  return join(', ', $tasks_performed);
439
}
440

    
441

    
442
/**
443
 * update for RELEASE 5.1:
444
 *  - enable new default TaxonRelationshipTypes: UUID_PROPARTE_MISAPPLIEDNAME_FOR,
445
 *    UUID_PARTIAL_MISAPPLIEDNAME_FOR, UUID_PROPARTE_SYNONYM_FOR, UUID_PARTIAL_SYNONYM_FOR.
446
 */
447
function cdm_dataportal_update_7500()
448
{
449

    
450
  $tasks_performed = array();
451

    
452
  $new_defaults = array(
453
    UUID_PROPARTE_MISAPPLIEDNAME_FOR,
454
    UUID_PARTIAL_MISAPPLIEDNAME_FOR,
455
    UUID_PROPARTE_SYNONYM_FOR,
456
    UUID_PARTIAL_SYNONYM_FOR
457
  );
458
  $taxonRelTypes = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
459
  foreach($new_defaults as $default_uuid){
460
    $taxonRelTypes[$default_uuid] = $default_uuid;
461
  }
462
  variable_set(CDM_TAXON_RELATIONSHIP_TYPES, $taxonRelTypes);
463
  $tasks_performed[] = "enabled new default TaxonRelationshipTypes: PROPARTE_MISAPPLIEDNAME_FOR,  PARTIAL_MISAPPLIEDNAME_FOR, ROPARTE_SYNONYM_FOR, PARTIAL_SYNONYM_FOR;";
464

    
465
  return join(', ', $tasks_performed);
466
};
467
/* ======================================================================== */
468

    
469
/**
470
 * Renames a persistent variable.
471
 *
472
 * @return
473
 *   A message string of the performed operation.
474
 */
475
function _rename_variable($old_name, $new_name) {
476
  $success = FALSE;
477
  $value = variable_get($old_name, FALSE);
478
  variable_del($old_name);
479
  if ($value !== FALSE) {
480
    variable_set($new_name, $value);
481
    $success = variable_get($new_name, FALSE) === $value;
482
  }
483
  else {
484
    $success = TRUE;
485
  }
486

    
487
  if($success){
488
    return "Variable '$old_name' to '$new_name' renamed. ";
489
  } else {
490
    return "ERROR: Renaming variable '$old_name' to '$new_name'. ";
491
  }
492

    
493
}
494

    
495
/**
496
 * Unsets a persistent variable.
497
 *
498
 * Calls variable_del() and returns a message string.
499
 *
500
 * @return
501
 *   A message string of the performed operation.
502
 */
503
function _remove_variable($name) {
504
  variable_del($name);
505
  return "Variable '$name' removed. ";
506
}
507

    
508
/**
509
 * Sets a persistent variable.
510
 *
511
 * Calls variable_set() and returns a message string.
512
 *
513
 * @return
514
 *   A message string of the performed operation.
515
 */
516
function _create_variable($name, $value) {
517
  variable_set($name, $value);
518
  return "Variable '$name' created with value: '$value'. ";
519
}
520

    
521
/**
522
 * Overwrites a persistent variable.
523
 *
524
 * Calls _create_variable() and returns a message string.
525
 *
526
 * @return
527
 *   A message string of the performed operation.
528
 */
529
function _modify_variable($name, $value_override) {
530
  /*
531
   * FIXME take care for correct handling of tree variables
532
   * for example description_gallery contains the array:
533
   * Array
534
   (
535
   [cdm_dataportal_show_thumbnail_captions] => 1
536
   [cdm_dataportal_media_maxextend] => 120
537
   [cdm_dataportal_media_cols] => 4
538
   )
539
   * -----> solutions merge arrays !!!
540
   */
541
  _create_variable($name, $value_override);
542
  return "Variable '$name' updated with value: '$value_override'. ";
543
}
544

    
545

    
(9-9/18)