Project

General

Profile

Download (15.3 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 18a83116 Andreas Kohlbecker
function cdm_annotation_footnotes($footnoteListKey = null, $enclosingTag = span) {
185 6657531f Andreas Kohlbecker
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
186
    return '';
187
  }
188
  return theme('cdm_footnotes', array('footnoteListKey' => $footnoteListKey . '-annotations', 'enclosingTag' => $enclosingTag));
189
}
190
191
/**
192
 * @todo Please document this function.
193
 * @see http://drupal.org/node/1354
194
 */
195
function theme_cdm_annotation_content($variables) {
196
  $AnnotationTO = $variables['AnnotationTO'];
197
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_annotations.js');
198
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.form.js');
199
200 5611d467 Andreas Kohlbecker
  $out = theme('cdm_list_of_annotations', array('annotationElements' => $AnnotationTO->annotationElements));
201 6657531f Andreas Kohlbecker
202
  $annotationUrl = cdm_compose_url(CDM_WS_ANNOTATIONS, array(
203
    $AnnotationTO->uuid,
204
  ));
205
  $annotationProxyUrl = url('cdm_api/proxy/' . urlencode($annotationUrl) . '/cdm_annotation_post');
206
207
  // TODO users have to be authenticated to the dataportal to be able to write
208
  // annotations.
209
  $out .= '
210
        <div class="annotation_create">
211
          <form action="' . $annotationProxyUrl . '" method="POST">
212
            <textarea name="annotation"></textarea>
213
            <input type="hidden" name="commentator" value="">
214
            <input type="submit" value="' . t('Save annotation') . '" />
215
          </form>
216
       </div>
217
  ';
218
219
  return $out;
220
}
221
222
/**
223
 * @todo Please document this function.
224
 * @see http://drupal.org/node/1354
225
 */
226
function theme_cdm_list_of_annotations($variables) {
227
  $annotationElements = $variables['annotationElements'];
228
  $out = '<ul class="annotation_list">';
229
230
  foreach ($annotationElements as $key => $row) {
231
    $created[$key] = $row;
232
  }
233
  array_multisort($created, SORT_ASC, $annotationElements);
234
235
  foreach ($annotationElements as $annotation) {
236
    $out .= '<li>' . $annotation->text . '</li>';
237
  }
238
239
  $out .= '</ul>';
240
241
  return $out;
242
}
243
244
/* ============================ footnotes ============================= */
245
/**
246
 * @todo Please document this function.
247
 * @see http://drupal.org/node/1354
248
 */
249
function theme_cdm_footnote_key($variables) {
250
251
  $footnoteKey = $variables['footnoteKey'];
252
  $separator = $variables['separator'];
253
  $highlightable = $variables['highlightable'];
254
  $separator_off = $variables['separator_off'];
255
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
256
    return '';
257
  }
258
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
259
    return '';
260
  }
261
262
  if ($separator_off) {
263
    $separator = '';
264
  }
265
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">' . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
266
  return $out;
267
}
268
269
/**
270
 * @todo Please document this function.
271
 * @see http://drupal.org/node/1354
272
 */
273
function theme_cdm_footnote($variables) {
274
  $footnoteKey = $variables['footnoteKey'];
275
  $footnoteText = $variables['footnoteText'];
276 5611d467 Andreas Kohlbecker
  $enclosing_tag = $variables['enclosing_tag'];
277 6657531f Andreas Kohlbecker
  _add_js_footnotes();
278 5611d467 Andreas Kohlbecker
  $out = '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
279 fea89e38 Andreas Kohlbecker
    . '<a name="footnote-' . $footnoteKey . '"></a>'
280
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
281 5611d467 Andreas Kohlbecker
    . '</' . $enclosing_tag . '>';
282 6657531f Andreas Kohlbecker
  return $out;
283
}
284
285
/**
286
 * @todo Please document this function.
287
 * @see http://drupal.org/node/1354
288
 */
289
function theme_cdm_footnotes($variables) {
290
  $footnoteListKey = $variables['footnoteListKey'];
291
  $enclosingTag = $variables['enclosingTag'];
292
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
293
    return '';
294
  }
295 fea89e38 Andreas Kohlbecker
296
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnoteListKey . ' ">'
297
    . FootnoteManager::renderFootnoteList($footnoteListKey)
298
    . '</' . $enclosingTag . '>';
299
300 6657531f Andreas Kohlbecker
  FootnoteManager::removeFootnoteList($footnoteListKey);
301
  return $out;
302
}
303
304 991ae630 Andreas Kohlbecker
/**
305
 * Renders the footnotes for annotations and sources, etc.
306
 *
307
 * @param string $footnote_list_key
308
 *    RenderHints::getFootnoteListKey() will be used if this parameter is undefined.
309
 * @param string $enclosingTag
310
 *    Default tag is 'span'
311
 *
312
 * @return string
313
 *    The markup string
314
 *
315
 */
316 032f17da Andreas Kohlbecker
function render_cdm_footnotes($footnote_list_key = NULL, $enclosingTag = 'span'){
317 991ae630 Andreas Kohlbecker
318
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
319
    return '';
320
  }
321
  if(!$footnote_list_key){
322
    $footnote_list_key = RenderHints::getFootnoteListKey();
323
  }
324
325
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
326
    . FootnoteManager::renderFootnoteList($footnote_list_key . '-annotations') . ' ' . FootnoteManager::renderFootnoteList($footnote_list_key)
327
    . '</' . $enclosingTag . '>';
328
  FootnoteManager::removeFootnoteList($footnote_list_key . '-annotations');
329
  FootnoteManager::removeFootnoteList($footnote_list_key);
330
  return $out;
331
}
332
333 6657531f Andreas Kohlbecker
/**
334
 * @todo Please document this function.
335
 * @see http://drupal.org/node/1354
336
 */
337
function theme_cdm_annotations($variables) {
338
  $annotations = $variables['annotations'];
339
  $enclosingTag = $variables['enclosingTag'];
340
  if (!is_array($annotations)) {
341
    return;
342
  }
343
  $out = '<' . $enclosingTag . ' class="annotations">';
344
  $i = 0;
345
  foreach ($annotations as $annotation) {
346
    $out .= ($i++ > 0 ? ', ' : '') . $annotation->text;
347
  }
348
  $out .= '</' . $enclosingTag . '>';
349
  return $out;
350
}
351
352
/**
353
 * @todo Please document this function.
354
 * @see http://drupal.org/node/1354
355
 */
356
function cdm_exist_footnote($footnote_list, $footnote) {
357
  $result = FALSE;
358
  if (is_array($footnote_list)) {
359
    foreach ($footnote_list as $element) {
360
      if ($element == $footnote) {
361
        $result = TRUE;
362
      }
363
    }
364
  }
365
  return $result;
366
}
367
368
/**
369
 * @todo Please document this function.
370
 * @see http://drupal.org/node/1354
371
 */
372
function cdm_add_footnote_to_array(&$footnote_list, $footnote) {
373
  if (!cdm_exist_footnote($footnote_list, $footnote)) {
374
    $footnote_list[] = $footnote;
375
  }
376
}
377
378 4ae6064e Andreas Kohlbecker
/**
379
 * Theme function for CDM marker instances
380
 *
381
 * @see compose_cdm_marker();
382
 * @param array $variables
383
 *   - markerType_representation_l10n: the localized representation of the marker.markerType field
384
 */
385
function theme_cdm_marker($variables) {
386
  $class_attribute = null;
387
  //TODO class attribute hacked?, use generic drupal way?
388
  if(isset($variables['attributes']['class'])){
389
    $class_attribute = $variables['attributes']['class'];
390
  }
391
  return '<span class="' . $class_attribute . '">' . $variables['label'] . '</span>';
392
}
393
394 6657531f Andreas Kohlbecker
/* ============================ pager ============================= */
395 c278f2a0 Andreas Kohlbecker
396 6657531f Andreas Kohlbecker
/**
397
 * @todo Please document this function.
398
 * @see http://drupal.org/node/1354
399
 */
400
function theme_cdm_pager($variables) {
401
  $pager = $variables['pager'];
402
  $path = $variables['path'];
403
  $parameters = $variables['parameters'];
404
  $out = '';
405
406
  if ($pager->pagesAvailable > 1) {
407
408
    $out .= '<div class="pager">';
409
    if ($pager->currentIndex > 0) {
410
      $out .= theme('cdm_pager_link', array(
411 c278f2a0 Andreas Kohlbecker
        'text' => '« ' . t('First'),
412 6657531f Andreas Kohlbecker
        'linkIndex' => 0,
413
        'pager' => $pager,
414
        'path' => $path,
415
        'parameters' => $parameters,
416
        'attributes' => array('class' => array('pager-first')),
417
        ));
418
      $out .= theme('cdm_pager_link', array(
419 c278f2a0 Andreas Kohlbecker
        'text' => '‹ ' . t('Previous'),
420 6657531f Andreas Kohlbecker
        'linkIndex' => $pager->currentIndex - 1,
421
        'pager' => $pager,
422
        'path' => $path,
423
        'parameters' => $parameters,
424
        'attributes' => array('class' => array('pager-previous')),
425
        ));
426
    }
427
428
    if ($pager->indices[0] > 0) {
429
      $out .= '<div class="pager-list-dots-left">...</div>';
430
    }
431
432
    foreach ($pager->indices as $index) {
433
      $label = $index + 1;
434
      $out .= theme('cdm_pager_link', array('text' => $label, 'linkIndex' => $index, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
435
  'class' => array('pager-first'),
436
)));
437
    }
438
    if ($pager->indices[count($pager->indices) - 1] < $pager->pagesAvailable - 1) {
439
      $out .= '<div class="pager-list-dots-right">...</div>';
440
    }
441
442
    if ($pager->nextIndex) {
443 c278f2a0 Andreas Kohlbecker
      $out .= theme(
444
        'cdm_pager_link',
445
        array(
446
          'text' => t('Next') . ' ›',
447
          'linkIndex' => $pager->nextIndex,
448
          'pager' => $pager,
449
          'path' => $path,
450
          'parameters' => $parameters,
451
          'attributes' => array(
452
            'class' => array('pager-next'),
453
          )
454
        )
455
      );
456
      $out .= theme(
457
        'cdm_pager_link',
458
        array(
459
          'text' => t('Last') . ' »',
460
          'linkIndex' => $pager->pagesAvailable - 1,
461
          'pager' => $pager,
462
          'path' => $path,
463
          'parameters' => $parameters,
464
          'attributes' => array(
465
            'class' => array('pager-last'),
466
          )
467
        )
468
      );
469 6657531f Andreas Kohlbecker
    }
470
    $out .= '</div>';
471
472
    return $out;
473
  }
474
}
475
476
/**
477
 * @todo Please document this function.
478
 * @see http://drupal.org/node/1354
479
 */
480
function theme_cdm_pager_link($variables) {
481
  $text = $variables['text'];
482
  $linkIndex = $variables['linkIndex'];
483
  $pager = $variables['pager'];
484
  $path = $variables['path'];
485
  $parameters = $variables['parameters'];
486
  $attributes = $variables['attributes'];
487 34dd7be9 Andreas Kohlbecker
488 17cfc574 Andreas Kohlbecker
  // the parameters may still contain the q param,
489
  // but this is already in the path variable
490
  unset($parameters['q']);
491
492 34dd7be9 Andreas Kohlbecker
  $parameters['pager']['pageNumber'] = $linkIndex;
493 6657531f Andreas Kohlbecker
  if ($linkIndex == $pager->currentIndex) {
494
    $out = '<strong>' . $text . '</strong>';
495
  }
496
  else {
497
    // $queryString = drupal_query_string_encode($parameters);
498
    $queryString = $parameters;
499
    $out = l($text, $path, array(
500
      'attributes' => $attributes,
501
      'query' => $queryString,
502
    ));
503
  }
504
  return $out;
505
}
506
507
/* ============================ special buttons ============================= */
508
509
/**
510
 * @todo Please document this function.
511
 * @see http://drupal.org/node/1354
512
 */
513
function theme_cdm_print_button() {
514
515
  drupal_add_js('jQuery(document).ready(function() {
516
         jQuery(\'#print_button\').click(function () {
517
         window.print();
518
     });
519
  });', array('type' => 'inline'));
520
521 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') . '" />';
522 6657531f Andreas Kohlbecker
  // .t('Print this page');
523
  // $output .= l('Print this page', '');
524
  $output .= '<span>Print this page</span>';
525
  $output .= '</div>';
526
527
  return $output;
528
}