Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AbstractXhtmlSearchElement | |
0.00% |
0 / 65 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| setValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| isMultiValue | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createClientCode | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
| createBaseClientVariables | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| createInitSelectionValues | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| createHiddenInput | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\helpers\form\elements\xhtml; |
| 26 | |
| 27 | use oat\tao\helpers\form\elements\ElementValue; |
| 28 | use oat\tao\helpers\form\elements\AbstractSearchElement; |
| 29 | use tao_helpers_Display; |
| 30 | use tao_helpers_form_elements_xhtml_Hidden; |
| 31 | use tao_helpers_Uri; |
| 32 | |
| 33 | abstract class AbstractXhtmlSearchElement extends AbstractSearchElement |
| 34 | { |
| 35 | use XhtmlRenderingTrait; |
| 36 | |
| 37 | protected $attributes = [ |
| 38 | 'class' => 'form_radlst', |
| 39 | ]; |
| 40 | |
| 41 | /** |
| 42 | * @inheritDoc |
| 43 | */ |
| 44 | public function setValue($value): void |
| 45 | { |
| 46 | $this->addValue($value); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @inheritDoc |
| 51 | */ |
| 52 | public function render(): string |
| 53 | { |
| 54 | if (!empty($this->unit)) { |
| 55 | $this->addClass('has-unit'); |
| 56 | $label = sprintf( |
| 57 | '<label class="%s" for="%s">%s</label>', |
| 58 | 'unit', |
| 59 | $this->name, |
| 60 | tao_helpers_Display::htmlize($this->unit) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | $html = []; |
| 65 | |
| 66 | $html[] = $this->renderLabel(); |
| 67 | $html[] = sprintf( |
| 68 | '<script>%s</script>', |
| 69 | $this->createClientCode() |
| 70 | ); |
| 71 | $html[] = sprintf( |
| 72 | '<div%s data-testid="%s">%s</div>', |
| 73 | $this->renderAttributes(), |
| 74 | $this->getDescription(), |
| 75 | $this->createHiddenInput()->render() . ($label ?? '') |
| 76 | ); |
| 77 | |
| 78 | return implode('', $html); |
| 79 | } |
| 80 | |
| 81 | abstract protected function isMultiValue(): bool; |
| 82 | |
| 83 | private function createClientCode(): string |
| 84 | { |
| 85 | $searchUrl = tao_helpers_Uri::url('get', 'PropertyValues', 'tao'); |
| 86 | |
| 87 | $baseVariables = $this->createBaseClientVariables(); |
| 88 | $initSelectionValues = $this->createInitSelectionValues(); |
| 89 | |
| 90 | if ($this->isMultiValue()) { |
| 91 | $multipleValue = 'true'; |
| 92 | } else { |
| 93 | $initSelectionValues = reset($initSelectionValues); |
| 94 | $multipleValue = 'false'; |
| 95 | } |
| 96 | |
| 97 | $initSelection = json_encode($initSelectionValues); |
| 98 | |
| 99 | return <<<javascript |
| 100 | require(['jquery'], function ($) { |
| 101 | $baseVariables |
| 102 | |
| 103 | var getPropertyDataAndParseUri = function(callback, initialSelection, multiple) { |
| 104 | $.ajax({ |
| 105 | url: '$searchUrl', |
| 106 | data: { |
| 107 | propertyUri: '$this->name', |
| 108 | subject: ' ', |
| 109 | exclude: [], |
| 110 | parentListValues: getParentListValues() |
| 111 | }, |
| 112 | global: false, |
| 113 | success: function(response) { |
| 114 | let parsedResponse = normalizeResponse(response).results; |
| 115 | |
| 116 | if (multiple) { |
| 117 | initialSelection.forEach(selection => { |
| 118 | let index = parsedResponse.findIndex(parsedSelection => parsedSelection.id === selection.id); |
| 119 | if (index !== -1) { |
| 120 | selection.text = parsedResponse[index].text; |
| 121 | } |
| 122 | }); |
| 123 | } else { |
| 124 | initialSelection.text = parsedResponse.find(selection => selection.id === initialSelection.id).text; |
| 125 | } |
| 126 | callback(initialSelection); |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | \$input.select2({ |
| 132 | width: '100%', |
| 133 | multiple: $multipleValue, |
| 134 | minimumInputLength: 3, |
| 135 | ajax: { |
| 136 | quietMillis: 200, |
| 137 | url: '$searchUrl', |
| 138 | data: createRequestData, |
| 139 | results: normalizeResponse |
| 140 | }, |
| 141 | formatSelection: choice => { |
| 142 | if (!choice) { |
| 143 | return choice; |
| 144 | } |
| 145 | |
| 146 | return choice.isValid |
| 147 | ? choice.text |
| 148 | : `<span class="invalid-choice">\${choice.text}</span>`; |
| 149 | }, |
| 150 | initSelection: function (element, callback) { |
| 151 | let initialSelection = $initSelection; |
| 152 | |
| 153 | if (initialSelection) { |
| 154 | if ((Array.isArray(initialSelection) |
| 155 | && initialSelection.find(selection => selection.id === selection.text)) |
| 156 | || initialSelection.id === initialSelection.text) { |
| 157 | return getPropertyDataAndParseUri(callback, initialSelection, $multipleValue); |
| 158 | } |
| 159 | |
| 160 | callback(initialSelection); |
| 161 | } |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | \$input.parent().find('.select2-search-choice:has(.invalid-choice)').each((index, element) => { |
| 166 | $(element).addClass('select2-search-choice-error'); |
| 167 | }); |
| 168 | }); |
| 169 | javascript; |
| 170 | } |
| 171 | |
| 172 | private function createBaseClientVariables(): string |
| 173 | { |
| 174 | $delimiter = static::VALUE_DELIMITER; |
| 175 | |
| 176 | return <<<javascript |
| 177 | var \$input = $('#$this->name'); |
| 178 | |
| 179 | var normalizeItem = function (item) { return { id: item.uri, text: item.label } }; |
| 180 | |
| 181 | var normalizeResponse = function (data) { return { results: data.data.map(normalizeItem) } }; |
| 182 | |
| 183 | var getCurrentValues = function () { return \$input.val().split('$delimiter') }; |
| 184 | |
| 185 | var getParentListValues = function () { |
| 186 | var primaryPropUri = \$input.parent().data('depends-on-property'); |
| 187 | |
| 188 | return primaryPropUri ? $('#' + primaryPropUri).val().split(',') : ''; |
| 189 | } |
| 190 | |
| 191 | var createRequestData = function (term, exclude) { |
| 192 | return { |
| 193 | propertyUri: '$this->name', |
| 194 | subject: term, |
| 195 | exclude: getCurrentValues(), |
| 196 | parentListValues: getParentListValues() |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | javascript; |
| 201 | } |
| 202 | |
| 203 | private function createInitSelectionValues(): array |
| 204 | { |
| 205 | $result = []; |
| 206 | $invalidValues = $this->getInvalidValues(); |
| 207 | |
| 208 | foreach ($this->getRawValue() as $value) { |
| 209 | $encodedUri = tao_helpers_Uri::encode($value->getUri()); |
| 210 | |
| 211 | $result[] = [ |
| 212 | 'id' => $encodedUri, |
| 213 | 'text' => $value->getLabel(), |
| 214 | 'isValid' => !array_key_exists($encodedUri, $invalidValues), |
| 215 | ]; |
| 216 | } |
| 217 | |
| 218 | return $result; |
| 219 | } |
| 220 | |
| 221 | private function createHiddenInput(): tao_helpers_form_elements_xhtml_Hidden |
| 222 | { |
| 223 | $input = new tao_helpers_form_elements_xhtml_Hidden($this->name); |
| 224 | |
| 225 | $uris = array_map( |
| 226 | static function (ElementValue $value) { |
| 227 | return $value->getUri(); |
| 228 | }, |
| 229 | $this->getRawValue() |
| 230 | ); |
| 231 | |
| 232 | $input->setValue(implode(static::VALUE_DELIMITER, $uris)); |
| 233 | |
| 234 | return $input; |
| 235 | } |
| 236 | } |