Project

General

Profile

Download (16.1 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
  if(module_exists('extlinks')){
91
    $extlinks_permissions = array(
92
        'access extlinks content',
93
        'administer extlinks',
94
    );
95
    user_role_grant_permissions($role->rid, $extlinks_permissions);
96
    $tasks_performed[] = 'permissions for extlinks';
97
  }
98

    
99
  foreach (filter_formats() as $key=>$format){
100
    if($key == 'full_html' || $format->format == "Full HTML"){
101
      $formats_permissions = array(
102
          'use text format full_html'
103
      );
104
      user_role_grant_permissions($role->rid, $formats_permissions);
105
      $tasks_performed[] = 'use text format full_html';
106
    }
107
  }
108

    
109
  // assure  'create url aliases' can be set
110
  if(!module_exists('path')){
111
    module_enable(array('path'), TRUE);
112
    $tasks_performed[] = 'module path enabled';
113
  }
114
  $other_permissions = array(
115
      'create url aliases',
116
      'delete revisions',
117
      'revert revisions',
118
      'view own unpublished content',
119
      'flush caches'
120
  );
121
  user_role_grant_permissions($role->rid, $other_permissions);
122
  $tasks_performed[] = 'and other permissions';
123

    
124
  return "adding missing permissions for role CDM Admin: " . join(', ', $tasks_performed);
125
}
126

    
127

    
128
/**
129
 * update for RELEASE 3.2.2:
130
 *  - migrating variable cdm_dataportal_show_default_image to cdm_taxon_profile_image['show']
131
 *  - enabling required module file
132
 *  - enable new debug block for all dataportals
133
 *  - migrating variables for map settings
134
 */
135
function cdm_dataportal_update_7304() {
136

    
137
    module_enable(array('file'), TRUE);
138
    $tasks_performed[] = 'module file enabled';
139

    
140
    if(!variable_get(CDM_TAXON_PROFILE_IMAGE, FALSE)){
141
      $cdm_taxon_profile_image_settings = unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT);
142
      $cdm_taxon_profile_image_settings['show'] = variable_get('cdm_dataportal_show_default_image', 0);
143
      variable_set(CDM_TAXON_PROFILE_IMAGE, $cdm_taxon_profile_image_settings);
144
      variable_del('cdm_dataportal_show_default_image');
145
      $tasks_performed[] = 'migrating variable cdm_dataportal_show_default_image to cdm_taxon_profile_image[\'show\']';
146
    }
147

    
148

    
149
    // CDM web service debug block
150
    $cdm_ws_debug_block = array(
151
        'block' => array(
152
            'module' => 'cdm_api',
153
            'delta' => 'cdm_ws_debug',
154
            'theme' => NULL,
155
            'status' => 1,
156
            'weight' => -50,
157
            'region' => 'sidebar_first',
158
            'pages' => '',
159
            'cache' => -1,
160
        ),
161
        'block_role' =>
162
            array(
163

    
164
        )
165
    );
166

    
167
    $admin_user = user_load(1);
168

    
169
    // harmonize cdm admin role names if needed
170
    $cdm_admin_role = user_role_load_by_name('CDM Admin');
171
    if(is_object($cdm_admin_role) && $cdm_admin_role->name !== 'CDM admin'){
172
      $cdm_admin_role->name = 'CDM admin';
173
      user_role_save($cdm_admin_role);
174
      $tasks_performed[] = $cdm_admin_role->name . ' role name harmonized';
175
    }
176
    $cdm_admin_role = user_role_load_by_name('CDM admin');
177

    
178
    if(is_object($cdm_admin_role)) {
179
      // make sure admin is member of role CDM admin
180
      if (!isset($admin_user->roles[$admin_role->rid])) {
181
        user_multiple_role_edit(array(1), 'add_role', $cdm_admin_role->rid);
182
        $tasks_performed[] = 'admin user (uid=1) added to role ' . $cdm_admin_role->name;
183
      }
184
      $cdm_ws_debug_block['block_role'][] =  array(
185
        'module' => 'cdm_api',
186
        'delta' => 'cdm_ws_debug',
187
        'rid' => $cdm_admin_role->rid
188
      );
189
    }
190

    
191
    $admin_role = user_role_load_by_name("admin"); // Drupal 5
192

    
193
    if(is_object($admin_role)){
194
      $admin_role->name == 'administrator';
195
      user_role_save($admin_role);
196
      $tasks_performed[] = 'administrator role name harmonized';
197
    } else {
198
      $admin_role = user_role_load_by_name("administrator"); // Drupal 7
199
    }
200
    if(is_object($admin_role)){
201
      // make sure admin is member of role admin
202
      if (!isset($admin_user->roles[$admin_role->rid])) {
203
        user_multiple_role_edit(array(1), 'add_role', $admin_role->rid);
204
        $tasks_performed[] = 'admin user (uid=1) added to role ' . $admin_role->name;
205
      }
206
      $cdm_ws_debug_block['block_role'][] =  array(
207
          'module' => 'cdm_api',
208
          'delta' => 'cdm_ws_debug',
209
          'rid' => $admin_role->rid
210
      );
211
    }
212

    
213
    // clean up existing block configuration which might be set by drupal automatically, we need to override this
214
    db_delete('block')->condition('module', 'cdm_api')->execute();
215
    db_delete('block_role')->condition('module', 'cdm_api')->execute();
216
    // insert
217
    $block_insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
218
    $block_role_insert = db_insert('block_role')->fields(array('module', 'delta', 'rid'));
219

    
220
    $themes = system_list('theme');
221
    foreach ($themes as $theme) {
222
      if($theme->status == "1"){ // only for enabled themes
223
        $cdm_ws_debug_block['block']['theme'] = $theme->name;
224
        $block_insert->values($cdm_ws_debug_block['block']);
225
      }
226
    }
227
    foreach($cdm_ws_debug_block['block_role'] as $block_role){
228
      $block_role_insert->values($block_role);
229
    }
230
    $block_insert->execute();
231
    $block_role_insert->execute();
232
    $tasks_performed[] = 'CDM web service debug block enabled for \'CDM admin\' users in all themes';
233
    $tasks_performed[] = _remove_variable('cdm_webservice_debug');
234
    $tasks_performed[] = _remove_variable('cdm_webservice_isStub');
235

    
236
    /*
237
     *  migrating variables for map settings
238
     */
239
    $cdm_map_distribution = array();
240

    
241
    $cdm_map_distribution['width'] = variable_get('cdm_dataportal_geoservice_display_width', 390);
242
    $cdm_map_distribution['height'] = $cdm_map_distribution['width'] / 2;
243
    _remove_variable('cdm_dataportal_geoservice_display_width');
244

    
245
    $cdm_map_distribution['bbox'] = variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90');
246
    _remove_variable('cdm_dataportal_geoservice_bounding_box');
247

    
248
    $cdm_map_distribution['show_labels'] = variable_get('cdm_dataportal_geoservice_labels_on', FALSE);
249
    _remove_variable('cdm_dataportal_geoservice_labels_on');
250

    
251
    $cdm_map_distribution['caption'] = variable_get('cdm_dataportal_geoservice_map_caption', '');
252
    _remove_variable('cdm_dataportal_geoservice_map_caption');
253

    
254
    $cdm_map_distribution['distribution_opacity'] = variable_get('cdm_dataportal_geoservice_distributionOpacity', '0.5');
255
    _remove_variable('cdm_dataportal_geoservice_distributionOpacity');
256

    
257
    $cdm_map_distribution['mapType'] = variable_get('cdm_dataportal_map_openlayers', 1);
258
    _remove_variable('cdm_dataportal_map_openlayers');
259

    
260
    // imageMap
261
    $cdm_map_distribution['image_map'] = array();
262
    $cdm_map_distribution['image_map']['base_layer']= variable_get('map_base_layer', 'cyprusdivs');
263
    _remove_variable('map_base_layer');
264

    
265
    $cdm_map_distribution['image_map']['bg_color'] = variable_get('map_bg_color', '1874CD');
266
    _remove_variable('map_bg_color');
267

    
268
    $cdm_map_distribution['image_map']['layer_style'] = variable_get('map_base_layer_style', 'ffffff,606060,,');
269
    _remove_variable('map_base_layer_style');
270

    
271
    // OpenLayers
272
    $cdm_map_distribution['openlayers'] = array();
273
    $cdm_map_distribution['openlayers']['base_layers'] = variable_get('baselayers', array('mapproxy_vmap0' => 'mapproxy_vmap0', 'PREFERRED' => 'mapproxy_vmap0'));
274
    _remove_variable('baselayers');
275

    
276
    $cdm_map_distribution['openlayers']['show_layer_switcher'] = variable_get('cdm_dataportal_geoservice_showLayerSwitcher', TRUE);
277
    _remove_variable('cdm_dataportal_geoservice_showLayerSwitcher');
278

    
279
    $cdm_map_distribution['legend'] = array();
280
    $cdm_map_distribution['legend']['show'] = variable_get('cdm_dataportal_geoservice_legend_on', TRUE);
281
    _remove_variable('cdm_dataportal_geoservice_legend_on');
282

    
283
    $cdm_map_distribution['legend']['opacity'] = variable_get('cdm_dataportal_geoservice_legendOpacity', '0.5');
284
    _remove_variable('cdm_dataportal_geoservice_legendOpacity');
285

    
286
    $cdm_map_distribution['legend']['font_size'] = variable_get('cdm_dataportal_geoservice_legend_font_size', 10);
287
    _remove_variable('cdm_dataportal_geoservice_legend_font_size');
288

    
289
    $cdm_map_distribution['legend']['font_style'] = variable_get('cdm_dataportal_geoservice_legend_font_style', FALSE);
290
    _remove_variable('cdm_dataportal_geoservice_legend_font_style');
291

    
292
    $cdm_map_distribution['legend']['icon_width'] = variable_get('cdm_dataportal_geoservice_legend_icon_width', 35);
293
    _remove_variable('cdm_dataportal_geoservice_legend_icon_width');
294

    
295
    $cdm_map_distribution['legend']['icon_height'] = variable_get('cdm_dataportal_geoservice_legend_icon_height', 15);
296
    _remove_variable('cdm_dataportal_geoservice_legend_icon_height');
297

    
298
    variable_set('cdm_map_distribution', $cdm_map_distribution);
299
    $tasks_performed[] =  'variables for map settings migrated to new variable "cdm_map_distribution"';
300

    
301
    return  join(', ',$tasks_performed);
302
}
303

    
304

    
305
/**
306
 * update for RELEASE 3.2.3:
307
 *  - remove variable 'cdm_search_use_default_values'
308
 */
309
function cdm_dataportal_update_7305() {
310
  $tasks_performed = array();
311
  $tasks_performed[] = _remove_variable('cdm_search_use_default_values');
312

    
313
  return join(', ',$tasks_performed);
314
}
315

    
316
  /**
317
   * update for RELEASE 3.7.0:
318
   *  - add 'custom_placeholder_enabled' to  'cdm_taxon_profile_image'
319
   */
320
  function cdm_dataportal_update_7306() {
321
    $tasks_performed = array();
322

    
323
    $cdm_taxon_profile_image = variable_get('cdm_taxon_profile_image');
324
    if(!isset($cdm_taxon_profile_image['custom_placeholder_enabled'])){
325
      $cdm_taxon_profile_image['custom_placeholder_enabled'] = 1;
326
    }
327

    
328
    $tasks_performed[] = _create_variable('cdm_taxon_profile_image', $cdm_taxon_profile_image);
329

    
330
    return join(', ',$tasks_performed);
331
  }
332

    
333
  /**
334
   * update for RELEASE 3.8.0:
335
   *  - replace variable 'distribution_sort' with new 'distribution_order_mode'
336
   *  - add variable distribution_tree_omit_levels and set TDWG2 if required
337
   */
338
  function cdm_dataportal_update_7307() {
339
    $tasks_performed = array();
340

    
341
    // replace distribution_sort with new variable
342
    $distribution_sort = variable_get('distribution_sort');
343
    $distribution_order_mode = 'FLAT_ALPHA';
344
    $distribution_tree_omit_levels = array();
345
    if($distribution_sort == 'HIDE_TDWG2'){
346
      $distribution_order_mode = 'TREE';
347
      $distribution_tree_omit_levels[UUID_NAMEDAREALEVEL_TDWGLEVEL_2] = UUID_NAMEDAREALEVEL_TDWGLEVEL_2;
348

    
349
    }
350
    $tasks_performed[] = _remove_variable('distribution_sort');
351
    $tasks_performed[] = _create_variable(DISTRIBUTION_ORDER_MODE, $distribution_order_mode);
352

    
353
    // add variable distribution_tree_omit_levels and set TDWG2 if required
354
    $tasks_performed[] = _create_variable(DISTRIBUTION_TREE_OMIT_LEVELS, $distribution_tree_omit_levels);
355

    
356
    return join(', ',$tasks_performed);
357
  }
358

    
359
/**
360
 * update for RELEASE 3.12:
361
 *  - move values for map width and height into 'image_map' settings
362
 *  - set map aspect ratio based on the old width and height values
363
 */
364
function cdm_dataportal_update_7308() {
365

    
366
  $tasks_performed = array();
367
  //  move values for map width and height into 'image_map' settings
368
  //  and
369
  //  set map aspect ratio based on the old width and height values
370
  $map_distribution = get_array_variable_merged(CDM_MAP_DISTRIBUTION, CDM_MAP_DISTRIBUTION_DEFAULT);
371

    
372
  $w = 512 * 2;
373
  $h = 512;
374
  if(isset($map_distribution['width'])) {
375
    $w = $map_distribution['width'];
376
    unset($map_distribution['width']);
377
  }
378
  if(isset($map_distribution['height'])) {
379
    $h = $map_distribution['height'];
380
    unset($map_distribution['height']);
381
  }
382
  $map_distribution['image_map']['height'] = $w;
383
  $map_distribution['image_map']['width'] = $h;
384
  $map_distribution['aspect_ratio'] = $w / $h;
385

    
386
  $tasks_performed[] = _modify_variable(CDM_MAP_DISTRIBUTION, $map_distribution);
387

    
388
  return join(', ',$tasks_performed);
389
}
390

    
391
/**
392
 * update for RELEASE 4.2:
393
 *  -  rename variable taxontree_ranklimit to cdm_taxontree_ranklimit
394
 */
395
function cdm_dataportal_update_7309() {
396

    
397
  $tasks_performed = array();
398
  $tasks_performed[] = _rename_variable('taxontree_ranklimit', 'cdm_taxontree_ranklimit');
399
  return join(', ',$tasks_performed);
400
}
401

    
402

    
403
/* ======================================================================== */
404

    
405
/**
406
 * Renames a persistent variable.
407
 *
408
 * @return
409
 *   A message string of the performed operation.
410
 */
411
function _rename_variable($old_name, $new_name) {
412
  $success = FALSE;
413
  $value = variable_get($old_name, FALSE);
414
  variable_del($old_name);
415
  if ($value !== FALSE) {
416
    variable_set($new_name, $value);
417
    $success = variable_get($new_name, FALSE) === $value;
418
  }
419
  else {
420
    $success = TRUE;
421
  }
422

    
423
  if($success){
424
    return "Variable '$old_name' to '$new_name' renamed. ";
425
  } else {
426
    return "ERROR: Renaming variable '$old_name' to '$new_name'. ";
427
  }
428

    
429
}
430

    
431
/**
432
 * Unsets a persistent variable.
433
 *
434
 * Calls variable_del() and returns a message string.
435
 *
436
 * @return
437
 *   A message string of the performed operation.
438
 */
439
function _remove_variable($name) {
440
  variable_del($name);
441
  return "Variable '$name' removed. ";
442
}
443

    
444
/**
445
 * Sets a persistent variable.
446
 *
447
 * Calls variable_set() and returns a message string.
448
 *
449
 * @return
450
 *   A message string of the performed operation.
451
 */
452
function _create_variable($name, $value) {
453
  variable_set($name, $value);
454
  return "Variable '$name' created with value: '$value'. ";
455
}
456

    
457
/**
458
 * Overwrites a persistent variable.
459
 *
460
 * Calls _create_variable() and returns a message string.
461
 *
462
 * @return
463
 *   A message string of the performed operation.
464
 */
465
function _modify_variable($name, $value_override) {
466
  /*
467
   * FIXME take care for correct handling of tree variables
468
   * for example description_gallery contains the array:
469
   * Array
470
   (
471
   [cdm_dataportal_show_thumbnail_captions] => 1
472
   [cdm_dataportal_media_maxextend] => 120
473
   [cdm_dataportal_media_cols] => 4
474
   )
475
   * -----> solutions merge arrays !!!
476
   */
477
  _create_variable($name, $value_override);
478
  return "Variable '$name' updated with value: '$value_override'. ";
479
}
480

    
481

    
(9-9/16)