Project

General

Profile

Download (15.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
 * @todo Please document this function.
54
 * @see http://drupal.org/node/1354
55
 */
56
function taxon_in_current_tree($taxon_uuid) {
57
  $taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon_uuid);
58
  $taxon_in_current_tree = FALSE;
59
  if (is_array($taxon_nodes)) {
60
    foreach ($taxon_nodes as $node) {
61
      if (get_taxonomictree_uuid_selected() == $node->classification->uuid) {
62
        $taxon_in_current_tree = TRUE;
63
        break;
64
      }
65
    }
66
  }
67
  return $taxon_in_current_tree;
68
}
69

    
70
/**
71
 * TODO if getting fragment from request is possible remove
72
 * $_REQUEST['highlite'] HACK
73
 * NOT WORKING since fragments are not available to the server
74
 *
75
 * function fragment(){
76
 *    global $fragment;
77
 *    if(!$fragment){
78
 *       $fragment = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '#'));
79
 *    }
80
 *   return $fragment;
81
 * }
82
 */
83
function uuid_anchor($uuid, $innerHTML) {
84
  $highlite = FALSE;
85
  $params = drupal_get_query_parameters();
86
  if (isset($params['highlite']) && $params['highlite'] == $uuid) {
87
    $highlite = TRUE;
88
  }
89

    
90
  return '<a name="' . $uuid . '" ></a><span class="' . ($highlite ? 'highlite' : '') . '">' . $innerHTML . '</span>';
91
}
92

    
93
/**
94
 * @todo Enter description here...
95
 * @deprecated looks like this is not used anymore
96
 */
97
/*
98
function tagNameParts($name, $numOfNameTokens) {
99
  $out = '<span class="name">';
100

    
101
  $token = strtok($name, " \n\t");
102
  $i = 0;
103
  $noSpace = TRUE;
104
  while ($token != FALSE) {
105
    if ($i == $numOfNameTokens) {
106
      $out .= '</span> <span class="authors">';
107
      $noSpace = TRUE;
108
    }
109
    $out .= ($noSpace ? '' : ' ') . $token;
110
    $noSpace = FALSE;
111
    $token = strtok(" \n\t");
112
    $i++;
113
  }
114
  return $out . '</span>';
115
}
116
*/
117

    
118
/**
119
 * Returns HTML for an array of TaggedText items.
120
 *
121
 * Converts an array of TaggedText items into a sequence of corresponding html
122
 * tags whereas each item will provided with a class attribute which set to
123
 * the key of the TaggedText item.
124
 *
125
 * @param array $variables
126
 *   An associative array containing:
127
 *   - taggedtxt: The array to format into html.
128
 *   - tag: The html tag to use.
129
 *   - glue: The string by which the chained text tokens are
130
 *     concatenated together. Default is a blank character.
131
 *   - skiptags: tags to skip.
132
 *
133
 * @ingroup themeable
134
 */
135
function theme_cdm_taggedtext2html($variables) {
136

    
137
  $taggedtxt = $variables['taggedtxt'];
138
  $tag = $variables['tag'];
139
  $glue = $variables['glue'];
140
  $skiptags = $variables['skiptags'];
141
  $out = '';
142
  $i = 0;
143
  foreach ($taggedtxt as $tt) {
144
    if (!in_array($tt->type, $skiptags) && strlen($tt->text) > 0) {
145
      $out .= (strlen($out) > 0 && ++$i < count($taggedtxt) ? $glue : '') . '<' . $tag . ' class="' . $tt->type . '">' . t($tt->text) . '</' . $tag . '>';
146
    }
147
  }
148
  return $out;
149
}
150

    
151
/* ============================ annotations ============================= */
152

    
153
/**
154
 * Returns HTML for annotations to cdm objects.
155
 *
156
 * Almost any cdmObject may be annotated. Therefore we provide a generic way to
157
 * display as well as create or update annotations. The following cdm classes
158
 * are annotatable:
159
 *
160
 * - DescriptionElementBase
161
 * - EventBase
162
 * - HomotypicalGroup
163
 * - IdentifiableEntity
164
 * - DescriptionBase
165
 * - IdentifiableMediaEntity
166
 * - Media
167
 * - Sequence
168
 * - TaxonBase
169
 * - TaxonNameBase
170
 * - TaxonomicTree
171
 * - TermBase
172
 * - LanguageStringBase
173
 * - ReferencedEntityBase
174
 * - NomenclaturalStatus
175
 * - OriginalSourceBase
176
 * - RelationshipBase
177
 * - TypeDesignationBase
178
 * - TaxonNode
179
 * - WorkingSet
180
 *
181
 * @param array $variables
182
 *   An associative array containing:
183
 *  - cdmBase_list: An array of CdmBase instances or a single instance.
184
 *  - footnote_list_key
185
 *
186
 * @ingroup themeable
187
 */
188
function theme_cdm_annotations_as_footnotekeys($variables) {
189
  $cdmBase_list = $variables['cdmBase_list'];
190
  $footnote_list_key = $variables['footnote_list_key'];
191
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
192
    return '';
193
  }
194
  $out = '';
195
  $footNoteKeys = cdm_annotations_as_footnotekeys($cdmBase_list, $footnote_list_key);
196
  foreach ($footNoteKeys as $a) {
197
    // $out .= theme('cdm_footnote_key', $a, $a->footnoteListKey, (isset($out)?
198
    // ',' : ''));
199
    $out .= theme('cdm_footnote_key', array('footnoteKey' => $a, 'separator' => ($out ? ',' : '')));
200
  }
201
  return $out;
202
}
203

    
204
/**
205
 * @todo Please document this function.
206
 * @see http://drupal.org/node/1354
207
 */
208
function theme_cdm_annotation_footnotes($variables) {
209
  $footnoteListKey = $variables['footnoteListKey'];
210
  $enclosingTag = $variables['enclosingTag'];
211
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
212
    return '';
213
  }
214
  return theme('cdm_footnotes', array('footnoteListKey' => $footnoteListKey . '-annotations', 'enclosingTag' => $enclosingTag));
215
}
216

    
217
/**
218
 * @todo Please document this function.
219
 * @see http://drupal.org/node/1354
220
 */
221
function theme_cdm_annotation_content($variables) {
222
  $AnnotationTO = $variables['AnnotationTO'];
223
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_annotations.js');
224
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.form.js');
225

    
226
  $out = theme('cdm_list_of_annotations', array('annotationElements' => $AnnotationTO->annotationElements));
227

    
228
  $annotationUrl = cdm_compose_url(CDM_WS_ANNOTATIONS, array(
229
    $AnnotationTO->uuid,
230
  ));
231
  $annotationProxyUrl = url('cdm_api/proxy/' . urlencode($annotationUrl) . '/cdm_annotation_post');
232

    
233
  // TODO users have to be authenticated to the dataportal to be able to write
234
  // annotations.
235
  $out .= '
236
        <div class="annotation_create">
237
          <form action="' . $annotationProxyUrl . '" method="POST">
238
            <textarea name="annotation"></textarea>
239
            <input type="hidden" name="commentator" value="">
240
            <input type="submit" value="' . t('Save annotation') . '" />
241
          </form>
242
       </div>
243
  ';
244

    
245
  return $out;
246
}
247

    
248
/**
249
 * @todo Please document this function.
250
 * @see http://drupal.org/node/1354
251
 */
252
function theme_cdm_list_of_annotations($variables) {
253
  $annotationElements = $variables['annotationElements'];
254
  $out = '<ul class="annotation_list">';
255

    
256
  foreach ($annotationElements as $key => $row) {
257
    $created[$key] = $row;
258
  }
259
  array_multisort($created, SORT_ASC, $annotationElements);
260

    
261
  foreach ($annotationElements as $annotation) {
262
    $out .= '<li>' . $annotation->text . '</li>';
263
  }
264

    
265
  $out .= '</ul>';
266

    
267
  return $out;
268
}
269

    
270
/* ============================ footnotes ============================= */
271
/**
272
 * @todo Please document this function.
273
 * @see http://drupal.org/node/1354
274
 */
275
function theme_cdm_footnote_key($variables) {
276

    
277
  $footnoteKey = $variables['footnoteKey'];
278
  $separator = $variables['separator'];
279
  $highlightable = $variables['highlightable'];
280
  $separator_off = $variables['separator_off'];
281
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
282
    return '';
283
  }
284
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
285
    return '';
286
  }
287

    
288
  if ($separator_off) {
289
    $separator = '';
290
  }
291
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">' . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
292
  return $out;
293
}
294

    
295
/**
296
 * @todo Please document this function.
297
 * @see http://drupal.org/node/1354
298
 */
299
function theme_cdm_footnote($variables) {
300
  $footnoteKey = $variables['footnoteKey'];
301
  $footnoteText = $variables['footnoteText'];
302
  $enclosing_tag = $variables['enclosing_tag'];
303
  _add_js_footnotes();
304
  $out = '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
305
    . '<a name="footnote-' . $footnoteKey . '"></a>'
306
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
307
    . '</' . $enclosing_tag . '>';
308
  return $out;
309
}
310

    
311
/**
312
 * @todo Please document this function.
313
 * @see http://drupal.org/node/1354
314
 */
315
function theme_cdm_footnotes($variables) {
316
  $footnoteListKey = $variables['footnoteListKey'];
317
  $enclosingTag = $variables['enclosingTag'];
318
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
319
    return '';
320
  }
321

    
322
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnoteListKey . ' ">'
323
    . FootnoteManager::renderFootnoteList($footnoteListKey)
324
    . '</' . $enclosingTag . '>';
325

    
326
  FootnoteManager::removeFootnoteList($footnoteListKey);
327
  return $out;
328
}
329

    
330
/**
331
 * @todo Please document this function.
332
 * @see http://drupal.org/node/1354
333
 */
334
function theme_cdm_annotations($variables) {
335
  $annotations = $variables['annotations'];
336
  $enclosingTag = $variables['enclosingTag'];
337
  if (!is_array($annotations)) {
338
    return;
339
  }
340
  $out = '<' . $enclosingTag . ' class="annotations">';
341
  $i = 0;
342
  foreach ($annotations as $annotation) {
343
    $out .= ($i++ > 0 ? ', ' : '') . $annotation->text;
344
  }
345
  $out .= '</' . $enclosingTag . '>';
346
  return $out;
347
}
348

    
349
/**
350
 * @todo Please document this function.
351
 * @see http://drupal.org/node/1354
352
 */
353
function cdm_exist_footnote($footnote_list, $footnote) {
354
  $result = FALSE;
355
  if (is_array($footnote_list)) {
356
    foreach ($footnote_list as $element) {
357
      if ($element == $footnote) {
358
        $result = TRUE;
359
      }
360
    }
361
  }
362
  return $result;
363
}
364

    
365
/**
366
 * @todo Please document this function.
367
 * @see http://drupal.org/node/1354
368
 */
369
function cdm_add_footnote_to_array(&$footnote_list, $footnote) {
370
  if (!cdm_exist_footnote($footnote_list, $footnote)) {
371
    $footnote_list[] = $footnote;
372
  }
373
}
374

    
375
/**
376
 * Theme function for CDM marker instances
377
 *
378
 * @see compose_cdm_marker();
379
 * @param array $variables
380
 *   - markerType_representation_l10n: the localized representation of the marker.markerType field
381
 */
382
function theme_cdm_marker($variables) {
383
  $class_attribute = null;
384
  //TODO class attribute hacked?, use generic drupal way?
385
  if(isset($variables['attributes']['class'])){
386
    $class_attribute = $variables['attributes']['class'];
387
  }
388
  return '<span class="' . $class_attribute . '">' . $variables['label'] . '</span>';
389
}
390

    
391
/* ============================ pager ============================= */
392
/**
393
 * @todo Please document this function.
394
 * @see http://drupal.org/node/1354
395
 */
396
function theme_cdm_pager($variables) {
397
  $pager = $variables['pager'];
398
  $path = $variables['path'];
399
  $parameters = $variables['parameters'];
400
  $out = '';
401

    
402
  if ($pager->pagesAvailable > 1) {
403

    
404
    $out .= '<div class="pager">';
405
    if ($pager->currentIndex > 0) {
406
      $out .= theme('cdm_pager_link', array(
407
        'text' => t('« first'),
408
        'linkIndex' => 0,
409
        'pager' => $pager,
410
        'path' => $path,
411
        'parameters' => $parameters,
412
        'attributes' => array('class' => array('pager-first')),
413
        ));
414
      $out .= theme('cdm_pager_link', array(
415
        'text' => t('‹ previous'),
416
        'linkIndex' => $pager->currentIndex - 1,
417
        'pager' => $pager,
418
        'path' => $path,
419
        'parameters' => $parameters,
420
        'attributes' => array('class' => array('pager-previous')),
421
        ));
422
    }
423

    
424
    if ($pager->indices[0] > 0) {
425
      $out .= '<div class="pager-list-dots-left">...</div>';
426
    }
427

    
428
    foreach ($pager->indices as $index) {
429
      $label = $index + 1;
430
      $out .= theme('cdm_pager_link', array('text' => $label, 'linkIndex' => $index, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
431
  'class' => array('pager-first'),
432
)));
433
    }
434
    if ($pager->indices[count($pager->indices) - 1] < $pager->pagesAvailable - 1) {
435
      $out .= '<div class="pager-list-dots-right">...</div>';
436
    }
437

    
438
    if ($pager->nextIndex) {
439
      $out .= theme('cdm_pager_link', array('text' => t('next ›'), 'linkIndex' => $pager->nextIndex, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
440
  'class' => array('pager-next'),
441
)));
442
      $out .= theme('cdm_pager_link', array('text' => t('last »'), 'linkIndex' => $pager->pagesAvailable - 1, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
443
  'class' => array('pager-last'),
444
)));
445
    }
446
    $out .= '</div>';
447

    
448
    return $out;
449
  }
450
}
451

    
452
/**
453
 * @todo Please document this function.
454
 * @see http://drupal.org/node/1354
455
 */
456
function theme_cdm_pager_link($variables) {
457
  $text = $variables['text'];
458
  $linkIndex = $variables['linkIndex'];
459
  $pager = $variables['pager'];
460
  $path = $variables['path'];
461
  $parameters = $variables['parameters'];
462
  $attributes = $variables['attributes'];
463

    
464
  $out = '';
465
  $parameters['pager']['pageNumber'] = $linkIndex;
466
  if ($linkIndex == $pager->currentIndex) {
467
    $out = '<strong>' . $text . '</strong>';
468
  }
469
  else {
470
    // $queryString = drupal_query_string_encode($parameters);
471
    $queryString = $parameters;
472
    $out = l($text, $path, array(
473
      'attributes' => $attributes,
474
      'query' => $queryString,
475
    ));
476
  }
477
  return $out;
478
}
479

    
480
/* ============================ special buttons ============================= */
481
/**
482
 * @todo Please document this function.
483
 * @see http://drupal.org/node/1354
484
 */
485
function theme_cdm_back_to_search_result_button() {
486
  $out = '';
487

    
488
  $show_button = variable_get('cdm_dataportal_show_back_to_search_results');
489
  if (isset($_SESSION['cdm']['search']) && isset($show_button) &&  $show_button == 1) {
490
    /* ['cdm']['last_search'] */
491
    // $out .= '<div id="backButton">'.l(t('Back to search result'), $_SESSION
492
    // ).'</div>';
493
    $out .= '<div id="backButton" class="clearfix">' . l(t('Back to search result'), "http://" . $_SERVER['SERVER_NAME'] . $_SESSION['cdm']['last_search']) . '</div>';
494
  }
495
  return $out;
496
}
497

    
498
/**
499
 * @todo Please document this function.
500
 * @see http://drupal.org/node/1354
501
 */
502
function theme_cdm_back_to_image_gallery_button() {
503
  $out = '<div id="backToGalleryButton">' . l(t('Back to Images'), $_SESSION['cdm']['last_gallery']) . '</div>';
504
  return $out;
505
}
506

    
507
/**
508
 * @todo Please document this function.
509
 * @see http://drupal.org/node/1354
510
 */
511
function theme_cdm_print_button() {
512

    
513
  drupal_add_js('jQuery(document).ready(function() {
514
         jQuery(\'#print_button\').click(function () {
515
         window.print();
516
     });
517
  });', array('type' => 'inline'));
518

    
519
  $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') . '" />';
520
  // .t('Print this page');
521
  // $output .= l('Print this page', '');
522
  $output .= '<span>Print this page</span>';
523
  $output .= '</div>';
524

    
525
  return $output;
526
}
527

    
(1-1/9)