Project

General

Profile

Download (15.5 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
  _add_js_footnotes();
303
  $out = '<span class="footnote footnote-' . $footnoteKey . '">'
304
    . '<a name="footnote-' . $footnoteKey . '"></a>'
305
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
306
    . '</span>';
307
  return $out;
308
}
309

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

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

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

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

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

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

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

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

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

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

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

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

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

    
447
    return $out;
448
  }
449
}
450

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

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

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

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

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

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

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

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

    
524
  return $output;
525
}
526

    
(2-2/10)