@@ -12,29 +12,29 @@ |
||
12 | 12 | */ |
13 | 13 | class ReadonlyTagField extends TagField |
14 | 14 | { |
15 | - /** |
|
16 | - * {@inheritDoc} |
|
17 | - */ |
|
18 | - protected $readonly = true; |
|
15 | + /** |
|
16 | + * {@inheritDoc} |
|
17 | + */ |
|
18 | + protected $readonly = true; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Render the readonly field as HTML. |
|
22 | - * |
|
23 | - * @param array $properties |
|
24 | - * @return HTMLText |
|
25 | - */ |
|
26 | - public function Field($properties = array()) |
|
27 | - { |
|
28 | - $options = array(); |
|
20 | + /** |
|
21 | + * Render the readonly field as HTML. |
|
22 | + * |
|
23 | + * @param array $properties |
|
24 | + * @return HTMLText |
|
25 | + */ |
|
26 | + public function Field($properties = array()) |
|
27 | + { |
|
28 | + $options = array(); |
|
29 | 29 | |
30 | - foreach ($this->getOptions()->filter('Selected', true) as $option) { |
|
31 | - $options[] = $option->Title; |
|
32 | - } |
|
30 | + foreach ($this->getOptions()->filter('Selected', true) as $option) { |
|
31 | + $options[] = $option->Title; |
|
32 | + } |
|
33 | 33 | |
34 | - $field = ReadonlyField::create($this->name . '_Readonly', $this->title); |
|
34 | + $field = ReadonlyField::create($this->name . '_Readonly', $this->title); |
|
35 | 35 | |
36 | - $field->setForm($this->form); |
|
37 | - $field->setValue(implode(', ', $options)); |
|
38 | - return $field->Field(); |
|
39 | - } |
|
36 | + $field->setForm($this->form); |
|
37 | + $field->setValue(implode(', ', $options)); |
|
38 | + return $field->Field(); |
|
39 | + } |
|
40 | 40 | } |
@@ -31,7 +31,7 @@ |
||
31 | 31 | $options[] = $option->Title; |
32 | 32 | } |
33 | 33 | |
34 | - $field = ReadonlyField::create($this->name . '_Readonly', $this->title); |
|
34 | + $field = ReadonlyField::create($this->name.'_Readonly', $this->title); |
|
35 | 35 | |
36 | 36 | $field->setForm($this->form); |
37 | 37 | $field->setValue(implode(', ', $options)); |
@@ -25,348 +25,348 @@ |
||
25 | 25 | */ |
26 | 26 | class StringTagField extends DropdownField |
27 | 27 | { |
28 | - /** |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - private static $allowed_actions = [ |
|
32 | - 'suggest' |
|
33 | - ]; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var bool |
|
37 | - */ |
|
38 | - protected $shouldLazyLoad = false; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var int |
|
42 | - */ |
|
43 | - protected $lazyLoadItemLimit = 10; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var bool |
|
47 | - */ |
|
48 | - protected $canCreate = true; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var null|DataObject |
|
52 | - */ |
|
53 | - protected $record; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var bool |
|
57 | - */ |
|
58 | - protected $isMultiple = true; |
|
59 | - |
|
60 | - /** |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function getShouldLazyLoad() |
|
64 | - { |
|
65 | - return $this->shouldLazyLoad; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param bool $shouldLazyLoad |
|
70 | - * |
|
71 | - * @return static |
|
72 | - */ |
|
73 | - public function setShouldLazyLoad($shouldLazyLoad) |
|
74 | - { |
|
75 | - $this->shouldLazyLoad = $shouldLazyLoad; |
|
76 | - |
|
77 | - return $this; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @return int |
|
82 | - */ |
|
83 | - public function getLazyLoadItemLimit() |
|
84 | - { |
|
85 | - return $this->lazyLoadItemLimit; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param int $lazyLoadItemLimit |
|
90 | - * |
|
91 | - * @return static |
|
92 | - */ |
|
93 | - public function setLazyLoadItemLimit($lazyLoadItemLimit) |
|
94 | - { |
|
95 | - $this->lazyLoadItemLimit = $lazyLoadItemLimit; |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function getIsMultiple() |
|
104 | - { |
|
105 | - return $this->isMultiple; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param bool $isMultiple |
|
110 | - * |
|
111 | - * @return static |
|
112 | - */ |
|
113 | - public function setIsMultiple($isMultiple) |
|
114 | - { |
|
115 | - $this->isMultiple = $isMultiple; |
|
116 | - |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return null|DataObject |
|
122 | - */ |
|
123 | - public function getRecord() |
|
124 | - { |
|
125 | - if ($this->record) { |
|
126 | - return $this->record; |
|
127 | - } |
|
128 | - |
|
129 | - if ($form = $this->getForm()) { |
|
130 | - return $form->getRecord(); |
|
131 | - } |
|
132 | - |
|
133 | - return null; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param DataObject $record |
|
138 | - * |
|
139 | - * @return $this |
|
140 | - */ |
|
141 | - public function setRecord(DataObject $record) |
|
142 | - { |
|
143 | - $this->record = $record; |
|
144 | - |
|
145 | - return $this; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * {@inheritdoc} |
|
150 | - */ |
|
151 | - public function Field($properties = array()) |
|
152 | - { |
|
153 | - Requirements::css('silverstripe/tagfield:css/select2.min.css'); |
|
154 | - Requirements::css('silverstripe/tagfield:css/TagField.css'); |
|
155 | - |
|
156 | - Requirements::javascript('silverstripe/tagfield:js/select2.js'); |
|
157 | - Requirements::javascript('silverstripe/tagfield:js/TagField.js'); |
|
158 | - |
|
159 | - $this->addExtraClass('ss-tag-field'); |
|
160 | - |
|
161 | - if ($this->getIsMultiple()) { |
|
162 | - $this->setAttribute('multiple', 'multiple'); |
|
163 | - } |
|
164 | - |
|
165 | - if ($this->getShouldLazyLoad()) { |
|
166 | - $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); |
|
167 | - } else { |
|
168 | - $properties = array_merge($properties, array( |
|
169 | - 'Options' => $this->getOptions() |
|
170 | - )); |
|
171 | - } |
|
172 | - |
|
173 | - $this->setAttribute('data-can-create', (int) $this->getCanCreate()); |
|
174 | - |
|
175 | - return $this |
|
176 | - ->customise($properties) |
|
177 | - ->renderWith(TagField::class); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return string |
|
182 | - */ |
|
183 | - protected function getSuggestURL() |
|
184 | - { |
|
185 | - return Controller::join_links($this->Link(), 'suggest'); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return ArrayList |
|
190 | - */ |
|
191 | - protected function getOptions() |
|
192 | - { |
|
193 | - $options = ArrayList::create(); |
|
194 | - |
|
195 | - $source = $this->getSource(); |
|
196 | - |
|
197 | - if ($source instanceof Iterator) { |
|
198 | - $source = iterator_to_array($source); |
|
199 | - } |
|
200 | - |
|
201 | - $values = $this->Value(); |
|
202 | - |
|
203 | - foreach ($source as $value) { |
|
204 | - $options->push( |
|
205 | - ArrayData::create(array( |
|
206 | - 'Title' => $value, |
|
207 | - 'Value' => $value, |
|
208 | - 'Selected' => in_array($value, $values), |
|
209 | - )) |
|
210 | - ); |
|
211 | - } |
|
212 | - |
|
213 | - return $options; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * {@inheritdoc} |
|
218 | - */ |
|
219 | - public function setValue($value, $source = null) |
|
220 | - { |
|
221 | - if (is_string($value)) { |
|
222 | - $value = explode(',', $value); |
|
223 | - } |
|
224 | - |
|
225 | - if ($source instanceof DataObject) { |
|
226 | - $name = $this->getName(); |
|
227 | - $value = explode(',', $source->$name); |
|
228 | - } |
|
229 | - |
|
230 | - if ($source instanceof SS_List) { |
|
231 | - $value = $source->column('ID'); |
|
232 | - } |
|
233 | - |
|
234 | - if (is_null($value)) { |
|
235 | - $value = array(); |
|
236 | - } |
|
237 | - |
|
238 | - return parent::setValue(array_filter($value)); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * {@inheritdoc} |
|
243 | - */ |
|
244 | - public function getAttributes() |
|
245 | - { |
|
246 | - return array_merge( |
|
247 | - parent::getAttributes(), |
|
248 | - array('name' => $this->getName() . '[]') |
|
249 | - ); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * {@inheritdoc} |
|
254 | - */ |
|
255 | - public function saveInto(DataObjectInterface $record) |
|
256 | - { |
|
257 | - parent::saveInto($record); |
|
258 | - |
|
259 | - $name = $this->getName(); |
|
260 | - |
|
261 | - $record->$name = join(',', $this->Value()); |
|
262 | - $record->write(); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Returns a JSON string of tags, for lazy loading. |
|
267 | - * |
|
268 | - * @param HTTPRequest $request |
|
269 | - * @return HTTPResponse |
|
270 | - */ |
|
271 | - public function suggest(HTTPRequest $request) |
|
272 | - { |
|
273 | - $responseBody = Convert::raw2json( |
|
274 | - array('items' => array()) |
|
275 | - ); |
|
276 | - |
|
277 | - $response = new HTTPResponse; |
|
278 | - $response->addHeader('Content-Type', 'application/json'); |
|
279 | - |
|
280 | - if ($record = $this->getRecord()) { |
|
281 | - $tags = array(); |
|
282 | - $term = $request->getVar('term'); |
|
283 | - |
|
284 | - if ($record->hasField($this->getName())) { |
|
285 | - $tags = $this->getTags($term); |
|
286 | - } |
|
287 | - |
|
288 | - $responseBody = Convert::raw2json( |
|
289 | - array('items' => $tags) |
|
290 | - ); |
|
291 | - } |
|
292 | - |
|
293 | - $response->setBody($responseBody); |
|
294 | - |
|
295 | - return $response; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Returns array of arrays representing tags. |
|
300 | - * |
|
301 | - * @param string $term |
|
302 | - * |
|
303 | - * @return array |
|
304 | - */ |
|
305 | - protected function getTags($term) |
|
306 | - { |
|
307 | - $record = $this->getRecord(); |
|
308 | - |
|
309 | - if (!$record) { |
|
310 | - return array(); |
|
311 | - } |
|
312 | - |
|
313 | - $fieldName = $this->getName(); |
|
314 | - $className = $record->getClassName(); |
|
315 | - |
|
316 | - $term = Convert::raw2sql($term); |
|
317 | - |
|
318 | - $query = $className::get() |
|
319 | - ->filter($fieldName . ':PartialMatch:nocase', $term) |
|
320 | - ->limit($this->getLazyLoadItemLimit()); |
|
321 | - |
|
322 | - $items = array(); |
|
323 | - |
|
324 | - foreach ($query->column($fieldName) as $tags) { |
|
325 | - $tags = explode(',', $tags); |
|
326 | - |
|
327 | - foreach ($tags as $i => $tag) { |
|
328 | - if (stripos($tag, $term) !== false && !in_array($tag, $items)) { |
|
329 | - $items[] = array( |
|
330 | - 'id' => $tag, |
|
331 | - 'text' => $tag |
|
332 | - ); |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - |
|
337 | - return $items; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * DropdownField assumes value will be a scalar so we must |
|
342 | - * override validate. This only applies to Silverstripe 3.2+ |
|
343 | - * |
|
344 | - * @param Validator $validator |
|
345 | - * @return bool |
|
346 | - */ |
|
347 | - public function validate($validator) |
|
348 | - { |
|
349 | - return true; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * @return bool |
|
354 | - */ |
|
355 | - public function getCanCreate() |
|
356 | - { |
|
357 | - return $this->canCreate; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * @param bool $canCreate |
|
362 | - * |
|
363 | - * @return static |
|
364 | - */ |
|
365 | - public function setCanCreate($canCreate) |
|
366 | - { |
|
367 | - $this->canCreate = $canCreate; |
|
368 | - |
|
369 | - return $this; |
|
370 | - } |
|
28 | + /** |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + private static $allowed_actions = [ |
|
32 | + 'suggest' |
|
33 | + ]; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var bool |
|
37 | + */ |
|
38 | + protected $shouldLazyLoad = false; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var int |
|
42 | + */ |
|
43 | + protected $lazyLoadItemLimit = 10; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var bool |
|
47 | + */ |
|
48 | + protected $canCreate = true; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var null|DataObject |
|
52 | + */ |
|
53 | + protected $record; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var bool |
|
57 | + */ |
|
58 | + protected $isMultiple = true; |
|
59 | + |
|
60 | + /** |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function getShouldLazyLoad() |
|
64 | + { |
|
65 | + return $this->shouldLazyLoad; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param bool $shouldLazyLoad |
|
70 | + * |
|
71 | + * @return static |
|
72 | + */ |
|
73 | + public function setShouldLazyLoad($shouldLazyLoad) |
|
74 | + { |
|
75 | + $this->shouldLazyLoad = $shouldLazyLoad; |
|
76 | + |
|
77 | + return $this; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @return int |
|
82 | + */ |
|
83 | + public function getLazyLoadItemLimit() |
|
84 | + { |
|
85 | + return $this->lazyLoadItemLimit; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param int $lazyLoadItemLimit |
|
90 | + * |
|
91 | + * @return static |
|
92 | + */ |
|
93 | + public function setLazyLoadItemLimit($lazyLoadItemLimit) |
|
94 | + { |
|
95 | + $this->lazyLoadItemLimit = $lazyLoadItemLimit; |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function getIsMultiple() |
|
104 | + { |
|
105 | + return $this->isMultiple; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param bool $isMultiple |
|
110 | + * |
|
111 | + * @return static |
|
112 | + */ |
|
113 | + public function setIsMultiple($isMultiple) |
|
114 | + { |
|
115 | + $this->isMultiple = $isMultiple; |
|
116 | + |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return null|DataObject |
|
122 | + */ |
|
123 | + public function getRecord() |
|
124 | + { |
|
125 | + if ($this->record) { |
|
126 | + return $this->record; |
|
127 | + } |
|
128 | + |
|
129 | + if ($form = $this->getForm()) { |
|
130 | + return $form->getRecord(); |
|
131 | + } |
|
132 | + |
|
133 | + return null; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param DataObject $record |
|
138 | + * |
|
139 | + * @return $this |
|
140 | + */ |
|
141 | + public function setRecord(DataObject $record) |
|
142 | + { |
|
143 | + $this->record = $record; |
|
144 | + |
|
145 | + return $this; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * {@inheritdoc} |
|
150 | + */ |
|
151 | + public function Field($properties = array()) |
|
152 | + { |
|
153 | + Requirements::css('silverstripe/tagfield:css/select2.min.css'); |
|
154 | + Requirements::css('silverstripe/tagfield:css/TagField.css'); |
|
155 | + |
|
156 | + Requirements::javascript('silverstripe/tagfield:js/select2.js'); |
|
157 | + Requirements::javascript('silverstripe/tagfield:js/TagField.js'); |
|
158 | + |
|
159 | + $this->addExtraClass('ss-tag-field'); |
|
160 | + |
|
161 | + if ($this->getIsMultiple()) { |
|
162 | + $this->setAttribute('multiple', 'multiple'); |
|
163 | + } |
|
164 | + |
|
165 | + if ($this->getShouldLazyLoad()) { |
|
166 | + $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); |
|
167 | + } else { |
|
168 | + $properties = array_merge($properties, array( |
|
169 | + 'Options' => $this->getOptions() |
|
170 | + )); |
|
171 | + } |
|
172 | + |
|
173 | + $this->setAttribute('data-can-create', (int) $this->getCanCreate()); |
|
174 | + |
|
175 | + return $this |
|
176 | + ->customise($properties) |
|
177 | + ->renderWith(TagField::class); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return string |
|
182 | + */ |
|
183 | + protected function getSuggestURL() |
|
184 | + { |
|
185 | + return Controller::join_links($this->Link(), 'suggest'); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return ArrayList |
|
190 | + */ |
|
191 | + protected function getOptions() |
|
192 | + { |
|
193 | + $options = ArrayList::create(); |
|
194 | + |
|
195 | + $source = $this->getSource(); |
|
196 | + |
|
197 | + if ($source instanceof Iterator) { |
|
198 | + $source = iterator_to_array($source); |
|
199 | + } |
|
200 | + |
|
201 | + $values = $this->Value(); |
|
202 | + |
|
203 | + foreach ($source as $value) { |
|
204 | + $options->push( |
|
205 | + ArrayData::create(array( |
|
206 | + 'Title' => $value, |
|
207 | + 'Value' => $value, |
|
208 | + 'Selected' => in_array($value, $values), |
|
209 | + )) |
|
210 | + ); |
|
211 | + } |
|
212 | + |
|
213 | + return $options; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * {@inheritdoc} |
|
218 | + */ |
|
219 | + public function setValue($value, $source = null) |
|
220 | + { |
|
221 | + if (is_string($value)) { |
|
222 | + $value = explode(',', $value); |
|
223 | + } |
|
224 | + |
|
225 | + if ($source instanceof DataObject) { |
|
226 | + $name = $this->getName(); |
|
227 | + $value = explode(',', $source->$name); |
|
228 | + } |
|
229 | + |
|
230 | + if ($source instanceof SS_List) { |
|
231 | + $value = $source->column('ID'); |
|
232 | + } |
|
233 | + |
|
234 | + if (is_null($value)) { |
|
235 | + $value = array(); |
|
236 | + } |
|
237 | + |
|
238 | + return parent::setValue(array_filter($value)); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * {@inheritdoc} |
|
243 | + */ |
|
244 | + public function getAttributes() |
|
245 | + { |
|
246 | + return array_merge( |
|
247 | + parent::getAttributes(), |
|
248 | + array('name' => $this->getName() . '[]') |
|
249 | + ); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * {@inheritdoc} |
|
254 | + */ |
|
255 | + public function saveInto(DataObjectInterface $record) |
|
256 | + { |
|
257 | + parent::saveInto($record); |
|
258 | + |
|
259 | + $name = $this->getName(); |
|
260 | + |
|
261 | + $record->$name = join(',', $this->Value()); |
|
262 | + $record->write(); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Returns a JSON string of tags, for lazy loading. |
|
267 | + * |
|
268 | + * @param HTTPRequest $request |
|
269 | + * @return HTTPResponse |
|
270 | + */ |
|
271 | + public function suggest(HTTPRequest $request) |
|
272 | + { |
|
273 | + $responseBody = Convert::raw2json( |
|
274 | + array('items' => array()) |
|
275 | + ); |
|
276 | + |
|
277 | + $response = new HTTPResponse; |
|
278 | + $response->addHeader('Content-Type', 'application/json'); |
|
279 | + |
|
280 | + if ($record = $this->getRecord()) { |
|
281 | + $tags = array(); |
|
282 | + $term = $request->getVar('term'); |
|
283 | + |
|
284 | + if ($record->hasField($this->getName())) { |
|
285 | + $tags = $this->getTags($term); |
|
286 | + } |
|
287 | + |
|
288 | + $responseBody = Convert::raw2json( |
|
289 | + array('items' => $tags) |
|
290 | + ); |
|
291 | + } |
|
292 | + |
|
293 | + $response->setBody($responseBody); |
|
294 | + |
|
295 | + return $response; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Returns array of arrays representing tags. |
|
300 | + * |
|
301 | + * @param string $term |
|
302 | + * |
|
303 | + * @return array |
|
304 | + */ |
|
305 | + protected function getTags($term) |
|
306 | + { |
|
307 | + $record = $this->getRecord(); |
|
308 | + |
|
309 | + if (!$record) { |
|
310 | + return array(); |
|
311 | + } |
|
312 | + |
|
313 | + $fieldName = $this->getName(); |
|
314 | + $className = $record->getClassName(); |
|
315 | + |
|
316 | + $term = Convert::raw2sql($term); |
|
317 | + |
|
318 | + $query = $className::get() |
|
319 | + ->filter($fieldName . ':PartialMatch:nocase', $term) |
|
320 | + ->limit($this->getLazyLoadItemLimit()); |
|
321 | + |
|
322 | + $items = array(); |
|
323 | + |
|
324 | + foreach ($query->column($fieldName) as $tags) { |
|
325 | + $tags = explode(',', $tags); |
|
326 | + |
|
327 | + foreach ($tags as $i => $tag) { |
|
328 | + if (stripos($tag, $term) !== false && !in_array($tag, $items)) { |
|
329 | + $items[] = array( |
|
330 | + 'id' => $tag, |
|
331 | + 'text' => $tag |
|
332 | + ); |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + |
|
337 | + return $items; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * DropdownField assumes value will be a scalar so we must |
|
342 | + * override validate. This only applies to Silverstripe 3.2+ |
|
343 | + * |
|
344 | + * @param Validator $validator |
|
345 | + * @return bool |
|
346 | + */ |
|
347 | + public function validate($validator) |
|
348 | + { |
|
349 | + return true; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * @return bool |
|
354 | + */ |
|
355 | + public function getCanCreate() |
|
356 | + { |
|
357 | + return $this->canCreate; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * @param bool $canCreate |
|
362 | + * |
|
363 | + * @return static |
|
364 | + */ |
|
365 | + public function setCanCreate($canCreate) |
|
366 | + { |
|
367 | + $this->canCreate = $canCreate; |
|
368 | + |
|
369 | + return $this; |
|
370 | + } |
|
371 | 371 | |
372 | 372 | } |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | { |
246 | 246 | return array_merge( |
247 | 247 | parent::getAttributes(), |
248 | - array('name' => $this->getName() . '[]') |
|
248 | + array('name' => $this->getName().'[]') |
|
249 | 249 | ); |
250 | 250 | } |
251 | 251 | |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | $term = Convert::raw2sql($term); |
317 | 317 | |
318 | 318 | $query = $className::get() |
319 | - ->filter($fieldName . ':PartialMatch:nocase', $term) |
|
319 | + ->filter($fieldName.':PartialMatch:nocase', $term) |
|
320 | 320 | ->limit($this->getLazyLoadItemLimit()); |
321 | 321 | |
322 | 322 | $items = array(); |
@@ -12,112 +12,112 @@ |
||
12 | 12 | */ |
13 | 13 | class StringTagFieldTest extends SapphireTest |
14 | 14 | { |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected static $fixture_file = 'StringTagFieldTest.yml'; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected static $extra_dataobjects = array( |
|
24 | - StringTagFieldTestBlogPost::class, |
|
25 | - ); |
|
26 | - |
|
27 | - public function testItSavesTagsOnNewRecords() |
|
28 | - { |
|
29 | - $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
30 | - |
|
31 | - $field = new StringTagField('Tags'); |
|
32 | - $field->setValue(array('Tag1', 'Tag2')); |
|
33 | - $field->saveInto($record); |
|
34 | - |
|
35 | - $record->write(); |
|
36 | - |
|
37 | - $this->assertEquals('Tag1,Tag2', $record->Tags); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * @param string $name |
|
42 | - * |
|
43 | - * @return StringTagFieldTestBlogPost |
|
44 | - */ |
|
45 | - protected function getNewStringTagFieldTestBlogPost($name) |
|
46 | - { |
|
47 | - return $this->objFromFixture( |
|
48 | - StringTagFieldTestBlogPost::class, |
|
49 | - $name |
|
50 | - ); |
|
51 | - } |
|
52 | - |
|
53 | - public function testItSavesTagsOnExistingRecords() |
|
54 | - { |
|
55 | - $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
56 | - $record->write(); |
|
57 | - |
|
58 | - $field = new StringTagField('Tags'); |
|
59 | - $field->setValue(array('Tag1', 'Tag2')); |
|
60 | - $field->saveInto($record); |
|
61 | - |
|
62 | - $this->assertEquals('Tag1,Tag2', $record->Tags); |
|
63 | - } |
|
64 | - |
|
65 | - public function testItSuggestsTags() |
|
66 | - { |
|
67 | - $record = $this->getNewStringTagFieldTestBlogPost('BlogPost2'); |
|
68 | - |
|
69 | - $field = new StringTagField('Tags'); |
|
70 | - $field->setRecord($record); |
|
71 | - |
|
72 | - /** |
|
73 | - * Partial tag title match. |
|
74 | - */ |
|
75 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
76 | - |
|
77 | - $this->assertEquals( |
|
78 | - '{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}', |
|
79 | - $field->suggest($request)->getBody() |
|
80 | - ); |
|
81 | - |
|
82 | - /** |
|
83 | - * Exact tag title match. |
|
84 | - */ |
|
85 | - $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
86 | - |
|
87 | - $this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}'); |
|
88 | - |
|
89 | - /** |
|
90 | - * Case-insensitive tag title match. |
|
91 | - */ |
|
92 | - $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
93 | - |
|
94 | - $this->assertEquals( |
|
95 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
96 | - $field->suggest($request)->getBody() |
|
97 | - ); |
|
98 | - |
|
99 | - /** |
|
100 | - * No tag title match. |
|
101 | - */ |
|
102 | - $request = $this->getNewRequest(array('term' => 'unknown')); |
|
103 | - |
|
104 | - $this->assertEquals( |
|
105 | - '{"items":[]}', |
|
106 | - $field->suggest($request)->getBody() |
|
107 | - ); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param array $parameters |
|
112 | - * |
|
113 | - * @return HTTPRequest |
|
114 | - */ |
|
115 | - protected function getNewRequest(array $parameters) |
|
116 | - { |
|
117 | - return new HTTPRequest( |
|
118 | - 'get', |
|
119 | - 'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest', |
|
120 | - $parameters |
|
121 | - ); |
|
122 | - } |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected static $fixture_file = 'StringTagFieldTest.yml'; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected static $extra_dataobjects = array( |
|
24 | + StringTagFieldTestBlogPost::class, |
|
25 | + ); |
|
26 | + |
|
27 | + public function testItSavesTagsOnNewRecords() |
|
28 | + { |
|
29 | + $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
30 | + |
|
31 | + $field = new StringTagField('Tags'); |
|
32 | + $field->setValue(array('Tag1', 'Tag2')); |
|
33 | + $field->saveInto($record); |
|
34 | + |
|
35 | + $record->write(); |
|
36 | + |
|
37 | + $this->assertEquals('Tag1,Tag2', $record->Tags); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * @param string $name |
|
42 | + * |
|
43 | + * @return StringTagFieldTestBlogPost |
|
44 | + */ |
|
45 | + protected function getNewStringTagFieldTestBlogPost($name) |
|
46 | + { |
|
47 | + return $this->objFromFixture( |
|
48 | + StringTagFieldTestBlogPost::class, |
|
49 | + $name |
|
50 | + ); |
|
51 | + } |
|
52 | + |
|
53 | + public function testItSavesTagsOnExistingRecords() |
|
54 | + { |
|
55 | + $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1'); |
|
56 | + $record->write(); |
|
57 | + |
|
58 | + $field = new StringTagField('Tags'); |
|
59 | + $field->setValue(array('Tag1', 'Tag2')); |
|
60 | + $field->saveInto($record); |
|
61 | + |
|
62 | + $this->assertEquals('Tag1,Tag2', $record->Tags); |
|
63 | + } |
|
64 | + |
|
65 | + public function testItSuggestsTags() |
|
66 | + { |
|
67 | + $record = $this->getNewStringTagFieldTestBlogPost('BlogPost2'); |
|
68 | + |
|
69 | + $field = new StringTagField('Tags'); |
|
70 | + $field->setRecord($record); |
|
71 | + |
|
72 | + /** |
|
73 | + * Partial tag title match. |
|
74 | + */ |
|
75 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
76 | + |
|
77 | + $this->assertEquals( |
|
78 | + '{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}', |
|
79 | + $field->suggest($request)->getBody() |
|
80 | + ); |
|
81 | + |
|
82 | + /** |
|
83 | + * Exact tag title match. |
|
84 | + */ |
|
85 | + $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
86 | + |
|
87 | + $this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}'); |
|
88 | + |
|
89 | + /** |
|
90 | + * Case-insensitive tag title match. |
|
91 | + */ |
|
92 | + $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
93 | + |
|
94 | + $this->assertEquals( |
|
95 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
96 | + $field->suggest($request)->getBody() |
|
97 | + ); |
|
98 | + |
|
99 | + /** |
|
100 | + * No tag title match. |
|
101 | + */ |
|
102 | + $request = $this->getNewRequest(array('term' => 'unknown')); |
|
103 | + |
|
104 | + $this->assertEquals( |
|
105 | + '{"items":[]}', |
|
106 | + $field->suggest($request)->getBody() |
|
107 | + ); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param array $parameters |
|
112 | + * |
|
113 | + * @return HTTPRequest |
|
114 | + */ |
|
115 | + protected function getNewRequest(array $parameters) |
|
116 | + { |
|
117 | + return new HTTPRequest( |
|
118 | + 'get', |
|
119 | + 'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest', |
|
120 | + $parameters |
|
121 | + ); |
|
122 | + } |
|
123 | 123 | } |
@@ -12,21 +12,21 @@ |
||
12 | 12 | |
13 | 13 | class StringTagFieldTestController extends Controller implements TestOnly |
14 | 14 | { |
15 | - public function StringTagFieldTestForm() |
|
16 | - { |
|
17 | - $fields = new FieldList( |
|
18 | - $tagField = new StringTagField('Tags') |
|
19 | - ); |
|
15 | + public function StringTagFieldTestForm() |
|
16 | + { |
|
17 | + $fields = new FieldList( |
|
18 | + $tagField = new StringTagField('Tags') |
|
19 | + ); |
|
20 | 20 | |
21 | - $actions = new FieldList( |
|
22 | - new FormAction('StringTagFieldTestFormSubmit') |
|
23 | - ); |
|
21 | + $actions = new FieldList( |
|
22 | + new FormAction('StringTagFieldTestFormSubmit') |
|
23 | + ); |
|
24 | 24 | |
25 | - return new Form($this, 'StringTagFieldTestForm', $fields, $actions); |
|
26 | - } |
|
25 | + return new Form($this, 'StringTagFieldTestForm', $fields, $actions); |
|
26 | + } |
|
27 | 27 | |
28 | - public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
29 | - { |
|
30 | - $form->saveInto($dataObject); |
|
31 | - } |
|
28 | + public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
29 | + { |
|
30 | + $form->saveInto($dataObject); |
|
31 | + } |
|
32 | 32 | } |
@@ -8,14 +8,14 @@ |
||
8 | 8 | |
9 | 9 | class TagFieldTestBlogPost extends DataObject implements TestOnly |
10 | 10 | { |
11 | - private static $table_name = 'TagFieldTestBlogPost'; |
|
11 | + private static $table_name = 'TagFieldTestBlogPost'; |
|
12 | 12 | |
13 | - private static $db = [ |
|
14 | - 'Title' => 'Text', |
|
15 | - 'Content' => 'Text' |
|
16 | - ]; |
|
13 | + private static $db = [ |
|
14 | + 'Title' => 'Text', |
|
15 | + 'Content' => 'Text' |
|
16 | + ]; |
|
17 | 17 | |
18 | - private static $many_many = [ |
|
19 | - 'Tags' => TagFieldTestBlogTag::class |
|
20 | - ]; |
|
18 | + private static $many_many = [ |
|
19 | + 'Tags' => TagFieldTestBlogTag::class |
|
20 | + ]; |
|
21 | 21 | } |
@@ -13,21 +13,21 @@ |
||
13 | 13 | |
14 | 14 | class TagFieldTestController extends Controller implements TestOnly |
15 | 15 | { |
16 | - public function TagFieldTestForm() |
|
17 | - { |
|
18 | - $fields = new FieldList( |
|
19 | - $tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
20 | - ); |
|
16 | + public function TagFieldTestForm() |
|
17 | + { |
|
18 | + $fields = new FieldList( |
|
19 | + $tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag')) |
|
20 | + ); |
|
21 | 21 | |
22 | - $actions = new FieldList( |
|
23 | - new FormAction('TagFieldTestFormSubmit') |
|
24 | - ); |
|
22 | + $actions = new FieldList( |
|
23 | + new FormAction('TagFieldTestFormSubmit') |
|
24 | + ); |
|
25 | 25 | |
26 | - return new Form($this, 'TagFieldTestForm', $fields, $actions); |
|
27 | - } |
|
26 | + return new Form($this, 'TagFieldTestForm', $fields, $actions); |
|
27 | + } |
|
28 | 28 | |
29 | - public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
30 | - { |
|
31 | - $form->saveInto($dataObject); |
|
32 | - } |
|
29 | + public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form) |
|
30 | + { |
|
31 | + $form->saveInto($dataObject); |
|
32 | + } |
|
33 | 33 | } |
@@ -8,15 +8,15 @@ |
||
8 | 8 | |
9 | 9 | class TagFieldTestBlogTag extends DataObject implements TestOnly |
10 | 10 | { |
11 | - private static $table_name = 'TagFieldTestBlogTag'; |
|
11 | + private static $table_name = 'TagFieldTestBlogTag'; |
|
12 | 12 | |
13 | - private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC'; |
|
13 | + private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC'; |
|
14 | 14 | |
15 | - private static $db = [ |
|
16 | - 'Title' => 'Varchar(200)' |
|
17 | - ]; |
|
15 | + private static $db = [ |
|
16 | + 'Title' => 'Varchar(200)' |
|
17 | + ]; |
|
18 | 18 | |
19 | - private static $belongs_many_many = [ |
|
20 | - 'BlogPosts' => TagFieldTestBlogPost::class |
|
21 | - ]; |
|
19 | + private static $belongs_many_many = [ |
|
20 | + 'BlogPosts' => TagFieldTestBlogPost::class |
|
21 | + ]; |
|
22 | 22 | } |
@@ -7,11 +7,11 @@ |
||
7 | 7 | |
8 | 8 | class StringTagFieldTestBlogPost extends DataObject implements TestOnly |
9 | 9 | { |
10 | - private static $table_name = 'StringTagFieldTestBlogPost'; |
|
10 | + private static $table_name = 'StringTagFieldTestBlogPost'; |
|
11 | 11 | |
12 | - private static $db = [ |
|
13 | - 'Title' => 'Text', |
|
14 | - 'Content' => 'Text', |
|
15 | - 'Tags' => 'Text', |
|
16 | - ]; |
|
12 | + private static $db = [ |
|
13 | + 'Title' => 'Text', |
|
14 | + 'Content' => 'Text', |
|
15 | + 'Tags' => 'Text', |
|
16 | + ]; |
|
17 | 17 | } |
@@ -19,337 +19,337 @@ |
||
19 | 19 | */ |
20 | 20 | class TagFieldTest extends SapphireTest |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected static $fixture_file = 'TagFieldTest.yml'; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected static $extra_dataobjects = [ |
|
31 | - TagFieldTestBlogTag::class, |
|
32 | - TagFieldTestBlogPost::class, |
|
33 | - ]; |
|
34 | - |
|
35 | - public function testItSavesLinksToNewTagsOnNewRecords() |
|
36 | - { |
|
37 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
38 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
39 | - $field->setValue(array('Tag3', 'Tag4')); |
|
40 | - $field->saveInto($record); |
|
41 | - $record->write(); |
|
42 | - $this->compareExpectedAndActualTags( |
|
43 | - array('Tag3', 'Tag4'), |
|
44 | - $record |
|
45 | - ); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string $name |
|
50 | - * |
|
51 | - * @return TagFieldTestBlogPost |
|
52 | - */ |
|
53 | - protected function getNewTagFieldTestBlogPost($name) |
|
54 | - { |
|
55 | - return $this->objFromFixture( |
|
56 | - TagFieldTestBlogPost::class, |
|
57 | - $name |
|
58 | - ); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param array $expected |
|
63 | - * @param TagFieldTestBlogPost $record |
|
64 | - */ |
|
65 | - protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
66 | - { |
|
67 | - $this->compareTagLists($expected, $record->Tags()); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Ensure a source of tags matches the given string tag names |
|
72 | - * |
|
73 | - * @param array $expected |
|
74 | - * @param DataList $actualSource |
|
75 | - */ |
|
76 | - protected function compareTagLists(array $expected, DataList $actualSource) |
|
77 | - { |
|
78 | - $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
79 | - sort($expected); |
|
80 | - sort($actual); |
|
81 | - |
|
82 | - $this->assertEquals( |
|
83 | - $expected, |
|
84 | - $actual |
|
85 | - ); |
|
86 | - } |
|
87 | - |
|
88 | - public function testItSavesLinksToNewTagsOnExistingRecords() |
|
89 | - { |
|
90 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
91 | - $record->write(); |
|
92 | - |
|
93 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
94 | - $field->setValue(array('Tag3', 'Tag4')); |
|
95 | - $field->saveInto($record); |
|
96 | - |
|
97 | - $this->compareExpectedAndActualTags( |
|
98 | - array('Tag3', 'Tag4'), |
|
99 | - $record |
|
100 | - ); |
|
101 | - } |
|
102 | - |
|
103 | - public function testItSavesLinksToExistingTagsOnNewRecords() |
|
104 | - { |
|
105 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
106 | - |
|
107 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
108 | - $field->setValue(array('Tag1', 'Tag2')); |
|
109 | - $field->saveInto($record); |
|
110 | - |
|
111 | - $record->write(); |
|
112 | - |
|
113 | - $this->compareExpectedAndActualTags( |
|
114 | - array('Tag1', 'Tag2'), |
|
115 | - $record |
|
116 | - ); |
|
117 | - } |
|
118 | - |
|
119 | - public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
120 | - { |
|
121 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
122 | - $record->write(); |
|
123 | - |
|
124 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
125 | - $field->setValue(array('Tag1', 'Tag2')); |
|
126 | - $field->saveInto($record); |
|
127 | - |
|
128 | - $this->compareExpectedAndActualTags( |
|
129 | - array('Tag1', 'Tag2'), |
|
130 | - $record |
|
131 | - ); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Ensure that {@see TagField::saveInto} respects existing tags |
|
136 | - */ |
|
137 | - public function testSaveDuplicateTags() |
|
138 | - { |
|
139 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
140 | - $record->write(); |
|
141 | - $tag2ID = $this->idFromFixture(TagFieldTestBlogTag::class, 'Tag2'); |
|
142 | - |
|
143 | - // Check tags before write |
|
144 | - $this->compareExpectedAndActualTags( |
|
145 | - array('Tag1', '222'), |
|
146 | - $record |
|
147 | - ); |
|
148 | - $this->compareTagLists( |
|
149 | - array('Tag1', '222'), |
|
150 | - TagFieldTestBlogTag::get() |
|
151 | - ); |
|
152 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
153 | - |
|
154 | - // Write new tags |
|
155 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
156 | - $field->setValue(array('222', 'Tag3')); |
|
157 | - $field->saveInto($record); |
|
158 | - |
|
159 | - // Check only one new tag was added |
|
160 | - $this->compareExpectedAndActualTags( |
|
161 | - array('222', 'Tag3'), |
|
162 | - $record |
|
163 | - ); |
|
164 | - |
|
165 | - // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
166 | - $this->compareTagLists( |
|
167 | - array('Tag1', '222', 'Tag3'), |
|
168 | - TagFieldTestBlogTag::get() |
|
169 | - ); |
|
170 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
171 | - } |
|
172 | - |
|
173 | - public function testItSuggestsTags() |
|
174 | - { |
|
175 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
176 | - |
|
177 | - /** |
|
178 | - * Partial tag title match. |
|
179 | - */ |
|
180 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
181 | - |
|
182 | - $this->assertEquals( |
|
183 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
184 | - $field->suggest($request)->getBody() |
|
185 | - ); |
|
186 | - |
|
187 | - /** |
|
188 | - * Exact tag title match. |
|
189 | - */ |
|
190 | - $request = $this->getNewRequest(array('term' => '222')); |
|
191 | - |
|
192 | - $this->assertEquals( |
|
193 | - '{"items":[{"id":"222","text":"222"}]}', |
|
194 | - $field->suggest($request)->getBody() |
|
195 | - ); |
|
196 | - |
|
197 | - /** |
|
198 | - * Case-insensitive tag title match. |
|
199 | - */ |
|
200 | - $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
201 | - |
|
202 | - $this->assertEquals( |
|
203 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
204 | - $field->suggest($request)->getBody() |
|
205 | - ); |
|
206 | - |
|
207 | - /** |
|
208 | - * No tag title match. |
|
209 | - */ |
|
210 | - $request = $this->getNewRequest(array('term' => 'unknown')); |
|
211 | - |
|
212 | - $this->assertEquals( |
|
213 | - '{"items":[]}', |
|
214 | - $field->suggest($request)->getBody() |
|
215 | - ); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Tests that TagField supports pre-filtered data sources |
|
220 | - */ |
|
221 | - public function testRestrictedSuggestions() |
|
222 | - { |
|
223 | - $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
224 | - $field = new TagField('Tags', '', $source); |
|
225 | - |
|
226 | - /** |
|
227 | - * Partial tag title match. |
|
228 | - */ |
|
229 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
230 | - |
|
231 | - $this->assertEquals( |
|
232 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
233 | - $field->suggest($request)->getBody() |
|
234 | - ); |
|
235 | - |
|
236 | - /** |
|
237 | - * Exact tag title match. |
|
238 | - */ |
|
239 | - $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
240 | - |
|
241 | - $this->assertEquals( |
|
242 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
243 | - $field->suggest($request)->getBody() |
|
244 | - ); |
|
245 | - |
|
246 | - /** |
|
247 | - * Excluded item doesn't appear in matches |
|
248 | - */ |
|
249 | - $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
250 | - |
|
251 | - $this->assertEquals( |
|
252 | - '{"items":[]}', |
|
253 | - $field->suggest($request)->getBody() |
|
254 | - ); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @param array $parameters |
|
259 | - * |
|
260 | - * @return HTTPRequest |
|
261 | - */ |
|
262 | - protected function getNewRequest(array $parameters) |
|
263 | - { |
|
264 | - return new HTTPRequest( |
|
265 | - 'get', |
|
266 | - 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
267 | - $parameters |
|
268 | - ); |
|
269 | - } |
|
270 | - |
|
271 | - public function testItDisplaysValuesFromRelations() |
|
272 | - { |
|
273 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
274 | - $record->write(); |
|
275 | - |
|
276 | - $form = new Form( |
|
277 | - new TagFieldTestController($record), |
|
278 | - 'Form', |
|
279 | - new FieldList( |
|
280 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)) |
|
281 | - ), |
|
282 | - new FieldList() |
|
283 | - ); |
|
284 | - |
|
285 | - $form->loadDataFrom( |
|
286 | - $this->objFromFixture(TagFieldTestBlogPost::class, 'BlogPost2') |
|
287 | - ); |
|
288 | - |
|
289 | - $ids = TagFieldTestBlogTag::get()->column('Title'); |
|
290 | - |
|
291 | - $this->assertEquals($field->Value(), $ids); |
|
292 | - } |
|
293 | - |
|
294 | - public function testItIgnoresNewTagsIfCannotCreate() |
|
295 | - { |
|
296 | - $this->markTestSkipped( |
|
297 | - 'This test has not been updated yet.' |
|
298 | - ); |
|
299 | - |
|
300 | - $record = new TagFieldTestBlogPost(); |
|
301 | - $record->write(); |
|
302 | - |
|
303 | - $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
304 | - |
|
305 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), array($tag->Title, 'Tag3')); |
|
306 | - $field->setCanCreate(false); |
|
307 | - $field->saveInto($record); |
|
308 | - |
|
309 | - /** |
|
310 | - * @var TagFieldTestBlogPost $record |
|
311 | - */ |
|
312 | - $record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID); |
|
313 | - |
|
314 | - $this->compareExpectedAndActualTags( |
|
315 | - array('Tag1'), |
|
316 | - $record |
|
317 | - ); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Test you can save without a source set |
|
322 | - */ |
|
323 | - public function testSaveEmptySource() |
|
324 | - { |
|
325 | - $record = new TagFieldTestBlogPost(); |
|
326 | - $record->write(); |
|
327 | - |
|
328 | - // Clear database of tags |
|
329 | - TagFieldTestBlogTag::get()->removeAll(); |
|
330 | - |
|
331 | - $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
332 | - $field->setValue(['New Tag']); |
|
333 | - $field->setCanCreate(true); |
|
334 | - $field->saveInto($record); |
|
335 | - |
|
336 | - $tag = TagFieldTestBlogTag::get()->first(); |
|
337 | - $this->assertNotEmpty($tag); |
|
338 | - $this->assertEquals('New Tag', $tag->Title); |
|
339 | - $record = TagFieldTestBlogPost::get()->byID($record->ID); |
|
340 | - $this->assertEquals( |
|
341 | - $tag->ID, |
|
342 | - $record->Tags()->first()->ID |
|
343 | - ); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Test read only fields are returned |
|
348 | - */ |
|
349 | - public function testReadonlyTransformation() |
|
350 | - { |
|
351 | - $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
352 | - $readOnlyField = $field->performReadonlyTransformation(); |
|
353 | - $this->assertEquals(ReadonlyTagField::class, get_class($readOnlyField)); |
|
354 | - } |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected static $fixture_file = 'TagFieldTest.yml'; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected static $extra_dataobjects = [ |
|
31 | + TagFieldTestBlogTag::class, |
|
32 | + TagFieldTestBlogPost::class, |
|
33 | + ]; |
|
34 | + |
|
35 | + public function testItSavesLinksToNewTagsOnNewRecords() |
|
36 | + { |
|
37 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
38 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
39 | + $field->setValue(array('Tag3', 'Tag4')); |
|
40 | + $field->saveInto($record); |
|
41 | + $record->write(); |
|
42 | + $this->compareExpectedAndActualTags( |
|
43 | + array('Tag3', 'Tag4'), |
|
44 | + $record |
|
45 | + ); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string $name |
|
50 | + * |
|
51 | + * @return TagFieldTestBlogPost |
|
52 | + */ |
|
53 | + protected function getNewTagFieldTestBlogPost($name) |
|
54 | + { |
|
55 | + return $this->objFromFixture( |
|
56 | + TagFieldTestBlogPost::class, |
|
57 | + $name |
|
58 | + ); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param array $expected |
|
63 | + * @param TagFieldTestBlogPost $record |
|
64 | + */ |
|
65 | + protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
66 | + { |
|
67 | + $this->compareTagLists($expected, $record->Tags()); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Ensure a source of tags matches the given string tag names |
|
72 | + * |
|
73 | + * @param array $expected |
|
74 | + * @param DataList $actualSource |
|
75 | + */ |
|
76 | + protected function compareTagLists(array $expected, DataList $actualSource) |
|
77 | + { |
|
78 | + $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
79 | + sort($expected); |
|
80 | + sort($actual); |
|
81 | + |
|
82 | + $this->assertEquals( |
|
83 | + $expected, |
|
84 | + $actual |
|
85 | + ); |
|
86 | + } |
|
87 | + |
|
88 | + public function testItSavesLinksToNewTagsOnExistingRecords() |
|
89 | + { |
|
90 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
91 | + $record->write(); |
|
92 | + |
|
93 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
94 | + $field->setValue(array('Tag3', 'Tag4')); |
|
95 | + $field->saveInto($record); |
|
96 | + |
|
97 | + $this->compareExpectedAndActualTags( |
|
98 | + array('Tag3', 'Tag4'), |
|
99 | + $record |
|
100 | + ); |
|
101 | + } |
|
102 | + |
|
103 | + public function testItSavesLinksToExistingTagsOnNewRecords() |
|
104 | + { |
|
105 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
106 | + |
|
107 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
108 | + $field->setValue(array('Tag1', 'Tag2')); |
|
109 | + $field->saveInto($record); |
|
110 | + |
|
111 | + $record->write(); |
|
112 | + |
|
113 | + $this->compareExpectedAndActualTags( |
|
114 | + array('Tag1', 'Tag2'), |
|
115 | + $record |
|
116 | + ); |
|
117 | + } |
|
118 | + |
|
119 | + public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
120 | + { |
|
121 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
122 | + $record->write(); |
|
123 | + |
|
124 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
125 | + $field->setValue(array('Tag1', 'Tag2')); |
|
126 | + $field->saveInto($record); |
|
127 | + |
|
128 | + $this->compareExpectedAndActualTags( |
|
129 | + array('Tag1', 'Tag2'), |
|
130 | + $record |
|
131 | + ); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Ensure that {@see TagField::saveInto} respects existing tags |
|
136 | + */ |
|
137 | + public function testSaveDuplicateTags() |
|
138 | + { |
|
139 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
140 | + $record->write(); |
|
141 | + $tag2ID = $this->idFromFixture(TagFieldTestBlogTag::class, 'Tag2'); |
|
142 | + |
|
143 | + // Check tags before write |
|
144 | + $this->compareExpectedAndActualTags( |
|
145 | + array('Tag1', '222'), |
|
146 | + $record |
|
147 | + ); |
|
148 | + $this->compareTagLists( |
|
149 | + array('Tag1', '222'), |
|
150 | + TagFieldTestBlogTag::get() |
|
151 | + ); |
|
152 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
153 | + |
|
154 | + // Write new tags |
|
155 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
156 | + $field->setValue(array('222', 'Tag3')); |
|
157 | + $field->saveInto($record); |
|
158 | + |
|
159 | + // Check only one new tag was added |
|
160 | + $this->compareExpectedAndActualTags( |
|
161 | + array('222', 'Tag3'), |
|
162 | + $record |
|
163 | + ); |
|
164 | + |
|
165 | + // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
166 | + $this->compareTagLists( |
|
167 | + array('Tag1', '222', 'Tag3'), |
|
168 | + TagFieldTestBlogTag::get() |
|
169 | + ); |
|
170 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
171 | + } |
|
172 | + |
|
173 | + public function testItSuggestsTags() |
|
174 | + { |
|
175 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
176 | + |
|
177 | + /** |
|
178 | + * Partial tag title match. |
|
179 | + */ |
|
180 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
181 | + |
|
182 | + $this->assertEquals( |
|
183 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
184 | + $field->suggest($request)->getBody() |
|
185 | + ); |
|
186 | + |
|
187 | + /** |
|
188 | + * Exact tag title match. |
|
189 | + */ |
|
190 | + $request = $this->getNewRequest(array('term' => '222')); |
|
191 | + |
|
192 | + $this->assertEquals( |
|
193 | + '{"items":[{"id":"222","text":"222"}]}', |
|
194 | + $field->suggest($request)->getBody() |
|
195 | + ); |
|
196 | + |
|
197 | + /** |
|
198 | + * Case-insensitive tag title match. |
|
199 | + */ |
|
200 | + $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
201 | + |
|
202 | + $this->assertEquals( |
|
203 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
204 | + $field->suggest($request)->getBody() |
|
205 | + ); |
|
206 | + |
|
207 | + /** |
|
208 | + * No tag title match. |
|
209 | + */ |
|
210 | + $request = $this->getNewRequest(array('term' => 'unknown')); |
|
211 | + |
|
212 | + $this->assertEquals( |
|
213 | + '{"items":[]}', |
|
214 | + $field->suggest($request)->getBody() |
|
215 | + ); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Tests that TagField supports pre-filtered data sources |
|
220 | + */ |
|
221 | + public function testRestrictedSuggestions() |
|
222 | + { |
|
223 | + $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
224 | + $field = new TagField('Tags', '', $source); |
|
225 | + |
|
226 | + /** |
|
227 | + * Partial tag title match. |
|
228 | + */ |
|
229 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
230 | + |
|
231 | + $this->assertEquals( |
|
232 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
233 | + $field->suggest($request)->getBody() |
|
234 | + ); |
|
235 | + |
|
236 | + /** |
|
237 | + * Exact tag title match. |
|
238 | + */ |
|
239 | + $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
240 | + |
|
241 | + $this->assertEquals( |
|
242 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
243 | + $field->suggest($request)->getBody() |
|
244 | + ); |
|
245 | + |
|
246 | + /** |
|
247 | + * Excluded item doesn't appear in matches |
|
248 | + */ |
|
249 | + $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
250 | + |
|
251 | + $this->assertEquals( |
|
252 | + '{"items":[]}', |
|
253 | + $field->suggest($request)->getBody() |
|
254 | + ); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @param array $parameters |
|
259 | + * |
|
260 | + * @return HTTPRequest |
|
261 | + */ |
|
262 | + protected function getNewRequest(array $parameters) |
|
263 | + { |
|
264 | + return new HTTPRequest( |
|
265 | + 'get', |
|
266 | + 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
267 | + $parameters |
|
268 | + ); |
|
269 | + } |
|
270 | + |
|
271 | + public function testItDisplaysValuesFromRelations() |
|
272 | + { |
|
273 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
274 | + $record->write(); |
|
275 | + |
|
276 | + $form = new Form( |
|
277 | + new TagFieldTestController($record), |
|
278 | + 'Form', |
|
279 | + new FieldList( |
|
280 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)) |
|
281 | + ), |
|
282 | + new FieldList() |
|
283 | + ); |
|
284 | + |
|
285 | + $form->loadDataFrom( |
|
286 | + $this->objFromFixture(TagFieldTestBlogPost::class, 'BlogPost2') |
|
287 | + ); |
|
288 | + |
|
289 | + $ids = TagFieldTestBlogTag::get()->column('Title'); |
|
290 | + |
|
291 | + $this->assertEquals($field->Value(), $ids); |
|
292 | + } |
|
293 | + |
|
294 | + public function testItIgnoresNewTagsIfCannotCreate() |
|
295 | + { |
|
296 | + $this->markTestSkipped( |
|
297 | + 'This test has not been updated yet.' |
|
298 | + ); |
|
299 | + |
|
300 | + $record = new TagFieldTestBlogPost(); |
|
301 | + $record->write(); |
|
302 | + |
|
303 | + $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
304 | + |
|
305 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), array($tag->Title, 'Tag3')); |
|
306 | + $field->setCanCreate(false); |
|
307 | + $field->saveInto($record); |
|
308 | + |
|
309 | + /** |
|
310 | + * @var TagFieldTestBlogPost $record |
|
311 | + */ |
|
312 | + $record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID); |
|
313 | + |
|
314 | + $this->compareExpectedAndActualTags( |
|
315 | + array('Tag1'), |
|
316 | + $record |
|
317 | + ); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Test you can save without a source set |
|
322 | + */ |
|
323 | + public function testSaveEmptySource() |
|
324 | + { |
|
325 | + $record = new TagFieldTestBlogPost(); |
|
326 | + $record->write(); |
|
327 | + |
|
328 | + // Clear database of tags |
|
329 | + TagFieldTestBlogTag::get()->removeAll(); |
|
330 | + |
|
331 | + $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
332 | + $field->setValue(['New Tag']); |
|
333 | + $field->setCanCreate(true); |
|
334 | + $field->saveInto($record); |
|
335 | + |
|
336 | + $tag = TagFieldTestBlogTag::get()->first(); |
|
337 | + $this->assertNotEmpty($tag); |
|
338 | + $this->assertEquals('New Tag', $tag->Title); |
|
339 | + $record = TagFieldTestBlogPost::get()->byID($record->ID); |
|
340 | + $this->assertEquals( |
|
341 | + $tag->ID, |
|
342 | + $record->Tags()->first()->ID |
|
343 | + ); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Test read only fields are returned |
|
348 | + */ |
|
349 | + public function testReadonlyTransformation() |
|
350 | + { |
|
351 | + $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
352 | + $readOnlyField = $field->performReadonlyTransformation(); |
|
353 | + $this->assertEquals(ReadonlyTagField::class, get_class($readOnlyField)); |
|
354 | + } |
|
355 | 355 | } |