Project

General

Profile

Download (87 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * CDM Dataportal settings.
5
 */
6

    
7
// TODO Genus UUID.
8
define('DEFAULT_TAXONTREE_RANKLIMIT', '1b11c34c-48a8-4efa-98d5-84f7f66ef43a');
9
define('CDM_TAXONOMICTREE_UUID', 'cdm_taxonomictree_uuid');
10

    
11
define('CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE', 25);
12
define('CDM_DATAPORTAL_NOMREF_IN_TITLE', 1);
13
define('CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR', 0);
14
define('CDM_DATAPORTAL_ALL_FOOTNOTES', 0);
15
define('CDM_DATAPORTAL_ANNOTATIONS_FOOTNOTES', 0);
16
define('CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX', 999);
17

    
18
/* annotationTypeKeys */
19
$annotationTypeKeys = array_keys(cdm_Vocabulary_as_option(UUID_ANNOTATION_TYPE));
20
if (in_array(UUID_ANNOTATION_TYPE_TECHNICAL, $annotationTypeKeys)) {
21
  $annotationTypeKeys = array_flip($annotationTypeKeys);
22

    
23
  // Technical annotation are off by default.
24
  unset($annotationTypeKeys[UUID_ANNOTATION_TYPE_TECHNICAL]);
25
  $annotationTypeKeys = array_flip($annotationTypeKeys);
26
  // Additional value for the NULL case.
27
  $annotationTypeKeys[] = 'NULL_VALUE';
28
}
29
define('ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT', serialize($annotationTypeKeys));
30

    
31
/* taxonRelationshipTypes */
32
define('CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT', serialize(array(UUID_MISAPPLIED_NAME_FOR, UUID_INVALID_DESIGNATION_FOR)));
33

    
34
/* Gallery variables. */
35
$gallery_settings = array(
36
  "cdm_dataportal_show_taxon_thumbnails" => 1,
37
  "cdm_dataportal_show_synonym_thumbnails" => 0,
38
  "cdm_dataportal_show_thumbnail_captions" => 1,
39
  "cdm_dataportal_media_maxextend" => 120,
40
  "cdm_dataportal_media_cols" => 3,
41
  "cdm_dataportal_media_maxRows" => 1,
42
);
43

    
44
/* ---- MAP SETTING CONSTANTS ---- */
45
/**
46
 * @var array of URIs eg. http://edit.africamuseum.be"
47
 *   An options array
48
 */
49
define('EDIT_MAPSERVER_URI', serialize(
50
    array(
51
      'http://edit.africamuseum.be'=>'Primary (http://edit.africamuseum.be)',
52
      'http://edit.br.fgov.be'=>'Secondary (http://edit.br.fgov.be)',
53
    )
54
  )
55
);
56
define('EDIT_MAPSERVER_PATH', '/edit_wp5');
57
/**
58
 * @var array of versions eg. "v1.2"
59
 *   An options array
60
 */
61
define('EDIT_MAPSERVER_VERSION', serialize(
62
    array(
63
      'v1' => 'v1' ,
64
      'v1.1' => 'v1.1',
65
      'v1.2_dev' => 'v1.2_dev'
66
    )
67
  )
68
);
69
define('EDIT_MAPSERVER_URI_DEFAULT', 'http://edit.africamuseum.be');
70
define('EDIT_MAPSERVER_VERSION_DEFAULT', 'v1.1');
71

    
72
// --- Taxon profile settings --- /
73
define('LAYOUT_SETTING_PREFIX', 'layout_');
74
define('FEATURE_TREE_LAYOUT_DEFAULTS', serialize(
75
  array(
76
    'enabled' => FALSE,
77
    'enclosingTag' => 'ul',
78
    'entryEnclosingTag' => 'li',
79
    'glue' => ' ',
80
  )));
81

    
82
define('DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP', 'distribution_textdata_on_top');
83
define('CDM_TAXON_PROFILE_IMAGE', 'cdm_taxon_profile_image');
84
define('CDM_TAXON_PROFILE_IMAGE_DEFAULT', serialize(
85
    array(
86
        'show' => 1,
87
        'maxextend' => 184,
88
        'custom_placeholder_image_on' => 0,
89
        'custom_placeholder_image_fid' => ''
90
    )
91
  )
92
);
93

    
94
/**
95
 * @todo document this function
96
 */
97
function get_taxon_tabs_list() {
98
  return array(
99
    0 => 'General',
100
    1 => 'Synonymy',
101
    2 => 'Images',
102
    3 => 'Specimens',
103
    4 => 'Keys',
104
  );
105
}
106

    
107
$taxon_tab_options = get_taxon_tabs_list();
108
$taxon_tab_options[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX] = 'Last visited tab';
109

    
110
define('CDM_DATAPORTAL_DEFAULT_TAXON_TAB', serialize($taxon_tab_options));
111

    
112
/**
113
 * @todo document this function.
114
 */
115
function get_taxon_options_list() {
116
  $taxon_tab_options = array_flip(get_taxon_tabs_list());
117
  foreach ($taxon_tab_options as $key => $value) {
118
    $taxon_tab_options[$key] = t($key);
119
  }
120
  return $taxon_tab_options;
121

    
122
}
123

    
124
define('CDM_PART_DEFINITIONS', 'cdm-part-definitions');
125
define('CDM_PART_DEFINITIONS_DEFAULT', serialize(
126
  array(
127
      'ZoologicalName' => array(
128
        'namePart' => array('name' => TRUE),
129
        'nameAuthorPart' => array('name' => TRUE),
130
        'referencePart' => array('authors' => TRUE),
131
        'microreferencePart' => array('microreference' => TRUE),
132
        'statusPart' => array('status' => TRUE),
133
        'descriptionPart' => array('description' => TRUE),
134
      ),
135
      'BotanicalName'=> array(
136
        'namePart' => array('name' => TRUE),
137
        'nameAuthorPart' => array('name' => TRUE, 'authors' => TRUE),
138
        'referencePart' => array('reference' => TRUE, 'microreference' => TRUE),
139
        'statusPart' => array('status' => TRUE),
140
        'descriptionPart' => array('description' => TRUE),
141
      ),
142
     '#DEFAULT' => array(
143
        'namePart' => array(
144
            'name' => TRUE
145
        ),
146
        'nameAuthorPart' => array(
147
            'name' => TRUE,
148
            'authors' => TRUE
149
        ),
150
        'referencePart' => array(
151
            'reference' => TRUE
152
        ),
153
        'microreferencePart' => array(
154
            'microreference' => TRUE,
155
        ),
156
        'statusPart' => array(
157
            'status' => TRUE,
158
        ),
159
        'descriptionPart' => array(
160
            'description' => TRUE,
161
        ),
162
      )
163
    )
164
  )
165
);
166
define('CDM_NAME_RENDER_TEMPLATES', 'cdm-name-render-templates');
167
define('CDM_NAME_RENDER_TEMPLATES_DEFAULT', serialize(
168
   array (
169
     'taxon_page_title,polytomousKey'=> array(
170
          'namePart' => array('#uri' => TRUE),
171
        ),
172
      'taxon_page_synonymy,related_taxon'=> array(
173
          'nameAuthorPart' => array('#uri' => TRUE),
174
          'referencePart' => TRUE,
175
          'statusPart' => TRUE,
176
          'descriptionPart' => TRUE,
177
        ),
178
      'acceptedFor,typedesignations,list_of_taxa' => array(
179
          'nameAuthorPart' => array('#uri' => TRUE),
180
          'referencePart' => TRUE,
181
        ),
182
      '#DEFAULT' => array(
183
          'nameAuthorPart' => array('#uri' => TRUE),
184
          'referencePart' => TRUE,
185
       )
186
    )
187
));
188

    
189
define('TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT', serialize(get_taxon_options_list()));
190
define('CDM_DATAPORTAL_GALLERY_SETTINGS', serialize($gallery_settings));
191
define('CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME', 'specimen_gallery');
192
define('CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME', "description_gallery");
193
define('CDM_DATAPORTAL_MEDIA_GALLERY_NAME', "media_gallery");
194
define('CDM_DATAPORTAL_TAXON_MEDIA_GALLERY_NAME_TAB', "taxon_tab_media_gallery");
195
define('CDM_DATAPORTAL_SEARCH_GALLERY_NAME', "search_gallery");
196
define('CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS', 'cdm_dataportal_display_taxon_relationships');
197
define('CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS', 'cdm_dataportal_display_name_relations');
198
// define('CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS_2', array("default" => t('Display all')));
199
define('CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT', 1);
200
define('CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS_DEFAULT', 1);
201
/**
202
 * The drupal variable key for the array containing the uuids of the taxon relationship types to display in
203
 * the snonymy.
204
 *
205
 * @var string
206
 */
207
define('CDM_TAXON_RELATIONSHIP_TYPES', 'cdm_taxon_relationship_types');
208
/**
209
 * The drupal variable for the configuration of the information aggregation along
210
 * the taxon relation ships. The mapped arrayis associative and holds two elements:
211
 *    - direct: the uuids of the taxon relationship types to take into account in invers
212
 *      direction.
213
 *    - invers: the uuids of the taxon relationship types to take into account in direct
214
 *      direction.
215
 *
216
 * @var String
217
 */
218
define('CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS', 'cdm_aggregate_by_taxon_relationships');
219
define('CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT', serialize(
220
    array(
221
        'direct'=>array(),
222
        'invers'=>array()
223
     )
224
   ));
225
define('CDM_PROFILE_FEATURETREE_UUID', 'cdm_dataportal_featuretree_uuid');
226
define('CDM_OCCURRENCE_FEATURETREE_UUID', 'cdm_occurrence_featuretree_uuid');
227
define('CDM_DATAPORTAL_STRUCTURED_DESCRIPTION_FEATURETREE_UUID', 'cdm_dataportal_structdesc_featuretree_uuid');
228

    
229
define('CDM_TAXON_MEDIA_FILTER', 'cdm_taxon_media_filter');
230
define('CDM_TAXON_MEDIA_FILTER_DEFAULT', serialize(
231
    array(
232
        'includeTaxonDescriptions' => 'includeTaxonDescriptions',
233
        'includeOccurrences' => 0,
234
        'includeTaxonNameDescriptions' => 0
235
     )
236
  ));
237

    
238
/**
239
 * @todo document this function.
240
 */
241
function getGallerySettings($gallery_config_form_name) {
242
  $default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
243
  return variable_get($gallery_config_form_name, $default_values);
244
}
245

    
246
/**
247
 * Returns the string representation of the default tab.
248
 *
249
 * @param bool $returnTabIndex
250
 *   Defaults to FALSE, if set true this function will return the index number
251
 *   of the default tab. (used to supply default values to form elements).
252
 */
253
function get_default_taxon_tab($returnTabIndex = FALSE) {
254

    
255
  global $user;
256
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
257
  $user_tab_active = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
258
  $user_tab = 'cdm_dataportal_' . $user->uid . '_default_tab';
259
  // Get the user value if the used has chosen to overwrite the system settings.
260
  $user_tab_on = variable_get($user_tab_active, FALSE);
261
  if ($user_tab_on) {
262
    $user_value = variable_get($user_tab, 0);
263
    $index_value = $user_value;
264
    // Get the system value.
265
  }
266
  else {
267
    $system_value = variable_get('cdm_dataportal_default_tab', 0);
268
    $index_value = $system_value;
269
  }
270

    
271
  // Return the index value or the string representation.
272
  if ($returnTabIndex) {
273
    return $index_value;
274
  }
275
  else {
276
    return ($values[$index_value]);
277
  }
278

    
279
}
280

    
281
/**
282
 * @todo Please document this function.
283
 * @see http://drupal.org/node/1354
284
 */
285
function cdm_dataportal_menu_admin(&$items) {
286
  // Display section on admin/config page.
287
  $items['admin/config/cdm_dataportal'] = array(
288
    'title' => 'CDM Dataportal',
289
    'description' => 'Settings for the CDM DataPortal.',
290
    'position' => 'right',
291
    'weight' => 10,
292
    'page callback' => 'system_admin_menu_block_page',
293
    'access arguments' => array('administer cdm_dataportal'),
294
    'file' => 'system.admin.inc',
295
    'file path' => drupal_get_path('module', 'system'),
296
  );
297
  $items['admin/config/cdm_dataportal/settings'] = array(
298
    'title' => 'Settings',
299
    'description' => 'Settings for the CDM DataPortal.',
300
    'weight' => 0,
301
    'page callback' => 'drupal_get_form',
302
    'page arguments' => array('cdm_settings_general'),
303
    'access arguments' => array('administer cdm_dataportal'),
304
    'type' => MENU_NORMAL_ITEM,
305
  );
306
  $items['admin/config/cdm_dataportal/settings/general'] = array(
307
    'title' => 'General',
308
    'description' => 'General',
309
    'weight' => 0,
310
    'page callback' => 'drupal_get_form',
311
    'page arguments' => array('cdm_settings_general'),
312
    'access arguments' => array('administer cdm_dataportal'),
313
    'type' => MENU_DEFAULT_LOCAL_TASK,
314
  );
315

    
316
  $items['admin/config/cdm_dataportal/settings/cachesite'] = array(
317
    'title' => 'Cache',
318
    'description' => 'Cache',
319
    'access arguments' => array('administer cdm_dataportal'),
320
    'page callback' => 'drupal_get_form',
321
    'page arguments' => array('cdm_settings_cache'),
322
    'weight' => 10,
323
    'type' => MENU_LOCAL_TASK,
324
  );
325

    
326
  $items['admin/config/cdm_dataportal/settings/geo'] = array(
327
    'title' => 'Geo & Map',
328
    'description' => 'Geo & Map',
329
    'access arguments' => array('administer cdm_dataportal'),
330
    'page callback' => 'drupal_get_form',
331
    'page arguments' => array('cdm_settings_geo'),
332
    'weight' => 1,
333
    'type' => MENU_LOCAL_TASK,
334
  );
335

    
336
  $items['admin/config/cdm_dataportal/settings/layout'] = array(
337
    'title' => 'Layout',
338
    'description' => 'Configure and adjust the layout of your DataPortal ',
339
    'access arguments' => array('administer cdm_dataportal'),
340
    'page callback' => 'drupal_get_form',
341
    'page arguments' => array('cdm_settings_layout'),
342
    'weight' => 2,
343
    'type' => MENU_LOCAL_TASK,
344
  );
345

    
346
  $items['admin/config/cdm_dataportal/settings/layout/taxon'] = array(
347
    'title' => 'Taxon',
348
    'description' => 'Configure and adjust the layout of your DataPortal ',
349
    'access arguments' => array('administer cdm_dataportal'),
350
    'page callback' => 'drupal_get_form',
351
    'page arguments' => array('cdm_settings_layout_taxon'),
352
    'weight' => 1,
353
    'type' => MENU_LOCAL_TASK,
354
  );
355
  /*
356
  $items[] = array(
357
  'path' => 'admin/config/cdm_dataportal/layout/synonymy',
358
  'title' => t('Synonymy'),
359
  'description' => t('Configure and adjust the layout of your DataPortal '),
360
  'access' => user_access('administer cdm_dataportal'),
361
  'callback' => 'drupal_get_form',
362
  'callback arguments' => array('cdm_settings_layout_synonymy'),
363
  'weight' => 1,
364
  'type' => MENU_LOCAL_TASK,
365
  );
366

    
367
  $items[] = array(
368
  'path' => 'admin/config/cdm_dataportal/layout/specimens',
369
  'title' => t('Specimens'),
370
  'description' => t('Configure and adjust the layout of your DataPortal '),
371
  'access' => user_access('administer cdm_dataportal'),
372
  'callback' => 'drupal_get_form',
373
  'callback arguments' => array('cdm_settings_layout_specimens'),
374
  'weight' => 1,
375
  'type' => MENU_LOCAL_TASK,
376
  );
377
  */
378
  $items['admin/config/cdm_dataportal/settings/layout/search'] = array(
379
    'title' => 'Search',
380
    'description' => 'Configure and adjust the layout of your DataPortal ',
381
    'access arguments' => array('administer cdm_dataportal'),
382
    'page callback' => 'drupal_get_form',
383
    'page arguments' => array('cdm_settings_layout_search'),
384
    'weight' => 2,
385
    'type' => MENU_LOCAL_TASK,
386
  );
387

    
388
  $items['admin/config/cdm_dataportal/settings/layout/media'] = array(
389
    'title' => 'Media',
390
    'description' => 'Configure and adjust the layout of your DataPortal ',
391
    'access arguments' => array('administer cdm_dataportal'),
392
    'page callback' => 'drupal_get_form',
393
    'page arguments' => array('cdm_settings_layout_media'),
394
    'weight' => 3,
395
    'type' => MENU_LOCAL_TASK,
396
  );
397

    
398
}
399

    
400
/**
401
 * @todo document this function.
402
 */
403
function cdm_help_general_cache() {
404
  $form = array();
405
  $form['cache_help'] = array(
406
    '#type' => 'fieldset',
407
    '#title' => t('Help'),
408
    '#collapsible' => TRUE,
409
    '#collapsed' => TRUE,
410
  );
411
  $form['cache_help']['test'] = array('#value' => t('probando'));
412
  return drupal_render($form);
413
  $res = array();
414
  $res['default'] = drupal_render($help);
415
  return $res;
416
}
417

    
418
/**
419
 * Configures the settings form for the CDM-API module.
420
 *
421
 * @return array
422
 *   Drupal settings form.
423
 */
424
function cdm_settings_general() {
425

    
426
  $form['cdm_webservice'] = array(
427
    '#type' => 'fieldset',
428
    '#title' => t('CDM Server'),
429
    '#collapsible' => FALSE,
430
    '#collapsed' => FALSE,
431
    '#description' => t('The <em>CDM Server</em> exposes data stored in a
432
        CDM data base to the web via RESTful web services and thus is the source of the data
433
        to be displayed by a CDM DataPotal.'),
434
  );
435

    
436
  $form['cdm_webservice']['cdm_webservice_url'] = array(
437
    '#type' => 'textfield',
438
    '#title' => t('CDM web service URL') . ':',
439
    '#description' => t('This is the URL to the CDM-Server exposing your data
440
      e.g. <em>"http://localhost:8080/cichorieae/"</em> The URL <strong>must end
441
      with a slash</strong> character!'),
442
    '#default_value' => variable_get('cdm_webservice_url', NULL),
443
  );
444

    
445
  $form['cdm_webservice']['cdm_webservice_debug'] = array(
446
    '#type' => 'checkbox',
447
    '#title' => t('<b>Debug CDM Web Service</b>'),
448
    '#default_value' => variable_get('cdm_webservice_debug', 1),
449
    '#description' => t('The black web service debug box will appear at the top
450
      of each page. When clicked it toggles open and provides a list of all HTTP
451
      requests which have been made while building of this page.<br />
452
      <strong>Note:</strong> this is a feature dedicated to developers. It will
453
      only be visible when logged in and if the user has suffucicient rights to
454
      see this debug box.'),
455
  );
456

    
457
  $form['cdm_webservice']['freetext_index'] = array(
458
    '#type' => 'fieldset',
459
    '#title' => t('Freetext index'),
460
    '#collapsible' => FALSE,
461
    '#collapsed' => FALSE,
462
  );
463

    
464
  // Check the cdmserver port number and display a waring if it is not port 80
465
  preg_match("#http[s]?://[0-9\p{L}\.]*:([0-9]*)/.*#u", variable_get('cdm_webservice_url', ''), $portNumberMatch, PREG_OFFSET_CAPTURE);
466
  if (isset($portNumberMatch[1]) && $portNumberMatch[1] != '80') {
467
    $form['cdm_webservice']['freetext_index']['message'] = array(
468
      '#markup' => "<div class=\"description\">"
469
      . t("The CDM web service URL contains a portnumber other than standart HTTP port 80: '!port'."
470
      . " Due to this the reindex and purge fuctions may not be working if there is a firewall in between you and the CDM Server."
471
      . " You may want to contact the maintainer of the according CDM Server in order to solve this problem.", array('!port' => $portNumberMatch[1][0]))
472
      . "</div>",
473
    );
474
  };
475

    
476
  $frontentURL = urlencode(variable_get('cdm_webservice_url', ''));
477
  $trigger_link_options = array(
478
    'attributes' => array(
479
      'class' => 'index-trigger',
480
    ),
481
  );
482
  $form['cdm_webservice']['freetext_index']['operations'] = array(
483
    '#markup' => "<div>" . t('Operations: !url1 !url2', array(
484
        '!url1' => l(t("Purge"), cdm_compose_url(CDM_WS_MANAGE_PURGE, NULL, 'frontendBaseUrl=' . $frontentURL), $trigger_link_options),
485
        '!url2' => l(t("Reindex"), cdm_compose_url(CDM_WS_MANAGE_REINDEX, NULL, 'frontendBaseUrl=' . $frontentURL), $trigger_link_options),
486
      ))
487
    . '<div id="index-progress"></div></div>',
488
  );
489
  _add_js_cdm_ws_progressbar(".index-trigger", "#index-progress");
490

    
491
  $form['cdm_webservice']['proxy'] = array(
492
    '#type' => 'fieldset',
493
    '#title' => t('Proxy'),
494
    '#collapsible' => TRUE,
495
    '#collapsed' => TRUE,
496
  );
497

    
498
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_url'] = array(
499
    '#type' => 'textfield',
500
    '#title' => t('Proxy URL') . ':',
501
    '#description' => t('If this proxy url is set the cdm api tries
502
    to connect the web service over the given proxy server.
503
    Otherwise proxy usage is deactivated.'),
504
    '#default_value' => variable_get('cdm_webservice_proxy_url', FALSE),
505
  );
506

    
507
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_port'] = array(
508
    '#type' => 'textfield',
509
    '#title' => t('Proxy port') . ':',
510
    '#default_value' => variable_get('cdm_webservice_proxy_port', '80'),
511
  );
512

    
513
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_usr'] = array(
514
    '#type' => 'textfield',
515
    '#title' => t('Login') . ':',
516
    '#default_value' => variable_get('cdm_webservice_proxy_usr', FALSE),
517
  );
518

    
519
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_pwd'] = array(
520
    '#type' => 'textfield',
521
    '#title' => t('Password') . ':',
522
    '#default_value' => variable_get('cdm_webservice_proxy_pwd', FALSE),
523
  );
524

    
525
  // TODO: settings are still incomplete, compare with
526
  // trunk/dataportal/inc/config_default.php.inc.
527
  $form['taxon_tree'] = array(
528
    '#type' => 'fieldset',
529
    '#title' => t('Taxon Tree'),
530
    '#collapsible' => FALSE,
531
    '#collapsed' => TRUE,
532
    '#description' => t('<p>When you explore your collection, you can navigate
533
      it through a tree structure also called <em>Taxon Tree</em>.</p><p>To be
534
      able to navigate through your collection the
535
      <a href="http://drupal.org/handbook/blocks">drupal block</a>
536
      <em>CDM Taxon Tree</em> should be visible for users. Enable the block at
537
      <a href="./?q=admin/build/block">Administer&#45&#62Site building&#45&#62Blocks
538
      </a></p>'),
539
  );
540

    
541
  $form['taxon_tree'][CDM_TAXONOMICTREE_UUID] = array(
542
    '#type' => 'select',
543
    '#title' => t('Available classifications') . ':',
544
    '#default_value' => variable_get(CDM_TAXONOMICTREE_UUID, FALSE),
545
    '#options' => cdm_get_taxontrees_as_options(),
546
    '#description' => t('Select the default taxa classification for your
547
      <em>taxon tree</em>, the other classifications will be also available but
548
      with a manual user change.'),
549
  );
550

    
551
  $form['taxon_tree']['taxontree_ranklimit'] = array(
552
    '#type' => 'select',
553
    '#title' => t('Rank of highest displayed taxon') . ':',
554
     // Before DEFAULT_TAXONTREE_RANKLIMIT_UUID.
555
    '#default_value' => variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT),
556
    '#options' => cdm_rankVocabulary_as_option(),
557
    '#description' => t('This is the rank of the highest displayed taxon in the
558
      <em>taxon tree</em>. You can select here which rank should be at the top
559
      level of the tree structure.'),
560
  );
561

    
562
  $form['aggregation'] = array(
563
      '#type' => 'fieldset',
564
      '#title' => t('Aggregation of data'),
565
      '#collapsible' => FALSE,
566
      '#description' => t("This section covers the different aspects of aggregating information.
567
          <p>
568
          </p>"),
569

    
570
  );
571

    
572
  $form['aggregation'][CDM_TAXON_MEDIA_FILTER] = array(
573
      '#type' => 'checkboxes',
574
      '#title' => 'Taxon media filter',
575
      '#default_value' => variable_get(CDM_TAXON_MEDIA_FILTER, unserialize(CDM_TAXON_MEDIA_FILTER_DEFAULT)),
576
      '#options' => array(
577
          'includeTaxonDescriptions' => 'Media in taxon descriptions',
578
          'includeTaxonNameDescriptions' => 'Media in name descriptions',
579
          'includeOccurrences' => 'Media related to specimens and occurrences',
580
      ),
581
      '#description' => 'This filter configures which images should be taken into account.',
582
  );
583

    
584
  $form['aggregation']['notice'] = array(
585
      '#markup' => '<strong>NOTICE:</strong> The below aggregation settings can slow down the data portal, so you may want to sensibly apply these setting and you may also
586
          want to make use of the caching capabilities of the dataportal.',
587
  );
588

    
589
  $form['aggregation']['media_aggregation'] = array(
590
      '#type' => 'fieldset',
591
      '#title' => t('Media aggregation'),
592
      '#collapsible' => FALSE,
593
      '#collapsed' => TRUE,
594
      '#description' => t("The media aggregation is also affected by the settigs in \"<strong>Aggregation via taxon relationsships</strong>\" below."),
595

    
596
  );
597
  $form['aggregation']['media_aggregation']['cdm_images_include_children'] = array(
598
      '#type' => 'select',
599
      '#title' => t('Aggregation of taxon pictures') . ':',
600
      '#default_value' => variable_get('cdm_images_include_children', FALSE),
601
      '#options' => array(
602
          0 => "Show only pictures of the current taxon",
603
          1 => "Include pictures of taxonomic children",
604
      ),
605
      '#description' => t("Choose whether to include the images of the taxonomic children. This will affect the <em>Images</em> tab and image tumbnails like in the search results."),
606
  );
607

    
608
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS] = array(
609
      '#type' => 'fieldset',
610
      '#attributes' => array('class'=>array('clearfix')),
611
      '#title' => t('Aggregation via taxon relationsships'),
612
      '#collapsible' => TRUE,
613
      '#collapsed' => TRUE,
614
      '#tree' => TRUE,
615
      '#description' => t('Information on taxa will be aggregated along the below chosen
616
          taxon relation ships. This will affect images and occurrences (specimens).
617
          Taxon relation ships are directed and point form one taxon to another. The taxon
618
          relationships to be taken into accunt can therefore configured for the direct direction
619
          and for the inverse.'),
620
  );
621

    
622
  $taxonRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_TAXON_RELATIONSHIP_TYPE, '_cdm_relationship_type_term_label_callback');
623
  $aggregate_by_taxon_relationships = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
624

    
625
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS]['direct'] = array(
626
      '#type' => 'checkboxes',
627
      '#title' => t('Direct'),
628
      '#options' => $taxonRelationshipTypeOptions,
629
      '#default_value' => $aggregate_by_taxon_relationships['direct'],
630
  );
631
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS]['invers'] = array(
632
      '#type' => 'checkboxes',
633
      '#title' => t('Invers'),
634
      '#options' => $taxonRelationshipTypeOptions,
635
      '#default_value' => $aggregate_by_taxon_relationships['invers'],
636
  );
637

    
638
  // Comment @WA: D7 form api does not support reset buttons,
639
  // so to mimic the D5 reset button we add one like this.
640
  $form['actions']['reset'] = array(
641
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
642
    '#weight' => 1000,
643
  );
644

    
645
  return system_settings_form($form);
646
}
647

    
648

    
649
/**
650
 * LAYOUT settings
651
 * @return unknown_type
652
 *   todo
653
 */
654
function cdm_settings_layout() {
655

    
656
  $form = array();
657

    
658
  $form['about'] = array(
659
    '#markup' => '<h4>' . t('Portal Layout') . '</h4><p>' . t('This settings contains the general configurations
660
      layout. If you want to configure the specific sites layout visit the
661
      respective configuration site for taxon, search or media.') . '</p>',
662
  );
663

    
664
  // ---- footnotes --- //
665
  $form['footnotes'] = array(
666
    '#type' => 'fieldset',
667
    '#title' => t('Footnotes'),
668
    '#collapsible' => FALSE,
669
    '#collapsed' => FALSE,
670
    '#description' => t('Taxa data such authors, synonyms names, descriptions,
671
      media or distribution areas may have annotations or footnotes. When the
672
      footnotes are enabled they will be visible (if they exist).'),
673
  );
674

    
675
  $form['footnotes']['cdm_dataportal_all_footnotes'] = array(
676
    '#type' => 'checkbox',
677
    '#title' => t('Do not show footnotes'),
678
    '#default_value' => variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES),
679
    '#description' => t('Check this if you do not want to show any footnotes'),
680
  );
681

    
682
  $form['footnotes']['cdm_dataportal_annotations_footnotes'] = array(
683
    '#type' => 'checkbox',
684
    '#title' => t('Do not show annotations footnotes'),
685
    '#default_value' => variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ANNOTATIONS_FOOTNOTES),
686
    '#description' => t('Check this if you do not want to show annotation footnotes'),
687
  );
688

    
689
  $annotationTypeOptions = cdm_Vocabulary_as_option(UUID_ANNOTATION_TYPE);
690
  // Additional option for the NULL case.
691
  $annotationTypeOptions['NULL_VALUE'] = t('untyped');
692
  $form['footnotes']['annotations_types_as_footnotes'] = array(
693
    '#type' => 'checkboxes',
694
    '#title' => t('Annotation types as footnotes'),
695
    '#description' => t("Only annotations of the selected type will be displayed
696
       as footnotes. You may want to turn 'technical annotations' off."),
697
    '#options' => $annotationTypeOptions,
698
  );
699
  $annotationsTypesAsFootnotes = variable_get('annotations_types_as_footnotes', unserialize(ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT));
700
  if (!empty($annotationsTypesAsFootnotes)) {
701
    $form['footnotes']['annotations_types_as_footnotes']['#default_value'] = $annotationsTypesAsFootnotes;
702
  }
703

    
704
  // --- Advanced Search --- //
705
  $form['asearch'] = array(
706
      '#type' => 'fieldset',
707
      '#title' => t('Advanced search'),
708
      '#collapsible' => FALSE,
709
      '#collapsed' => FALSE,
710
  );
711
  $form['asearch']['cdm_dataportal_show_advanced_search'] = array(
712
      '#type' => 'checkbox',
713
      '#title' => t('Show advanced search link'),
714
      '#default_value' => variable_get('cdm_dataportal_show_advanced_search', 1),
715
      '#description' => t('Check this box if the link to advanced search should be show below the search box.'),
716
  );
717

    
718
  // ---- Taxon Name Rendering --- //
719
  $form['taxon_name'] = array(
720
      '#type' => 'fieldset',
721
      '#title' => t('Taxon name display'),
722
      '#collapsible' => TRUE,
723
      '#collapsed' => TRUE,
724
      '#description' => t('The display of taxon names is configured by two parts.'
725
          . 'The <srong>name render templates</strong> define the parts of the name to be displayed in the different areas of the data portal pages.'
726
          . 'The name parts are defined in the <stong>part definitions</strong>'),
727
  );
728

    
729
  drupal_add_js(
730
      'jQuery(document).ready( function( ) {
731
         // pretty print json
732
         var json_text = jQuery(\'#edit-' . CDM_PART_DEFINITIONS . '\').val();
733
         var obj = JSON.parse(json_text);
734
         jQuery(\'#edit-' . CDM_PART_DEFINITIONS . '\').val(JSON.stringify(obj, undefined, 2));
735

    
736
         json_text = jQuery(\'#edit-' . CDM_NAME_RENDER_TEMPLATES . '\').val();
737
         obj = JSON.parse(json_text);
738
         jQuery(\'#edit-' . CDM_NAME_RENDER_TEMPLATES . '\').val(JSON.stringify(obj, undefined, 2));
739
       });'
740
      ,array(
741
          'type' => 'inline',
742
          'scope' => 'footer'
743
      )
744
  );
745

    
746
  //TODO remove after all portals are using the settings
747
  $default_part_definitions = theme('get_partDefinition', array());
748
  if(!is_array($default_part_definitions)) {
749
    $default_part_definitions = unserialize(CDM_PART_DEFINITIONS_DEFAULT);
750
  }
751

    
752
  $form['taxon_name'][CDM_PART_DEFINITIONS] = array(
753
      '#type' => 'textarea',
754
      '#title' => t('Part definitions'),
755
      '#element_validate' => array('form_element_validate_json'),
756
      '#default_value' =>  json_encode(variable_get(CDM_PART_DEFINITIONS, $default_part_definitions)),
757
      '#description' => '
758
          <p>
759
          (Clearing the text area will reset it to the default)
760
          </p>
761
          <p>
762
           The part definitions define the specific parts of which a rendered taxon name plus additional information will consist.
763
          </p>
764
          <p>
765
           A full taxon name plus additional information can consist of the following elements:
766
          <ul>
767
             <li>name: the taxon name inclugin rank nbut without author</li>
768
             <li>authors:  The authors of a reference, also used in taxon names</li>
769
             <li>reference: the nomenclatural reference,</li>
770
             <li>microreference:  Volume, page number etc.</li>
771
             <li>status:  The nomenclatural status of a name</li>
772
             <li>description: name descriptions like protologues etc ...</li>
773
          </ul>
774
          </p>
775
          <p>
776
           These elements are combined in the part definitions array to from the specific parts to be rendered.
777
           (The taxon name "Lapsana communis L., Sp. Pl.: 811. 1753" shall be an example in the following)
778
           Usually the following parts are formed:
779
          <ul>
780
            <li>namePart: the name and rank (in example: "Lapsana communis")</li>
781
            <li>authorshipPart: the author (in example: "L.")</li>
782
            <li>nameAuthorPart: the combination of name and author part (in example: "Lapsana communis L.").</li>
783
               This is useful for zoological names where the authorshipPart belongs to the name and both should</li>
784
               be combined when a link to the taxon is rendered.</li>
785
            <li>referencePart: the nomencaltural reference (in example: "Sp. Pl. 1753")</li>
786
            <li>microreferencePart: usually the page number (in example ": 811.")</li>
787
            <li>statusPart: the nomenclatorical status</li>
788
            <li>descriptionPart: name descriptions like protologues etc ...</li>
789
          </ul>
790
          </p>
791
          <p>
792
           Each set of parts is dedicated to render a specific TaxonName type, the type names are used as keys for the
793
           specific parts part definitions:
794
          <ul>
795
            <li>BotanicalName</li>
796
            <li>ZoologicalName</li>
797
            <li>#DEFAULT: covers ViralNames and other NonViralNames
798
          </ul>
799
           An example:
800
          <pre>
801
           {
802
            "ZoologicalName": {
803
              "namePart": {
804
                "name": true
805
              },
806
              "referencePart": {
807
                "authors": true
808
              },
809
              "microreferencePart": {
810
                "microreference": true
811
              },
812
              "statusPart": {
813
                "status": true
814
              },
815
              "descriptionPart": {
816
                "description": true
817
              }
818
            },
819
            "BotanicalName": {
820
              "namePart": {
821
                "name": true,
822
                "authors": true
823
              },
824
              "referencePart": {
825
                "reference": true,
826
                "microreference": true
827
              },
828
              "statusPart": {
829
                "status": true
830
              },
831
              "descriptionPart": {
832
                "description": true
833
              }
834
            }
835
          }
836
           </pre>',
837
  );
838

    
839
  //TODO remove after all portals are using the settings
840
  $default_render_templates = theme('get_nameRenderTemplate', array());
841
  if(!is_array($default_render_templates)) {
842
    $default_render_templates = unserialize(CDM_NAME_RENDER_TEMPLATES_DEFAULT);
843
  }
844

    
845
  $form['taxon_name'][CDM_NAME_RENDER_TEMPLATES] = array(
846
      '#type' => 'textarea',
847
      '#title' => t('Name render templates'),
848
      '#element_validate' => array('form_element_validate_json'),
849
      '#default_value' =>  json_encode(variable_get(CDM_NAME_RENDER_TEMPLATES, $default_render_templates)),
850
      '#description' => '
851
          <p>
852
          (Clearing the text area will reset it to the default)
853
          </p>
854
          <p>
855
          The render templates array contains one or more name render templates to be used within the page areas identified by the
856
          render path. The render path is used as key of the array sub subelements whereas the name render template array is set as value.
857
          The following render Path keys are curretly recognized:
858
          <ul>
859
            <li>list_of_taxa:</li>
860
            <li>acceptedFor:</li>
861
            <li>taxon_page_synonymy</li>
862
            <li>typedesignations</li>
863
            <li>taxon_page_title</li>
864
            <li>polytomousKey</li>
865
            <li>na: name + authorship</li>
866
            <li>nar:name + authorship + reference</li>
867
            <li>#DEFAULT</li>
868
          </ul>
869
          A single render template can be used for multiple render paths. In this case the according key of the render templates
870
          array element should be a comma separated list of render paths, without any whitespace!.
871
          </p>
872
          <p>
873
          A render template is an associative array. The keys of this array are referring to the keys as defined in the part
874
          definitions array. See <a href="#edit-cdm-part-definitions">Part definitions</a> above for more information.
875
          <p>
876
          The value of the render template element must be set to TRUE in order to let this part being rendered.
877
          The namePart, nameAuthorPart and referencePart can also hold an associative array with a single
878
          element: array(\'#uri\' => TRUE). The value of the #uri element will be replaced by the according
879
          links if the paramters $nameLink or $refenceLink are given to the name render function
880
          (this is hard coded and cannot be configured here).',
881
  );
882

    
883
  // @WA: D7 form api does not support reset buttons,
884
  // so to mimic the D5 reset button we add one like this.
885
  $form['actions']['reset'] = array(
886
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
887
    '#weight' => 1000,
888
  );
889

    
890
  $form['#submit'] = array('submit_json_as_php_array');
891
  // #json_elements especially defined for submit_json_as_php_array()
892
  $form['#json_elements'] = array(CDM_NAME_RENDER_TEMPLATES, CDM_PART_DEFINITIONS);
893
  return system_settings_form($form);
894
}
895

    
896

    
897
/**
898
 * @todo Please document this function.
899
 * @see http://drupal.org/node/1354
900
 */
901
function cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description = '') {
902
  $form[$form_name] = array(
903
    '#type' => 'fieldset',
904
    '#title' => t($form_title),
905
    '#collapsible' => TRUE,
906
    '#collapsed' => $collapsed,
907
    '#tree' => TRUE,
908
    '#description' => t($form_description),
909
  );
910

    
911
  $default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
912
  $gallery_settings = variable_get($form_name, $default_values);
913
  // $test = variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE);
914
  if ($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME) {
915
    /*
916
    TODO: why cdm_dataportal_search_items_on_page does not save the value on $test???
917
    $form[$form_name]['cdm_dataportal_search_items_on_page'] = array(
918
    '#type' => 'textfield',
919
    '#title' => t('Search Page Size'),
920
    '#default_value' => $test,
921
    '#description' => t('Number of Names to display per page in search results.')
922
    );
923
    */
924
    $form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
925
      '#type' => 'checkbox',
926
      '#title' => t('Show media thumbnails for accepted taxa'),
927
      '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
928
    );
929

    
930
    $form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
931
      '#type' => 'checkbox',
932
      '#title' => t('Show media thumbnails for synonyms'),
933
      '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
934
      '#description' => '',
935
    );
936
  }
937

    
938
  // $showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
939
  $form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
940
    '#type' => 'checkbox',
941
    '#title' => t('Show captions under thumbnails'),
942
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
943
    '#description' => '',
944
  );
945

    
946
  $form[$form_name]['cdm_dataportal_media_maxextend'] = array(
947
    '#type' => 'textfield',
948
    '#title' => t('Thumbnail size') . ':',
949
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
950
    '#description' => t('Select the size of each individual thumbnail.'),
951
  );
952

    
953
  if ($form_name != CDM_DATAPORTAL_MEDIA_GALLERY_NAME) {
954
    $form[$form_name]['cdm_dataportal_media_cols'] = array(
955
      '#type' => 'textfield',
956
      '#title' => t('Number of columns') . ':',
957
      '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
958
      '#description' => t('Group the thumbnails in columns: select how many
959
        columns the gallery should display.'),
960
    );
961
  }
962

    
963
  if ($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME) {
964
    $form[$form_name]['cdm_dataportal_media_maxRows'] = array(
965
      '#type' => 'textfield',
966
      '#title' => t('Maximum number of rows') . ':',
967
      '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
968
      '#description' => t('You can group the thumbnails in rows, select in how
969
        many rows should be the thumbnails grouped.<br/><strong>Note:</strong>
970
        If you want an unlimited number of rows please set to 0.'),
971
    );
972
  }
973

    
974
  return $form;
975
}
976

    
977
/**
978
 * @todo document this function.
979
 */
980
function cdm_settings_layout_taxon() {
981
  $collapsed = FALSE;
982
  $form = array();
983

    
984
  $form['#submit'][] = 'cdm_settings_layout_taxon_submit';
985

    
986
  $form['cdm_dataportal_show_back_to_search_results'] = array(
987
      '#type' => 'checkbox',
988
      '#title' => t('Show <em>Back to search results</em> link at the taxon site.'),
989
      '#default_value' => variable_get('cdm_dataportal_show_back_to_search_results', 1),
990
      '#description' => t('<p>If checked the link to search results is rendered at
991
       the top of the taxon site. Clicking on the link the last search performed
992
       is rendered again.</p>'),
993
  );
994

    
995
  // --------- TABBED TAXON ------- //
996
  $form['taxon_tabs'] = array(
997
    '#type' => 'fieldset',
998
    '#title' => t('Taxon tabs'),
999
    '#collapsible' => TRUE,
1000
    '#collapsed' => FALSE,
1001
    '#description' => t('If tabbed taxon page is enabled the taxon profile will
1002
      be splitted in four diferent tabs; General, Synonymy, Images and
1003
      Specimens. If the taxon has no information for any of the tabs/sections
1004
      such tab will be not displayed.'),
1005
  );
1006

    
1007
  $form['taxon_tabs']['cdm_dataportal_taxonpage_tabs'] = array(
1008
    '#type' => 'checkbox',
1009
    '#title' => t('Tabbed taxon page'),
1010
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
1011
    '#description' => t('<p>If selected split the taxon page into individual
1012
      tabs for description, images, synonymy and specimens. If not the taxon
1013
      data is rendered as a long single page without tabs.</p>'),
1014
  );
1015

    
1016
  $form['taxon_tabs']['cdm_taxonpage_tabs_visibility'] = array(
1017
    '#type' => 'checkboxes',
1018
    '#title' => t('Tabs visibility options') . ':',
1019
    '#default_value' => variable_get('cdm_taxonpage_tabs_visibility', get_taxon_options_list()),
1020
    '#options' => get_taxon_options_list(),
1021
    '#description' => t('Enable or disable Tabs in the Tabbed page display'),
1022
  );
1023

    
1024
  $form['taxon_tabs']['cdm_dataportal_default_tab'] = array(
1025
    '#type' => 'select',
1026
    '#title' => t('Default tab to display') . ':',
1027
    '#default_value' => variable_get('cdm_dataportal_default_tab', 0),
1028
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
1029
    '#description' => t('<p>Select the default tab to display when visiting a
1030
      taxon page. Only available if Tabbed Taxon Page is enable.</p>
1031
      <strong>Note:</strong> After performing a search and clicking in any
1032
      synonym, the taxon tab to be rendered will be the synonymy of the accepted
1033
      taxon and not the above selected tab.'),
1034
  );
1035

    
1036
  /* ======  TAXON_PROFILE ====== */
1037
  $form['taxon_profile'] = array(
1038
    '#type' => 'fieldset',
1039
    '#title' => t('Taxon profile (tab)'),
1040
    '#description' => t('<p>This section covers the settings related to the taxon
1041
      profile tab, also known as the <strong>"General"</strong> tab.</p>'),
1042
    '#collapsible' => TRUE,
1043
    '#collapsed' => TRUE,
1044
  );
1045

    
1046
  // ---- PROFILE PICTURE ----//
1047

    
1048
  $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE] = array(
1049
    '#type' => 'fieldset',
1050
    '#tree' => TRUE,
1051
    '#title' => t('Taxon profile picture'),
1052
    '#collapsible' => TRUE,
1053
    '#collapsed' => FALSE,
1054
    '#description' => t('This sections allows configuring the display of the so called taxon profile image which is displayed in the taxon profile tab.'),
1055
  );
1056

    
1057
  //FIXME migrate variables:
1058
  //  cdm_dataportal_show_default_image ---> CDM_TAXON_PROFILE_IMAGE['show']
1059
  // FIXME
1060
  //  eanable file modul in profile and in update
1061

    
1062
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
1063
  /*
1064
   * 'show' => 1,
1065
   * 'maxextend' => 184,
1066
   * 'custom_placeholder_image_on' => 1,
1067
   * 'custom_placeholder_image_fid' => ''
1068
   */
1069
  $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['show'] = array(
1070
    '#type' => 'checkbox',
1071
    '#title' => t('Enable profil picture'),
1072
    '#description' => t('Show the profil picture.'),
1073
    '#default_value' => $taxon_profile_image_settings['show'],
1074
  );
1075

    
1076
  $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['maxextend'] = array(
1077
      '#type' => 'textfield',
1078
      '#tree' => TRUE,
1079
      '#title' => t('Profil picture maximum extend'),
1080
      '#default_value' =>  $taxon_profile_image_settings['maxextend'],
1081
      '#field_suffix' => 'px',
1082
      '#maxlength' => 4,
1083
      '#size' => 4,
1084
      '#description' => t('The maximum extend in either dimension, width or height, of the profil picture in pixels.')
1085
  );
1086

    
1087
  $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['custom_placeholder_image_on'] = array(
1088
      '#type' => 'checkbox',
1089
      '#title' => t('Use a custom placeholder image'),
1090
      '#description' => t("This image is shown as replacement if no image of the taxon is available."),
1091
      '#default_value' => $taxon_profile_image_settings['custom_placeholder_image_on']
1092
  );
1093

    
1094
  if($taxon_profile_image_settings['custom_placeholder_image_on'] == 1){
1095
    $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['custom_placeholder_image_fid'] = array(
1096
        '#type' => 'managed_file',
1097
        '#title' => t('Custom placeholder image file'),
1098
        '#progress_indicator' => 'bar',
1099
        '#default_value' => $taxon_profile_image_settings['custom_placeholder_image_fid'],
1100
    //       '#name' => 'custom_placeholder_image',
1101
        '#upload_location' => 'public://' . CDM_TAXON_PROFILE_IMAGE .'/'
1102
    );
1103

    
1104
    if($taxon_profile_image_settings['custom_placeholder_image_fid']){
1105
      $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
1106
      $url = file_create_url($profile_image_file->uri);
1107
      $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['preview'] = array(
1108
                '#type' => 'item',
1109
                '#markup' => '<div class="image-preview"><img src="' . $url . '"/></div>',
1110
      );
1111
    }
1112
  } else {
1113
    $form['taxon_profile'][CDM_TAXON_PROFILE_IMAGE]['custom_placeholder_image_fid'] = array(
1114
        '#type' => 'hidden',
1115
        '#default_value' => $taxon_profile_image_settings['custom_placeholder_image_fid']
1116
    );
1117
  }
1118

    
1119

    
1120

    
1121
  $options = cdm_rankVocabulary_as_option();
1122
  array_unshift($options, '-- DISABLED --');
1123
  $form['taxon_profile']['picture']['image_hide_rank'] = array(
1124
    '#type' => 'select',
1125
    '#title' => t('Hide profile picture for higher ranks') . ':',
1126
    '#default_value' => variable_get('image_hide_rank', '0'),
1127
    '#options' => $options,
1128
    '#description' => t('The taxon profile picture will not be shown for taxa with rank higher that the selected.'),
1129
  );
1130

    
1131
  // -- MEDIA THUMBNAILS -- //
1132
  $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME;
1133
  $form_title = 'Taxon Profile Images';
1134
  $form_description = '<p>The different section in the taxon  profile can have images associated with them. These images are displayed in a gallery of thumbnails wich can be configuered here:</p>';
1135
  $form['taxon_profile'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description);
1136

    
1137
  // ---- FEATURE TREE ---- //
1138
  $form['taxon_profile']['feature_trees'] = array(
1139
    '#type' => 'fieldset',
1140
    '#title' => t('Features'),
1141
    '#collapsible' => TRUE,
1142
    '#collapsed' => FALSE,
1143
    '#description' => t("This section covers settings related to the taxon's
1144
      <em>Feature Tree</em>. The <em>feature tree</em> are the taxon's
1145
      features such as description, distribution, common names, etc. that Drupal
1146
      will render at the taxon profile page."),
1147
  );
1148
  $featureTrees = cdm_get_featureTrees_as_options(TRUE);
1149
  $saved_uuid = variable_get(CDM_PROFILE_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE);
1150
  if(!isset($featureTrees['options'][$saved_uuid])) {
1151
    $saved_uuid = UUID_DEFAULT_FEATURETREE;
1152
  }
1153
  $form['taxon_profile']['feature_trees'][CDM_PROFILE_FEATURETREE_UUID] = array(
1154
    '#type' => 'radios',
1155
    '#title' => t('Taxon profile sections') . ':',
1156
    '#default_value' => $saved_uuid,
1157
    '#options' =>  $featureTrees['options'],
1158
    '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
1159
    '#options_suffixes' => $featureTrees['treeRepresentations'],
1160
    '#description' => t('Select the Feature Tree to be displayed at the taxon
1161
      profile. Click "Show Details" to see the Feature Tree elements.'
1162
    ),
1163
  );
1164
  $featureTrees = cdm_get_featureTrees_as_options();
1165
  $saved_uuid = variable_get(CDM_DATAPORTAL_STRUCTURED_DESCRIPTION_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE);
1166
  if(!isset($featureTrees['options'][$saved_uuid])) {
1167
    $saved_uuid = NULL;
1168
  }
1169
  $form['taxon_profile']['feature_trees'][CDM_DATAPORTAL_STRUCTURED_DESCRIPTION_FEATURETREE_UUID] = array(
1170
    '#type' => 'radios',
1171
    '#title' => t('Natural language representation of structured descriptions') . ':',
1172
    '#default_value' => $saved_uuid,
1173
    '#options' => $featureTrees['options'],
1174
    '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
1175
    '#options_suffixes' => $featureTrees['treeRepresentations'],
1176
    '#description' => t('Taxon descriptions can be stored in a highly structured
1177
      form. The feature tree selected here will be used to generate textual
1178
      representation in natural language.'
1179
    ),
1180
  );
1181

    
1182
  // ---- LAYOUT PER FEATURE ---- //
1183
  $feature_tree = get_profile_featureTree();
1184
  if (isset($feature_tree->root->children)) {
1185

    
1186
    $form_feature_list_layout = array(
1187
      '#title' => t('Taxon profile layout'),
1188
      '#collapsible' => TRUE,
1189
      '#collapsed' => FALSE,
1190
      '#type' => 'fieldset',
1191
      '#description' => t('Will be available in a future release.'),
1192
    );
1193

    
1194
    $feature_list_layout_settings_disabled = TRUE;
1195
    foreach ($feature_tree->root->children as $featureNode) {
1196

    
1197
      if (!$feature_list_layout_settings_disabled && isset($featureNode->feature)) {
1198
        // Must not exceed 45 characters !!!
1199
        $subform_id = LAYOUT_SETTING_PREFIX . $featureNode->feature->uuid;
1200

    
1201
        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
1202
        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
1203

    
1204
        $form_feature_list_layout[$subform_id] = array(
1205
          '#tree' => TRUE,
1206
          '#title' => 'test' . $featureNode->feature->representation_L10n,
1207
          '#collapsible' => FALSE,
1208
          '#collapsed' => FALSE,
1209
          '#type' => 'fieldset',
1210
          '#description' => t(''),
1211
        );
1212

    
1213
        $form_feature_list_layout[$subform_id]['enabled'] = array(
1214
          '#type' => 'checkbox',
1215
          '#title' => t('Enable'),
1216
          '#default_value' => $settings['enabled'],
1217
          '#description' => t('Enable user defined layout for this feature'),
1218
        );
1219

    
1220
        $form_feature_list_layout[$subform_id]['enclosingTag'] = array(
1221
          '#type' => 'textfield',
1222
          '#title' => t('Enclosing tag'),
1223
          '#disabled' => !$settings['enabled'],
1224
          '#default_value' => $settings['enclosingTag'],
1225
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['enclosingTag'] . "</code>'",
1226
        );
1227

    
1228
        $form_feature_list_layout[$subform_id]['entryEnclosingTag'] = array(
1229
          '#type' => 'textfield',
1230
          '#title' => t('Entry enclosing tag'),
1231
          '#disabled' => !$settings['enabled'],
1232
          '#default_value' => $settings['entryEnclosingTag'],
1233
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['entryEnclosingTag'] . "</code>'",
1234
        );
1235

    
1236
        $form_feature_list_layout[$subform_id]['glue'] = array(
1237
          '#type' => 'textfield',
1238
          '#title' => t('Glue'),
1239
          '#disabled' => !$settings['enabled'],
1240
          '#default_value' => $settings['glue'],
1241
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['glue'] . "</code>'",
1242
        );
1243

    
1244
      }
1245

    
1246
      $form['taxon_profile']['feature_list_layout'] = $form_feature_list_layout;
1247
    }
1248
  }
1249

    
1250
  // ---- DISTRIBUTION LAYOUT ---- //
1251
  $form['taxon_profile']['distribution_layout'] = array(
1252
    '#title' => t('Distribution'),
1253
    '#collapsible' => TRUE,
1254
    '#collapsed' => FALSE,
1255
    '#type' => 'fieldset',
1256
    '#description' => t('Select if you want to sort or not the distribution text
1257
      located below the distribution map.'),
1258
  );
1259

    
1260
  $form['taxon_profile']['distribution_layout']['distribution_sort'] = array(
1261
    '#type' => 'radios',
1262
    '#title' => t('Sort') . ':',
1263
    '#default_value' => variable_get('distribution_sort', 'NO_SORT'),
1264
    '#options' => array(
1265
      'NO_SORT' => t('Standard (No sort)'),
1266
      'HIDE_TDWG2' => t('Sorted without TDWG Level 2'),
1267
    ),
1268
  );
1269

    
1270
  $form['taxon_profile']['distribution_layout'][DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP] = array(
1271
    '#type' => 'checkbox',
1272
    '#title' => t('Show TextData elements on top of the map'),
1273
    '#default_value' => variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0),
1274
    '#description' => t('Check this if you want to appear all <code>TextData</code>
1275
      elements on top of the map. Otherwise all <code>TextData</code>
1276
      distribution elements will be listed below the other area elements.
1277
      This option is useful if you need to have descriptive texts for each
1278
      distribution map.'),
1279
  );
1280

    
1281

    
1282
  /* ====== SYNONYMY ====== */
1283
  $form['taxon_synonymy'] = array(
1284
    '#type' => 'fieldset',
1285
    '#title' => t('Taxon synonymy (tab)'),
1286
    '#collapsible' => TRUE,
1287
    '#collapsed' => TRUE,
1288
    '#description' => t('This section covers the settings related to the taxon
1289
      <strong>synonymy</strong> tab.'),
1290
  );
1291

    
1292
  $form['taxon_synonymy']['cdm_dataportal_nomref_in_title'] = array(
1293
    '#type' => 'checkbox',
1294
    '#title' => t('Show accepted taxon on top of the synonymy'),
1295
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
1296
    '#description' => t('If checked, the first homotypic taxon is a repetition
1297
      of the accepted taxon most likely with the full nomenclatural reference
1298
      (depending on the currently chosen theme).'),
1299
  );
1300

    
1301
  $form['taxon_synonymy']['cdm_dataportal_display_is_accepted_for'] = array(
1302
    '#type' => 'checkbox',
1303
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when
1304
      coming from a synonym link.'),
1305
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
1306
    '#description' => t('Check this if after doing a search and clicking on a
1307
      synonym you want to see the "accept of" text for the accepted synonym.'),
1308
  );
1309

    
1310
  $nameRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_NAME_RELATIONSHIP_TYPE);
1311
  $form['taxon_synonymy']['name_relationships']['name_relationships_to_show'] = array(
1312
    '#type' => 'checkboxes',
1313
    '#title' => t('Display name relationships') . ':',
1314
    '#default_value' => variable_get('name_relationships_to_show', 0),
1315
    '#options' => $nameRelationshipTypeOptions,
1316
    '#description' => t('Select the name relationships you want to show for the
1317
      accepted taxa.'),
1318
  );
1319

    
1320
  $form['taxon_synonymy'][CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS] = array(
1321
    '#type' => 'checkbox',
1322
    '#title' => t('Show taxon relations ships of accepted taxon'),
1323
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
1324
    '#description' => t('If this option is enabled the synonymy will show the
1325
      below selected taxon relationships of accepted taxa.'),
1326
  );
1327

    
1328
  $taxonRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_TAXON_RELATIONSHIP_TYPE, '_cdm_relationship_type_term_label_callback');
1329
  $form['taxon_synonymy'][CDM_TAXON_RELATIONSHIP_TYPES] = array(
1330
    '#type' => 'checkboxes',
1331
    '#title' => t('Taxon relationship types') . ':',
1332
    '#description' => t('Only taxon relationships of the selected type will be
1333
      displayed'),
1334
    '#options' => $taxonRelationshipTypeOptions,
1335
    '#default_value' => variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT)),
1336
    '#disabled' => !variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
1337
  );
1338

    
1339
  // ====== SPECIMENS ====== //
1340
  $form['taxon_specimens'] = array(
1341
    '#type' => 'fieldset',
1342
    '#title' => t('Taxon specimens (tab)'),
1343
    '#collapsible' => TRUE,
1344
    '#collapsed' => TRUE,
1345
    '#description' => t('This section covers the settings related to the taxon
1346
      <strong>specimens</strong> tab.'),
1347
  );
1348

    
1349
  $featureTrees = cdm_get_featureTrees_as_options(TRUE);
1350
  $saved_uuid = variable_get(CDM_OCCURRENCE_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE);
1351
  if(!isset($featureTrees['options'][$saved_uuid])) {
1352
    $saved_uuid = UUID_DEFAULT_FEATURETREE;
1353
  }
1354
  $form['taxon_specimens']['feature_trees'][CDM_OCCURRENCE_FEATURETREE_UUID] = array(
1355
      '#type' => 'radios',
1356
      '#title' => t('Specimen description feature tree') . ':',
1357
      '#default_value' => $saved_uuid,
1358
      '#options' =>  $featureTrees['options'],
1359
      '#pre_render' => array('form_pre_render_conditional_form_element', 'radios_prepare_options_suffix'),
1360
      '#options_suffixes' => $featureTrees['treeRepresentations'],
1361
      '#description' => t('Select the feature tree to be used for displaying specimen descriptions. Click "Show Details" to see the Feature Tree elements.'
1362
      ),
1363
  );
1364

    
1365
  $form_name = CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME;
1366
  $form_title = t('Specimen media');
1367
  $form_description = t('Specimens may have media which is displayed at the
1368
     Specimen tab/section as a gallery. It is possible to configure the
1369
     thumbnails gallery here, however for configuring how a single media should
1370
     be displayed please go to !url.</p>',
1371
     array(
1372
       '!url' => l(t('Layout -> Media'), 'admin/config/cdm_dataportal/settings/layout/media'),
1373
     ));
1374
  $form['taxon_specimens'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, FALSE, $form_description);
1375

    
1376
  // --- MEDIA GALLERY ---- //
1377
  $form_name = CDM_DATAPORTAL_TAXON_MEDIA_GALLERY_NAME_TAB;
1378
  $form_title = 'Media gallery (tab)';
1379
  $form_description = '<p>This section covers the settings related to the taxon <strong>media</strong> tab.
1380
   Taxa may have media (usually images) and they are as thumbnails displayed. It is possible to configure
1381
   the thumbnails gallery here, however for configuring how a single media should be displayed please go to
1382
   <a href="./?q=admin/settings/cdm_dataportal/layout/media">Layout -&gt; Media</a></p>
1383
   <p><strong>Note:</strong> These settings are only taken into account when the standard
1384
   gallery viewer is selected at <a href="./?q=admin/settings/cdm_dataportal/layout/media">Layout -&gt; Media</a>.</p>';
1385
  $form['taxon_media'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, TRUE, $form_description);
1386

    
1387
  // Comment @WA: D7 form api does not support reset buttons,
1388
  // so to mimic the D5 reset button we add one like this.
1389
  $form['actions']['reset'] = array(
1390
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1391
    '#weight' => 1000,
1392
  );
1393
  return system_settings_form($form);
1394
}
1395

    
1396
/**
1397
 * @todo document this function.
1398
 */
1399
function cdm_settings_layout_search() {
1400

    
1401
  $form = array();
1402

    
1403
  $form['search_settings'] = array(
1404
    '#type' => 'fieldset',
1405
    '#title' => t('Taxa Search'),
1406
    '#collapsible' => FALSE,
1407
    '#collapsed' => FALSE,
1408
    '#description' => t('<p>The data portal allows the users to perform searchs.</p><p>To perform searchs
1409
         the block <em>CDM Taxon Search</em> should be enabled and visible for users
1410
         where they can write the text to be searched. You can find Drupal block configuration
1411
         site at <a href="./?q=admin/build/block">Administer&#45&#62Site building&#45&#62Blocks</a></p> '),
1412
  );
1413

    
1414
  $form['search_settings']['simple_search_ignore_classification'] = array(
1415
      '#type' => 'checkbox',
1416
      '#title' => t('Ignore the chosen classification in simple search'),
1417
      '#default_value' => variable_get('simple_search_ignore_classification', TRUE),
1418
      '#description' => t('The simple search, which can be executed via the search block,
1419
          will by default search on all classifications. Remove the tick if you want your
1420
          portal to search on the classification selected in the classification browser
1421
          selector.'),
1422
  );
1423

    
1424
  $form['search_settings']['cdm_dataportal_search_items_on_page'] = array(
1425
    '#type' => 'textfield',
1426
    '#title' => t('Results per page') . ':',
1427
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
1428
    '#description' => t('Number of results to display per page.'),
1429
  );
1430

    
1431
  $form['search_settings']['cdm_search_taxa'] = array(
1432
    '#type' => 'checkbox',
1433
    '#title' => t('Sets the default value of <em>Search for accepted taxa</em> in the advanced search form.'),
1434
    '#default_value' => variable_get('cdm_search_taxa', 1),
1435
    '#description' => t('<p>If checked the <em>Search for accepted taxa </em> option will be enabled in the advanced form.</p>'),
1436
  );
1437

    
1438
  $form['search_settings']['cdm_search_synonyms'] = array(
1439
    '#type' => 'checkbox',
1440
    '#title' => t('Sets the default value of <em>Search for synonyms</em> in the advanced search form.'),
1441
    '#default_value' => variable_get('cdm_search_synonyms', 1),
1442
    '#description' => t('<p>If checked the <em>Search for synonyms</em> option will be enabled in the advanced form.</p>'),
1443
  );
1444

    
1445
  $form['search_settings']['cdm_search_common_names'] = array(
1446
    '#type' => 'checkbox',
1447
    '#title' => t('Sets the default value of <em>Search for common names</em> in the advanced search form.'),
1448
    '#default_value' => variable_get('cdm_search_common_names', 0),
1449
    '#description' => t('<p>If checked the <em>Search for common names</em> option will be enabled in the advanced form.</p>'),
1450
  );
1451

    
1452
  $form['search_settings']['cdm_search_use_default_values'] = array(
1453
    '#type' => 'checkbox',
1454
    '#title' => t('Sets use of default values in the advanced search form.'),
1455
    '#default_value' => variable_get('cdm_search_use_default_values', 1),
1456
    '#description' => t('<p>If checked the defqult values set abovewill be used for the search.</p>'),
1457
  );
1458

    
1459

    
1460
  // --- SEARCH TAXA GALLERY ---- //
1461
  $items = variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE);
1462
  $collapsed = FALSE;
1463
  $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_NAME;
1464
  $form_title = 'Taxa Search thumbnails';
1465
  $form_description = 'Search results may show thumbnails. ';
1466
  $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description);
1467

    
1468
  // Comment @WA: D7 form api does not support reset buttons,
1469
  // so to mimic the D5 reset button we add one like this.
1470
  $form['actions']['reset'] = array(
1471
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1472
    '#weight' => 1000,
1473
  );
1474
  return system_settings_form($form);
1475
}
1476

    
1477
/**
1478
 * @todo document this function.
1479
 */
1480
function cdm_settings_layout_media() {
1481

    
1482
  $form = array();
1483

    
1484
  $form['media_settings'] = array(
1485
    '#type' => 'fieldset',
1486
    '#title' => t('Media settings'),
1487
    '#collapsible' => FALSE,
1488
    '#collapsed' => FALSE,
1489
    '#description' => 'This section covers layout settings for media pages.'
1490
      . 'Further media related settings may be found under the taxon layout settings and on the general settings.',
1491
  );
1492

    
1493
  $form['media_settings']['image_gallery_viewer'] = array(
1494
    '#type' => 'select',
1495
    '#title' => t('Image viewer') . ':',
1496
    '#default_value' => variable_get('image_gallery_viewer', 'default'),
1497
    '#options' => array(
1498
      'default' => t('Standard image viewer'),
1499
      'fsi' => t('FSI viewer (requires FSI server!)'),
1500
    ),
1501
  );
1502

    
1503
  // --- MEDIA GALLERY ---- //
1504
  $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_NAME;
1505
  $form_title = 'Standard viewer';
1506
  $form_description = '<p>Configure the standard image viewer.</p><p><strong>Note:</strong> the image viewer should selected otherwise settings are not taking into account.</p>';
1507
  // $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed);
1508
  $form['media_settings'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, FALSE, $form_description);
1509

    
1510
  // @WA: D7 form api does not support reset buttons,
1511
  // so to mimic the D5 reset button we add one like this.
1512
  $form['actions']['reset'] = array(
1513
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1514
    '#weight' => 1000,
1515
  );
1516
  return system_settings_form($form);
1517
}
1518

    
1519
/**
1520
 * GEOSERVICE and Map settings.
1521
 */
1522
function cdm_settings_geo($form, &$form_state) {
1523

    
1524
  $form = array();
1525

    
1526
  /*
1527
  GEO SERVER
1528
  */
1529
  $form['edit_map_server'] = array(
1530
    '#type' => 'fieldset',
1531
    '#tree' => true,
1532
    '#title' => t('EDIT map service'),
1533
    '#collapsible' => TRUE,
1534
    '#collapsed' => TRUE,
1535
    '#description' => t('Configuration and selection of your geo server.
1536
      The Geo Server is responsible for generating the maps.'),
1537
  );
1538

    
1539
  $current_geoserver_settings = get_edit_map_service_settings();
1540

    
1541
  $form['edit_map_server']['base_uri'] = array(
1542
    '#type' => 'select',
1543
    '#title' => t('EDIT map service') . ':',
1544
    '#default_value' => $current_geoserver_settings['base_uri'],
1545
    '#options' => unserialize(EDIT_MAPSERVER_URI),
1546
    '#description' => t('Select the EDIT map server you want to use within your data portal.'),
1547
  );
1548
  $form['edit_map_server']['version'] = array(
1549
      '#type' => 'select',
1550
      '#title' => t('Version') . ':',
1551
      '#default_value' => $current_geoserver_settings['version'],
1552
      '#options' => unserialize(EDIT_MAPSERVER_VERSION),
1553
      '#description' => t('The version of the EDIT map services'),
1554
  );
1555

    
1556
  /*
1557
  MAP SETTINGS
1558
  */
1559
  $form['map_settings'] = array(
1560
    '#type' => 'fieldset',
1561
    '#title' => t('Maps settings'),
1562
    '#collapsible' => TRUE,
1563
    '#collapsed' => TRUE,
1564
    '#description' => t('General configuration for all map types.'),
1565
  );
1566

    
1567
  $form['map_settings']['cdm_dataportal_geoservice_display_width'] = array(
1568
    '#type' => 'textfield',
1569
    '#title' => t('Maps width') . ':',
1570
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
1571
    '#description' => t('Choose the width of your maps, the height will always
1572
      be the half of the width. A value of 500 means the size will be 500 pixels
1573
      width and 250 pixels height.'),
1574
  );
1575

    
1576
  $form['map_settings']['cdm_dataportal_geoservice_bounding_box'] = array(
1577
    '#type' => 'textfield',
1578
    '#title' => t('Fixed bounding box') . ':',
1579
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
1580
    '#description' => t('Define surrounding of area to be displayed in maps.
1581
      Use "-180,-90,180,90" for the whole world. Leave <strong>empty</strong>
1582
      to let the map <strong>automatically zoom</strong> to the distribution
1583
      area.'),
1584
  );
1585

    
1586
  $form['map_settings']['cdm_dataportal_geoservice_labels_on'] = array(
1587
    '#type' => 'checkbox',
1588
    '#title' => '<b>' . t('Display area labels') . '</b>',
1589
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
1590
    '#description' => t('Check this if you like area names to be displayed in the maps. DOES IT WORK???? '),
1591
  );
1592

    
1593
  $form['map_settings']['cdm_dataportal_geoservice_map_caption'] = array(
1594
    '#type' => 'textfield',
1595
    '#title' => t('Map caption') . ':',
1596
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
1597
    '#description' => t('Define a caption for the map.'),
1598
  );
1599

    
1600
  $form['map_settings']['cdm_dataportal_geoservice_distributionOpacity'] = array(
1601
    '#type' => 'textfield',
1602
    '#title' => t('Distribution layer opacity') . ':',
1603
    '#default_value' => variable_get('cdm_dataportal_geoservice_distributionOpacity', '0.5'),
1604
    '#description' => t('Valid values range from 0.0 to 1.0. Value 1.0 means the distributions (the countries or regions) will
1605
                           fully visible, while a value near to 0.0 will be not much visible.'),
1606
  );
1607

    
1608
  $form['map_settings']['cdm_dataportal_map_openlayers'] = array(
1609
    '#type' => 'radios',
1610
    '#title' => t('Map Viewer') . ':',
1611
    '#options' => array(
1612
      1 => "OpenLayers dynamic mapviewer",
1613
      0 => "Plain image",
1614
    ),
1615
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
1616
    '#description' => t('You can choose from two different map viewers:
1617
      <ul><li><em>OpenLayers</em> displays the maps in an interactive viewer
1618
      which allows zooming and panning. If not enabled the maps will consist
1619
      on a static image. If enabled you can configure the default layer
1620
      (background of your maps) below. Only one of them will be rendered.</li>
1621
      <li><em>Plain image</em> displays the map as a plain non interactive
1622
      image.</li></ul>'),
1623
  );
1624

    
1625
  $openLayersEnabled = variable_get('cdm_dataportal_map_openlayers', 1) == 1;
1626

    
1627

    
1628
  // --- Plain Image Settings --- //
1629
  $form['map_image'] = array(
1630
    '#type' => 'fieldset',
1631
    '#title' => t('Plain image map settings'),
1632
    '#collapsible' => TRUE,
1633
    '#collapsed' => $openLayersEnabled,
1634
    '#description' => t('The settings in this section are still expertimental
1635
      and can only be used with the EDIT map service version 1.1 or above.'),
1636
  );
1637

    
1638
  $edit_mapserver_version = get_edit_map_service_version_number();
1639
  if ($edit_mapserver_version < 1.1) {
1640
    $form['map_image']['#description'] = '<div class="messages warning">' . t("The selected EDIT map service version has to small version number: $edit_mapserver_version") . '</div>'
1641
      . $form['map_image']['#description'];
1642
  }
1643

    
1644
  $form['map_image']['map_base_layer'] = array(
1645
    '#type' => 'textfield',
1646
    '#title' => t('Background layer') . ':',
1647
    '#default_value' => variable_get('map_base_layer', 'cyprusdivs'),
1648
    '#description' => t('Background layer. For available layers inspect !url1 or !url2.', array(
1649
      '!url1' => l(t('deegree-csw'), 'http://edit.africamuseum.be:8080/deegree-csw/md_search.jsp'),
1650
      '!url2' => l(t('geoserver layers'), 'http://edit.africamuseum.be/geoserver/web/'), // http://edit.africamuseum.be/geoserver/rest/layers
1651
    )),
1652
  );
1653

    
1654
  $form['map_image']['map_bg_color'] = array(
1655
    '#type' => 'textfield',
1656
    '#title' => t('Background color') . ':',
1657
    '#default_value' => variable_get('map_bg_color', '1874CD'),
1658
  );
1659

    
1660
  $form['map_image']['map_base_layer_style'] = array(
1661
    '#type' => 'textfield',
1662
    '#title' => t('Background layer area style') . ':',
1663
     // Only line color by now.
1664
    '#default_value' => variable_get('map_base_layer_style', 'ffffff,606060,,'),
1665
    '#description' => t('Syntax: {Area fill color},{Area stroke color},{Area stroke width},{Area stroke dash style}'),
1666
  );
1667

    
1668

    
1669
  // --- OpenLayers Settings --- //
1670
  $form['openlayers'] = array(
1671
    '#type' => 'fieldset',
1672
    '#title' => t('OpenLayers settings'),
1673
    '#collapsible' => TRUE,
1674
    '#collapsed' => !$openLayersEnabled,
1675
    '#description' => '',
1676
  );
1677

    
1678
  if (!$openLayersEnabled) {
1679
    $form['openlayers']['#description'] = '<div class="messages warning">' . t('The Openlayers viewer is currently not enabled! (see section Maps settings above )') . '</div>'
1680
      . $form['openlayers']['#description'];
1681
  }
1682

    
1683
  $baselayer_options = array(
1684
    /*
1685
   NOTICE: must correspond to the layers defined in
1686
   js/openlayers_,ap.js#getLayersByName()
1687
   */
1688
    'osgeo_vmap0' => "Metacarta Vmap0 (OSGeo server)", // EPSG:4326: EPSG:900913
1689
    'metacarta_vmap0' => "Metacarta Vmap0 (MetaCarta Labs server)", // EPSG:4326, EPSG:900913
1690
    // all others EPSG:900913
1691
    // 'edit-vmap0_world_basic' => 'EDIT Vmap0',
1692
    'edit-etopo1' => "ETOPO1 Global Relief Model",
1693
    'mapnik' => 'OpenStreetMap',
1694
    'osmarender' => 'OpenStreetMap (Tiles@home)',
1695
    'gmap' => 'Google Streets',
1696
    'gsat' => 'Google Satellite',
1697
    'ghyb' => 'Google Hybrid',
1698
    'veroad' => 'Virtual Earth Roads',
1699
    'veaer' => 'Virtual Earth Aerial',
1700
    'vehyb' => 'Virtual Earth Hybrid',
1701
    // 'yahoo' => 'Yahoo Street',
1702
    // 'yahoosat' => 'Yahoo Satellite',
1703
    // 'yahoohyb' => 'Yahoo Hybrid',
1704
  );
1705

    
1706
  $form['openlayers']['baselayers'] = array(
1707
    '#type' => 'checkboxes_preferred',
1708
    '#title' => t('Base Layers') . ':',
1709
    '#options' => $baselayer_options,
1710
    '#default_value' => variable_get('baselayers', array('metacarta_vmap0' => "metacarta_vmap0", 'PREFERRED' => 'metacarta_vmap0')),
1711
    '#description' => t('Choose the baselayer layer you prefer to use as map background in the OpenLayers dynamic mapviewer.'),
1712
  );
1713

    
1714
  // cdm_dataportal_geoservice_showLayerSwitcher
1715
  $form['openlayers']['cdm_dataportal_geoservice_showLayerSwitcher'] = array(
1716
    '#type' => 'checkbox',
1717
    '#title' => '<b>' . t('Show Layer Switcher') . '</b>',
1718
    '#default_value' => variable_get('cdm_dataportal_geoservice_showLayerSwitcher', TRUE),
1719
    '#description' => t('
1720
      The Layer Switcher control displays a table of contents
1721
      for the map.  This allows the user interface to switch between
1722
      BaseLayers and to show or hide Overlays.  By default the switcher is
1723
      shown minimized on the right edge of the map, the user may expand it
1724
      by clicking on the handle.'
1725
    ),
1726
  );
1727

    
1728
  $localhostkey = 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ';
1729
  $gmap_api_key = variable_get('gmap_api_key', 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ');
1730
  $form['openlayers']['gmap_api_key'] = array(
1731
    '#type' => 'textfield',
1732
    '#title' => t('Google maps API key') . ':',
1733
    '#default_value' => variable_get('gmap_api_key', $gmap_api_key),
1734
    '#description' => t('If you want to use the Google Maps Layer, a key is
1735
      needed. If you need a key, visit
1736
      <a href="http://code.google.com/intl/en/apis/maps/signup.html">google maps api key</a>.
1737
      <br/><strong>Note:</strong> The following key: <code>!localhostkey</code>
1738
      is the default key for the localhost (127.0.0.1). The key in use is the
1739
      one above this text.', array('!localhostkey' => $localhostkey)),
1740
  );
1741

    
1742
  $form['cdm_dataportal_geoservice_map_legend'] = array(
1743
    '#type' => 'fieldset',
1744
    '#title' => t('Map legend'),
1745
    '#collapsible' => TRUE,
1746
    '#collapsed' => TRUE,
1747
    '#description' => t('Configure the maps legend.'),
1748
  );
1749

    
1750
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
1751
    '#type' => 'checkbox',
1752
    '#title' => '<b>' . t('Display a map legend') . '</b>',
1753
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
1754
    '#description' => t('Check this if you like a legend to be displayed with the maps.'),
1755
  );
1756

    
1757
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legendOpacity'] = array(
1758
    '#type' => 'textfield',
1759
    '#title' => t('Legend opacity'),
1760
    '#default_value' => variable_get('cdm_dataportal_geoservice_legendOpacity', '0.5'),
1761
    '#description' => t('Valid values range from 0.0 to 1.0. Value 1.0 means the legend will be fully visible, while a value near
1762
                         to 0.0 will be not much visible.'),
1763
  );
1764

    
1765
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
1766
    '#type' => 'textfield',
1767
    '#title' => t('Font size'),
1768
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
1769
    '#description' => t('Font size in pixels.'),
1770
  );
1771

    
1772
  $fontStyles = array(
1773
    0 => "plane",
1774
    1 => "italic",
1775
  );
1776
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
1777
    '#type' => 'select',
1778
    '#title' => t('Available font styles'),
1779
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', FALSE),
1780
    '#options' => $fontStyles,
1781
    '#description' => t('Select a font style for the map legend.'),
1782
  );
1783

    
1784
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
1785
    '#type' => 'textfield',
1786
    '#title' => t('Legend icon width'),
1787
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
1788
    '#description' => t('Legend icon width in pixels.'),
1789
  );
1790
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
1791
    '#type' => 'textfield',
1792
    '#title' => t('Legend icon height'),
1793
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
1794
    '#description' => t('Legend icon height in pixels.'),
1795
  );
1796

    
1797
  // @WA: D7 form api does not support reset buttons,
1798
  // so to mimic the D5 reset button we add one like this.
1799
  $form['actions']['reset'] = array(
1800
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1801
    '#weight' => 1000,
1802
  );
1803

    
1804
  return system_settings_form($form);
1805
}
1806

    
1807
/**
1808
 * @todo document this function.
1809
 */
1810
function cdm_settings_cache() {
1811

    
1812
  $form = array();
1813

    
1814
  $form['cache_settings'] = array(
1815
    '#type' => 'fieldset',
1816
    '#title' => t('Cache Settings'),
1817
    '#collapsible' => FALSE,
1818
    '#collapsed' => FALSE,
1819
    '#description' => t('<p>When caching is enabled all single taxon sites are
1820
      stored in an internal drupal cache doing the portal response of taxa pages
1821
      faster. This is possible because the sites are loaded from the cache and
1822
      are not created from scratch.</p>'),
1823
  );
1824

    
1825
  $form['cache_settings']['cdm_webservice_cache'] = array(
1826
    '#type' => 'checkbox',
1827
    '#title' => t('<strong>Enable caching</strong>'),
1828
    '#options' => cdm_help_general_cache(),
1829
    '#default_value' => variable_get('cdm_webservice_cache', 1),
1830
    '#description' => t('<p>Enable drupal to load taxa pages from the cache.</p>
1831
       <p><strong>Note:</strong> If taxa are modified by the editor or any other
1832
       application the changes will be not visible till the cache is erased.
1833
       Therefore developers should deactived this feature when they are working
1834
       on the CDM Dataportal Module.</p>'),
1835
  );
1836

    
1837
  $form['cache_settings']['cdm_run_cache'] = array(
1838
    '#markup' => cdm_view_cache_site(),
1839
  );
1840

    
1841
  // @WA: D7 form api does not support reset buttons,
1842
  // so to mimic the D5 reset button we add one like this.
1843
  $form['actions']['reset'] = array(
1844
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1845
    '#weight' => 1000,
1846
  );
1847
  return system_settings_form($form);
1848
}
1849

    
1850
/**
1851
 * Walk and cache all taxon pages.
1852
 */
1853
function cdm_view_cache_site() {
1854

    
1855
  $out = '';
1856

    
1857
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cache_all_taxa.js');
1858

    
1859
  $request_params = array();
1860
  $request_params['class'] = "Taxon";
1861

    
1862
  $cdm_ws_page_taxa_url = cdm_compose_url(CDM_WS_TAXON . ".json", NULL, queryString($request_params));
1863
  $cdm_ws_page_taxa_url = uri_uriByProxy($cdm_ws_page_taxa_url);
1864
  $cdm_ws_page_taxa_url = rtrim($cdm_ws_page_taxa_url, '/');
1865

    
1866
  $out .= t('<p><strong>Cache all taxon pages</strong></p>');
1867
  $out .= '<p>When you launch the cache process the cache is filled and ready to be enabled.<br/>
1868
  Remember that when you load the taxa from the cache last changes on taxa will be not visible till you erase
1869
  the cache and fill it again.</p>';
1870
  $out .= '<p>Before  running the cache bot you have to empty the cache manually.</p>';
1871

    
1872
  $out .= '<div>' . t('This caching process may take long time and could cause heavy load on your server') . '</div>';
1873
  $out .= '<div id="progress"></div>';
1874

    
1875
  // Comment @WA: A form within a form is not valid html and not needed here.
1876
  // Also, it would be recommended just to include this part of the form in the
1877
  // rest of the form array in cdm_settings_cache().
1878
  // $out .= '<form id="cache_site">';
1879
  $out .= '<input type="hidden" name="pageTaxaUrl" value="' . $cdm_ws_page_taxa_url . '"/>';
1880
  $out .= '<input type="hidden" name="taxonPageUrl" value="' . url('cdm_dataportal/taxon/') . '"/>';
1881
  $out .= '<input type="button" name="start" value="' . t('Start') . '"/>';
1882
  $out .= '<input type="button" name="stop" value="' . t('Stop') . '"/>';
1883
  // $out .= '</form>';
1884
  $out .= '</div>';
1885
  /*
1886
  foreach($taxonPager->records as $taxon){
1887
    cdm_dataportal_taxon_view($uuid);
1888
  }
1889
  */
1890
  return $out;
1891
}
1892

    
1893

    
1894
function cdm_settings_layout_taxon_submit($form, &$form_state){
1895
  if (isset($form_state['values'][CDM_TAXON_PROFILE_IMAGE]['custom_placeholder_image_fid'])) {
1896
    $file = file_load($form_state['values'][CDM_TAXON_PROFILE_IMAGE]['custom_placeholder_image_fid']);
1897
    if(is_object($file)){
1898
      $file->status = FILE_STATUS_PERMANENT;
1899
      file_save($file);
1900
      file_usage_add($file, 'cdm_dataportal', CDM_TAXON_PROFILE_IMAGE, 0);
1901
    }
1902
  }
1903
}
1904

    
1905
/**
1906
 * Form validation handler for ?
1907
 *
1908
 * Comment @WA: currently this handler is not used.
1909
 * It seems partly for cdm_settings_general en partly for cdm_settings_cache?
1910
 * Then you can change the name into cdm_settings_general_validate() or
1911
 * cdm_settings_cache_validate().
1912
 * But cdm_api_secref_cache_clear is not doing anything (yet)?
1913
 */
1914
function cdm_settings_validate($form, &$form_state) {
1915

    
1916
  if (!str_endsWith($form_state['values']['cdm_webservice_url'], '/')) {
1917
    // form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
1918
    $form_state['values']['cdm_webservice_url'] .= '/';
1919
  }
1920

    
1921
  if ($form_state['values']['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)) {
1922
    cache_clear_all(NULL, 'cache_cdm_ws');
1923
    // Better clear secref_cache since I can not be sure if the cache has not
1924
    // be used during this response.
1925
    cdm_api_secref_cache_clear();
1926
  }
1927
}
1928

    
1929
/**
1930
 * Returns an associative array of the currently chosen settings for the EDIT map service or the defaults as
1931
 * specified in EDIT_MAPSERVER_URI_DEFAULT and EDIT_MAPSERVER_VERSION_DEFAULT:
1932
 *  - base_uri: the protocol and host part , e.g.: http://edit.africamuseum.be
1933
 *  - version: the version, e.g.: v1.1
1934
 *
1935
 * @return array
1936
 *    An associative array of the currently chosen settings for the EDIT map service or the defaults.
1937
 */
1938
function get_edit_map_service_settings() {
1939

    
1940
  $settings = variable_get('edit_map_server', array(
1941
      'base_uri' => EDIT_MAPSERVER_URI_DEFAULT,
1942
      'version' => EDIT_MAPSERVER_VERSION_DEFAULT
1943
      )
1944
  );
1945
  // replace old non tree like settings by default
1946
  // TODO to be removed after release 3.1.5
1947
  if(!is_array($settings)){
1948
    variable_del('edit_map_server');
1949
    return get_edit_map_service_settings();
1950
  }
1951

    
1952
  return $settings;
1953
}
1954

    
1955
/**
1956
 * Returns the full edit map service URI e.g.: http://edit.africamuseum.be/edit_wp5/v1.1
1957
 *
1958
 * @return string
1959
 *   The full edit map service URI e.g.: http://edit.africamuseum.be/edit_wp5/v1.1
1960
 */
1961
function get_edit_map_service_full_uri() {
1962
  $settings = get_edit_map_service_settings();
1963
  return $settings['base_uri'] . EDIT_MAPSERVER_PATH .  '/' . $settings['version'];
1964
}
1965

    
1966

    
1967
/**
1968
 * Returns the version number of the currently selected edit mapserver as a float
1969
 *
1970
 * @return float
1971
 *   The version number of the currently selected edit mapserver as a float.
1972
 *   Returns 0 on error.
1973
 */
1974
function get_edit_map_service_version_number() {
1975

    
1976
  $pattern = '/v([\d\.]+).*$/';
1977

    
1978
  $settings = get_edit_map_service_settings();
1979
  preg_match($pattern, $settings['version'], $matches, PREG_OFFSET_CAPTURE);
1980
  if (isset($matches[1])) {
1981
    // Convert string to float.
1982
    $version = 1 + $matches[1][0] - 1;
1983
    return $version;
1984
  }
1985
  else {
1986
    // Report error.
1987
    drupal_set_message(t(" Invalid EDIT map service version number: '!version'",
1988
        array(
1989
          '!version' => $settings['version'],
1990
          'warning')
1991
        )
1992
      );
1993
    return 0;
1994
  }
1995
}
1996

    
1997
/**
1998
 * Returns the array of selected items in a options array
1999
 *
2000
 * @param array $options
2001
 *   An options array as generated by a form element like checkoxes, select ...,
2002
 */
2003
function get_selection($options) {
2004
  $selection = array();
2005
  foreach ($options as $key=>$val) {
2006
    if (!empty($val)) {
2007
      $selection[] = $val;
2008
    }
2009
  }
2010
  return $selection;
2011
}
2012

    
2013

    
2014
/**
2015
 * Implements hook_element_info().
2016
 *
2017
 * Allows modules to declare their own Form API element types and specify their default values.
2018
 *
2019
 * @see http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_element_info/7
2020
 */
2021
function cdm_dataportal_element_info() {
2022
  $type['checkboxes_preferred'] = array(
2023
    '#input' => TRUE,
2024
    '#process' => array('checkboxes_preferred_expand'),
2025
    '#after_build' => array('checkboxes_preferred_after_build'),
2026
    '#theme' => array('checkboxes_preferred'),
2027
    // '#theme_wrapper' => array('form_element'),
2028
  );
2029
  return $type;
2030
}
2031

    
2032
/**
2033
 * #process callback function for the custom form element type 'checkbox_preferred'
2034
 *
2035
 *
2036
 */
2037
function checkboxes_preferred_expand($element, &$form_state, $form) {
2038

    
2039
  // First of all create checkboxes for each of the elements
2040
  $element = form_process_checkboxes($element);
2041

    
2042
  $children = element_children($element);
2043

    
2044
  $element['table_start'] = array(
2045
    '#markup' => '<table class="checkboxes_preferred"><tr><th></th><th>' . t('Enabled') . '</th><th>' . t('Default') . '</th></tr>',
2046
    '#weight' => -1,
2047
  );
2048

    
2049
  // prepare first part each of the table rows which contains the row label
2050
  $weight = 0;
2051
  foreach ($children as $key) {
2052
    $odd_even = $weight % 4 == 0 ? 'odd' : 'even';
2053
    $element[$key]['#weight'] = $weight;
2054
    $element[$key]['#prefix'] = '<tr class="' . $odd_even . '"><td>' . t($element['#options'][$key]) . '</td><td>';
2055
    $element[$key]['#suffix'] = '</td>';
2056
    unset($element[$key]['#title']);
2057
    $weight += 2;
2058
  }
2059
  $weight = 0;
2060

    
2061
  // add a radio button to each of the checkboxes, the
2062
  // check boxes have already been created at the beginning
2063
  // of this function
2064
  if (count($element['#options']) > 0) {
2065
    foreach ($element['#options'] as $key => $choice) {
2066
      if (!isset($element[$key . '_preferred'])) {
2067
        $element[$key . '_preferred'] = array(
2068
          '#type' => 'radio',
2069
          '#name' => $element['#parents'][0] . '_preferred',
2070
          '#return_value' => check_plain($key),
2071
          '#default_value' => empty($element['#default_value_2']) ? NULL : $element['#default_value_2'],
2072
          '#attributes' => $element['#attributes'],
2073
          '#parents' => $element['#parents'],
2074
          // '#spawned' => TRUE,
2075
          '#weight' => $weight + 1,
2076
          '#prefix' => '<td>',        // add a prefix to start a new table cell
2077
          '#suffix' => '</td></tr>',  // add a prefix to close the tabel row
2078
        );
2079
      }
2080
      $weight += 2;
2081
    }
2082
  }
2083

    
2084
  // end the table
2085
  $element['table_end'] = array(
2086
    '#markup' => '</table>',
2087
    '#weight' => $weight++,
2088
  );
2089

    
2090
  return $element;
2091
}
2092

    
2093
/**
2094
 * Theme function for the custom form field 'checkboxes_preferred'.
2095
 */
2096
function theme_checkboxes_preferred($variables) {
2097
  $element = $variables['element'];
2098
  $out = '<div id="edit-baselayers-wrapper" class="form-item">';
2099
  $out .= '<label for="edit-baselayers">' . $element['#title'] . '</label>';
2100
  $out .= drupal_render_children($element);
2101
  $out .= '<div class="description">' . $element['#description'] . '</div>';
2102
  $out .= '</div>';
2103
  return $out;
2104
}
2105

    
2106
/**
2107
 * Callback for checkboxes preferred for widget which will
2108
 * be called after the form or element is built. The call
2109
 * back is configured in the form element by setting it as
2110
 * #after_build parameter.
2111
 *
2112
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#after_build
2113
 *
2114
 * @param $form
2115
 *   Nested array of form elements that comprise the form.
2116
 * @param $form_state
2117
 *   A keyed array containing the current state of the form.
2118
 *
2119
 * @return the modified form array
2120
 */
2121
function checkboxes_preferred_after_build($form, &$form_state) {
2122

    
2123
  $parent_id = $form['#parents'][0];
2124

    
2125
  if ($_POST && count($_POST) > 0) {
2126
    // First pass of form processing.
2127
    $preferred_layer = $_POST[$parent_id . '_preferred'];
2128
    $form['#value']['PREFERRED'] = $preferred_layer;
2129
    $form_state[$parent_id] = $form['#value'];
2130
    $form_state['values']['baselayers'] = $form['#value'];
2131
  }
2132
  else {
2133
    // Second pass of form processing.
2134
    $preferred_layer = $form['#value']['PREFERRED'];
2135
  }
2136

    
2137
  // Also set the chosen value (not sure if this is good Drupal style ....).
2138
  foreach ($children = element_children($form) as $key) {
2139
    if (!empty($form[$key]['#type']) && $form[$key]['#type'] == 'radio') {
2140
      $form[$key]['#value'] = $preferred_layer;
2141
    }
2142
  }
2143
  // The default layer must always be enabled.
2144
  $form[$preferred_layer]['#value'] = $preferred_layer;
2145

    
2146
  return $form;
2147
}
2148

    
2149
function radios_prepare_options_suffix(&$elements){
2150

    
2151
  $childrenKeys = element_children($elements);
2152
  foreach($childrenKeys as $key){
2153
    if(!is_array($elements[$key]['#theme_wrappers'])){
2154
      $elements[$key]['#theme_wrappers'] = array();
2155
    }
2156
    if(isset($elements['#options_suffixes'][$key])){
2157
      $elements[$key]['#theme_wrappers'][] = 'radio_options_suffix';
2158
      $elements[$key]['#options_suffix'] = $elements['#options_suffixes'][$key];
2159
    }
2160
  }
2161
  return $elements;
2162

    
2163
}
2164

    
2165
/**
2166
 * TODO
2167
 * @param unknown $variables
2168
 */
2169
function theme_radio_options_suffix($variables) {
2170
  $element = $variables['element'];
2171
  if(isset($element['#options_suffix'])) {
2172
    $element['#children'] .= $element['#options_suffix'];
2173
  }
2174
  return $element['#children'];
2175
}
2176

    
2177

    
2178
/**
2179
 * Element validate callback for text field and arrays containing json.
2180
 *
2181
 * @param $element
2182
 *   The form element to validate
2183
 * @param $form
2184
 *   Nested array of form elements that comprise the form.
2185
 * @param $form_state
2186
 *   A keyed array containing the current state of the form.
2187
 */
2188
function form_element_validate_json($element, &$form_state, $form) {
2189
   if (!empty($element['#value'])) {
2190
     json_decode($element['#value']);
2191
     if(json_last_error() != JSON_ERROR_NONE){
2192
       form_error($element, t('The form element %title contains invalid JSON. You can check the syntax with ', array('%title' => $element['#title'])) . l('JSONLint', 'http://jsonlint.com/'));
2193
     }
2194
   }
2195
}
2196

    
2197
/**
2198
 * Form submission handler for textareas and textfields containing JSON.
2199
 *
2200
 * The contained JSON will be converted into an php array
2201
 * or object and will be stores in the variables as such.
2202
 *
2203
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit
2204
 *
2205
 * @param $form
2206
 *   Nested array of form elements that comprise the form.
2207
 * @param $form_state
2208
 *   A keyed array containing the current state of the form.
2209
 *
2210
 */
2211
function submit_json_as_php_array($form, &$form_state) {
2212
  if (is_array($form['#json_elements'])) {
2213
    foreach ($form['#json_elements'] as $element){
2214
      if (trim($form_state['values'][$element])) {
2215
        $form_state['values'][$element] = (array) json_decode($form_state['values'][$element]);
2216
      } else {
2217
        $form_state['values'][$element] = NULL;
2218
      }
2219
    }
2220
  }
2221
}
(13-13/13)