Project

General

Profile

Download (82.4 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

    
84

    
85
/**
86
 * @todo document this function
87
 */
88
function get_taxon_tabs_list() {
89
  return array(
90
    0 => 'General',
91
    1 => 'Synonymy',
92
    2 => 'Images',
93
    3 => 'Specimens',
94
    4 => 'Keys',
95
  );
96
}
97

    
98
$taxon_tab_options = get_taxon_tabs_list();
99
$taxon_tab_options[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX] = 'Last visited tab';
100

    
101
define('CDM_DATAPORTAL_DEFAULT_TAXON_TAB', serialize($taxon_tab_options));
102

    
103
/**
104
 * @todo document this function.
105
 */
106
function get_taxon_options_list() {
107
  $taxon_tab_options = array_flip(get_taxon_tabs_list());
108
  foreach ($taxon_tab_options as $key => $value) {
109
    $taxon_tab_options[$key] = t($key);
110
  }
111
  return $taxon_tab_options;
112

    
113
}
114

    
115
define('CDM_PART_DEFINITIONS', 'cdm-part-definitions');
116
define('CDM_PART_DEFINITIONS_DEFAULT', serialize(
117
  array(
118
      'ZoologicalName' => array(
119
          'namePart' => array('name' => TRUE),
120
          'referencePart' => array('authorTeam' => TRUE),
121
          'microreferencePart' => array('microreference' => TRUE),
122
          'statusPart' => array('status' => TRUE),
123
          'descriptionPart' => array('description' => TRUE),
124
      ),
125
      'BotanicalName'=> array(
126
        'namePart' => array('name' => TRUE),
127
        'nameAuthorPart' => array('name' => TRUE, 'authors' => TRUE),
128
        'referencePart' => array('reference' => TRUE, 'microreference' => TRUE),
129
        'statusPart' => array('status' => TRUE),
130
        'descriptionPart' => array('description' => TRUE),
131
      ),
132
     '#DEFAULT' => array(
133
          'namePart' => array(
134
              'name' => TRUE,
135
              'authorTeam' => TRUE
136
          ),
137
          'referencePart' => array(
138
              'reference' => TRUE
139
          ),
140
          'microreferencePart' => array(
141
              'microreference' => TRUE,
142
          ),
143
          'statusPart' => array(
144
              'status' => TRUE,
145
          ),
146
          'descriptionPart' => array(
147
              'description' => TRUE,
148
          ),
149
      )
150
    )
151
  )
152
);
153
define('CDM_NAME_RENDER_TEMPLATES', 'cdm-name-render-templates');
154
define('CDM_NAME_RENDER_TEMPLATES_DEFAULT', serialize(
155
    array (
156
        'list_of_taxa, acceptedFor, taxon_page_synonymy, typedesignations, taxon_page_title, polytomousKey, na' => array(
157
            'namePart' => array('#uri' => TRUE),
158
        ),
159
        'nar'=> array(
160
            'namePart' => array('#uri' => TRUE),
161
            'authorshipPart' => TRUE,
162
            'referencePart' => array('#uri' => TRUE),
163
            'microreferencePart' => TRUE,
164
        ),
165
        '#DEFAULT' => array(
166
            'namePart' => array('#uri' => TRUE),
167
            'authorshipPart' => TRUE,
168
            'referencePart' => array('#uri' => TRUE),
169
            'microreferencePart' => TRUE,
170
            'statusPart' => TRUE,
171
            'descriptionPart' => TRUE,
172
        ),
173
    )
174
));
175

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

    
216
/**
217
 * @todo document this function.
218
 */
219
function getGallerySettings($gallery_config_form_name) {
220
  $default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
221
  return variable_get($gallery_config_form_name, $default_values);
222
}
223

    
224
/**
225
 * Returns the string representation of the default tab.
226
 *
227
 * @param bool $returnTabIndex
228
 *   Defaults to FALSE, if set true this function will return the index number
229
 *   of the default tab. (used to supply default values to form elements).
230
 */
231
function get_default_taxon_tab($returnTabIndex = FALSE) {
232

    
233
  global $user;
234
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
235
  $user_tab_active = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
236
  $user_tab = 'cdm_dataportal_' . $user->uid . '_default_tab';
237
  // Get the user value if the used has chosen to overwrite the system settings.
238
  $user_tab_on = variable_get($user_tab_active, FALSE);
239
  if ($user_tab_on) {
240
    $user_value = variable_get($user_tab, 0);
241
    $index_value = $user_value;
242
    // Get the system value.
243
  }
244
  else {
245
    $system_value = variable_get('cdm_dataportal_default_tab', 0);
246
    $index_value = $system_value;
247
  }
248

    
249
  // Return the index value or the string representation.
250
  if ($returnTabIndex) {
251
    return $index_value;
252
  }
253
  else {
254
    return ($values[$index_value]);
255
  }
256

    
257
}
258

    
259
/**
260
 * @todo Please document this function.
261
 * @see http://drupal.org/node/1354
262
 */
263
function cdm_dataportal_menu_admin(&$items) {
264
  // Display section on admin/config page.
265
  $items['admin/config/cdm_dataportal'] = array(
266
    'title' => 'CDM Dataportal',
267
    'description' => 'Settings for the CDM DataPortal.',
268
    'position' => 'right',
269
    'weight' => 10,
270
    'page callback' => 'system_admin_menu_block_page',
271
    'access arguments' => array('cdm_settings_general'),
272
    'file' => 'system.admin.inc',
273
    'file path' => drupal_get_path('module', 'system'),
274
  );
275
  $items['admin/config/cdm_dataportal/settings'] = array(
276
    'title' => 'Settings',
277
    'description' => 'Settings for the CDM DataPortal.',
278
    'weight' => 0,
279
    'page callback' => 'drupal_get_form',
280
    'page arguments' => array('cdm_settings_general'),
281
    'access arguments' => array('administer cdm_dataportal'),
282
    'type' => MENU_NORMAL_ITEM,
283
  );
284
  $items['admin/config/cdm_dataportal/settings/general'] = array(
285
    'title' => 'General',
286
    'description' => 'General',
287
    'weight' => 0,
288
    'page callback' => 'drupal_get_form',
289
    'page arguments' => array('cdm_settings_general'),
290
    'access arguments' => array('administer cdm_dataportal'),
291
    'type' => MENU_DEFAULT_LOCAL_TASK,
292
  );
293

    
294
  $items['admin/config/cdm_dataportal/settings/cachesite'] = array(
295
    'title' => 'Cache',
296
    'description' => 'Cache',
297
    'access arguments' => array('administer cdm_dataportal'),
298
    'page callback' => 'drupal_get_form',
299
    'page arguments' => array('cdm_settings_cache'),
300
    'weight' => 10,
301
    'type' => MENU_LOCAL_TASK,
302
  );
303

    
304
  $items['admin/config/cdm_dataportal/settings/geo'] = array(
305
    'title' => 'Geo & Map',
306
    'description' => 'Geo & Map',
307
    'access arguments' => array('administer cdm_dataportal'),
308
    'page callback' => 'drupal_get_form',
309
    'page arguments' => array('cdm_settings_geo'),
310
    'weight' => 1,
311
    'type' => MENU_LOCAL_TASK,
312
  );
313

    
314
  $items['admin/config/cdm_dataportal/settings/layout'] = array(
315
    'title' => 'Layout',
316
    'description' => 'Configure and adjust the layout of your DataPortal ',
317
    'access arguments' => array('administer cdm_dataportal'),
318
    'page callback' => 'drupal_get_form',
319
    'page arguments' => array('cdm_settings_layout'),
320
    'weight' => 2,
321
    'type' => MENU_LOCAL_TASK,
322
  );
323

    
324
  $items['admin/config/cdm_dataportal/settings/layout/taxon'] = array(
325
    'title' => 'Taxon',
326
    'description' => 'Configure and adjust the layout of your DataPortal ',
327
    'access arguments' => array('administer cdm_dataportal'),
328
    'page callback' => 'drupal_get_form',
329
    'page arguments' => array('cdm_settings_layout_taxon'),
330
    'weight' => 1,
331
    'type' => MENU_LOCAL_TASK,
332
  );
333
  /*
334
  $items[] = array(
335
  'path' => 'admin/config/cdm_dataportal/layout/synonymy',
336
  'title' => t('Synonymy'),
337
  'description' => t('Configure and adjust the layout of your DataPortal '),
338
  'access' => user_access('administer cdm_dataportal'),
339
  'callback' => 'drupal_get_form',
340
  'callback arguments' => array('cdm_settings_layout_synonymy'),
341
  'weight' => 1,
342
  'type' => MENU_LOCAL_TASK,
343
  );
344

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

    
366
  $items['admin/config/cdm_dataportal/settings/layout/media'] = array(
367
    'title' => 'Media',
368
    'description' => 'Configure and adjust the layout of your DataPortal ',
369
    'access arguments' => array('administer cdm_dataportal'),
370
    'page callback' => 'drupal_get_form',
371
    'page arguments' => array('cdm_settings_layout_media'),
372
    'weight' => 3,
373
    'type' => MENU_LOCAL_TASK,
374
  );
375

    
376
}
377

    
378
/**
379
 * @todo document this function.
380
 */
381
function cdm_help_general_cache() {
382
  $form = array();
383
  $form['cache_help'] = array(
384
    '#type' => 'fieldset',
385
    '#title' => t('Help'),
386
    '#collapsible' => TRUE,
387
    '#collapsed' => TRUE,
388
  );
389
  $form['cache_help']['test'] = array('#value' => t('probando'));
390
  return drupal_render($form);
391
  $res = array();
392
  $res['default'] = drupal_render($help);
393
  return $res;
394
}
395

    
396
/**
397
 * Configures the settings form for the CDM-API module.
398
 *
399
 * @return array
400
 *   Drupal settings form.
401
 */
402
function cdm_settings_general() {
403

    
404
  $form['cdm_webservice'] = array(
405
    '#type' => 'fieldset',
406
    '#title' => t('CDM Server'),
407
    '#collapsible' => FALSE,
408
    '#collapsed' => FALSE,
409
    '#description' => t('The <em>CDM Server</em> exposes data stored in a
410
        CDM data base to the web via RESTful web services and thus is the source of the data
411
        to be displayed by a CDM DataPotal.'),
412
  );
413

    
414
  $form['cdm_webservice']['cdm_webservice_url'] = array(
415
    '#type' => 'textfield',
416
    '#title' => t('CDM web service URL') . ':',
417
    '#description' => t('This is the URL to the CDM-Server exposing your data
418
      e.g. <em>"http://localhost:8080/cichorieae/"</em> The URL <strong>must end
419
      with a slash</strong> character!'),
420
    '#default_value' => variable_get('cdm_webservice_url', NULL),
421
  );
422

    
423
  $form['cdm_webservice']['cdm_webservice_debug'] = array(
424
    '#type' => 'checkbox',
425
    '#title' => t('<b>Debug CDM Web Service</b>'),
426
    '#default_value' => variable_get('cdm_webservice_debug', 1),
427
    '#description' => t('The black web service debug box will appear at the top
428
      of each page. When clicked it toggles open and provides a list of all HTTP
429
      requests which have been made while building of this page.<br />
430
      <strong>Note:</strong> this is a feature dedicated to developers. It will
431
      only be visible when logged in and if the user has suffucicient rights to
432
      see this debug box.'),
433
  );
434

    
435
  $form['cdm_webservice']['freetext_index'] = array(
436
    '#type' => 'fieldset',
437
    '#title' => t('Freetext index'),
438
    '#collapsible' => FALSE,
439
    '#collapsed' => FALSE,
440
  );
441

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

    
454
  $frontentURL = urlencode(variable_get('cdm_webservice_url', ''));
455
  $trigger_link_options = array(
456
    'attributes' => array(
457
      'class' => 'index-trigger',
458
    ),
459
  );
460
  $form['cdm_webservice']['freetext_index']['operations'] = array(
461
    '#markup' => "<div>" . t('Operations: !url1 !url2', array(
462
        '!url1' => l(t("Purge"), cdm_compose_url(CDM_WS_MANAGE_PURGE, NULL, 'frontendBaseUrl=' . $frontentURL), $trigger_link_options),
463
        '!url2' => l(t("Reindex"), cdm_compose_url(CDM_WS_MANAGE_REINDEX, NULL, 'frontendBaseUrl=' . $frontentURL), $trigger_link_options),
464
      ))
465
    . '<div id="index-progress"></div></div>',
466
  );
467
  _add_js_cdm_ws_progressbar(".index-trigger", "#index-progress");
468

    
469
  $form['cdm_webservice']['proxy'] = array(
470
    '#type' => 'fieldset',
471
    '#title' => t('Proxy'),
472
    '#collapsible' => TRUE,
473
    '#collapsed' => TRUE,
474
  );
475

    
476
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_url'] = array(
477
    '#type' => 'textfield',
478
    '#title' => t('Proxy URL') . ':',
479
    '#description' => t('If this proxy url is set the cdm api tries
480
    to connect the web service over the given proxy server.
481
    Otherwise proxy usage is deactivated.'),
482
    '#default_value' => variable_get('cdm_webservice_proxy_url', FALSE),
483
  );
484

    
485
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_port'] = array(
486
    '#type' => 'textfield',
487
    '#title' => t('Proxy port') . ':',
488
    '#default_value' => variable_get('cdm_webservice_proxy_port', '80'),
489
  );
490

    
491
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_usr'] = array(
492
    '#type' => 'textfield',
493
    '#title' => t('Login') . ':',
494
    '#default_value' => variable_get('cdm_webservice_proxy_usr', FALSE),
495
  );
496

    
497
  $form['cdm_webservice']['proxy']['cdm_webservice_proxy_pwd'] = array(
498
    '#type' => 'textfield',
499
    '#title' => t('Password') . ':',
500
    '#default_value' => variable_get('cdm_webservice_proxy_pwd', FALSE),
501
  );
502

    
503
  // TODO: settings are still incomplete, compare with
504
  // trunk/dataportal/inc/config_default.php.inc.
505
  $form['taxon_tree'] = array(
506
    '#type' => 'fieldset',
507
    '#title' => t('Taxon Tree'),
508
    '#collapsible' => FALSE,
509
    '#collapsed' => TRUE,
510
    '#description' => t('<p>When you explore your collection, you can navigate
511
      it through a tree structure also called <em>Taxon Tree</em>.</p><p>To be
512
      able to navigate through your collection the
513
      <a href="http://drupal.org/handbook/blocks">drupal block</a>
514
      <em>CDM Taxon Tree</em> should be visible for users. Enable the block at
515
      <a href="./?q=admin/build/block">Administer&#45&#62Site building&#45&#62Blocks
516
      </a></p>'),
517
  );
518

    
519
  $form['taxon_tree'][CDM_TAXONOMICTREE_UUID] = array(
520
    '#type' => 'select',
521
    '#title' => t('Available classifications') . ':',
522
    '#default_value' => variable_get(CDM_TAXONOMICTREE_UUID, FALSE),
523
    '#options' => cdm_get_taxontrees_as_options(),
524
    '#description' => t('Select the default taxa classification for your
525
      <em>taxon tree</em>, the other classifications will be also available but
526
      with a manual user change.'),
527
  );
528

    
529
  $form['taxon_tree']['taxontree_ranklimit'] = array(
530
    '#type' => 'select',
531
    '#title' => t('Rank of highest displayed taxon') . ':',
532
     // Before DEFAULT_TAXONTREE_RANKLIMIT_UUID.
533
    '#default_value' => variable_get('taxontree_ranklimit', DEFAULT_TAXONTREE_RANKLIMIT),
534
    '#options' => cdm_rankVocabulary_as_option(),
535
    '#description' => t('This is the rank of the highest displayed taxon in the
536
      <em>taxon tree</em>. You can select here which rank should be at the top
537
      level of the tree structure.'),
538
  );
539

    
540
  $form['aggregation'] = array(
541
      '#type' => 'fieldset',
542
      '#title' => t('Aggregation of data'),
543
      '#collapsible' => FALSE,
544
      '#description' => t("This section covers the different aspects of aggregating information.
545
          <p>
546
          <strong>NOTICE:</strong> Aggregation may slow down your data portal, so you may want to sensibly apply these setting and you may also
547
          whant to make use of the caching capabilities of the dataportal.</p>"),
548

    
549
  );
550

    
551
  $form['aggregation']['media_aggregation'] = array(
552
      '#type' => 'fieldset',
553
      '#title' => t('Media aggregation'),
554
      '#collapsible' => FALSE,
555
      '#collapsed' => TRUE,
556
      '#description' => t("The media aggregation is also affected by the settigs in \"<strong>Aggregation via taxon relationsships</strong>\" below."),
557

    
558
  );
559
  $form['aggregation']['media_aggregation']['cdm_images_include_children'] = array(
560
      '#type' => 'select',
561
      '#title' => t('Aggregation of taxon pictures') . ':',
562
      '#default_value' => variable_get('cdm_images_include_children', FALSE),
563
      '#options' => array(
564
          0 => "Show only pictures of the current taxon",
565
          1 => "Include pictures of taxonomic children",
566
      ),
567
      '#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."),
568
  );
569

    
570
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS] = array(
571
      '#type' => 'fieldset',
572
      '#attributes' => array('class'=>array('clearfix')),
573
      '#title' => t('Aggregation via taxon relationsships'),
574
      '#collapsible' => TRUE,
575
      '#collapsed' => TRUE,
576
      '#tree' => TRUE,
577
      '#description' => t('Information on taxa will be aggregated along the below chosen
578
          taxon relation ships. This will affect images and occurrences (specimens).
579
          Taxon relation ships are directed and point form one taxon to another. The taxon
580
          relationships to be taken into accunt can therefore configured for the direct direction
581
          and for the inverse.'),
582
  );
583

    
584
  $taxonRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_TAXON_RELATIONSHIP_TYPE, '_cdm_relationship_type_term_label_callback');
585
  $aggregate_by_taxon_relationships = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
586

    
587
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS]['direct'] = array(
588
      '#type' => 'checkboxes',
589
      '#title' => t('Direct'),
590
      '#options' => $taxonRelationshipTypeOptions,
591
      '#default_value' => $aggregate_by_taxon_relationships['direct'],
592
  );
593
  $form['aggregation']['aggregate_by_taxon_relationships'][CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS]['invers'] = array(
594
      '#type' => 'checkboxes',
595
      '#title' => t('Invers'),
596
      '#options' => $taxonRelationshipTypeOptions,
597
      '#default_value' => $aggregate_by_taxon_relationships['invers'],
598
  );
599

    
600
  // Comment @WA: D7 form api does not support reset buttons,
601
  // so to mimic the D5 reset button we add one like this.
602
  $form['actions']['reset'] = array(
603
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
604
    '#weight' => 1000,
605
  );
606

    
607
  return system_settings_form($form);
608
}
609

    
610

    
611
/**
612
 * LAYOUT settings
613
 * @return unknown_type
614
 *   todo
615
 */
616
function cdm_settings_layout() {
617

    
618
  $form = array();
619

    
620
  $form['about'] = array(
621
    '#markup' => '<h4>' . t('Portal Layout') . '</h4><p>' . t('This settings contains the general configurations
622
      layout. If you want to configure the specific sites layout visit the
623
      respective configuration site for taxon, search or media.') . '</p>',
624
  );
625

    
626
  // ---- footnotes --- //
627
  $form['footnotes'] = array(
628
    '#type' => 'fieldset',
629
    '#title' => t('Footnotes'),
630
    '#collapsible' => FALSE,
631
    '#collapsed' => FALSE,
632
    '#description' => t('Taxa data such authors, synonyms names, descriptions,
633
      media or distribution areas may have annotations or footnotes. When the
634
      footnotes are enabled they will be visible (if they exist).'),
635
  );
636

    
637
  $form['footnotes']['cdm_dataportal_all_footnotes'] = array(
638
    '#type' => 'checkbox',
639
    '#title' => t('Do not show footnotes'),
640
    '#default_value' => variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES),
641
    '#description' => t('Check this if you do not want to show any footnotes'),
642
  );
643

    
644
  $form['footnotes']['cdm_dataportal_annotations_footnotes'] = array(
645
    '#type' => 'checkbox',
646
    '#title' => t('Do not show annotations footnotes'),
647
    '#default_value' => variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ANNOTATIONS_FOOTNOTES),
648
    '#description' => t('Check this if you do not want to show annotation footnotes'),
649
  );
650

    
651
  $annotationTypeOptions = cdm_Vocabulary_as_option(UUID_ANNOTATION_TYPE);
652
  // Additional option for the NULL case.
653
  $annotationTypeOptions['NULL_VALUE'] = t('untyped');
654
  $form['footnotes']['annotations_types_as_footnotes'] = array(
655
    '#type' => 'checkboxes',
656
    '#title' => t('Annotation types as footnotes'),
657
    '#description' => t("Only annotations of the selected type will be displayed
658
       as footnotes. You may want to turn 'technical annotations' off."),
659
    '#options' => $annotationTypeOptions,
660
  );
661
  $annotationsTypesAsFootnotes = variable_get('annotations_types_as_footnotes', unserialize(ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT));
662
  if (!empty($annotationsTypesAsFootnotes)) {
663
    $form['footnotes']['annotations_types_as_footnotes']['#default_value'] = $annotationsTypesAsFootnotes;
664
  }
665

    
666
  // --- Advanced Search --- //
667
  $form['asearch'] = array(
668
      '#type' => 'fieldset',
669
      '#title' => t('Advanced search'),
670
      '#collapsible' => FALSE,
671
      '#collapsed' => FALSE,
672
  );
673
  $form['asearch']['cdm_dataportal_show_advanced_search'] = array(
674
      '#type' => 'checkbox',
675
      '#title' => t('Show advanced search link'),
676
      '#default_value' => variable_get('cdm_dataportal_show_advanced_search', 1),
677
      '#description' => t('Check this box if the link to advanced search should be show below the search box.'),
678
  );
679

    
680
  // ---- Taxon Name Rendering --- //
681
  $form['taxon_name'] = array(
682
      '#type' => 'fieldset',
683
      '#title' => t('Taxon name display'),
684
      '#collapsible' => TRUE,
685
      '#collapsed' => TRUE,
686
      '#description' => t('The display of taxon names is configured by two parts.'
687
          . 'The <srong>name render templates</strong> define the parts of the name to be displayed in the different areas of the data portal pages.'
688
          . 'The name parts are defined in the <stong>part definitions</strong>'),
689
  );
690

    
691
  drupal_add_js(
692
      'jQuery(document).ready( function( ) {
693
         // pretty print json
694
         var json_text = jQuery(\'#edit-' . CDM_PART_DEFINITIONS . '\').val();
695
         var obj = JSON.parse(json_text);
696
         jQuery(\'#edit-' . CDM_PART_DEFINITIONS . '\').val(JSON.stringify(obj, undefined, 2));
697

    
698
         json_text = jQuery(\'#edit-' . CDM_NAME_RENDER_TEMPLATES . '\').val();
699
         obj = JSON.parse(json_text);
700
         jQuery(\'#edit-' . CDM_NAME_RENDER_TEMPLATES . '\').val(JSON.stringify(obj, undefined, 2));
701
       });'
702
      ,array(
703
          'type' => 'inline',
704
          'scope' => 'footer'
705
      )
706
  );
707

    
708
  //TODO remove after all portals are using the settings
709
  $default_part_definitions = theme('get_partDefinition', array());
710
  if(!is_array($default_part_definitions)) {
711
    $default_part_definitions = unserialize(CDM_PART_DEFINITIONS_DEFAULT);
712
  }
713

    
714
  $form['taxon_name'][CDM_PART_DEFINITIONS] = array(
715
      '#type' => 'textarea',
716
      '#title' => t('Part definitions'),
717
      '#element_validate' => array('form_element_validate_json'),
718
      '#default_value' =>  json_encode(variable_get(CDM_PART_DEFINITIONS, $default_part_definitions)),
719
      '#description' => '
720
          <p>
721
          (Clearing the text area will reset it to the default)
722
          </p>
723
          <p>
724
           The part definitions define the specific parts of which a rendered taxon name plus additional information will consist.
725
          </p>
726
          <p>
727
           A full taxon name plus additional information can consist of the following elements:
728
          <ul>
729
             <li>name: the taxon name inclugin rank nbut without author</li>
730
             <li>authorTeam:  The authors of a reference, also used in taxon names</li>
731
             <li>authors:  The authors of a reference, also used in taxon names</li>
732
             <li>reference: the nomenclatural reference,</li>
733
             <li>microreference:  Volume, page number etc.</li>
734
             <li>status:  The nomenclatural status of a name</li>
735
             <li>description: name descriptions like protologues etc ...</li>
736
          </ul>
737
          </p>
738
          <p>
739
           These elements are combined in the part definitions array to from the specific parts to be rendered.
740
           (The taxon name "Lapsana communis L., Sp. Pl.: 811. 1753" shall be an example in the following)
741
           Usually the following parts are formed:
742
          <ul>
743
            <li>namePart: the name and rank (in example: "Lapsana communis")</li>
744
            <li>authorshipPart: the author (in example: "L.")</li>
745
            <li>nameAuthorPart: the combination of name and author part (in example: "Lapsana communis L.").</li>
746
               This is useful for zoological names where the authorshipPart belongs to the name and both should</li>
747
               be combined when a link to the taxon is rendered.</li>
748
            <li>referencePart: the nomencaltural reference (in example: "Sp. Pl. 1753")</li>
749
            <li>microreferencePart: usually the page number (in example ": 811.")</li>
750
            <li>statusPart: the nomenclatorical status</li>
751
            <li>descriptionPart: name descriptions like protologues etc ...</li>
752
          </ul>
753
          </p>
754
          <p>
755
           Each set of parts is dedicated to render a specific TaxonName type, the type names are used as keys for the
756
           specific parts part definitions:
757
          <ul>
758
            <li>BotanicalName</li>
759
            <li>ZoologicalName</li>
760
            <li>#DEFAULT: covers ViralNames and other NonViralNames
761
          </ul>
762
           An example:
763
          <pre>
764
           {
765
            "ZoologicalName": {
766
              "namePart": {
767
                "name": true
768
              },
769
              "referencePart": {
770
                "authorTeam": true
771
              },
772
              "microreferencePart": {
773
                "microreference": true
774
              },
775
              "statusPart": {
776
                "status": true
777
              },
778
              "descriptionPart": {
779
                "description": true
780
              }
781
            },
782
            "BotanicalName": {
783
              "namePart": {
784
                "name": true,
785
                "authors": true
786
              },
787
              "referencePart": {
788
                "reference": true,
789
                "microreference": true
790
              },
791
              "statusPart": {
792
                "status": true
793
              },
794
              "descriptionPart": {
795
                "description": true
796
              }
797
            }
798
          }
799
           </pre>',
800
  );
801

    
802
  //TODO remove after all portals are using the settings
803
  $default_render_templates = theme('get_nameRenderTemplate', array());
804
  if(!is_array($default_render_templates)) {
805
    $default_render_templates = unserialize(CDM_NAME_RENDER_TEMPLATES_DEFAULT);
806
  }
807

    
808
  $form['taxon_name'][CDM_NAME_RENDER_TEMPLATES] = array(
809
      '#type' => 'textarea',
810
      '#title' => t('Name render templates'),
811
      '#element_validate' => array('form_element_validate_json'),
812
      '#default_value' =>  json_encode(variable_get(CDM_NAME_RENDER_TEMPLATES, $default_render_templates)),
813
      '#description' => '
814
          <p>
815
          (Clearing the text area will reset it to the default)
816
          </p>
817
          <p>
818
          The render templates array contains one or more name render templates to be used within the page areas identified by the
819
          render path. The render path is used as key of the array sub subelements whereas the name render template array is set as value.
820
          The following render Path keys are curretly recognized:
821
          <ul>
822
          	<li>list_of_taxa:</li>
823
            <li>acceptedFor:</li>
824
            <li>taxon_page_synonymy</li>
825
            <li>typedesignations</li>
826
            <li>taxon_page_title</li>
827
            <li>polytomousKey</li>
828
            <li>na: name + authorship</li>
829
            <li>nar:name + authorship + reference</li>
830
            <li>#DEFAULT</li>
831
          </ul>
832
          A single render template can be used for multiple render paths. In this case the according key of the render templates
833
          array element should be a comma separated list of render paths, without any whitespace!.
834
          </p>
835
          <p>
836
          A render template is an associative array. The keys of this array are referring to the keys as defined in the part
837
          definitions array. See <a href="#edit-cdm-part-definitions">Part definitions</a> above for more information.
838
          <p>
839
          The value of the render template element must be set to TRUE in order to let this part being rendered.
840
          The namePart, nameAuthorPart and referencePart can also hold an associative array with a single
841
          element: array(\'#uri\' => TRUE). The value of the #uri element will be replaced by the according
842
          links if the paramters $nameLink or $refenceLink are given to the name render function
843
          (this is hard coded and cannot be configured here).',
844
  );
845

    
846
  // @WA: D7 form api does not support reset buttons,
847
  // so to mimic the D5 reset button we add one like this.
848
  $form['actions']['reset'] = array(
849
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
850
    '#weight' => 1000,
851
  );
852

    
853
  $form['#submit'] = array('submit_json_as_php_array');
854
  // #json_elements especially defined for submit_json_as_php_array()
855
  $form['#json_elements'] = array(CDM_NAME_RENDER_TEMPLATES, CDM_PART_DEFINITIONS);
856
  return system_settings_form($form);
857
}
858

    
859

    
860
/**
861
 * @todo Please document this function.
862
 * @see http://drupal.org/node/1354
863
 */
864
function cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description = '') {
865
  $form[$form_name] = array(
866
    '#type' => 'fieldset',
867
    '#title' => t($form_title),
868
    '#collapsible' => TRUE,
869
    '#collapsed' => $collapsed,
870
    '#tree' => TRUE,
871
    '#description' => t($form_description),
872
  );
873

    
874
  $default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
875
  $gallery_settings = variable_get($form_name, $default_values);
876
  // $test = variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE);
877
  if ($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME) {
878
    /*
879
    TODO: why cdm_dataportal_search_items_on_page does not save the value on $test???
880
    $form[$form_name]['cdm_dataportal_search_items_on_page'] = array(
881
    '#type' => 'textfield',
882
    '#title' => t('Search Page Size'),
883
    '#default_value' => $test,
884
    '#description' => t('Number of Names to display per page in search results.')
885
    );
886
    */
887
    $form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
888
      '#type' => 'checkbox',
889
      '#title' => t('Show media thumbnails for accepted taxa'),
890
      '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
891
    );
892

    
893
    $form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
894
      '#type' => 'checkbox',
895
      '#title' => t('Show media thumbnails for synonyms'),
896
      '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
897
      '#description' => '',
898
    );
899
  }
900

    
901
  // $showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
902
  $form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
903
    '#type' => 'checkbox',
904
    '#title' => t('Show captions under thumbnails'),
905
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
906
    '#description' => '',
907
  );
908

    
909
  $form[$form_name]['cdm_dataportal_media_maxextend'] = array(
910
    '#type' => 'textfield',
911
    '#title' => t('Thumbnail size') . ':',
912
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
913
    '#description' => t('Select the size of each individual thumbnail.'),
914
  );
915

    
916
  if ($form_name != CDM_DATAPORTAL_MEDIA_GALLERY_NAME) {
917
    $form[$form_name]['cdm_dataportal_media_cols'] = array(
918
      '#type' => 'textfield',
919
      '#title' => t('Number of columns') . ':',
920
      '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
921
      '#description' => t('Group the thumbnails in columns: select how many
922
        columns the gallery should display.'),
923
    );
924
  }
925

    
926
  if ($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME) {
927
    $form[$form_name]['cdm_dataportal_media_maxRows'] = array(
928
      '#type' => 'textfield',
929
      '#title' => t('Maximum number of rows') . ':',
930
      '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
931
      '#description' => t('You can group the thumbnails in rows, select in how
932
        many rows should be the thumbnails grouped.<br/><strong>Note:</strong>
933
        If you want an unlimited number of rows please set to 0.'),
934
    );
935
  }
936

    
937
  return $form;
938
}
939

    
940
/**
941
 * @todo document this function.
942
 */
943
function cdm_settings_layout_taxon() {
944
  $collapsed = FALSE;
945
  $form = array();
946

    
947
  // --------- TABBED TAXON ------- //
948
  $form['taxon_tabs'] = array(
949
    '#type' => 'fieldset',
950
    '#title' => t('Taxon tabs'),
951
    '#collapsible' => TRUE,
952
    '#collapsed' => FALSE,
953
    '#description' => t('If tabbed taxon page is enabled the taxon profile will
954
      be splitted in four diferent tabs; General, Synonymy, Images and
955
      Specimens. If the taxon has no information for any of the tabs/sections
956
      such tab will be not displayed.'),
957
  );
958

    
959
  $form['taxon_tabs']['cdm_dataportal_taxonpage_tabs'] = array(
960
    '#type' => 'checkbox',
961
    '#title' => t('Tabbed taxon page'),
962
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
963
    '#description' => t('<p>If selected split the taxon page into individual
964
      tabs for description, images, synonymy and specimens. If not the taxon
965
      data is rendered as a long single page without tabs.</p>'),
966
  );
967

    
968
  $form['taxon_tabs']['cdm_taxonpage_tabs_visibility'] = array(
969
    '#type' => 'checkboxes',
970
    '#title' => t('Tabs visibility options') . ':',
971
    '#default_value' => variable_get('cdm_taxonpage_tabs_visibility', get_taxon_options_list()),
972
    '#options' => get_taxon_options_list(),
973
    '#description' => t('Enable or disable Tabs in the Tabbed page display'),
974
  );
975

    
976
  $form['taxon_tabs']['cdm_dataportal_default_tab'] = array(
977
    '#type' => 'select',
978
    '#title' => t('Default tab to display') . ':',
979
    '#default_value' => variable_get('cdm_dataportal_default_tab', 0),
980
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
981
    '#description' => t('<p>Select the default tab to display when visiting a
982
      taxon page. Only available if Tabbed Taxon Page is enable.</p>
983
      <strong>Note:</strong> After performing a search and clicking in any
984
      synonym, the taxon tab to be rendered will be the synonymy of the accepted
985
      taxon and not the above selected tab.'),
986
  );
987

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

    
997
  /* ======  TAXON_PROFILE ====== */
998
  $form['taxon_profile'] = array(
999
    '#type' => 'fieldset',
1000
    '#title' => t('Taxon profile (tab)'),
1001
    '#description' => t('<p>This section covers the settings related to the taxon
1002
      profile tab, also known as the <strong>"General"</strong> tab.</p>'),
1003
    '#collapsible' => TRUE,
1004
    '#collapsed' => TRUE,
1005
  );
1006

    
1007
  // ---- PROFILE PICTURE ----//
1008
  $form['taxon_profile']['picture'] = array(
1009
    '#type' => 'fieldset',
1010
    '#title' => t('Taxon profile picture'),
1011
    '#collapsible' => TRUE,
1012
    '#collapsed' => FALSE,
1013
    '#description' => t('This sections allows configuring the display of the so called taxon profile image which is displayed in the taxon profile tab.'),
1014
  );
1015

    
1016
  $form['taxon_profile']['picture']['cdm_dataportal_show_default_image'] = array(
1017
    '#type' => 'checkbox',
1018
    '#title' => t('Enable profil picture'),
1019
    '#default_value' => variable_get('cdm_dataportal_show_default_image', FALSE),
1020
    '#description' => t('Show the profil picture.'),
1021
  );
1022

    
1023
  $options = cdm_rankVocabulary_as_option();
1024
  array_unshift($options, '-- DISABLED --');
1025
  $form['taxon_profile']['picture']['image_hide_rank'] = array(
1026
    '#type' => 'select',
1027
    '#title' => t('Hide profile picture for higher ranks') . ':',
1028
    '#default_value' => variable_get('image_hide_rank', '0'),
1029
    '#options' => $options,
1030
    '#description' => t('The taxon profile picture will not be shown for taxa with rank higher that the selected.'),
1031
  );
1032

    
1033
  // -- MEDIA THUMBNAILS -- //
1034
  $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME;
1035
  $form_title = 'Taxon Profile Images';
1036
  $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>';
1037
  $form['taxon_profile'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description);
1038

    
1039
  // ---- FEATURE TREE ---- //
1040
  $form['taxon_profile']['feature_trees'] = array(
1041
    '#type' => 'fieldset',
1042
    '#title' => t('Features'),
1043
    '#collapsible' => TRUE,
1044
    '#collapsed' => FALSE,
1045
    '#description' => t("This section covers settings related to the taxon's
1046
      <em>Feature Tree</em>. The <em>feature tree</em> are the taxon's
1047
      features such as description, distribution, common names, etc. that Drupal
1048
      will render at the taxon profile page."),
1049
  );
1050
  $featureTrees = cdm_get_featureTrees_as_options(TRUE);
1051
  $form['taxon_profile']['feature_trees'][CDM_PROFILE_FEATURETREE_UUID] = array(
1052
    '#type' => 'radios',
1053
    '#title' => t('Taxon profile sections') . ':',
1054
    '#default_value' => variable_get(CDM_PROFILE_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE),
1055
    '#options' =>  $featureTrees['options'],
1056
    // Comment @WA: because #options are sanitized in D7, it would
1057
    // strip html like <fieldset>, so we put the fieldset in a suffix.
1058
    '#field_suffix' => $featureTrees['treeRepresentations'],
1059
    '#description' => t('Select the Feature Tree to be displayed at the taxon
1060
      profile. Click "Show Details" to see the Feature Tree elements.'
1061
    ),
1062
  );
1063
  $featureTrees = cdm_get_featureTrees_as_options();
1064
  $form['taxon_profile']['feature_trees'][CDM_DATAPORTAL_STRUCTURED_DESCRIPTION_FEATURETREE_UUID] = array(
1065
    '#type' => 'radios',
1066
    '#title' => t('Natural language representation of structured descriptions') . ':',
1067
    '#default_value' => variable_get(CDM_DATAPORTAL_STRUCTURED_DESCRIPTION_FEATURETREE_UUID, NULL),
1068
    '#options' => $featureTrees['options'],
1069
      // Comment @WA: because #options are sanitized in D7, it would
1070
      // strip html like <fieldset>, so we put the fieldset in a suffix.
1071
    '#field_suffix' => $featureTrees['treeRepresentations'],
1072
    '#description' => t('Taxon descriptions can be stored in a highly structured
1073
      form. The feature tree selected here will be used to generate textual
1074
      representation in natural language.'
1075
      // If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
1076
    ),
1077
  );
1078

    
1079
  // ---- LAYOUT PER FEATURE ---- //
1080
  $feature_tree = get_profile_featureTree();
1081
  if (isset($feature_tree->root->children)) {
1082

    
1083
    $form_feature_list_layout = array(
1084
      '#title' => t('Taxon profile layout'),
1085
      '#collapsible' => TRUE,
1086
      '#collapsed' => FALSE,
1087
      '#type' => 'fieldset',
1088
      '#description' => t('Will be available in a future release.'),
1089
    );
1090

    
1091
    $feature_list_layout_settings_disabled = TRUE;
1092
    foreach ($feature_tree->root->children as $featureNode) {
1093

    
1094
      if (!$feature_list_layout_settings_disabled && isset($featureNode->feature)) {
1095
        // Must not exceed 45 characters !!!
1096
        $subform_id = LAYOUT_SETTING_PREFIX . $featureNode->feature->uuid;
1097

    
1098
        $settings = mixed_variable_get($subform_id, FEATURE_TREE_LAYOUT_DEFAULTS);
1099
        $systemDefaults = unserialize(FEATURE_TREE_LAYOUT_DEFAULTS);
1100

    
1101
        $form_feature_list_layout[$subform_id] = array(
1102
          '#tree' => TRUE,
1103
          '#title' => 'test' . $featureNode->feature->representation_L10n,
1104
          '#collapsible' => FALSE,
1105
          '#collapsed' => FALSE,
1106
          '#type' => 'fieldset',
1107
          '#description' => t(''),
1108
        );
1109

    
1110
        $form_feature_list_layout[$subform_id]['enabled'] = array(
1111
          '#type' => 'checkbox',
1112
          '#title' => t('Enable'),
1113
          '#default_value' => $settings['enabled'],
1114
          '#description' => t('Enable user defined layout for this feature'),
1115
        );
1116

    
1117
        $form_feature_list_layout[$subform_id]['enclosingTag'] = array(
1118
          '#type' => 'textfield',
1119
          '#title' => t('Enclosing tag'),
1120
          '#disabled' => !$settings['enabled'],
1121
          '#default_value' => $settings['enclosingTag'],
1122
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['enclosingTag'] . "</code>'",
1123
        );
1124

    
1125
        $form_feature_list_layout[$subform_id]['entryEnclosingTag'] = array(
1126
          '#type' => 'textfield',
1127
          '#title' => t('Entry enclosing tag'),
1128
          '#disabled' => !$settings['enabled'],
1129
          '#default_value' => $settings['entryEnclosingTag'],
1130
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['entryEnclosingTag'] . "</code>'",
1131
        );
1132

    
1133
        $form_feature_list_layout[$subform_id]['glue'] = array(
1134
          '#type' => 'textfield',
1135
          '#title' => t('Glue'),
1136
          '#disabled' => !$settings['enabled'],
1137
          '#default_value' => $settings['glue'],
1138
          '#description' => t('Default is: ') . "'<code>" . $systemDefaults['glue'] . "</code>'",
1139
        );
1140

    
1141
      }
1142

    
1143
      $form['taxon_profile']['feature_list_layout'] = $form_feature_list_layout;
1144
    }
1145
  }
1146

    
1147
  // ---- DISTRIBUTION LAYOUT ---- //
1148
  $form['taxon_profile']['distribution_layout'] = array(
1149
    '#title' => t('Distribution'),
1150
    '#collapsible' => TRUE,
1151
    '#collapsed' => FALSE,
1152
    '#type' => 'fieldset',
1153
    '#description' => t('Select if you want to sort or not the distribution text
1154
      located below the distribution map.'),
1155
  );
1156

    
1157
  $form['taxon_profile']['distribution_layout']['distribution_sort'] = array(
1158
    '#type' => 'radios',
1159
    '#title' => t('Sort') . ':',
1160
    '#default_value' => variable_get('distribution_sort', 'NO_SORT'),
1161
    '#options' => array(
1162
      'NO_SORT' => t('Standard (No sort)'),
1163
      'HIDE_TDWG2' => t('Sorted without TDWG Level 2'),
1164
    ),
1165
  );
1166

    
1167
  $form['taxon_profile']['distribution_layout'][DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP] = array(
1168
    '#type' => 'checkbox',
1169
    '#title' => t('Show TextData elements on top of the map'),
1170
    '#default_value' => variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0),
1171
    '#description' => t('Check this if you want to appear all <code>TextData</code>
1172
      elements on top of the map. Otherwise all <code>TextData</code>
1173
      distribution elements will be listed below the other area elements.
1174
      This option is useful if you need to have descriptive texts for each
1175
      distribution map.'),
1176
  );
1177

    
1178

    
1179
  /* ====== SYNONYMY ====== */
1180
  $form['taxon_synonymy'] = array(
1181
    '#type' => 'fieldset',
1182
    '#title' => t('Taxon synonymy (tab)'),
1183
    '#collapsible' => TRUE,
1184
    '#collapsed' => TRUE,
1185
    '#description' => t('This section covers the settings related to the taxon
1186
      <strong>synonymy</strong> tab.####'),
1187
  );
1188

    
1189
  $form['taxon_synonymy']['cdm_dataportal_nomref_in_title'] = array(
1190
    '#type' => 'checkbox',
1191
    '#title' => t('Show accepted taxon on top of the synonymy'),
1192
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
1193
    '#description' => t('If checked, the first homotypic taxon is a repetition
1194
      of the accepted taxon most likely with the full nomenclatural reference
1195
      (depending on the currently chosen theme).'),
1196
  );
1197

    
1198
  $form['taxon_synonymy']['cdm_dataportal_display_is_accepted_for'] = array(
1199
    '#type' => 'checkbox',
1200
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when
1201
      coming from a synonym link.'),
1202
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
1203
    '#description' => t('Check this if after doing a search and clicking on a
1204
      synonym you want to see the "accept of" text for the accepted synonym.'),
1205
  );
1206

    
1207
  $nameRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_NAME_RELATIONSHIP_TYPE);
1208
  $form['taxon_synonymy']['name_relationships']['name_relationships_to_show'] = array(
1209
    '#type' => 'checkboxes',
1210
    '#title' => t('Display name relationships') . ':',
1211
    '#default_value' => variable_get('name_relationships_to_show', 0),
1212
    '#options' => $nameRelationshipTypeOptions,
1213
    '#description' => t('Select the name relationships you want to show for the
1214
      accepted taxa.'),
1215
  );
1216

    
1217
  $form['taxon_synonymy'][CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS] = array(
1218
    '#type' => 'checkbox',
1219
    '#title' => t('Show taxon relations ships of accepted taxon'),
1220
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
1221
    '#description' => t('If this option is enabled the synonymy will show the
1222
      below selected taxon relationships of accepted taxa.'),
1223
  );
1224

    
1225
  $taxonRelationshipTypeOptions = cdm_Vocabulary_as_option(UUID_TAXON_RELATIONSHIP_TYPE, '_cdm_relationship_type_term_label_callback');
1226
  $form['taxon_synonymy'][CDM_TAXON_RELATIONSHIP_TYPES] = array(
1227
    '#type' => 'checkboxes',
1228
    '#title' => t('Taxon relationship types') . ':',
1229
    '#description' => t('Only taxon relationships of the selected type will be
1230
      displayed'),
1231
    '#options' => $taxonRelationshipTypeOptions,
1232
    '#default_value' => variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT)),
1233
    '#disabled' => !variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
1234
  );
1235

    
1236
  // ====== SPECIMENS ====== //
1237
  $form['taxon_specimens'] = array(
1238
    '#type' => 'fieldset',
1239
    '#title' => t('Taxon specimens (tab)'),
1240
    '#collapsible' => TRUE,
1241
    '#collapsed' => TRUE,
1242
    '#description' => t('This section covers the settings related to the taxon
1243
      <strong>specimens</strong> tab.'),
1244
  );
1245

    
1246
  $featureTrees = cdm_get_featureTrees_as_options(TRUE);
1247
  $form['taxon_specimens']['feature_trees'][CDM_OCCURRENCE_FEATURETREE_UUID] = array(
1248
      '#type' => 'radios',
1249
      '#title' => t('Specimen description feature tree') . ':',
1250
      '#default_value' => variable_get(CDM_OCCURRENCE_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE),
1251
      '#options' =>  $featureTrees['options'],
1252
      // Comment @WA: because #options are sanitized in D7, it would
1253
      // strip html like <fieldset>, so we put the fieldset in a suffix.
1254
      '#field_suffix' => $featureTrees['treeRepresentations'],
1255
      '#description' => t('Select the feature tree to be used for displaying specimen descriptions. Click "Show Details" to see the Feature Tree elements.'
1256
      ),
1257
  );
1258

    
1259
  $form_name = CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME;
1260
  $form_title = t('Specimen media');
1261
  $form_description = t('Specimens may have media which is displayed at the
1262
     Specimen tab/section as a gallery. It is possible to configure the
1263
     thumbnails gallery here, however for configuring how a single media should
1264
     be displayed please go to !url.</p>',
1265
     array(
1266
       '!url' => l(t('Layout -> Media'), 'admin/config/cdm_dataportal/settings/layout/media'),
1267
     ));
1268
  $form['taxon_specimens'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, FALSE, $form_description);
1269

    
1270
  // --- MEDIA GALLERY ---- //
1271
  $form_name = CDM_DATAPORTAL_TAXON_MEDIA_GALLERY_NAME_TAB;
1272
  $form_title = 'Media gallery (tab)';
1273
  $form_description = '<p>This section covers the settings related to the taxon <strong>media</strong> tab.
1274
   Taxa may have media (usually images) and they are as thumbnails displayed. It is possible to configure
1275
   the thumbnails gallery here, however for configuring how a single media should be displayed please go to
1276
   <a href="./?q=admin/settings/cdm_dataportal/layout/media">Layout -&gt; Media</a></p>
1277
   <p><strong>Note:</strong> These settings are only taken into account when the standard
1278
   gallery viewer is selected at <a href="./?q=admin/settings/cdm_dataportal/layout/media">Layout -&gt; Media</a>.</p>';
1279
  $form['taxon_media'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, TRUE, $form_description);
1280

    
1281
  // Comment @WA: D7 form api does not support reset buttons,
1282
  // so to mimic the D5 reset button we add one like this.
1283
  $form['actions']['reset'] = array(
1284
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1285
    '#weight' => 1000,
1286
  );
1287
  return system_settings_form($form);
1288
}
1289

    
1290
/**
1291
 * @todo document this function.
1292
 */
1293
function cdm_settings_layout_search() {
1294

    
1295
  $form = array();
1296

    
1297
  $form['search_settings'] = array(
1298
    '#type' => 'fieldset',
1299
    '#title' => t('Taxa Search'),
1300
    '#collapsible' => FALSE,
1301
    '#collapsed' => FALSE,
1302
    '#description' => t('<p>The data portal allows the users to perform searchs.</p><p>To perform searchs
1303
         the block <em>CDM Taxon Search</em> should be enabled and visible for users
1304
         where they can write the text to be searched. You can find Drupal block configuration
1305
         site at <a href="./?q=admin/build/block">Administer&#45&#62Site building&#45&#62Blocks</a></p> '),
1306
  );
1307

    
1308
  $form['search_settings']['simple_search_ignore_classification'] = array(
1309
      '#type' => 'checkbox',
1310
      '#title' => t('Ignore the chosen classification in simple search'),
1311
      '#default_value' => variable_get('simple_search_ignore_classification', TRUE),
1312
      '#description' => t('The simple search, which can be executed via the search block,
1313
          will by default search on all classifications. Remove the tick if you want your
1314
          portal to search on the classification selected in the classification browser
1315
          selector.'),
1316
  );
1317

    
1318
  $form['search_settings']['cdm_dataportal_search_items_on_page'] = array(
1319
    '#type' => 'textfield',
1320
    '#title' => t('Results per page') . ':',
1321
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
1322
    '#description' => t('Number of results to display per page.'),
1323
  );
1324

    
1325
  $form['search_settings']['cdm_search_taxa'] = array(
1326
    '#type' => 'checkbox',
1327
    '#title' => t('Sets the default value of <em>Search for accepted taxa</em> in the advanced search form.'),
1328
    '#default_value' => variable_get('cdm_search_taxa', 1),
1329
    '#description' => t('<p>If checked the <em>Search for accepted taxa </em> option will be enabled in the advanced form.</p>'),
1330
  );
1331

    
1332
  $form['search_settings']['cdm_search_synonyms'] = array(
1333
    '#type' => 'checkbox',
1334
    '#title' => t('Sets the default value of <em>Search for synonyms</em> in the advanced search form.'),
1335
    '#default_value' => variable_get('cdm_search_synonyms', 1),
1336
    '#description' => t('<p>If checked the <em>Search for synonyms</em> option will be enabled in the advanced form.</p>'),
1337
  );
1338

    
1339
  $form['search_settings']['cdm_search_common_names'] = array(
1340
    '#type' => 'checkbox',
1341
    '#title' => t('Sets the default value of <em>Search for common names</em> in the advanced search form.'),
1342
    '#default_value' => variable_get('cdm_search_common_names', 0),
1343
    '#description' => t('<p>If checked the <em>Search for common names</em> option will be enabled in the advanced form.</p>'),
1344
  );
1345

    
1346
  $form['search_settings']['cdm_search_use_default_values'] = array(
1347
    '#type' => 'checkbox',
1348
    '#title' => t('Sets use of default values in the advanced search form.'),
1349
    '#default_value' => variable_get('cdm_search_use_default_values', 1),
1350
    '#description' => t('<p>If checked the defqult values set abovewill be used for the search.</p>'),
1351
  );
1352

    
1353

    
1354
  // --- SEARCH TAXA GALLERY ---- //
1355
  $items = variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE);
1356
  $collapsed = FALSE;
1357
  $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_NAME;
1358
  $form_title = 'Taxa Search thumbnails';
1359
  $form_description = 'Search results may show thumbnails. ';
1360
  $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description);
1361

    
1362
  // Comment @WA: D7 form api does not support reset buttons,
1363
  // so to mimic the D5 reset button we add one like this.
1364
  $form['actions']['reset'] = array(
1365
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1366
    '#weight' => 1000,
1367
  );
1368
  return system_settings_form($form);
1369
}
1370

    
1371
/**
1372
 * @todo document this function.
1373
 */
1374
function cdm_settings_layout_media() {
1375

    
1376
  $form = array();
1377

    
1378
  $form['media_settings'] = array(
1379
    '#type' => 'fieldset',
1380
    '#title' => t('Media display settings'),
1381
    '#collapsible' => TRUE,
1382
    '#collapsed' => FALSE,
1383
    '#description' => t('This section covers the settings related to the taxa media, that is how each single media should be displayed.'),
1384
  );
1385

    
1386
  $form['media_settings']['image_gallery_viewer'] = array(
1387
    '#type' => 'select',
1388
    '#title' => t('Image viewer') . ':',
1389
    '#default_value' => variable_get('image_gallery_viewer', 'default'),
1390
    '#options' => array(
1391
      'default' => t('Standard image viewer'),
1392
      'fsi' => t('FSI viewer (requires FSI server!)'),
1393
    ),
1394
  );
1395

    
1396
  // --- MEDIA GALLERY ---- //
1397
  $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_NAME;
1398
  $form_title = 'Standard viewer';
1399
  $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>';
1400
  // $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed);
1401
  // Comment @WA: $collapsed is not defined.
1402
  // $form['media_settings'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, $collapsed, $form_description);
1403
  $form['media_settings'][] = cdm_dataportal_create_gallery_settings_form($form_name, $form_title, FALSE, $form_description);
1404

    
1405
  // @WA: D7 form api does not support reset buttons,
1406
  // so to mimic the D5 reset button we add one like this.
1407
  $form['actions']['reset'] = array(
1408
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1409
    '#weight' => 1000,
1410
  );
1411
  return system_settings_form($form);
1412
}
1413

    
1414
/**
1415
 * GEOSERVICE and Map settings.
1416
 */
1417
function cdm_settings_geo($form, &$form_state) {
1418

    
1419
  $form = array();
1420

    
1421
  /*
1422
  GEO SERVER
1423
  */
1424
  $form['edit_map_server'] = array(
1425
    '#type' => 'fieldset',
1426
    '#tree' => true,
1427
    '#title' => t('EDIT map service'),
1428
    '#collapsible' => TRUE,
1429
    '#collapsed' => TRUE,
1430
    '#description' => t('Configuration and selection of your geo server.
1431
      The Geo Server is responsible for generating the maps.'),
1432
  );
1433

    
1434
  $current_geoserver_settings = get_edit_map_service_settings();
1435

    
1436
  $form['edit_map_server']['base_uri'] = array(
1437
    '#type' => 'select',
1438
    '#title' => t('EDIT map service') . ':',
1439
    '#default_value' => $current_geoserver_settings['base_uri'],
1440
    '#options' => unserialize(EDIT_MAPSERVER_URI),
1441
    '#description' => t('Select the EDIT map server you want to use within your data portal.'),
1442
  );
1443
  $form['edit_map_server']['version'] = array(
1444
      '#type' => 'select',
1445
      '#title' => t('Version') . ':',
1446
      '#default_value' => $current_geoserver_settings['version'],
1447
      '#options' => unserialize(EDIT_MAPSERVER_VERSION),
1448
      '#description' => t('The version of the EDIT map services'),
1449
  );
1450

    
1451
  /*
1452
  MAP SETTINGS
1453
  */
1454
  $form['map_settings'] = array(
1455
    '#type' => 'fieldset',
1456
    '#title' => t('Maps settings'),
1457
    '#collapsible' => TRUE,
1458
    '#collapsed' => TRUE,
1459
    '#description' => t('General configuration for all map types.'),
1460
  );
1461

    
1462
  $form['map_settings']['cdm_dataportal_geoservice_display_width'] = array(
1463
    '#type' => 'textfield',
1464
    '#title' => t('Maps width') . ':',
1465
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
1466
    '#description' => t('Choose the width of your maps, the height will always
1467
      be the half of the width. A value of 500 means the size will be 500 pixels
1468
      width and 250 pixels height.'),
1469
  );
1470

    
1471
  $form['map_settings']['cdm_dataportal_geoservice_bounding_box'] = array(
1472
    '#type' => 'textfield',
1473
    '#title' => t('Fixed bounding box') . ':',
1474
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
1475
    '#description' => t('Define surrounding of area to be displayed in maps.
1476
      Use "-180,-90,180,90" for the whole world. Leave <strong>empty</strong>
1477
      to let the map <strong>automatically zoom</strong> to the distribution
1478
      area.'),
1479
  );
1480

    
1481
  $form['map_settings']['cdm_dataportal_geoservice_labels_on'] = array(
1482
    '#type' => 'checkbox',
1483
    '#title' => '<b>' . t('Display area labels') . '</b>',
1484
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
1485
    '#description' => t('Check this if you like area names to be displayed in the maps. DOES IT WORK???? '),
1486
  );
1487

    
1488
  $form['map_settings']['cdm_dataportal_geoservice_map_caption'] = array(
1489
    '#type' => 'textfield',
1490
    '#title' => t('Map caption') . ':',
1491
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
1492
    '#description' => t('Define a caption for the map.'),
1493
  );
1494

    
1495
  $form['map_settings']['cdm_dataportal_geoservice_distributionOpacity'] = array(
1496
    '#type' => 'textfield',
1497
    '#title' => t('Distribution layer opacity') . ':',
1498
    '#default_value' => variable_get('cdm_dataportal_geoservice_distributionOpacity', '0.5'),
1499
    '#description' => t('Valid values range from 0.0 to 1.0. Value 1.0 means the distributions (the countries or regions) will
1500
                           fully visible, while a value near to 0.0 will be not much visible.'),
1501
  );
1502

    
1503
  $form['map_settings']['cdm_dataportal_map_openlayers'] = array(
1504
    '#type' => 'radios',
1505
    '#title' => t('Map Viewer') . ':',
1506
    '#options' => array(
1507
      1 => "OpenLayers dynamic mapviewer",
1508
      0 => "Plain image",
1509
    ),
1510
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
1511
    '#description' => t('You can choose from two different map viewers:
1512
      <ul><li><em>OpenLayers</em> displays the maps in an interactive viewer
1513
      which allows zooming and panning. If not enabled the maps will consist
1514
      on a static image. If enabled you can configure the default layer
1515
      (background of your maps) below. Only one of them will be rendered.</li>
1516
      <li><em>Plain image</em> displays the map as a plain non interactive
1517
      image.</li></ul>'),
1518
  );
1519

    
1520
  $openLayersEnabled = variable_get('cdm_dataportal_map_openlayers', 1) == 1;
1521

    
1522

    
1523
  // --- Plain Image Settings --- //
1524
  $form['map_image'] = array(
1525
    '#type' => 'fieldset',
1526
    '#title' => t('Plain image map settings'),
1527
    '#collapsible' => TRUE,
1528
    '#collapsed' => $openLayersEnabled,
1529
    '#description' => t('The settings in this section are still expertimental
1530
      and can only be used with the EDIT map service version 1.1 or above.'),
1531
  );
1532

    
1533
  $edit_mapserver_version = get_edit_map_service_version_number();
1534
  if ($edit_mapserver_version < 1.1) {
1535
    $form['map_image']['#description'] = '<div class="messages warning">' . t("The selected EDIT map service version has to small version number: $edit_mapserver_version") . '</div>'
1536
      . $form['map_image']['#description'];
1537
  }
1538

    
1539
  $form['map_image']['map_base_layer'] = array(
1540
    '#type' => 'textfield',
1541
    '#title' => t('Background layer') . ':',
1542
    '#default_value' => variable_get('map_base_layer', 'cyprusdivs'),
1543
    '#description' => t('Background layer. For available layers inspect !url1 or !url2.', array(
1544
      '!url1' => l(t('deegree-csw'), 'http://edit.africamuseum.be:8080/deegree-csw/md_search.jsp'),
1545
      '!url2' => l(t('geoserver layers'), 'http://edit.africamuseum.be/geoserver/web/'), // http://edit.africamuseum.be/geoserver/rest/layers
1546
    )),
1547
  );
1548

    
1549
  $form['map_image']['map_bg_color'] = array(
1550
    '#type' => 'textfield',
1551
    '#title' => t('Background color') . ':',
1552
    '#default_value' => variable_get('map_bg_color', '1874CD'),
1553
  );
1554

    
1555
  $form['map_image']['map_base_layer_style'] = array(
1556
    '#type' => 'textfield',
1557
    '#title' => t('Background layer area style') . ':',
1558
     // Only line color by now.
1559
    '#default_value' => variable_get('map_base_layer_style', 'ffffff,606060,,'),
1560
    '#description' => t('Syntax: {Area fill color},{Area stroke color},{Area stroke width},{Area stroke dash style}'),
1561
  );
1562

    
1563

    
1564
  // --- OpenLayers Settings --- //
1565
  $form['openlayers'] = array(
1566
    '#type' => 'fieldset',
1567
    '#title' => t('OpenLayers settings'),
1568
    '#collapsible' => TRUE,
1569
    '#collapsed' => !$openLayersEnabled,
1570
    '#description' => '',
1571
  );
1572

    
1573
  if (!$openLayersEnabled) {
1574
    $form['openlayers']['#description'] = '<div class="messages warning">' . t('The Openlayers viewer is currently not enabled! (see section Maps settings above )') . '</div>'
1575
      . $form['openlayers']['#description'];
1576
  }
1577

    
1578
  $baselayer_options = array(
1579
    /*
1580
   NOTICE: must correspond to the layers defined in
1581
   js/openlayers_,ap.js#getLayersByName()
1582
   */
1583
    'osgeo_vmap0' => "Metacarta Vmap0 (OSGeo server)", // EPSG:4326: EPSG:900913
1584
    'metacarta_vmap0' => "Metacarta Vmap0 (MetaCarta Labs server)", // EPSG:4326, EPSG:900913
1585
    // all others EPSG:900913
1586
    // 'edit-vmap0_world_basic' => 'EDIT Vmap0',
1587
    'edit-etopo1' => "ETOPO1 Global Relief Model",
1588
    'mapnik' => 'OpenStreetMap',
1589
    'osmarender' => 'OpenStreetMap (Tiles@home)',
1590
    'gmap' => 'Google Streets',
1591
    'gsat' => 'Google Satellite',
1592
    'ghyb' => 'Google Hybrid',
1593
    'veroad' => 'Virtual Earth Roads',
1594
    'veaer' => 'Virtual Earth Aerial',
1595
    'vehyb' => 'Virtual Earth Hybrid',
1596
    // 'yahoo' => 'Yahoo Street',
1597
    // 'yahoosat' => 'Yahoo Satellite',
1598
    // 'yahoohyb' => 'Yahoo Hybrid',
1599
  );
1600

    
1601
  $form['openlayers']['baselayers'] = array(
1602
    '#type' => 'checkboxes_preferred',
1603
    '#title' => t('Base Layers') . ':',
1604
    '#options' => $baselayer_options,
1605
    '#default_value' => variable_get('baselayers', array('metacarta_vmap0' => "metacarta_vmap0", 'PREFERRED' => 'metacarta_vmap0')),
1606
    '#description' => t('Choose the baselayer layer you prefer to use as map background in the OpenLayers dynamic mapviewer.'),
1607
  );
1608

    
1609
  // cdm_dataportal_geoservice_showLayerSwitcher
1610
  $form['openlayers']['cdm_dataportal_geoservice_showLayerSwitcher'] = array(
1611
    '#type' => 'checkbox',
1612
    '#title' => '<b>' . t('Show Layer Switcher') . '</b>',
1613
    '#default_value' => variable_get('cdm_dataportal_geoservice_showLayerSwitcher', TRUE),
1614
    '#description' => t('
1615
      The Layer Switcher control displays a table of contents
1616
      for the map.  This allows the user interface to switch between
1617
      BaseLayers and to show or hide Overlays.  By default the switcher is
1618
      shown minimized on the right edge of the map, the user may expand it
1619
      by clicking on the handle.'
1620
    ),
1621
  );
1622

    
1623
  $localhostkey = 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ';
1624
  $gmap_api_key = variable_get('gmap_api_key', 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ');
1625
  $form['openlayers']['gmap_api_key'] = array(
1626
    '#type' => 'textfield',
1627
    '#title' => t('Google maps API key') . ':',
1628
    '#default_value' => variable_get('gmap_api_key', $gmap_api_key),
1629
    '#description' => t('If you want to use the Google Maps Layer, a key is
1630
      needed. If you need a key, visit
1631
      <a href="http://code.google.com/intl/en/apis/maps/signup.html">google maps api key</a>.
1632
      <br/><strong>Note:</strong> The following key: <code>!localhostkey</code>
1633
      is the default key for the localhost (127.0.0.1). The key in use is the
1634
      one above this text.', array('!localhostkey' => $localhostkey)),
1635
  );
1636

    
1637
  $form['cdm_dataportal_geoservice_map_legend'] = array(
1638
    '#type' => 'fieldset',
1639
    '#title' => t('Map legend'),
1640
    '#collapsible' => TRUE,
1641
    '#collapsed' => TRUE,
1642
    '#description' => t('Configure the maps legend.'),
1643
  );
1644

    
1645
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
1646
    '#type' => 'checkbox',
1647
    '#title' => '<b>' . t('Display a map legend') . '</b>',
1648
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
1649
    '#description' => t('Check this if you like a legend to be displayed with the maps.'),
1650
  );
1651

    
1652
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legendOpacity'] = array(
1653
    '#type' => 'textfield',
1654
    '#title' => t('Legend opacity'),
1655
    '#default_value' => variable_get('cdm_dataportal_geoservice_legendOpacity', '0.5'),
1656
    '#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
1657
                         to 0.0 will be not much visible.'),
1658
  );
1659

    
1660
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
1661
    '#type' => 'textfield',
1662
    '#title' => t('Font size'),
1663
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
1664
    '#description' => t('Font size in pixels.'),
1665
  );
1666

    
1667
  $fontStyles = array(
1668
    0 => "plane",
1669
    1 => "italic",
1670
  );
1671
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
1672
    '#type' => 'select',
1673
    '#title' => t('Available font styles'),
1674
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', FALSE),
1675
    '#options' => $fontStyles,
1676
    '#description' => t('Select a font style for the map legend.'),
1677
  );
1678

    
1679
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
1680
    '#type' => 'textfield',
1681
    '#title' => t('Legend icon width'),
1682
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
1683
    '#description' => t('Legend icon width in pixels.'),
1684
  );
1685
  $form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
1686
    '#type' => 'textfield',
1687
    '#title' => t('Legend icon height'),
1688
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
1689
    '#description' => t('Legend icon height in pixels.'),
1690
  );
1691

    
1692
  // @WA: D7 form api does not support reset buttons,
1693
  // so to mimic the D5 reset button we add one like this.
1694
  $form['actions']['reset'] = array(
1695
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1696
    '#weight' => 1000,
1697
  );
1698

    
1699
  return system_settings_form($form);
1700
}
1701

    
1702
/**
1703
 * @todo document this function.
1704
 */
1705
function cdm_settings_cache() {
1706

    
1707
  $form = array();
1708

    
1709
  $form['cache_settings'] = array(
1710
    '#type' => 'fieldset',
1711
    '#title' => t('Cache Settings'),
1712
    '#collapsible' => FALSE,
1713
    '#collapsed' => FALSE,
1714
    '#description' => t('<p>When caching is enabled all single taxon sites are
1715
      stored in an internal drupal cache doing the portal response of taxa pages
1716
      faster. This is possible because the sites are loaded from the cache and
1717
      are not created from scratch.</p>'),
1718
  );
1719

    
1720
  $form['cache_settings']['cdm_webservice_cache'] = array(
1721
    '#type' => 'checkbox',
1722
    '#title' => t('<strong>Enable caching</strong>'),
1723
    '#options' => cdm_help_general_cache(),
1724
    '#default_value' => variable_get('cdm_webservice_cache', 1),
1725
    '#description' => t('<p>Enable drupal to load taxa pages from the cache.</p>
1726
       <p><strong>Note:</strong> If taxa are modified by the editor or any other
1727
       application the changes will be not visible till the cache is erased.
1728
       Therefore developers should deactived this feature when they are working
1729
       on the CDM Dataportal Module.</p>'),
1730
  );
1731

    
1732
  $form['cache_settings']['cdm_run_cache'] = array(
1733
    '#markup' => cdm_view_cache_site(),
1734
  );
1735

    
1736
  // @WA: D7 form api does not support reset buttons,
1737
  // so to mimic the D5 reset button we add one like this.
1738
  $form['actions']['reset'] = array(
1739
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
1740
    '#weight' => 1000,
1741
  );
1742
  return system_settings_form($form);
1743
}
1744

    
1745
/**
1746
 * Walk and cache all taxon pages.
1747
 */
1748
function cdm_view_cache_site() {
1749

    
1750
  $out = '';
1751

    
1752
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cache_all_taxa.js');
1753

    
1754
  $request_params = array();
1755
  $request_params['query'] = '%';
1756
  // Cache only the dafault classification.
1757
  $request_params['tree'] = variable_get('cdm_taxonomictree_uuid', FALSE);
1758
  $request_params['doTaxa'] = 1;
1759
  $request_params['doSynonyms'] = 0;
1760
  $request_params['doTaxaByCommonNames'] = 0;
1761

    
1762
  $search_url = cdm_compose_url(CDM_WS_PORTAL_TAXON_FIND . ".json", NULL, queryString($request_params));
1763

    
1764
  $search_url = uri_uriByProxy($search_url);
1765
  $search_url = rtrim($search_url, '/');
1766
  $taxon_page_url = url('cdm_dataportal/taxon/');
1767

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

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

    
1777
  // Comment @WA: A form within a form is not valid html and not needed here.
1778
  // Also, it would be recommended just to include this part of the form in the
1779
  // rest of the form array in cdm_settings_cache().
1780
  // $out .= '<form id="cache_site">';
1781
  $out .= '<input type="hidden" name="searchTaxaUrl" value="' . $search_url . '"/>';
1782
  $out .= '<input type="hidden" name="taxonPageUrl" value="' . $taxon_page_url . '"/>';
1783
  $out .= '<input type="button" name="start" value="' . t('Start') . '"/>';
1784
  $out .= '<input type="button" name="stop" value="' . t('Stop') . '"/>';
1785
  // $out .= '</form>';
1786
  $out .= '</div>';
1787
  /*
1788
  foreach($taxonPager->records as $taxon){
1789
    cdm_dataportal_taxon_view($uuid);
1790
  }
1791
  */
1792
  return $out;
1793
}
1794

    
1795
/**
1796
 * Form validation handler for ?
1797
 *
1798
 * Comment @WA: currently this handler is not used.
1799
 * It seems partly for cdm_settings_general en partly for cdm_settings_cache?
1800
 * Then you can change the name into cdm_settings_general_validate() or
1801
 * cdm_settings_cache_validate().
1802
 * But cdm_api_secref_cache_clear is not doing anything (yet)?
1803
 */
1804
function cdm_settings_validate($form, &$form_state) {
1805

    
1806
  if (!str_endsWith($form_state['values']['cdm_webservice_url'], '/')) {
1807
    // form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
1808
    $form_state['values']['cdm_webservice_url'] .= '/';
1809
  }
1810

    
1811
  if ($form_state['values']['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)) {
1812
    cache_clear_all(NULL, 'cache_cdm_ws');
1813
    // Better clear secref_cache since I can not be sure if the cache has not
1814
    // be used during this response.
1815
    cdm_api_secref_cache_clear();
1816
  }
1817
}
1818

    
1819
/**
1820
 * Returns an associative array of the currently chosen settings for the EDIT map service or the defaults as
1821
 * specified in EDIT_MAPSERVER_URI_DEFAULT and EDIT_MAPSERVER_VERSION_DEFAULT:
1822
 *  - base_uri: the protocol and host part , e.g.: http://edit.africamuseum.be
1823
 *  - version: the version, e.g.: v1.1
1824
 *
1825
 * @return array
1826
 *    An associative array of the currently chosen settings for the EDIT map service or the defaults.
1827
 */
1828
function get_edit_map_service_settings() {
1829

    
1830
  $settings = variable_get('edit_map_server', array(
1831
      'base_uri' => EDIT_MAPSERVER_URI_DEFAULT,
1832
      'version' => EDIT_MAPSERVER_VERSION_DEFAULT
1833
      )
1834
  );
1835
  // replace old non tree like settings by default
1836
  // TODO to be removed after release 3.1.5
1837
  if(!is_array($settings)){
1838
    variable_del('edit_map_server');
1839
    return get_edit_map_service_settings();
1840
  }
1841

    
1842
  return $settings;
1843
}
1844

    
1845
/**
1846
 * Returns the full edit map service URI e.g.: http://edit.africamuseum.be/edit_wp5/v1.1
1847
 *
1848
 * @return string
1849
 *   The full edit map service URI e.g.: http://edit.africamuseum.be/edit_wp5/v1.1
1850
 */
1851
function get_edit_map_service_full_uri() {
1852
  $settings = get_edit_map_service_settings();
1853
  return $settings['base_uri'] . EDIT_MAPSERVER_PATH .  '/' . $settings['version'];
1854
}
1855

    
1856

    
1857
/**
1858
 * Returns the version number of the currently selected edit mapserver as a float
1859
 *
1860
 * @return float
1861
 *   The version number of the currently selected edit mapserver as a float.
1862
 *   Returns 0 on error.
1863
 */
1864
function get_edit_map_service_version_number() {
1865

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

    
1868
  $settings = get_edit_map_service_settings();
1869
  preg_match($pattern, $settings['version'], $matches, PREG_OFFSET_CAPTURE);
1870
  if (isset($matches[1])) {
1871
    // Convert string to float.
1872
    $version = 1 + $matches[1][0] - 1;
1873
    return $version;
1874
  }
1875
  else {
1876
    // Report error.
1877
    drupal_set_message(t(" Invalid EDIT map service version number: '!version'",
1878
        array(
1879
          '!version' => $settings['version'],
1880
          'warning')
1881
        )
1882
      );
1883
    return 0;
1884
  }
1885
}
1886

    
1887
/**
1888
 * Returns the array of selected items in a options array
1889
 *
1890
 * @param array $options
1891
 *   An options array as generated by a form element like checkoxes, select ...,
1892
 */
1893
function get_selection($options) {
1894
  $selection = array();
1895
  foreach ($options as $key=>$val) {
1896
    if (!empty($val)) {
1897
      $selection[] = $val;
1898
    }
1899
  }
1900
  return $selection;
1901
}
1902

    
1903

    
1904
/**
1905
 * Implements hook_element_info().
1906
 *
1907
 * Allows modules to declare their own Form API element types and specify their default values.
1908
 *
1909
 * @see http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_element_info/7
1910
 */
1911
function cdm_dataportal_element_info() {
1912
  $type['checkboxes_preferred'] = array(
1913
    '#input' => TRUE,
1914
    '#process' => array('checkboxes_preferred_expand'),
1915
    '#after_build' => array('checkboxes_preferred_after_build'),
1916
    '#theme' => array('checkboxes_preferred'),
1917
    // '#theme_wrapper' => array('form_element'),
1918
  );
1919
  return $type;
1920
}
1921

    
1922
/**
1923
 * #process callback function for the custom form element type 'checkbox_preferred'
1924
 *
1925
 *
1926
 */
1927
function checkboxes_preferred_expand($element, &$form_state, $form) {
1928

    
1929
  // First of all create checkboxes for each of the elements
1930
  $element = form_process_checkboxes($element);
1931

    
1932
  $children = element_children($element);
1933

    
1934
  $element['table_start'] = array(
1935
    '#markup' => '<table class="checkboxes_preferred"><tr><th></th><th>' . t('Enabled') . '</th><th>' . t('Default') . '</th></tr>',
1936
    '#weight' => -1,
1937
  );
1938

    
1939
  // prepare first part each of the table rows which contains the row label
1940
  $weight = 0;
1941
  foreach ($children as $key) {
1942
    $odd_even = $weight % 4 == 0 ? 'odd' : 'even';
1943
    $element[$key]['#weight'] = $weight;
1944
    $element[$key]['#prefix'] = '<tr class="' . $odd_even . '"><td>' . t($element['#options'][$key]) . '</td><td>';
1945
    $element[$key]['#suffix'] = '</td>';
1946
    unset($element[$key]['#title']);
1947
    $weight += 2;
1948
  }
1949
  $weight = 0;
1950

    
1951
  // add a radio button to each of the checkboxes, the
1952
  // check boxes have already been created at the beginning
1953
  // of this function
1954
  if (count($element['#options']) > 0) {
1955
    foreach ($element['#options'] as $key => $choice) {
1956
      if (!isset($element[$key . '_preferred'])) {
1957
        $element[$key . '_preferred'] = array(
1958
          '#type' => 'radio',
1959
          '#name' => $element['#parents'][0] . '_preferred',
1960
          '#return_value' => check_plain($key),
1961
          '#default_value' => empty($element['#default_value_2']) ? NULL : $element['#default_value_2'],
1962
          '#attributes' => $element['#attributes'],
1963
          '#parents' => $element['#parents'],
1964
          // '#spawned' => TRUE,
1965
          '#weight' => $weight + 1,
1966
          '#prefix' => '<td>',        // add a prefix to start a new table cell
1967
          '#suffix' => '</td></tr>',  // add a prefix to close the tabel row
1968
        );
1969
      }
1970
      $weight += 2;
1971
    }
1972
  }
1973

    
1974
  // end the table
1975
  $element['table_end'] = array(
1976
    '#markup' => '</table>',
1977
    '#weight' => $weight++,
1978
  );
1979

    
1980
  return $element;
1981
}
1982

    
1983
/**
1984
 * Theme function for the custom form field 'checkboxes_preferred'.
1985
 */
1986
function theme_checkboxes_preferred($variables) {
1987
  $element = $variables['element'];
1988
  $out = '<div id="edit-baselayers-wrapper" class="form-item">';
1989
  $out .= '<label for="edit-baselayers">' . $element['#title'] . '</label>';
1990
  $out .= drupal_render_children($element);
1991
  $out .= '<div class="description">' . $element['#description'] . '</div>';
1992
  $out .= '</div>';
1993
  return $out;
1994
}
1995

    
1996
/**
1997
 * Callback for checkboxes preferred for widget which will
1998
 * be called after the form or element is built. The call
1999
 * back is configured in the form element by setting it as
2000
 * #after_build parameter.
2001
 *
2002
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#after_build
2003
 *
2004
 * @param $form
2005
 *   Nested array of form elements that comprise the form.
2006
 * @param $form_state
2007
 *   A keyed array containing the current state of the form.
2008
 *
2009
 * @return the modified form array
2010
 */
2011
function checkboxes_preferred_after_build($form, &$form_state) {
2012

    
2013
  $parent_id = $form['#parents'][0];
2014

    
2015
  if ($_POST && count($_POST) > 0) {
2016
    // First pass of form processing.
2017
    $preferred_layer = $_POST[$parent_id . '_preferred'];
2018
    $form['#value']['PREFERRED'] = $preferred_layer;
2019
    $form_state[$parent_id] = $form['#value'];
2020
    $form_state['values']['baselayers'] = $form['#value'];
2021
  }
2022
  else {
2023
    // Second pass of form processing.
2024
    $preferred_layer = $form['#value']['PREFERRED'];
2025
  }
2026

    
2027
  // Also set the chosen value (not sure if this is good Drupal style ....).
2028
  foreach ($children = element_children($form) as $key) {
2029
    if (!empty($form[$key]['#type']) && $form[$key]['#type'] == 'radio') {
2030
      $form[$key]['#value'] = $preferred_layer;
2031
    }
2032
  }
2033
  // The default layer must always be enabled.
2034
  $form[$preferred_layer]['#value'] = $preferred_layer;
2035

    
2036
  return $form;
2037
}
2038

    
2039
/**
2040
 * Element validate callback for text field and arrays containing json.
2041
 *
2042
 * @param $element
2043
 *   The form element to validate
2044
 * @param $form
2045
 *   Nested array of form elements that comprise the form.
2046
 * @param $form_state
2047
 *   A keyed array containing the current state of the form.
2048
 */
2049
function form_element_validate_json($element, &$form_state, $form) {
2050
   if (!empty($element['#value'])) {
2051
     json_decode($element['#value']);
2052
     if(json_last_error() != JSON_ERROR_NONE){
2053
       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/'));
2054
     }
2055
   }
2056
}
2057

    
2058
/**
2059
 * Form submission handler for textareas and textfields containing JSON.
2060
 *
2061
 * The contained JSON will be converted into an php array
2062
 * or object and will be stores in the variables as such.
2063
 *
2064
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit
2065
 *
2066
 * @param $form
2067
 *   Nested array of form elements that comprise the form.
2068
 * @param $form_state
2069
 *   A keyed array containing the current state of the form.
2070
 *
2071
 */
2072
function submit_json_as_php_array($form, &$form_state) {
2073
  if (is_array($form['#json_elements'])) {
2074
    foreach ($form['#json_elements'] as $element){
2075
      if (trim($form_state['values'][$element])) {
2076
        $form_state['values'][$element] = (array) json_decode($form_state['values'][$element]);
2077
      } else {
2078
        $form_state['values'][$element] = NULL;
2079
      }
2080
    }
2081
  }
2082
}
(13-13/13)