Project

General

Profile

Download (13.2 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_secref()
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 the tagged text for sec references with separator and citation detail from a tagged text array.
312
 * @param $tagged_text
313
 *    The tagged text to operate on
314
 * @param string $ref_tag_type
315
 *    The tagtype for a secreference is "secReference", but "relSecReference" is also used in case of relationships.
316
 * @param bool $replace_with_placeholder
317
 *    Indicates the method to add a empty placeholder tagged text alement as relpacement for the extrated tagged text
318
 *    elements.
319
 * @return array
320
 */
321
function tagged_text_extract_secref(&$tagged_text, $ref_tag_type = "secReference", $replace_with_placeholder = false) {
322

    
323
  $extracted_tt = array();
324
  if (is_array($tagged_text)) {
325
    $extract_pos = null;
326
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
327
      if ($tagged_text[$i + 1]->type == $ref_tag_type && $tagged_text[$i]->type == "separator"){
328
        $extracted_tt[0] = $tagged_text[$i];
329
        $extracted_tt[1] = $tagged_text[$i + 1];
330

    
331
        if($replace_with_placeholder){
332
          // text must not be null, see cdm_tagged_text_to_markup()
333
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $ref_tag_type, "PLACEHOLDER_" . $ref_tag_type);
334
        } else {
335
          unset($tagged_text[$i]);
336
        }
337
        unset($tagged_text[$i + 1]);
338
        // also get the microreference which could be in $tagged_text[$i + 3]
339
        if(isset($tagged_text[$i + 3])  && $tagged_text[$i + 2]->type == "separator" && $tagged_text[$i + 3]->type == "secMicroReference"){
340
          $extracted_tt[2] = $tagged_text[$i + 2];
341
          $extracted_tt[3] = $tagged_text[$i + 3];
342
          unset($tagged_text[$i + 2]);
343
          unset($tagged_text[$i + 3]);
344
        }
345
        break;
346
      }
347
    }
348
  }
349
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
350
  return $extracted_tt;
351
}
352

    
353
function tagged_text_extract_nomstatus(&$tagged_text) {
354

    
355
  $extracted_tt = array();
356
  if (is_array($tagged_text)) {
357
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
358
      if ($tagged_text[$i]->type == "nomStatus"){
359
        $extracted_tt[] = $tagged_text[$i];
360
        if(isset($tagged_text[$i + 1]) && $tagged_text[$i + 1]->type == "postSeparator"){
361
          $extracted_tt[] = $tagged_text[$i + 1];
362
          unset($tagged_text[$i + 1]);
363
        }
364
        if ($tagged_text[$i - 1]->type == "separator"){
365
          array_unshift($extracted_tt, $tagged_text[$i - 1]);
366
          unset($tagged_text[$i - 1]);
367
        }
368
        unset($tagged_text[$i]);
369
        break;
370
      }
371
    }
372
  }
373
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
374
  return $extracted_tt;
375
}
376

    
377
function tagged_text_extract(&$tagged_text, $type, $replace_with_placeholder = false) {
378
  $matching_elements = array();
379
  if (is_array($tagged_text)) {
380
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
381
      if($tagged_text[$i]->type == $type){
382
        $matching_elements[] = $tagged_text[$i];
383
        if($replace_with_placeholder){
384
          // text must not be null, see cdm_tagged_text_to_markup()
385
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $type, "PLACEHOLDER_" . $type);
386
        } else {
387
          unset($tagged_text[$i]);
388
        }
389
      }
390
    }
391
  }
392
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
393
  return $matching_elements;
394
}
395

    
396
function find_tagged_text_elements(&$tagged_text, $type){
397
  $matching_elements = array();
398
  if (is_array($tagged_text)) {
399
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
400
      if($tagged_text[$i]->type == $type){
401
        $matching_elements[] = $tagged_text[$i];
402
      }
403
    }
404
  }
405
  return $matching_elements;
406
}
(9-9/12)