Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:
1 6657531f Andreas Kohlbecker
<?php
2
/**
3
 * @file
4
 * Common Theming functions.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 */
15
16
define('FOOTNOTE_ANNOTATIONS', 'annotations');
17
18
/**
19
 * Get the true path to the root of the Drupal site.
20
 *
21
 * Better than using DOCUMENT_ROOT and base_path().
22
 */
23
function absolute_path_to_drupal() {
24
  static $absolute_path_to_drupal = NULL;
25
26
  if ($absolute_path_to_drupal === NULL) {
27
    // Get the absolute path to this file:
28
    $dir = rtrim(str_replace('\\', '/', dirname(__FILE__)), '/');
29
    $parts = explode('/', $dir);
30
    // Iterate up the directory hierarchy until we find the website root:
31
    $done = FALSE;
32
    do {
33
      // Check a couple of obvious things:
34
      $done = is_dir("$dir/sites") && is_dir("$dir/includes") && is_file("$dir/index.php");
35
      if (!$done) {
36
        // If there's no more path to examine, we didn't find the site root:
37
        if (empty($parts)) {
38
          $absolute_path_to_drupal = FALSE;
39
          break;
40
        }
41
        // Go up one level and look again:
42
        array_pop($parts);
43
        $dir = implode('/', $parts);
44
      }
45
    } while (!$done);
46
47
    $absolute_path_to_drupal = $dir;
48
  }
49
  return $absolute_path_to_drupal;
50
}
51
52
/**
53 6e2a9ba9 Andreas Kohlbecker
 * Checks if the taxon specified by the uuid is contained in the currently focused classification.
54
 *
55
 * @param $taxon_uuid
56
 * @return bool
57 6657531f Andreas Kohlbecker
 */
58 7663cd0b Andreas Kohlbecker
function taxon_in_current_classification($taxon_uuid) {
59 6657531f Andreas Kohlbecker
  $taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon_uuid);
60
  $taxon_in_current_tree = FALSE;
61
  if (is_array($taxon_nodes)) {
62
    foreach ($taxon_nodes as $node) {
63 64cfdac1 Andreas Kohlbecker
      if (get_current_classification_uuid() == $node->classificationUUID) {
64 6657531f Andreas Kohlbecker
        $taxon_in_current_tree = TRUE;
65
        break;
66
      }
67
    }
68
  }
69
  return $taxon_in_current_tree;
70
}
71
72
/**
73
 * TODO if getting fragment from request is possible remove
74
 * $_REQUEST['highlite'] HACK
75
 * NOT WORKING since fragments are not available to the server
76 7a2a14b3 Andreas Kohlbecker
 *
77 6657531f Andreas Kohlbecker
 * function fragment(){
78 7a2a14b3 Andreas Kohlbecker
 *    global $fragment;
79
 *    if(!$fragment){
80
 *       $fragment = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '#'));
81
 *    }
82
 *   return $fragment;
83 6657531f Andreas Kohlbecker
 * }
84
 */
85
function uuid_anchor($uuid, $innerHTML) {
86
  $highlite = FALSE;
87
  $params = drupal_get_query_parameters();
88
  if (isset($params['highlite']) && $params['highlite'] == $uuid) {
89
    $highlite = TRUE;
90
  }
91
92
  return '<a name="' . $uuid . '" ></a><span class="' . ($highlite ? 'highlite' : '') . '">' . $innerHTML . '</span>';
93
}
94
95
/**
96
 * @todo Enter description here...
97
 * @deprecated looks like this is not used anymore
98
 */
99
/*
100
function tagNameParts($name, $numOfNameTokens) {
101
  $out = '<span class="name">';
102
103
  $token = strtok($name, " \n\t");
104
  $i = 0;
105
  $noSpace = TRUE;
106
  while ($token != FALSE) {
107
    if ($i == $numOfNameTokens) {
108
      $out .= '</span> <span class="authors">';
109
      $noSpace = TRUE;
110
    }
111
    $out .= ($noSpace ? '' : ' ') . $token;
112
    $noSpace = FALSE;
113
    $token = strtok(" \n\t");
114
    $i++;
115
  }
116
  return $out . '</span>';
117
}
118
*/
119
120
/* ============================ annotations ============================= */
121
122
/**
123
 * Returns HTML for annotations to cdm objects.
124
 *
125
 * Almost any cdmObject may be annotated. Therefore we provide a generic way to
126
 * display as well as create or update annotations. The following cdm classes
127
 * are annotatable:
128
 *
129
 * - DescriptionElementBase
130
 * - EventBase
131
 * - HomotypicalGroup
132
 * - IdentifiableEntity
133
 * - DescriptionBase
134
 * - IdentifiableMediaEntity
135
 * - Media
136
 * - Sequence
137
 * - TaxonBase
138 102f8c26 Andreas Kohlbecker
 * - TaxonName
139 6657531f Andreas Kohlbecker
 * - TaxonomicTree
140
 * - TermBase
141
 * - LanguageStringBase
142
 * - ReferencedEntityBase
143
 * - NomenclaturalStatus
144
 * - OriginalSourceBase
145
 * - RelationshipBase
146
 * - TypeDesignationBase
147
 * - TaxonNode
148
 * - WorkingSet
149
 *
150
 * @param array $variables
151
 *   An associative array containing:
152
 *  - cdmBase_list: An array of CdmBase instances or a single instance.
153
 *  - footnote_list_key
154
 *
155
 * @ingroup themeable
156
 */
157 39a9e629 Andreas Kohlbecker
function theme_cdm_entities_annotations_as_footnotekeys($variables) {
158 65345976 Andreas Kohlbecker
  $cdm_entities = $variables['cdmBase_list'];
159 6657531f Andreas Kohlbecker
  $footnote_list_key = $variables['footnote_list_key'];
160 39a9e629 Andreas Kohlbecker
  // check if footnotes for annotations are disabled completely
161 6657531f Andreas Kohlbecker
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
162
    return '';
163
  }
164
  $out = '';
165 178d1ae6 Andreas Kohlbecker
  if(is_array($cdm_entities)){
166
    // array of entities
167 2fc5c836 Andreas Kohlbecker
    $footNoteKeys = cdm_entities_annotations_as_footnotekeys($cdm_entities, $footnote_list_key);
168 178d1ae6 Andreas Kohlbecker
  } else {
169
    // single entity
170
    $footNoteKeys = cdm_entity_annotations_as_footnotekeys($cdm_entities, $footnote_list_key);
171
  }
172 6657531f Andreas Kohlbecker
  foreach ($footNoteKeys as $a) {
173
    // $out .= theme('cdm_footnote_key', $a, $a->footnoteListKey, (isset($out)?
174
    // ',' : ''));
175
    $out .= theme('cdm_footnote_key', array('footnoteKey' => $a, 'separator' => ($out ? ',' : '')));
176
  }
177
  return $out;
178
}
179
180
/**
181
 * @todo Please document this function.
182
 * @see http://drupal.org/node/1354
183
 */
184
function theme_cdm_annotation_footnotes($variables) {
185
  $footnoteListKey = $variables['footnoteListKey'];
186
  $enclosingTag = $variables['enclosingTag'];
187
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
188
    return '';
189
  }
190
  return theme('cdm_footnotes', array('footnoteListKey' => $footnoteListKey . '-annotations', 'enclosingTag' => $enclosingTag));
191
}
192
193
/**
194
 * @todo Please document this function.
195
 * @see http://drupal.org/node/1354
196
 */
197
function theme_cdm_annotation_content($variables) {
198
  $AnnotationTO = $variables['AnnotationTO'];
199
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_annotations.js');
200
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.form.js');
201
202 5611d467 Andreas Kohlbecker
  $out = theme('cdm_list_of_annotations', array('annotationElements' => $AnnotationTO->annotationElements));
203 6657531f Andreas Kohlbecker
204
  $annotationUrl = cdm_compose_url(CDM_WS_ANNOTATIONS, array(
205
    $AnnotationTO->uuid,
206
  ));
207
  $annotationProxyUrl = url('cdm_api/proxy/' . urlencode($annotationUrl) . '/cdm_annotation_post');
208
209
  // TODO users have to be authenticated to the dataportal to be able to write
210
  // annotations.
211
  $out .= '
212
        <div class="annotation_create">
213
          <form action="' . $annotationProxyUrl . '" method="POST">
214
            <textarea name="annotation"></textarea>
215
            <input type="hidden" name="commentator" value="">
216
            <input type="submit" value="' . t('Save annotation') . '" />
217
          </form>
218
       </div>
219
  ';
220
221
  return $out;
222
}
223
224
/**
225
 * @todo Please document this function.
226
 * @see http://drupal.org/node/1354
227
 */
228
function theme_cdm_list_of_annotations($variables) {
229
  $annotationElements = $variables['annotationElements'];
230
  $out = '<ul class="annotation_list">';
231
232
  foreach ($annotationElements as $key => $row) {
233
    $created[$key] = $row;
234
  }
235
  array_multisort($created, SORT_ASC, $annotationElements);
236
237
  foreach ($annotationElements as $annotation) {
238
    $out .= '<li>' . $annotation->text . '</li>';
239
  }
240
241
  $out .= '</ul>';
242
243
  return $out;
244
}
245
246
/* ============================ footnotes ============================= */
247
/**
248
 * @todo Please document this function.
249
 * @see http://drupal.org/node/1354
250
 */
251
function theme_cdm_footnote_key($variables) {
252
253
  $footnoteKey = $variables['footnoteKey'];
254
  $separator = $variables['separator'];
255
  $highlightable = $variables['highlightable'];
256
  $separator_off = $variables['separator_off'];
257
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
258
    return '';
259
  }
260
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
261
    return '';
262
  }
263
264
  if ($separator_off) {
265
    $separator = '';
266
  }
267
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">' . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
268
  return $out;
269
}
270
271
/**
272
 * @todo Please document this function.
273
 * @see http://drupal.org/node/1354
274
 */
275
function theme_cdm_footnote($variables) {
276
  $footnoteKey = $variables['footnoteKey'];
277
  $footnoteText = $variables['footnoteText'];
278 5611d467 Andreas Kohlbecker
  $enclosing_tag = $variables['enclosing_tag'];
279 6657531f Andreas Kohlbecker
  _add_js_footnotes();
280 5611d467 Andreas Kohlbecker
  $out = '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
281 fea89e38 Andreas Kohlbecker
    . '<a name="footnote-' . $footnoteKey . '"></a>'
282
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
283 5611d467 Andreas Kohlbecker
    . '</' . $enclosing_tag . '>';
284 6657531f Andreas Kohlbecker
  return $out;
285
}
286
287
/**
288
 * @todo Please document this function.
289
 * @see http://drupal.org/node/1354
290
 */
291
function theme_cdm_footnotes($variables) {
292
  $footnoteListKey = $variables['footnoteListKey'];
293
  $enclosingTag = $variables['enclosingTag'];
294
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
295
    return '';
296
  }
297 fea89e38 Andreas Kohlbecker
298
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnoteListKey . ' ">'
299
    . FootnoteManager::renderFootnoteList($footnoteListKey)
300
    . '</' . $enclosingTag . '>';
301
302 6657531f Andreas Kohlbecker
  FootnoteManager::removeFootnoteList($footnoteListKey);
303
  return $out;
304
}
305
306 991ae630 Andreas Kohlbecker
/**
307
 * Renders the footnotes for annotations and sources, etc.
308
 *
309
 * @param string $footnote_list_key
310
 *    RenderHints::getFootnoteListKey() will be used if this parameter is undefined.
311
 * @param string $enclosingTag
312
 *    Default tag is 'span'
313
 *
314
 * @return string
315
 *    The markup string
316
 *
317
 */
318
function render_cdm_footnotes(string $footnote_list_key = NULL, string $enclosingTag = 'span'){
319
320
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
321
    return '';
322
  }
323
  if(!$footnote_list_key){
324
    $footnote_list_key = RenderHints::getFootnoteListKey();
325
  }
326
327
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
328
    . FootnoteManager::renderFootnoteList($footnote_list_key . '-annotations') . ' ' . FootnoteManager::renderFootnoteList($footnote_list_key)
329
    . '</' . $enclosingTag . '>';
330
  FootnoteManager::removeFootnoteList($footnote_list_key . '-annotations');
331
  FootnoteManager::removeFootnoteList($footnote_list_key);
332
  return $out;
333
}
334
335 6657531f Andreas Kohlbecker
/**
336
 * @todo Please document this function.
337
 * @see http://drupal.org/node/1354
338
 */
339
function theme_cdm_annotations($variables) {
340
  $annotations = $variables['annotations'];
341
  $enclosingTag = $variables['enclosingTag'];
342
  if (!is_array($annotations)) {
343
    return;
344
  }
345
  $out = '<' . $enclosingTag . ' class="annotations">';
346
  $i = 0;
347
  foreach ($annotations as $annotation) {
348
    $out .= ($i++ > 0 ? ', ' : '') . $annotation->text;
349
  }
350
  $out .= '</' . $enclosingTag . '>';
351
  return $out;
352
}
353
354
/**
355
 * @todo Please document this function.
356
 * @see http://drupal.org/node/1354
357
 */
358
function cdm_exist_footnote($footnote_list, $footnote) {
359
  $result = FALSE;
360
  if (is_array($footnote_list)) {
361
    foreach ($footnote_list as $element) {
362
      if ($element == $footnote) {
363
        $result = TRUE;
364
      }
365
    }
366
  }
367
  return $result;
368
}
369
370
/**
371
 * @todo Please document this function.
372
 * @see http://drupal.org/node/1354
373
 */
374
function cdm_add_footnote_to_array(&$footnote_list, $footnote) {
375
  if (!cdm_exist_footnote($footnote_list, $footnote)) {
376
    $footnote_list[] = $footnote;
377
  }
378
}
379
380 4ae6064e Andreas Kohlbecker
/**
381
 * Theme function for CDM marker instances
382
 *
383
 * @see compose_cdm_marker();
384
 * @param array $variables
385
 *   - markerType_representation_l10n: the localized representation of the marker.markerType field
386
 */
387
function theme_cdm_marker($variables) {
388
  $class_attribute = null;
389
  //TODO class attribute hacked?, use generic drupal way?
390
  if(isset($variables['attributes']['class'])){
391
    $class_attribute = $variables['attributes']['class'];
392
  }
393
  return '<span class="' . $class_attribute . '">' . $variables['label'] . '</span>';
394
}
395
396 6657531f Andreas Kohlbecker
/* ============================ pager ============================= */
397 c278f2a0 Andreas Kohlbecker
398 6657531f Andreas Kohlbecker
/**
399
 * @todo Please document this function.
400
 * @see http://drupal.org/node/1354
401
 */
402
function theme_cdm_pager($variables) {
403
  $pager = $variables['pager'];
404
  $path = $variables['path'];
405
  $parameters = $variables['parameters'];
406
  $out = '';
407
408
  if ($pager->pagesAvailable > 1) {
409
410
    $out .= '<div class="pager">';
411
    if ($pager->currentIndex > 0) {
412
      $out .= theme('cdm_pager_link', array(
413 c278f2a0 Andreas Kohlbecker
        'text' => '« ' . t('First'),
414 6657531f Andreas Kohlbecker
        'linkIndex' => 0,
415
        'pager' => $pager,
416
        'path' => $path,
417
        'parameters' => $parameters,
418
        'attributes' => array('class' => array('pager-first')),
419
        ));
420
      $out .= theme('cdm_pager_link', array(
421 c278f2a0 Andreas Kohlbecker
        'text' => '‹ ' . t('Previous'),
422 6657531f Andreas Kohlbecker
        'linkIndex' => $pager->currentIndex - 1,
423
        'pager' => $pager,
424
        'path' => $path,
425
        'parameters' => $parameters,
426
        'attributes' => array('class' => array('pager-previous')),
427
        ));
428
    }
429
430
    if ($pager->indices[0] > 0) {
431
      $out .= '<div class="pager-list-dots-left">...</div>';
432
    }
433
434
    foreach ($pager->indices as $index) {
435
      $label = $index + 1;
436
      $out .= theme('cdm_pager_link', array('text' => $label, 'linkIndex' => $index, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
437
  'class' => array('pager-first'),
438
)));
439
    }
440
    if ($pager->indices[count($pager->indices) - 1] < $pager->pagesAvailable - 1) {
441
      $out .= '<div class="pager-list-dots-right">...</div>';
442
    }
443
444
    if ($pager->nextIndex) {
445 c278f2a0 Andreas Kohlbecker
      $out .= theme(
446
        'cdm_pager_link',
447
        array(
448
          'text' => t('Next') . ' ›',
449
          'linkIndex' => $pager->nextIndex,
450
          'pager' => $pager,
451
          'path' => $path,
452
          'parameters' => $parameters,
453
          'attributes' => array(
454
            'class' => array('pager-next'),
455
          )
456
        )
457
      );
458
      $out .= theme(
459
        'cdm_pager_link',
460
        array(
461
          'text' => t('Last') . ' »',
462
          'linkIndex' => $pager->pagesAvailable - 1,
463
          'pager' => $pager,
464
          'path' => $path,
465
          'parameters' => $parameters,
466
          'attributes' => array(
467
            'class' => array('pager-last'),
468
          )
469
        )
470
      );
471 6657531f Andreas Kohlbecker
    }
472
    $out .= '</div>';
473
474
    return $out;
475
  }
476
}
477
478
/**
479
 * @todo Please document this function.
480
 * @see http://drupal.org/node/1354
481
 */
482
function theme_cdm_pager_link($variables) {
483
  $text = $variables['text'];
484
  $linkIndex = $variables['linkIndex'];
485
  $pager = $variables['pager'];
486
  $path = $variables['path'];
487
  $parameters = $variables['parameters'];
488
  $attributes = $variables['attributes'];
489 34dd7be9 Andreas Kohlbecker
490 17cfc574 Andreas Kohlbecker
  // the parameters may still contain the q param,
491
  // but this is already in the path variable
492
  unset($parameters['q']);
493
494 34dd7be9 Andreas Kohlbecker
  $parameters['pager']['pageNumber'] = $linkIndex;
495 6657531f Andreas Kohlbecker
  if ($linkIndex == $pager->currentIndex) {
496
    $out = '<strong>' . $text . '</strong>';
497
  }
498
  else {
499
    // $queryString = drupal_query_string_encode($parameters);
500
    $queryString = $parameters;
501
    $out = l($text, $path, array(
502
      'attributes' => $attributes,
503
      'query' => $queryString,
504
    ));
505
  }
506
  return $out;
507
}
508
509
/* ============================ special buttons ============================= */
510
511
/**
512
 * @todo Please document this function.
513
 * @see http://drupal.org/node/1354
514
 */
515
function theme_cdm_print_button() {
516
517
  drupal_add_js('jQuery(document).ready(function() {
518
         jQuery(\'#print_button\').click(function () {
519
         window.print();
520
     });
521
  });', array('type' => 'inline'));
522
523 c8234bdf Andreas Kohlbecker
  $output = '<div id="print_button"><img src="' . base_path()  .  drupal_get_path('module', 'cdm_dataportal') . '/images/print_icon.gif"' . ' alt="' . t('Print this page') . '" title="' . t('Print this page') . '" />';
524 6657531f Andreas Kohlbecker
  // .t('Print this page');
525
  // $output .= l('Print this page', '');
526
  $output .= '<span>Print this page</span>';
527
  $output .= '</div>';
528
529
  return $output;
530
}