Project

General

Profile

« Previous | Next » 

Revision 83dc60b9

Added by Andreas Kohlbecker over 5 years ago

ref #7658 adapting cdm_taxonRelationships() to new taxon relationship DTO webservice

  • test case
  • extending tagged text functions
  • adapting misapplied name redering

View differences:

modules/cdm_dataportal/cdm_api/cdm_api.module
192 192

  
193 193
// ===================== Tagged Text functions ================== //
194 194

  
195
function tagged_text_new($tag_type, $text = null){
196
  $tt = new stdClass();
197
  $tt->type = $tag_type;
198
  $tt->text = $text;
199
  return $tt;
200
}
201

  
195 202
/**
196 203
 * Walks the passed TaggedText array to find all elements which have a
197 204
 * TaggedText->entityReference. For each of these the taggedTexts is loaded
198
 * from the webservice and the original enty in the TaggedText array will be
205
 * from the webservice and the original entry in the TaggedText array will be
199 206
 * replaced by the newly loaded array.
200 207
 *
201 208
 * @param array $taggedtxt
......
234 241
 * Each item is provided with a class attribute which is set to the key of the
235 242
 * TaggedText item.
236 243
 *
244
 * Tagged text where the type starts with 'PLACEHOLDER_' will be added to the markup as plain text whereas the
245
 * taggedText->type wrapped in curly brackets: '{'. $tt->text . '}' is used as text.
246
 * see tagged_text_extract_secref()
247
 *
237 248
 * @param array $taggedtxt
238 249
 *   Array with text items to convert.
239 250
 * @param array $skiptags
240 251
 *   Array of tag names to skip
241 252
 *
242 253
 * @return string
243
 *   A string with HTML.
254
 *   The markup.
244 255
 */
245 256
function cdm_tagged_text_to_markup(array $taggedtxt, $skiptags = array()) {
246 257

  
......
258 269
      $is_last = $i + 1 == count($taggedtxt);
259 270
      $is_separator = is_tagged_text_sepatator_type($tt->type);
260 271
      $glue = !$is_separator && !$was_separator && !$is_last ? ' ' : '';
261
      $out .= '<' . $tag . ' class="' . $class_attr . '">'
262
        . t('@text', array('@text' => $tt->text))
263
        . '</' . $tag . '>'
264
        . $glue;
272
      if(str_beginsWith($tt->type, 'PLACEHOLDER_')){
273
        $out .=  '{'. $tt->type . '}';
274
      } else {
275
        $out .= '<' . $tag . ' class="' . $class_attr . '">'
276
          . t('@text', array('@text' => $tt->text))
277
          . '</' . $tag . '>'
278
          . $glue;
279
        }
280
      $was_separator = $is_separator;
281
    }
282
    $i++;
283
  }
284
  return $out;
285
}
286

  
287
/**
288
 * Converts an array of TaggedText items into corresponding plain text string
289
 *
290
 * Each item is provided with a class attribute which is set to the key of the
291
 * TaggedText item.
292
 *
293
 * @param array $taggedtxt
294
 *   Array with text items to convert.
295
 * @param array $skiptags
296
 *   Array of tag names to skip
297
 *
298
 * @return string
299
 *   The plain text
300
 */
301
function cdm_tagged_text_to_string(array $taggedtxt, $skiptags = array()) {
302

  
303
  $out = '';
304
  $was_separator = false;
305
  $i = 0;
306
  foreach ($taggedtxt as $tt) {
307
    if (!in_array($tt->type, $skiptags) && $tt->text) {
308
      $is_last = $i + 1 == count($taggedtxt);
309
      $is_separator = is_tagged_text_sepatator_type($tt->type);
310
      $glue = !$is_separator && !$was_separator && !$is_last ? ' ' : '';
311
      $out .= t('@text', array('@text' => $tt->text)) . $glue;
265 312
      $was_separator = $is_separator;
266 313
    }
267 314
    $i++;
......
351 398
  }
352 399
}
353 400

  
354
function split_secref_from_tagged_text(&$tagged_text) {
401
/**
402
 * Extracts the tagged text for sec references with separator and citation detail from a tagged text array.
403
 * @param $tagged_text
404
 *    The tagged text to operate on
405
 * @param string $ref_tag_type
406
 *    The tagtype for a secreference is "secReference", but "relSecReference" is also used in case of relationships.
407
 * @param bool $replace_with_placeholder
408
 *    Indicates the method to add a empty placeholder tagged text alement as relpacement for the extrated tagged text
409
 *    elements.
410
 * @return array
411
 */
412
function tagged_text_extract_secref(&$tagged_text, $ref_tag_type = "secReference", $replace_with_placeholder = false) {
355 413

  
356 414
  $extracted_tt = array();
357 415
  if (is_array($tagged_text)) {
416
    $extract_pos = null;
358 417
    for ($i = 0; $i < count($tagged_text) - 1; $i++) {
359
      if ($tagged_text[$i + 1]->type == "secReference" && $tagged_text[$i]->type == "separator"){
418
      if ($tagged_text[$i + 1]->type == $ref_tag_type && $tagged_text[$i]->type == "separator"){
360 419
        $extracted_tt[0] = $tagged_text[$i];
361 420
        $extracted_tt[1] = $tagged_text[$i + 1];
362
        unset($tagged_text[$i]);
421

  
422
        if($replace_with_placeholder){
423
          // text must not be null, see cdm_tagged_text_to_markup()
424
          $tagged_text[$i] = tagged_text_new("PLACEHOLDER_" . $ref_tag_type, "PLACEHOLDER_" . $ref_tag_type);
425
        } else {
426
          unset($tagged_text[$i]);
427
        }
363 428
        unset($tagged_text[$i + 1]);
364
        // also get the microfererence which could be in $tagged_text[$i + 3]
365
        if(isset($tagged_text[$i + 3])  && $tagged_text[$i + 2]->type == "separator" && $tagged_text[$i + 3]->type == "secReference"){
429
        // also get the microreference which could be in $tagged_text[$i + 3]
430
        if(isset($tagged_text[$i + 3])  && $tagged_text[$i + 2]->type == "separator" && $tagged_text[$i + 3]->type == $ref_tag_type){
366 431
          $extracted_tt[2] = $tagged_text[$i + 2];
367 432
          $extracted_tt[3] = $tagged_text[$i + 3];
433
          unset($tagged_text[$i + 2]);
434
          unset($tagged_text[$i + 3]);
368 435
        }
369 436
        break;
370 437
      }
371 438
    }
372 439
  }
440
  $tagged_text = array_values($tagged_text); // re-index array to make it continuous again
373 441
  return $extracted_tt;
374 442
}
375 443

  
376

  
377
function split_nomstatus_from_tagged_text(&$tagged_text) {
444
function tagged_text_extract_nomstatus(&$tagged_text) {
378 445

  
379 446
  $extracted_tt = array();
380 447
  if (is_array($tagged_text)) {

Also available in: Unified diff