ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDclSelectionRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
26 public function getValue()
27 {
28 if ($this->getField()->isMulti() && !is_array($this->value)) {
29 return [$this->value];
30 }
31 if (!$this->getField()->isMulti() && is_array($this->value)) {
32 return array_shift($this->value);
33 }
34
35 return $this->value;
36 }
37
41 public function parseExportValue($value): string
42 {
43 $values = ilDclSelectionOption::getValues((int) $this->getField()->getId(), $value);
44
45 return implode("; ", $values);
46 }
47
48 public function getValueFromExcel(ilExcel $excel, int $row, int $col)
49 {
50 $string = parent::getValueFromExcel($excel, $row, $col);
51 $old = $string;
52 if ($this->getField()->isMulti()) {
53 $string = $this->getMultipleValuesFromString($string);
54 $has_value = count($string);
55 } else {
56 $string = $this->getValueFromString($string);
57 $has_value = $string;
58 }
59
60 if (!$has_value && $old) {
61 $warning = "(" . $row . ", " . ilDataCollectionImporter::getExcelCharForInteger($col + 1) . ") " . $this->lng->txt("dcl_no_such_reference") . " "
62 . $old;
63
64 return ['warning' => $warning];
65 }
66
67 return $string;
68 }
69
78 protected function getMultipleValuesFromString(string $stringValues): array
79 {
80 $delimiter = strpos($stringValues, '; ') ? '; ' : ', ';
81 $slicedStrings = explode($delimiter, $stringValues);
82 $slicedReferences = [];
83 $resolved = 0;
84 for ($i = 0; $i < count($slicedStrings); $i++) {
85 $searchString = implode(array_slice($slicedStrings, $resolved, $i - $resolved + 1));
86 if ($ref = $this->getValueFromString($searchString)) {
87 $slicedReferences[] = $ref;
88 $resolved = $i;
89 continue;
90 }
91
92 $searchString = $slicedStrings[$i];
93 if ($ref = $this->getValueFromString($searchString)) {
94 $slicedReferences[] = $ref;
95 $resolved = $i;
96 }
97 }
98
99 return $slicedReferences;
100 }
101
102 protected function getValueFromString(string $string): ?int
103 {
104 foreach ($this->getField()->getProperty($this->field::PROP_SELECTION_OPTIONS) as $id => $value) {
105 if ($value == $string) {
106 return $id;
107 }
108 }
109
110 return null;
111 }
112}
static getValues(int $field_id, $opt_ids)
getValueFromExcel(ilExcel $excel, int $row, int $col)
getMultipleValuesFromString(string $stringValues)
Copied from reference field and slightly adjusted.