Project

General

Profile

Download (15.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * @file
5
 * Functions for handling CDM TaggedText arrays
6
 *
7
 *
8
 * @copyright
9
 *   (C) 2007-2018 EDIT
10
 *   European Distributed Institute of Taxonomy
11
 *   http://www.e-taxonomy.eu
12
 *
13
 *   The contents of this module are subject to the Mozilla
14
 *   Public License Version 1.1.
15
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
16
 *
17
 * @author
18
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
19
 */
20

    
21
function tagged_text_new($tag_type, $text = null){
22
  $tt = new stdClass();
23
  $tt->type = $tag_type;
24
  $tt->text = $text;
25
  return $tt;
26
}
27

    
28
/**
29
 * Adds an array of render options to specific tagged text elements.
30
 *
31
 * The attributes are added as $tagged_text_item['attributes']. Existing 'attributes' are
32
 * preserved by merging new ones  to the existing array.
33
 *
34
 * @param array $taggedtxt
35
 * @param array $attributes_map array
36
 *   An array of arrays with following elements in each element array:
37
 *    - 'filter-type': the tagged text type to which the attributes are applicable
38
 *    - 'filter-uuid': (optional) and optional filter to only match a specific uuid.
39
 *    - 'attributes': array of attributes as accepted by drupal_attributes()
40
 *    - 'prefix': like the drupal_render() '#prefix' option
41
 *    - 'suffix': like the drupal_render() '#prefix' option
42
 */
43
function cdm_tagged_text_add_options(array &$taggedtxt, array $attributes_map){
44

    
45
  foreach ($attributes_map as $attribute_data){
46
    foreach ($taggedtxt as &$tt){
47
      if($tt->type == $attribute_data['filter-type']){
48
        if(isset($attribute_data['filter-uuid'])){
49
          if($tt->uuid !== $attribute_data['filter-uuid']){
50
            // ignore
51
            continue;
52
          }
53
        }
54
        // $tt matched type and filter-uuid if set, apply options
55
        foreach (array('attributes', 'prefix', 'suffix') as $key ){
56
          if(isset($attribute_data[$key])){
57
            $tt->$key = $attribute_data[$key];
58
          }
59
        }
60
      }
61
    }
62
  }
63
}
64

    
65
/**
66
 * Walks the passed TaggedText array to find all elements which have a
67
 * TaggedText->entityReference. For each of these the taggedTexts is loaded
68
 * from the webservice and the original entry in the TaggedText array will be
69
 * replaced by the newly loaded array.
70
 *
71
 * Existing 'attributes' (@see cdm_tagged_text_add_options()) fields will be preserved by
72
 * copying them to each of the replacement tagged text items. 'prefix' will be added to
73
 * the first of the replace items and 'suffix' to the last one.
74
 *
75
 *
76
 * @param array $taggedtxt
77
 *    The original TaggedText array
78
 * @param array $skiptags
79
 *    Optional list of tag names to skip
80
 * @return array
81
 *    The new tagged text with all TaggedText->entityReference objects expanded
82
 */
83
function cdm_tagged_text_expand_entity_references(array $taggedtxt, $skiptags = array()) {
84
  $tagged_text_expanded = array();
85
  foreach ($taggedtxt as $tt) {
86
    if (isset($tt->entityReference) && !in_array($tt->type, $skiptags)) {
87
      $base_uri = cdm_ws_base_uri($tt->entityReference->type);
88
      if($base_uri){
89
        $tagged_text_method = "/taggedText";
90
        if($base_uri == CDM_WS_NAME){
91
          $tagged_text_method = "/taggedName";
92
        }
93
        $referenced_tt = cdm_ws_get($base_uri . "/" . $tt->entityReference->uuid . $tagged_text_method);
94
        if($referenced_tt){
95
          if(isset($tt->attributes)){
96
            foreach($referenced_tt as $reftt){
97
              $reftt->attributes = $tt->attributes;
98
            }
99
          }
100
          if(isset($tt->prefix)){
101
            $referenced_tt[0]->prefix = $tt->prefix;
102
          }
103
          if(isset($tt->suffix)){
104
            $referenced_tt[count($referenced_tt)-1]->suffix = $tt->suffix;
105
          }
106
          $tagged_text_expanded = array_merge($tagged_text_expanded, $referenced_tt);
107
          continue;
108
        }
109
      }
110
    }
111
    // default case
112
    $tagged_text_expanded[] = $tt;
113
  }
114
  return $tagged_text_expanded;
115
}
116

    
117
/**
118
 * Converts an array of TaggedText items into corresponding html tags.
119
 *
120
 * Each item is provided with a class attribute which is set to the key of the
121
 * TaggedText item.
122
 *
123
 * Tagged text where the type starts with 'PLACEHOLDER_' will be added to the markup as plain text whereas the
124
 * taggedText->type wrapped in curly brackets: '{'. $tt->text . '}' is used as text.
125
 * see tagged_text_extract_reference_and_detail()
126
 *
127
 * In addition to the tagged text element fields as defined in the cdm this method also recognizes:
128
 *    - 'attributes': array of attributes as accepted by drupal_attributes()
129
 *    - 'prefix': like the drupal_render() '#prefix' option
130
 *    - 'suffix': like the drupal_render() '#prefix' option
131
 * See also cdm_tagged_text_add_options()
132
 *
133
 * The algorithm of this functions is basically the same as for
134
 * eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper.createString(List<TaggedText> tags, HTMLTagRules htmlRules)
135
 *
136
 * @param array $taggedtxt
137
 *   Array with text items to convert.
138
 * @param array $skiptags
139
 *   Array of tag names to skip
140
 *
141
 * @return string
142
 *   The markup.
143
 */
144
function cdm_tagged_text_to_markup(array $taggedtxt, $skiptags = array(), $tag = 'span') {
145

    
146
  $out = '';
147
  $was_separator = false;
148
  $last_type = null;
149
  $last_suffix = '';
150
  $i = 0;
151
  foreach ($taggedtxt as $tt) {
152
    if (!in_array($tt->type, $skiptags) && $tt->text) {
153

    
154
      $is_first = $i == 0;
155
      $is_separator = is_tagged_text_sepatator_type($tt->type);
156
      if(str_beginsWith($tt->type, 'PLACEHOLDER_')){
157
        $out .=  '{'. $tt->type . '}';
158
      } else {
159

    
160
        // attributes
161
        $attributes = array();
162
        if(isset($tt->attributes)){
163
          $attributes = $tt->attributes;
164
        }
165
        if(!isset($attributes['class'])){
166
          $attributes['class'] = array();
167
        }
168
        $attributes['class'][] = $tt->type;
169
        if(isset($tt->entityReference)){
170
          $attributes['class'][] = html_class_attribute_ref($tt->entityReference);
171
        }
172

    
173
        // prefix and suffix
174
        $prefix = '';
175
        $suffix = '';
176
        if(isset($tt->prefix)){
177
          $prefix = $tt->prefix;
178
        }
179
        if(isset($tt->suffix)){
180
          $suffix = $tt->suffix;
181
        }
182

    
183
        if(($last_suffix || $last_type && $last_type != $tt->type) && $tag) {
184
          $out .= '</' . $tag . '>' . $last_suffix;
185
        }
186
        if(($prefix || !$last_type || $last_type != $tt->type) && $tag){
187
          $out .= $prefix . '<' . $tag . drupal_attributes($attributes) . '>';
188
        }
189
        if(!$is_separator && !$was_separator && !$is_first){
190
          $out .= " ";
191
        }
192
        $out .= t('@text', array('@text' => $tt->text));
193
        $was_separator = $is_separator;
194
        $last_type = $tt->type;
195
        $last_suffix = $suffix;
196
      }
197
    }
198
    $i++;
199
  }
200
  if($tag) {
201
    $out .= '</' . $tag . '>';
202
  }
203
  return $out;
204
}
205

    
206
/**
207
 * Converts an array of TaggedText items into corresponding plain text string
208
 *
209
 * Each item is provided with a class attribute which is set to the key of the
210
 * TaggedText item.
211
 *
212
 * The algorithm of this functions is basically the same as for
213
 * eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper.createString(List<TaggedText> tags)
214
 *
215
 * @param array $taggedtxt
216
 *   Array with text items to convert.
217
 * @param array $skiptags
218
 *   Array of tag names to skip
219
 *
220
 * @return string
221
 *   The plain text
222
 */
223
function cdm_tagged_text_to_string(array $taggedtxt, $skiptags = array()) {
224

    
225
  return cdm_tagged_text_to_markup($taggedtxt, $skiptags, null);
226
}
227

    
228
/**
229
 * See cdmlib: boolean eu.etaxonomy.cdm.strategy.cache.TagEnum.isSeparator();
230
 *
231
 * @return bool
232
 */
233
function is_tagged_text_sepatator_type($tagged_text_type){
234
  static $separator_names = array('separator', 'postSeparator');
235
  $result = array_search($tagged_text_type, $separator_names) !== false;
236
  return $result;
237
}
238

    
239

    
240
/**
241
 * Finds the text tagged with $tag_type in an array of taggedText instances.
242
 *
243
 *
244
 * @param array $taggedtxt
245
 *   Array with text items.
246
 * @param array $include_tag_types
247
 *   Array of the tag types for which to find text items in the $taggedtxt array, or NULL
248
 *   to return all texts.
249
 *
250
 * @return array
251
 *   An array with the texts mapped by $tag_type.
252
 */
253
function cdm_tagged_text_values(array $taggedtxt, $include_tag_types = NULL) {
254
  $tokens = array();
255
  if (!empty($taggedtxt)) {
256
    foreach ($taggedtxt as $tagtxt) {
257
      if ($include_tag_types === NULL || array_search($tagtxt->type, $include_tag_types) !== false) {
258
        $tokens[] = $tagtxt->text;
259
      }
260
    }
261
  }
262
  return $tokens;
263
}
264

    
265
/**
266
 * Preprocess the taggedTitle arrays.
267
 *
268
 * Step 1: Turns 'newly' introduces tag types ("hybridSign")
269
 * into tag type "name"
270
 *
271
 * Step 2: Two taggedTexts which have the same type and which have
272
 * a separator between them are merged together.
273
 *
274
 * @param array $taggedTextList
275
 *    An array of TaggedText objects
276
 */
277
function normalize_tagged_text(&$taggedTextList) {
278

    
279
  if (is_array($taggedTextList)) {
280

    
281
    // First pass: rename.
282
    for ($i = 0; $i < count($taggedTextList); $i++) {
283

    
284
      if ($taggedTextList[$i]->type == "hybridSign") {
285
        $taggedTextList[$i]->type = "name";
286
      }
287
    }
288

    
289
    // Second pass: resolve separators.
290
    $taggedNameListNew = array();
291
    for ($i = 0; $i < count($taggedTextList); $i++) {
292

    
293
      // elements of the same type concatenated by a separator should be merged together
294
      if (isset($taggedTextList[$i + 2]) && $taggedTextList[$i + 1]->type == "separator" && $taggedTextList[$i]->type == $taggedTextList[$i + 2]->type) {
295
        $taggedName = clone $taggedTextList[$i];
296
        $taggedName->text = $taggedName->text . $taggedTextList[$i + 1]->text . $taggedTextList[$i + 2]->text;
297
        $taggedNameListNew[] = $taggedName;
298
        ++$i;
299
        ++$i;
300
        continue;
301
      }
302
      // no special handling
303
      $taggedNameListNew[] = $taggedTextList[$i];
304

    
305
    }
306
    $taggedTextList = $taggedNameListNew;
307
  }
308
}
309

    
310
/**
311
 * Extracts from a tagged text array the tagged text for a references which is concatenated with citation
312
 * detail separator and citation detail into one single element.
313
 *
314
 * @param $tagged_text
315
 *    The tagged text to operate on
316
 * @param string $ref_tag_type
317
 *    The tagtype for a secreference is "secReference", but "relSecReference" is also used in case of relationships.
318
 * @param bool $replace_with_placeholder
319
 *    Indicates the method to add a empty placeholder tagged text alement as relpacement for the extrated tagged text
320
 *    elements.
321
 * @param string $ref_detail_tag_type
322
 * @return array
323
 */
324
function tagged_text_extract_reference(&$tagged_text, $ref_tag_type = "reference", $replace_with_placeholder = false) {
325

    
326
  $extracted_tt = array();
327
  if (is_array($tagged_text)) {
328
    $extract_pos = null;
329
    for ($i = 0; $i < count($tagged_text); $i++) {
330
      if ($tagged_text[$i]->type == $ref_tag_type){
331
        if ($i > 0 && $tagged_text[$i - 1]->type == 'separator') {
332
          // the reference may be preceeded by a separator in case it is not a in-reference
333
          $extracted_tt[] = $tagged_text[$i - 1];
334
          unset($tagged_text[$i - 1]);
335
        } else {
336
          // need to add a separator to since the reference tagged text will become the first element
337
          // ant thus will not be preceded by a separator
338
          $extracted_tt[] = tagged_text_new('separator', ' ');
339
        }
340
        $extracted_tt[] = $tagged_text[$i];
341
        if ($replace_with_placeholder) {
342
          // text must not be null, see cdm_tagged_text_to_markup()
343
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $ref_tag_type, "PLACEHOLDER_" . $ref_tag_type);
344
        } else {
345
          unset($tagged_text[$i]);
346
        }
347
        break;
348
      }
349
    }
350
  }
351
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
352
  return $extracted_tt;
353
}
354

    
355
/**
356
 * Extracts the tagged text for sec references with separator and citation detail from a tagged text array.
357
 * @param $tagged_text
358
 *    The tagged text to operate on
359
 * @param string $ref_tag_type
360
 *    The tagtype for a secreference is "secReference", but "relSecReference" is also used in case of relationships.
361
 * @param bool $replace_with_placeholder
362
 *    Indicates the method to add a empty placeholder tagged text alement as relpacement for the extrated tagged text
363
 *    elements.
364
 * @param string $ref_detail_tag_type
365
 * @return array
366
 */
367
function tagged_text_extract_reference_and_detail(&$tagged_text, $ref_tag_type = "secReference", $replace_with_placeholder = false) {
368

    
369
  $extracted_tt = array();
370
  if (is_array($tagged_text)) {
371
    $extract_pos = null;
372
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
373
      if ($tagged_text[$i + 1]->type == $ref_tag_type && $tagged_text[$i]->type == "separator"){
374
        $extracted_tt[0] = $tagged_text[$i];
375
        $extracted_tt[1] = $tagged_text[$i + 1];
376

    
377
        if($replace_with_placeholder){
378
          // text must not be null, see cdm_tagged_text_to_markup()
379
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $ref_tag_type, "PLACEHOLDER_" . $ref_tag_type);
380
        } else {
381
          unset($tagged_text[$i]);
382
        }
383
        unset($tagged_text[$i + 1]);
384
        // also get the microreference which could be in $tagged_text[$i + 3]
385
        if(isset($tagged_text[$i + 3])  && $tagged_text[$i + 2]->type == "separator" && $tagged_text[$i + 3]->type == "secMicroReference"){
386
          $extracted_tt[2] = $tagged_text[$i + 2];
387
          $extracted_tt[3] = $tagged_text[$i + 3];
388
          unset($tagged_text[$i + 2]);
389
          unset($tagged_text[$i + 3]);
390
        }
391
        break;
392
      }
393
    }
394
  }
395
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
396
  return $extracted_tt;
397
}
398

    
399
function tagged_text_extract_nomstatus(&$tagged_text) {
400

    
401
  $extracted_tt = array();
402
  if (is_array($tagged_text)) {
403
    for ($i = 0; $i < count($tagged_text); $i++) {
404
      if ($tagged_text[$i]->type == "nomStatus"){
405
        $extracted_tt[] = $tagged_text[$i];
406
        if(isset($tagged_text[$i + 1]) && $tagged_text[$i + 1]->type == "postSeparator"){
407
          $extracted_tt[] = $tagged_text[$i + 1];
408
          unset($tagged_text[$i + 1]);
409
        }
410
        if ($tagged_text[$i - 1]->type == "separator"){
411
          array_unshift($extracted_tt, $tagged_text[$i - 1]);
412
          unset($tagged_text[$i - 1]);
413
        }
414
        unset($tagged_text[$i]);
415
        break;
416
      }
417
    }
418
  }
419
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
420
  return $extracted_tt;
421
}
422

    
423
function tagged_text_extract(&$tagged_text, $type, $replace_with_placeholder = false) {
424
  $matching_elements = array();
425
  if (is_array($tagged_text)) {
426
    for ($i = 0; $i < count($tagged_text); $i++) {
427
      if($tagged_text[$i]->type == $type){
428
        $matching_elements[] = $tagged_text[$i];
429
        if($replace_with_placeholder){
430
          // text must not be null, see cdm_tagged_text_to_markup()
431
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $type, "PLACEHOLDER_" . $type);
432
        } else {
433
          unset($tagged_text[$i]);
434
        }
435
      }
436
    }
437
  }
438
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
439
  return $matching_elements;
440
}
441

    
442
function find_tagged_text_elements(&$tagged_text, $type){
443
  $matching_elements = array();
444
  if (is_array($tagged_text)) {
445
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
446
      if($tagged_text[$i]->type == $type){
447
        $matching_elements[] = $tagged_text[$i];
448
      }
449
    }
450
  }
451
  return $matching_elements;
452
}
(9-9/12)