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
 * Checks if the taxon specified by the uuid is contained in the currently focused classification.
54
 *
55
 * @param $taxon_uuid
56
 * @return bool
57
 */
58
function taxon_in_current_classification($taxon_uuid) {
59
  $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
      if (get_current_classification_uuid() == $node->classificationUUID) {
64
        $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
 *
77
 * function fragment(){
78
 *    global $fragment;
79
 *    if(!$fragment){
80
 *       $fragment = substr($_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], '#'));
81
 *    }
82
 *   return $fragment;
83
 * }
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
 * - TaxonName
139
 * - 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
function theme_cdm_entities_annotations_as_footnotekeys($variables) {
158
  $cdm_entities = $variables['cdmBase_list'];
159
  $footnote_list_key = $variables['footnote_list_key'];
160
  // check if footnotes for annotations are disabled completely
161
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
162
    return '';
163
  }
164
  $out = '';
165
  if(is_array($cdm_entities)){
166
    // array of entities
167
    $footNoteKeys = cdm_entities_annotations_as_footnotekeys($cdm_entities, $footnote_list_key);
168
  } else {
169
    // single entity
170
    $footNoteKeys = cdm_entity_annotations_as_footnotekeys($cdm_entities, $footnote_list_key);
171
  }
172
  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
 * Create markup for the footnotes mapped to the $footnoteListKey.
182
 *
183
 * @param null $footnote_list_key
184
 *  The footnote list key, see RenderHints::getFootnoteListKey()
185
 * @param $element_tag
186
 *  The tag for the footnote element
187
 *
188
 * @return string
189
 * @throws \Exception
190
 */
191
function cdm_annotation_footnotes($footnote_list_key = null, $element_tag = 'span') {
192
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
193
    return '';
194
  }
195
  return cdm_footnotes($footnote_list_key . '-annotations', $element_tag);
196
}
197

    
198
/**
199
 * @todo Please document this function.
200
 * @see http://drupal.org/node/1354
201
 */
202
function theme_cdm_annotation_content($variables) {
203
  $AnnotationTO = $variables['AnnotationTO'];
204
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_annotations.js');
205
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.form.js');
206

    
207
  $out = theme('cdm_list_of_annotations', array('annotationElements' => $AnnotationTO->annotationElements));
208

    
209
  $annotationUrl = cdm_compose_url(CDM_WS_ANNOTATIONS, array(
210
    $AnnotationTO->uuid,
211
  ));
212
  $annotationProxyUrl = url('cdm_api/proxy/' . urlencode($annotationUrl) . '/cdm_annotation_post');
213

    
214
  // TODO users have to be authenticated to the dataportal to be able to write
215
  // annotations.
216
  $out .= '
217
        <div class="annotation_create">
218
          <form action="' . $annotationProxyUrl . '" method="POST">
219
            <textarea name="annotation"></textarea>
220
            <input type="hidden" name="commentator" value="">
221
            <input type="submit" value="' . t('Save annotation') . '" />
222
          </form>
223
       </div>
224
  ';
225

    
226
  return $out;
227
}
228

    
229
/**
230
 * @todo Please document this function.
231
 * @see http://drupal.org/node/1354
232
 */
233
function theme_cdm_list_of_annotations($variables) {
234
  $annotationElements = $variables['annotationElements'];
235
  $out = '<ul class="annotation_list">';
236

    
237
  foreach ($annotationElements as $key => $row) {
238
    $created[$key] = $row;
239
  }
240
  array_multisort($created, SORT_ASC, $annotationElements);
241

    
242
  foreach ($annotationElements as $annotation) {
243
    $out .= '<li>' . $annotation->text . '</li>';
244
  }
245

    
246
  $out .= '</ul>';
247

    
248
  return $out;
249
}
250

    
251
/* ============================ footnotes ============================= */
252
/**
253
 * @todo Please document this function.
254
 * @see http://drupal.org/node/1354
255
 */
256
function theme_cdm_footnote_key($variables) {
257

    
258
  $footnoteKey = $variables['footnoteKey'];
259
  $separator = $variables['separator'];
260
  $highlightable = $variables['highlightable'];
261
  $separator_off = $variables['separator_off'];
262
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
263
    return '';
264
  }
265
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
266
    return '';
267
  }
268

    
269
  if ($separator_off) {
270
    $separator = '';
271
  }
272
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">' . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
273
  return $out;
274
}
275

    
276
/**
277
 * @param null $footnoteKey
278
 * @param null $footnoteText
279
 * @param string $enclosing_tag
280
 *   default is 'span'
281
 *
282
 * @return string
283
 */
284
function footnote_markup($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span') {
285
  _add_js_footnotes();
286
  if($enclosing_tag == null){
287
    $enclosing_tag = 'span';
288
  }
289
  return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
290
    . '<a name="footnote-' . $footnoteKey . '"></a>'
291
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
292
    . '</' . $enclosing_tag . '>';
293
}
294

    
295
/**
296
 * Create markup for the footnotes mapped to the $footnoteListKey.
297
 *
298
 * @param null $footnote_list_key
299
 *  The footnote list key, see RenderHints::getFootnoteListKey()
300
 * @param $element_tag
301
 *  The tag for the footnote element
302
 *
303
 * @return string
304
 */
305
function cdm_footnotes($footnote_list_key = null, $element_tag = 'span') {
306

    
307
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
308
    return '';
309
  }
310

    
311
  $out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
312
    . FootnoteManager::renderFootnoteList($footnote_list_key)
313
    . '</' . $element_tag . '>';
314

    
315
  FootnoteManager::removeFootnoteList($footnote_list_key);
316
  return $out;
317
}
318

    
319
/**
320
 * Renders the footnotes for annotations and sources, etc.
321
 *
322
 * @param string $footnote_list_key
323
 *    RenderHints::getFootnoteListKey() will be used if this parameter is undefined.
324
 * @param string $enclosingTag
325
 *    Default tag is 'span'
326
 *
327
 * @return string
328
 *    The markup string
329
 *
330
 */
331
function render_cdm_footnotes($footnote_list_key = NULL, $enclosingTag = 'span'){
332

    
333
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
334
    return '';
335
  }
336
  if(!$footnote_list_key){
337
    $footnote_list_key = RenderHints::getFootnoteListKey();
338
  }
339

    
340
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
341
    . FootnoteManager::renderFootnoteList($footnote_list_key . '-annotations') . ' ' . FootnoteManager::renderFootnoteList($footnote_list_key)
342
    . '</' . $enclosingTag . '>';
343
  FootnoteManager::removeFootnoteList($footnote_list_key . '-annotations');
344
  FootnoteManager::removeFootnoteList($footnote_list_key);
345
  return $out;
346
}
347

    
348
/**
349
 * Created markup for the annotations
350
 * @param null $annotations
351
 * @param $enclosingTag
352
 *  Tag name of the DOM element to enclose the annotations.
353
 *
354
 * @return string
355
 */
356
function cdm_annotations($annotations = null, $enclosingTag = span) {
357

    
358
  if (!is_array($annotations)) {
359
    return '';
360
  }
361
  $out = '<' . $enclosingTag . ' class="annotations">';
362
  $i = 0;
363
  foreach ($annotations as $annotation) {
364
    $out .= ($i++ > 0 ? ', ' : '') . $annotation->text;
365
  }
366
  $out .= '</' . $enclosingTag . '>';
367
  return $out;
368
}
369

    
370
/**
371
 * @todo Please document this function.
372
 * @see http://drupal.org/node/1354
373
 */
374
function cdm_exist_footnote($footnote_list, $footnote) {
375
  $result = FALSE;
376
  if (is_array($footnote_list)) {
377
    foreach ($footnote_list as $element) {
378
      if ($element == $footnote) {
379
        $result = TRUE;
380
      }
381
    }
382
  }
383
  return $result;
384
}
385

    
386
/**
387
 * @todo Please document this function.
388
 * @see http://drupal.org/node/1354
389
 */
390
function cdm_add_footnote_to_array(&$footnote_list, $footnote) {
391
  if (!cdm_exist_footnote($footnote_list, $footnote)) {
392
    $footnote_list[] = $footnote;
393
  }
394
}
395

    
396
/**
397
 * Theme function for CDM marker instances
398
 *
399
 * @see compose_cdm_marker();
400
 * @param array $variables
401
 *   - markerType_representation_l10n: the localized representation of the marker.markerType field
402
 */
403
function theme_cdm_marker($variables) {
404
  $class_attribute = null;
405
  //TODO class attribute hacked?, use generic drupal way?
406
  if(isset($variables['attributes']['class'])){
407
    $class_attribute = $variables['attributes']['class'];
408
  }
409
  return '<span class="' . $class_attribute . '">' . $variables['label'] . '</span>';
410
}
411

    
412
/* ============================ pager ============================= */
413

    
414
/**
415
 * @todo Please document this function.
416
 * @see http://drupal.org/node/1354
417
 */
418
function theme_cdm_pager($variables) {
419
  $pager = $variables['pager'];
420
  $path = $variables['path'];
421
  $parameters = $variables['parameters'];
422
  $out = '';
423

    
424
  if ($pager->pagesAvailable > 1) {
425

    
426
    $out .= '<div class="pager">';
427
    if ($pager->currentIndex > 0) {
428
      $out .= theme('cdm_pager_link', array(
429
        'text' => '« ' . t('First'),
430
        'linkIndex' => 0,
431
        'pager' => $pager,
432
        'path' => $path,
433
        'parameters' => $parameters,
434
        'attributes' => array('class' => array('pager-first')),
435
        ));
436
      $out .= theme('cdm_pager_link', array(
437
        'text' => '‹ ' . t('Previous'),
438
        'linkIndex' => $pager->currentIndex - 1,
439
        'pager' => $pager,
440
        'path' => $path,
441
        'parameters' => $parameters,
442
        'attributes' => array('class' => array('pager-previous')),
443
        ));
444
    }
445

    
446
    if ($pager->indices[0] > 0) {
447
      $out .= '<div class="pager-list-dots-left">...</div>';
448
    }
449

    
450
    foreach ($pager->indices as $index) {
451
      $label = $index + 1;
452
      $out .= theme('cdm_pager_link', array('text' => $label, 'linkIndex' => $index, 'pager' => $pager, 'path' => $path, 'parameters' => $parameters, 'attributes' => array(
453
  'class' => array('pager-first'),
454
)));
455
    }
456
    if ($pager->indices[count($pager->indices) - 1] < $pager->pagesAvailable - 1) {
457
      $out .= '<div class="pager-list-dots-right">...</div>';
458
    }
459

    
460
    if ($pager->nextIndex) {
461
      $out .= theme(
462
        'cdm_pager_link',
463
        array(
464
          'text' => t('Next') . ' ›',
465
          'linkIndex' => $pager->nextIndex,
466
          'pager' => $pager,
467
          'path' => $path,
468
          'parameters' => $parameters,
469
          'attributes' => array(
470
            'class' => array('pager-next'),
471
          )
472
        )
473
      );
474
      $out .= theme(
475
        'cdm_pager_link',
476
        array(
477
          'text' => t('Last') . ' »',
478
          'linkIndex' => $pager->pagesAvailable - 1,
479
          'pager' => $pager,
480
          'path' => $path,
481
          'parameters' => $parameters,
482
          'attributes' => array(
483
            'class' => array('pager-last'),
484
          )
485
        )
486
      );
487
    }
488
    $out .= '</div>';
489

    
490
    return $out;
491
  }
492
}
493

    
494
/**
495
 * @todo Please document this function.
496
 * @see http://drupal.org/node/1354
497
 */
498
function theme_cdm_pager_link($variables) {
499
  $text = $variables['text'];
500
  $linkIndex = $variables['linkIndex'];
501
  $pager = $variables['pager'];
502
  $path = $variables['path'];
503
  $parameters = $variables['parameters'];
504
  $attributes = $variables['attributes'];
505

    
506
  // the parameters may still contain the q param,
507
  // but this is already in the path variable
508
  unset($parameters['q']);
509

    
510
  $parameters['pager']['pageNumber'] = $linkIndex;
511
  if ($linkIndex == $pager->currentIndex) {
512
    $out = '<strong>' . $text . '</strong>';
513
  }
514
  else {
515
    // $queryString = drupal_query_string_encode($parameters);
516
    $queryString = $parameters;
517
    $out = l($text, $path, array(
518
      'attributes' => $attributes,
519
      'query' => $queryString,
520
    ));
521
  }
522
  return $out;
523
}
524

    
525
/* ============================ special buttons ============================= */
526

    
527
/**
528
 * @todo Please document this function.
529
 * @see http://drupal.org/node/1354
530
 */
531
function theme_cdm_print_button() {
532

    
533
  drupal_add_js('jQuery(document).ready(function() {
534
         jQuery(\'#print_button\').click(function () {
535
         window.print();
536
     });
537
  });', array('type' => 'inline'));
538

    
539
  $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') . '" />';
540
  // .t('Print this page');
541
  // $output .= l('Print this page', '');
542
  $output .= '<span>Print this page</span>';
543
  $output .= '</div>';
544

    
545
  return $output;
546
}
547

    
(1-1/9)