Completed
Pull Request — master (#81)
by Robbie
02:25
created
tests/TagFieldTest.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@
 block discarded – undo
48 48
     }
49 49
 
50 50
     /**
51
-     * @param array $expected
51
+     * @param string[] $expected
52 52
      * @param TagFieldTestBlogPost $record
53 53
      */
54 54
     protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record)
Please login to merge, or discard this patch.
Indentation   +357 added lines, -357 removed lines patch added patch discarded remove patch
@@ -5,329 +5,329 @@  discard block
 block discarded – undo
5 5
  */
6 6
 class TagFieldTest extends SapphireTest
7 7
 {
8
-    /**
9
-     * @var string
10
-     */
11
-    public static $fixture_file = 'tagfield/tests/TagFieldTest.yml';
12
-
13
-    /**
14
-     * @var array
15
-     */
16
-    protected $extraDataObjects = array(
17
-        'TagFieldTestBlogTag',
18
-        'TagFieldTestBlogPost',
19
-    );
20
-
21
-    public function testItSavesLinksToNewTagsOnNewRecords()
22
-    {
23
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost1');
24
-
25
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
26
-        $field->setValue(array('Tag3', 'Tag4'));
27
-        $field->saveInto($record);
28
-
29
-        $record->write();
30
-
31
-        $this->compareExpectedAndActualTags(
32
-            array('Tag3', 'Tag4'),
33
-            $record
34
-        );
35
-    }
36
-
37
-    /**
38
-     * @param string $name
39
-     *
40
-     * @return TagFieldTestBlogPost
41
-     */
42
-    protected function getNewTagFieldTestBlogPost($name)
43
-    {
44
-        return $this->objFromFixture(
45
-            'TagFieldTestBlogPost',
46
-            $name
47
-        );
48
-    }
49
-
50
-    /**
51
-     * @param array $expected
52
-     * @param TagFieldTestBlogPost $record
53
-     */
54
-    protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record)
55
-    {
56
-        $this->compareTagLists($expected, $record->Tags());
57
-    }
58
-
59
-    /**
60
-     * Ensure a source of tags matches the given string tag names
61
-     *
62
-     * @param array $expected
63
-     * @param DataList $actualSource
64
-     */
65
-    protected function compareTagLists(array $expected, DataList $actualSource)
66
-    {
67
-        $actual = array_values($actualSource->map('ID', 'Title')->toArray());
68
-
69
-        sort($expected);
70
-        sort($actual);
71
-
72
-        $this->assertEquals(
73
-            $expected,
74
-            $actual
75
-        );
76
-    }
77
-
78
-    public function testItSavesLinksToNewTagsOnExistingRecords()
79
-    {
80
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost1');
81
-        $record->write();
82
-
83
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
84
-        $field->setValue(array('Tag3', 'Tag4'));
85
-        $field->saveInto($record);
86
-
87
-        $this->compareExpectedAndActualTags(
88
-            array('Tag3', 'Tag4'),
89
-            $record
90
-        );
91
-    }
92
-
93
-    public function testItSavesLinksToExistingTagsOnNewRecords()
94
-    {
95
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost1');
96
-
97
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
98
-        $field->setValue(array('Tag1', 'Tag2'));
99
-        $field->saveInto($record);
100
-
101
-        $record->write();
102
-
103
-        $this->compareExpectedAndActualTags(
104
-            array('Tag1', 'Tag2'),
105
-            $record
106
-        );
107
-    }
108
-
109
-    public function testItSavesLinksToExistingTagsOnExistingRecords()
110
-    {
111
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost1');
112
-        $record->write();
113
-
114
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
115
-        $field->setValue(array('Tag1', 'Tag2'));
116
-        $field->saveInto($record);
117
-
118
-        $this->compareExpectedAndActualTags(
119
-            array('Tag1', 'Tag2'),
120
-            $record
121
-        );
122
-    }
123
-
124
-    /**
125
-     * Ensure that {@see TagField::saveInto} respects existing tags
126
-     */
127
-    public function testSaveDuplicateTags()
128
-    {
129
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost2');
130
-        $record->write();
131
-        $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
132
-
133
-        // Check tags before write
134
-        $this->compareExpectedAndActualTags(
135
-            array('Tag1', 'Tag2'),
136
-            $record
137
-        );
138
-        $this->compareTagLists(
139
-            array('Tag1', 'Tag2'),
140
-            TagFieldTestBlogTag::get()
141
-        );
142
-        $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
143
-
144
-        // Write new tags
145
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
146
-        $field->setValue(array('Tag2', 'Tag3'));
147
-        $field->saveInto($record);
148
-
149
-        // Check only one new tag was added
150
-        $this->compareExpectedAndActualTags(
151
-            array('Tag2', 'Tag3'),
152
-            $record
153
-        );
154
-
155
-        // Ensure that only one new dataobject was added and that tag2s id has not changed
156
-        $this->compareTagLists(
157
-            array('Tag1', 'Tag2', 'Tag3'),
158
-            TagFieldTestBlogTag::get()
159
-        );
160
-        $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
161
-    }
162
-
163
-    public function testItSuggestsTags()
164
-    {
165
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
166
-
167
-        /**
168
-         * Partial tag title match.
169
-         */
170
-        $request = $this->getNewRequest(array('term' => 'Tag'));
171
-
172
-        $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1');
173
-        $tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
174
-
175
-        $this->assertEquals(
176
-            sprintf('{"items":[{"id":%d,"text":"Tag1"},{"id":%d,"text":"Tag2"}]}', $tag1ID, $tag2ID),
177
-            $field->suggest($request)->getBody()
178
-        );
179
-
180
-        /**
181
-         * Exact tag title match.
182
-         */
183
-        $request = $this->getNewRequest(array('term' => 'Tag1'));
184
-
185
-        $this->assertEquals(
186
-            sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
187
-            $field->suggest($request)->getBody()
188
-        );
189
-
190
-        /**
191
-         * Case-insensitive tag title match.
192
-         */
193
-        $request = $this->getNewRequest(array('term' => 'TAG1'));
194
-
195
-        $this->assertEquals(
196
-            sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
197
-            $field->suggest($request)->getBody()
198
-        );
199
-
200
-        /**
201
-         * No tag title match.
202
-         */
203
-        $request = $this->getNewRequest(array('term' => 'unknown'));
204
-
205
-        $this->assertEquals(
206
-            '{"items":[]}',
207
-            $field->suggest($request)->getBody()
208
-        );
209
-    }
210
-
211
-    /**
212
-     * Tests that TagField supports pre-filtered data sources
213
-     */
214
-    public function testRestrictedSuggestions()
215
-    {
216
-        $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
217
-        $field = new TagField('Tags', '', $source);
218
-        $tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1');
219
-
220
-        /**
221
-         * Partial tag title match.
222
-         */
223
-        $request = $this->getNewRequest(array('term' => 'Tag'));
224
-
225
-        $this->assertEquals(
226
-            sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
227
-            $field->suggest($request)->getBody()
228
-        );
229
-
230
-        /**
231
-         * Exact tag title match.
232
-         */
233
-        $request = $this->getNewRequest(array('term' => 'Tag1'));
234
-
235
-        $this->assertEquals(
236
-            sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
237
-            $field->suggest($request)->getBody()
238
-        );
239
-
240
-        /**
241
-         * Excluded item doesn't appear in matches
242
-         */
243
-        $request = $this->getNewRequest(array('term' => 'Tag2'));
244
-
245
-        $this->assertEquals(
246
-            '{"items":[]}',
247
-            $field->suggest($request)->getBody()
248
-        );
249
-    }
250
-
251
-    /**
252
-     * @param array $parameters
253
-     *
254
-     * @return SS_HTTPRequest
255
-     */
256
-    protected function getNewRequest(array $parameters)
257
-    {
258
-        return new SS_HTTPRequest(
259
-            'get',
260
-            'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest',
261
-            $parameters
262
-        );
263
-    }
264
-
265
-    public function testItDisplaysValuesFromRelations()
266
-    {
267
-        $record = $this->getNewTagFieldTestBlogPost('BlogPost1');
268
-        $record->write();
269
-
270
-        $form = new Form(
271
-            new TagFieldTestController($record),
272
-            'Form',
273
-            new FieldList(
274
-                $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
275
-            ),
276
-            new FieldList()
277
-        );
278
-
279
-        $form->loadDataFrom(
280
-            $this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2')
281
-        );
282
-
283
-        $ids = TagFieldTestBlogTag::get()->map('ID', 'ID')->toArray();
284
-
285
-        $this->assertEquals($field->Value(), $ids);
286
-    }
287
-
288
-    public function testItIgnoresNewTagsIfCannotCreate()
289
-    {
290
-        $record = new TagFieldTestBlogPost();
291
-        $record->write();
292
-
293
-        $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first();
294
-
295
-        $field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3'));
296
-        $field->setCanCreate(false);
297
-        $field->saveInto($record);
298
-
299
-        /**
300
-         * @var TagFieldTestBlogPost $record
301
-         */
302
-        $record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID);
303
-
304
-        $this->compareExpectedAndActualTags(
305
-            array('Tag1'),
306
-            $record
307
-        );
308
-    }
8
+	/**
9
+	 * @var string
10
+	 */
11
+	public static $fixture_file = 'tagfield/tests/TagFieldTest.yml';
12
+
13
+	/**
14
+	 * @var array
15
+	 */
16
+	protected $extraDataObjects = array(
17
+		'TagFieldTestBlogTag',
18
+		'TagFieldTestBlogPost',
19
+	);
20
+
21
+	public function testItSavesLinksToNewTagsOnNewRecords()
22
+	{
23
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
24
+
25
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
26
+		$field->setValue(array('Tag3', 'Tag4'));
27
+		$field->saveInto($record);
28
+
29
+		$record->write();
30
+
31
+		$this->compareExpectedAndActualTags(
32
+			array('Tag3', 'Tag4'),
33
+			$record
34
+		);
35
+	}
36
+
37
+	/**
38
+	 * @param string $name
39
+	 *
40
+	 * @return TagFieldTestBlogPost
41
+	 */
42
+	protected function getNewTagFieldTestBlogPost($name)
43
+	{
44
+		return $this->objFromFixture(
45
+			'TagFieldTestBlogPost',
46
+			$name
47
+		);
48
+	}
49
+
50
+	/**
51
+	 * @param array $expected
52
+	 * @param TagFieldTestBlogPost $record
53
+	 */
54
+	protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record)
55
+	{
56
+		$this->compareTagLists($expected, $record->Tags());
57
+	}
58
+
59
+	/**
60
+	 * Ensure a source of tags matches the given string tag names
61
+	 *
62
+	 * @param array $expected
63
+	 * @param DataList $actualSource
64
+	 */
65
+	protected function compareTagLists(array $expected, DataList $actualSource)
66
+	{
67
+		$actual = array_values($actualSource->map('ID', 'Title')->toArray());
68
+
69
+		sort($expected);
70
+		sort($actual);
71
+
72
+		$this->assertEquals(
73
+			$expected,
74
+			$actual
75
+		);
76
+	}
77
+
78
+	public function testItSavesLinksToNewTagsOnExistingRecords()
79
+	{
80
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
81
+		$record->write();
82
+
83
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
84
+		$field->setValue(array('Tag3', 'Tag4'));
85
+		$field->saveInto($record);
86
+
87
+		$this->compareExpectedAndActualTags(
88
+			array('Tag3', 'Tag4'),
89
+			$record
90
+		);
91
+	}
92
+
93
+	public function testItSavesLinksToExistingTagsOnNewRecords()
94
+	{
95
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
96
+
97
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
98
+		$field->setValue(array('Tag1', 'Tag2'));
99
+		$field->saveInto($record);
100
+
101
+		$record->write();
102
+
103
+		$this->compareExpectedAndActualTags(
104
+			array('Tag1', 'Tag2'),
105
+			$record
106
+		);
107
+	}
108
+
109
+	public function testItSavesLinksToExistingTagsOnExistingRecords()
110
+	{
111
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
112
+		$record->write();
113
+
114
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
115
+		$field->setValue(array('Tag1', 'Tag2'));
116
+		$field->saveInto($record);
117
+
118
+		$this->compareExpectedAndActualTags(
119
+			array('Tag1', 'Tag2'),
120
+			$record
121
+		);
122
+	}
123
+
124
+	/**
125
+	 * Ensure that {@see TagField::saveInto} respects existing tags
126
+	 */
127
+	public function testSaveDuplicateTags()
128
+	{
129
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost2');
130
+		$record->write();
131
+		$tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
132
+
133
+		// Check tags before write
134
+		$this->compareExpectedAndActualTags(
135
+			array('Tag1', 'Tag2'),
136
+			$record
137
+		);
138
+		$this->compareTagLists(
139
+			array('Tag1', 'Tag2'),
140
+			TagFieldTestBlogTag::get()
141
+		);
142
+		$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
143
+
144
+		// Write new tags
145
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
146
+		$field->setValue(array('Tag2', 'Tag3'));
147
+		$field->saveInto($record);
148
+
149
+		// Check only one new tag was added
150
+		$this->compareExpectedAndActualTags(
151
+			array('Tag2', 'Tag3'),
152
+			$record
153
+		);
154
+
155
+		// Ensure that only one new dataobject was added and that tag2s id has not changed
156
+		$this->compareTagLists(
157
+			array('Tag1', 'Tag2', 'Tag3'),
158
+			TagFieldTestBlogTag::get()
159
+		);
160
+		$this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID'));
161
+	}
162
+
163
+	public function testItSuggestsTags()
164
+	{
165
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'));
166
+
167
+		/**
168
+		 * Partial tag title match.
169
+		 */
170
+		$request = $this->getNewRequest(array('term' => 'Tag'));
171
+
172
+		$tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1');
173
+		$tag2ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag2');
174
+
175
+		$this->assertEquals(
176
+			sprintf('{"items":[{"id":%d,"text":"Tag1"},{"id":%d,"text":"Tag2"}]}', $tag1ID, $tag2ID),
177
+			$field->suggest($request)->getBody()
178
+		);
179
+
180
+		/**
181
+		 * Exact tag title match.
182
+		 */
183
+		$request = $this->getNewRequest(array('term' => 'Tag1'));
184
+
185
+		$this->assertEquals(
186
+			sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
187
+			$field->suggest($request)->getBody()
188
+		);
189
+
190
+		/**
191
+		 * Case-insensitive tag title match.
192
+		 */
193
+		$request = $this->getNewRequest(array('term' => 'TAG1'));
194
+
195
+		$this->assertEquals(
196
+			sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
197
+			$field->suggest($request)->getBody()
198
+		);
199
+
200
+		/**
201
+		 * No tag title match.
202
+		 */
203
+		$request = $this->getNewRequest(array('term' => 'unknown'));
204
+
205
+		$this->assertEquals(
206
+			'{"items":[]}',
207
+			$field->suggest($request)->getBody()
208
+		);
209
+	}
210
+
211
+	/**
212
+	 * Tests that TagField supports pre-filtered data sources
213
+	 */
214
+	public function testRestrictedSuggestions()
215
+	{
216
+		$source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2');
217
+		$field = new TagField('Tags', '', $source);
218
+		$tag1ID = $this->idFromFixture('TagFieldTestBlogTag', 'Tag1');
219
+
220
+		/**
221
+		 * Partial tag title match.
222
+		 */
223
+		$request = $this->getNewRequest(array('term' => 'Tag'));
224
+
225
+		$this->assertEquals(
226
+			sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
227
+			$field->suggest($request)->getBody()
228
+		);
229
+
230
+		/**
231
+		 * Exact tag title match.
232
+		 */
233
+		$request = $this->getNewRequest(array('term' => 'Tag1'));
234
+
235
+		$this->assertEquals(
236
+			sprintf('{"items":[{"id":%d,"text":"Tag1"}]}', $tag1ID),
237
+			$field->suggest($request)->getBody()
238
+		);
239
+
240
+		/**
241
+		 * Excluded item doesn't appear in matches
242
+		 */
243
+		$request = $this->getNewRequest(array('term' => 'Tag2'));
244
+
245
+		$this->assertEquals(
246
+			'{"items":[]}',
247
+			$field->suggest($request)->getBody()
248
+		);
249
+	}
250
+
251
+	/**
252
+	 * @param array $parameters
253
+	 *
254
+	 * @return SS_HTTPRequest
255
+	 */
256
+	protected function getNewRequest(array $parameters)
257
+	{
258
+		return new SS_HTTPRequest(
259
+			'get',
260
+			'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest',
261
+			$parameters
262
+		);
263
+	}
264
+
265
+	public function testItDisplaysValuesFromRelations()
266
+	{
267
+		$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
268
+		$record->write();
269
+
270
+		$form = new Form(
271
+			new TagFieldTestController($record),
272
+			'Form',
273
+			new FieldList(
274
+				$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
275
+			),
276
+			new FieldList()
277
+		);
278
+
279
+		$form->loadDataFrom(
280
+			$this->objFromFixture('TagFieldTestBlogPost', 'BlogPost2')
281
+		);
282
+
283
+		$ids = TagFieldTestBlogTag::get()->map('ID', 'ID')->toArray();
284
+
285
+		$this->assertEquals($field->Value(), $ids);
286
+	}
287
+
288
+	public function testItIgnoresNewTagsIfCannotCreate()
289
+	{
290
+		$record = new TagFieldTestBlogPost();
291
+		$record->write();
292
+
293
+		$tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first();
294
+
295
+		$field = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'), array($tag->ID, 'Tag3'));
296
+		$field->setCanCreate(false);
297
+		$field->saveInto($record);
298
+
299
+		/**
300
+		 * @var TagFieldTestBlogPost $record
301
+		 */
302
+		$record = DataObject::get_by_id('TagFieldTestBlogPost', $record->ID);
303
+
304
+		$this->compareExpectedAndActualTags(
305
+			array('Tag1'),
306
+			$record
307
+		);
308
+	}
309 309
 }
310 310
 
311 311
 class TagFieldTestBlogTag extends DataObject implements TestOnly
312 312
 {
313
-    /**
314
-     * @var string
315
-     */
316
-    private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC';
317
-
318
-    /**
319
-     * @var array
320
-     */
321
-    private static $db = array(
322
-        'Title' => 'Varchar(200)',
323
-    );
324
-
325
-    /**
326
-     * @var array
327
-     */
328
-    private static $belongs_many_many = array(
329
-        'BlogPosts' => 'TagFieldTestBlogPost',
330
-    );
313
+	/**
314
+	 * @var string
315
+	 */
316
+	private static $default_sort = '"TagFieldTestBlogTag"."ID" ASC';
317
+
318
+	/**
319
+	 * @var array
320
+	 */
321
+	private static $db = array(
322
+		'Title' => 'Varchar(200)',
323
+	);
324
+
325
+	/**
326
+	 * @var array
327
+	 */
328
+	private static $belongs_many_many = array(
329
+		'BlogPosts' => 'TagFieldTestBlogPost',
330
+	);
331 331
 }
332 332
 
333 333
 /**
@@ -335,46 +335,46 @@  discard block
 block discarded – undo
335 335
  */
336 336
 class TagFieldTestBlogPost extends DataObject implements TestOnly
337 337
 {
338
-    /**
339
-     * @var array
340
-     */
341
-    private static $db = array(
342
-        'Title' => 'Text',
343
-        'Content' => 'Text',
344
-    );
345
-
346
-    /**
347
-     * @var array
348
-     */
349
-    private static $many_many = array(
350
-        'Tags' => 'TagFieldTestBlogTag',
351
-    );
338
+	/**
339
+	 * @var array
340
+	 */
341
+	private static $db = array(
342
+		'Title' => 'Text',
343
+		'Content' => 'Text',
344
+	);
345
+
346
+	/**
347
+	 * @var array
348
+	 */
349
+	private static $many_many = array(
350
+		'Tags' => 'TagFieldTestBlogTag',
351
+	);
352 352
 }
353 353
 
354 354
 class TagFieldTestController extends Controller implements TestOnly
355 355
 {
356
-    /**
357
-     * @return Form
358
-     */
359
-    public function TagFieldTestForm()
360
-    {
361
-        $fields = new FieldList(
362
-            $tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
363
-        );
364
-
365
-        $actions = new FieldList(
366
-            new FormAction('TagFieldTestFormSubmit')
367
-        );
368
-
369
-        return new Form($this, 'TagFieldTestForm', $fields, $actions);
370
-    }
371
-
372
-    /**
373
-     * @param DataObject $dataObject
374
-     * @param Form $form
375
-     */
376
-    public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form)
377
-    {
378
-        $form->saveInto($dataObject);
379
-    }
356
+	/**
357
+	 * @return Form
358
+	 */
359
+	public function TagFieldTestForm()
360
+	{
361
+		$fields = new FieldList(
362
+			$tagField = new TagField('Tags', '', new DataList('TagFieldTestBlogTag'))
363
+		);
364
+
365
+		$actions = new FieldList(
366
+			new FormAction('TagFieldTestFormSubmit')
367
+		);
368
+
369
+		return new Form($this, 'TagFieldTestForm', $fields, $actions);
370
+	}
371
+
372
+	/**
373
+	 * @param DataObject $dataObject
374
+	 * @param Form $form
375
+	 */
376
+	public function TagFieldTestFormSubmit(DataObject $dataObject, Form $form)
377
+	{
378
+		$form->saveInto($dataObject);
379
+	}
380 380
 }
Please login to merge, or discard this patch.
tests/StringTagFieldTest.php 1 patch
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -5,114 +5,114 @@  discard block
 block discarded – undo
5 5
  */
6 6
 class StringTagFieldTest extends SapphireTest
7 7
 {
8
-    /**
9
-     * @var string
10
-     */
11
-    public static $fixture_file = 'tagfield/tests/StringTagFieldTest.yml';
12
-
13
-    /**
14
-     * @var array
15
-     */
16
-    protected $extraDataObjects = array(
17
-        'StringTagFieldTestBlogPost',
18
-    );
19
-
20
-    public function testItSavesTagsOnNewRecords()
21
-    {
22
-        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
23
-
24
-        $field = new StringTagField('Tags');
25
-        $field->setValue(array('Tag1', 'Tag2'));
26
-        $field->saveInto($record);
27
-
28
-        $record->write();
29
-
30
-        $this->assertEquals('Tag1,Tag2', $record->Tags);
31
-    }
32
-
33
-    /**
34
-     * @param string $name
35
-     *
36
-     * @return StringTagFieldTestBlogPost
37
-     */
38
-    protected function getNewStringTagFieldTestBlogPost($name)
39
-    {
40
-        return $this->objFromFixture(
41
-            'StringTagFieldTestBlogPost',
42
-            $name
43
-        );
44
-    }
45
-
46
-    public function testItSavesTagsOnExistingRecords()
47
-    {
48
-        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
49
-        $record->write();
50
-
51
-        $field = new StringTagField('Tags');
52
-        $field->setValue(array('Tag1', 'Tag2'));
53
-        $field->saveInto($record);
54
-
55
-        $this->assertEquals('Tag1,Tag2', $record->Tags);
56
-    }
57
-
58
-    public function testItSuggestsTags()
59
-    {
60
-        $record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
61
-
62
-        $field = new StringTagField('Tags');
63
-        $field->setRecord($record);
64
-
65
-        /**
66
-         * Partial tag title match.
67
-         */
68
-        $request = $this->getNewRequest(array('term' => 'Tag'));
69
-
70
-        $this->assertEquals(
71
-            '{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
72
-            $field->suggest($request)->getBody()
73
-        );
74
-
75
-        /**
76
-         * Exact tag title match.
77
-         */
78
-        $request = $this->getNewRequest(array('term' => 'Tag1'));
79
-
80
-        $this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
81
-
82
-        /**
83
-         * Case-insensitive tag title match.
84
-         */
85
-        $request = $this->getNewRequest(array('term' => 'TAG1'));
86
-
87
-        $this->assertEquals(
88
-            '{"items":[{"id":"Tag1","text":"Tag1"}]}',
89
-            $field->suggest($request)->getBody()
90
-        );
91
-
92
-        /**
93
-         * No tag title match.
94
-         */
95
-        $request = $this->getNewRequest(array('term' => 'unknown'));
96
-
97
-        $this->assertEquals(
98
-            '{"items":[]}',
99
-            $field->suggest($request)->getBody()
100
-        );
101
-    }
102
-
103
-    /**
104
-     * @param array $parameters
105
-     *
106
-     * @return SS_HTTPRequest
107
-     */
108
-    protected function getNewRequest(array $parameters)
109
-    {
110
-        return new SS_HTTPRequest(
111
-            'get',
112
-            'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
113
-            $parameters
114
-        );
115
-    }
8
+	/**
9
+	 * @var string
10
+	 */
11
+	public static $fixture_file = 'tagfield/tests/StringTagFieldTest.yml';
12
+
13
+	/**
14
+	 * @var array
15
+	 */
16
+	protected $extraDataObjects = array(
17
+		'StringTagFieldTestBlogPost',
18
+	);
19
+
20
+	public function testItSavesTagsOnNewRecords()
21
+	{
22
+		$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
23
+
24
+		$field = new StringTagField('Tags');
25
+		$field->setValue(array('Tag1', 'Tag2'));
26
+		$field->saveInto($record);
27
+
28
+		$record->write();
29
+
30
+		$this->assertEquals('Tag1,Tag2', $record->Tags);
31
+	}
32
+
33
+	/**
34
+	 * @param string $name
35
+	 *
36
+	 * @return StringTagFieldTestBlogPost
37
+	 */
38
+	protected function getNewStringTagFieldTestBlogPost($name)
39
+	{
40
+		return $this->objFromFixture(
41
+			'StringTagFieldTestBlogPost',
42
+			$name
43
+		);
44
+	}
45
+
46
+	public function testItSavesTagsOnExistingRecords()
47
+	{
48
+		$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
49
+		$record->write();
50
+
51
+		$field = new StringTagField('Tags');
52
+		$field->setValue(array('Tag1', 'Tag2'));
53
+		$field->saveInto($record);
54
+
55
+		$this->assertEquals('Tag1,Tag2', $record->Tags);
56
+	}
57
+
58
+	public function testItSuggestsTags()
59
+	{
60
+		$record = $this->getNewStringTagFieldTestBlogPost('BlogPost2');
61
+
62
+		$field = new StringTagField('Tags');
63
+		$field->setRecord($record);
64
+
65
+		/**
66
+		 * Partial tag title match.
67
+		 */
68
+		$request = $this->getNewRequest(array('term' => 'Tag'));
69
+
70
+		$this->assertEquals(
71
+			'{"items":[{"id":"Tag1","text":"Tag1"},{"id":"Tag2","text":"Tag2"}]}',
72
+			$field->suggest($request)->getBody()
73
+		);
74
+
75
+		/**
76
+		 * Exact tag title match.
77
+		 */
78
+		$request = $this->getNewRequest(array('term' => 'Tag1'));
79
+
80
+		$this->assertEquals($field->suggest($request)->getBody(), '{"items":[{"id":"Tag1","text":"Tag1"}]}');
81
+
82
+		/**
83
+		 * Case-insensitive tag title match.
84
+		 */
85
+		$request = $this->getNewRequest(array('term' => 'TAG1'));
86
+
87
+		$this->assertEquals(
88
+			'{"items":[{"id":"Tag1","text":"Tag1"}]}',
89
+			$field->suggest($request)->getBody()
90
+		);
91
+
92
+		/**
93
+		 * No tag title match.
94
+		 */
95
+		$request = $this->getNewRequest(array('term' => 'unknown'));
96
+
97
+		$this->assertEquals(
98
+			'{"items":[]}',
99
+			$field->suggest($request)->getBody()
100
+		);
101
+	}
102
+
103
+	/**
104
+	 * @param array $parameters
105
+	 *
106
+	 * @return SS_HTTPRequest
107
+	 */
108
+	protected function getNewRequest(array $parameters)
109
+	{
110
+		return new SS_HTTPRequest(
111
+			'get',
112
+			'StringTagFieldTestController/StringTagFieldTestForm/fields/Tags/suggest',
113
+			$parameters
114
+		);
115
+	}
116 116
 }
117 117
 
118 118
 /**
@@ -120,40 +120,40 @@  discard block
 block discarded – undo
120 120
  */
121 121
 class StringTagFieldTestBlogPost extends DataObject implements TestOnly
122 122
 {
123
-    /**
124
-     * @var array
125
-     */
126
-    private static $db = array(
127
-        'Title' => 'Text',
128
-        'Content' => 'Text',
129
-        'Tags' => 'Text',
130
-    );
123
+	/**
124
+	 * @var array
125
+	 */
126
+	private static $db = array(
127
+		'Title' => 'Text',
128
+		'Content' => 'Text',
129
+		'Tags' => 'Text',
130
+	);
131 131
 }
132 132
 
133 133
 class StringTagFieldTestController extends Controller implements TestOnly
134 134
 {
135
-    /**
136
-     * @return Form
137
-     */
138
-    public function StringTagFieldTestForm()
139
-    {
140
-        $fields = new FieldList(
141
-            $tagField = new StringTagField('Tags')
142
-        );
143
-
144
-        $actions = new FieldList(
145
-            new FormAction('StringTagFieldTestFormSubmit')
146
-        );
147
-
148
-        return new Form($this, 'StringTagFieldTestForm', $fields, $actions);
149
-    }
150
-
151
-    /**
152
-     * @param DataObject $dataObject
153
-     * @param Form $form
154
-     */
155
-    public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form)
156
-    {
157
-        $form->saveInto($dataObject);
158
-    }
135
+	/**
136
+	 * @return Form
137
+	 */
138
+	public function StringTagFieldTestForm()
139
+	{
140
+		$fields = new FieldList(
141
+			$tagField = new StringTagField('Tags')
142
+		);
143
+
144
+		$actions = new FieldList(
145
+			new FormAction('StringTagFieldTestFormSubmit')
146
+		);
147
+
148
+		return new Form($this, 'StringTagFieldTestForm', $fields, $actions);
149
+	}
150
+
151
+	/**
152
+	 * @param DataObject $dataObject
153
+	 * @param Form $form
154
+	 */
155
+	public function StringTagFieldTestFormSubmit(DataObject $dataObject, Form $form)
156
+	{
157
+		$form->saveInto($dataObject);
158
+	}
159 159
 }
Please login to merge, or discard this patch.
code/StringTagField.php 3 patches
Doc Comments   +1 added lines patch added patch discarded remove patch
@@ -235,6 +235,7 @@
 block discarded – undo
235 235
 
236 236
     /**
237 237
      * {@inheritdoc}
238
+     * @param string[] $value
238 239
      */
239 240
     public function setValue($value, $source = null)
240 241
     {
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -142,13 +142,13 @@  discard block
 block discarded – undo
142 142
      */
143 143
     public function Field($properties = array())
144 144
     {
145
-        Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
146
-        Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
145
+        Requirements::css(TAG_FIELD_DIR.'/css/select2.min.css');
146
+        Requirements::css(TAG_FIELD_DIR.'/css/TagField.css');
147 147
 
148
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
149
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
150
-        Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
151
-        Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
148
+        Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
149
+        Requirements::javascript(THIRDPARTY_DIR.'/jquery-entwine/dist/jquery.entwine-dist.js');
150
+        Requirements::javascript(TAG_FIELD_DIR.'/js/select2.js');
151
+        Requirements::javascript(TAG_FIELD_DIR.'/js/TagField.js');
152 152
 
153 153
         $this->addExtraClass('ss-tag-field');
154 154
 
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
     {
238 238
         return array_merge(
239 239
             parent::getAttributes(),
240
-            array('name' => $this->getName() . '[]')
240
+            array('name' => $this->getName().'[]')
241 241
         );
242 242
     }
243 243
 
@@ -309,7 +309,7 @@  discard block
 block discarded – undo
309 309
         $term = Convert::raw2sql($term);
310 310
 
311 311
         $query = $className::get()
312
-            ->filter($fieldName . ':PartialMatch:nocase', $term)
312
+            ->filter($fieldName.':PartialMatch:nocase', $term)
313 313
             ->limit($this->getLazyLoadItemLimit());
314 314
 
315 315
         $items = array();
Please login to merge, or discard this patch.
Indentation   +332 added lines, -332 removed lines patch added patch discarded remove patch
@@ -11,336 +11,336 @@
 block discarded – undo
11 11
  */
12 12
 class StringTagField extends DropdownField
13 13
 {
14
-    /**
15
-     * @var array
16
-     */
17
-    public static $allowed_actions = array(
18
-        'suggest',
19
-    );
20
-
21
-    /**
22
-     * @var bool
23
-     */
24
-    protected $shouldLazyLoad = false;
25
-
26
-    /**
27
-     * @var int
28
-     */
29
-    protected $lazyLoadItemLimit = 10;
30
-
31
-    /**
32
-     * @var null|DataObject
33
-     */
34
-    protected $record;
35
-
36
-    /**
37
-     * @var bool
38
-     */
39
-    protected $isMultiple = true;
40
-
41
-    /**
42
-     * @param string $name
43
-     * @param string $title
44
-     * @param array|SS_List $source
45
-     * @param array|SS_List $value
46
-     */
47
-    public function __construct($name, $title = '', $source = array(), $value = array())
48
-    {
49
-        parent::__construct($name, $title, $source, $value);
50
-    }
51
-
52
-    /**
53
-     * @return bool
54
-     */
55
-    public function getShouldLazyLoad()
56
-    {
57
-        return $this->shouldLazyLoad;
58
-    }
59
-
60
-    /**
61
-     * @param bool $shouldLazyLoad
62
-     *
63
-     * @return static
64
-     */
65
-    public function setShouldLazyLoad($shouldLazyLoad)
66
-    {
67
-        $this->shouldLazyLoad = $shouldLazyLoad;
68
-
69
-        return $this;
70
-    }
71
-
72
-    /**
73
-     * @return int
74
-     */
75
-    public function getLazyLoadItemLimit()
76
-    {
77
-        return $this->lazyLoadItemLimit;
78
-    }
79
-
80
-    /**
81
-     * @param int $lazyLoadItemLimit
82
-     *
83
-     * @return static
84
-     */
85
-    public function setLazyLoadItemLimit($lazyLoadItemLimit)
86
-    {
87
-        $this->lazyLoadItemLimit = $lazyLoadItemLimit;
88
-
89
-        return $this;
90
-    }
91
-
92
-    /**
93
-     * @return bool
94
-     */
95
-    public function getIsMultiple()
96
-    {
97
-        return $this->isMultiple;
98
-    }
99
-
100
-    /**
101
-     * @param bool $isMultiple
102
-     *
103
-     * @return static
104
-     */
105
-    public function setIsMultiple($isMultiple)
106
-    {
107
-        $this->isMultiple = $isMultiple;
108
-
109
-        return $this;
110
-    }
111
-
112
-    /**
113
-     * @return null|DataObject
114
-     */
115
-    public function getRecord()
116
-    {
117
-        if ($this->record) {
118
-            return $this->record;
119
-        }
120
-
121
-        if ($form = $this->getForm()) {
122
-            return $form->getRecord();
123
-        }
124
-
125
-        return null;
126
-    }
127
-
128
-    /**
129
-     * @param DataObject $record
130
-     *
131
-     * @return $this
132
-     */
133
-    public function setRecord(DataObject $record)
134
-    {
135
-        $this->record = $record;
136
-
137
-        return $this;
138
-    }
139
-
140
-    /**
141
-     * {@inheritdoc}
142
-     */
143
-    public function Field($properties = array())
144
-    {
145
-        Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
146
-        Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
147
-
148
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
149
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
150
-        Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
151
-        Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
152
-
153
-        $this->addExtraClass('ss-tag-field');
154
-
155
-        if ($this->getIsMultiple()) {
156
-            $this->setAttribute('multiple', 'multiple');
157
-        }
158
-
159
-        if ($this->getShouldLazyLoad()) {
160
-            $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
161
-        } else {
162
-            $properties = array_merge($properties, array(
163
-                'Options' => $this->getOptions()
164
-            ));
165
-        }
166
-
167
-        $this->setAttribute('data-can-create', (int) $this->getCanCreate());
168
-
169
-        return $this
170
-            ->customise($properties)
171
-            ->renderWith(array("templates/TagField"));
172
-    }
173
-
174
-    /**
175
-     * @return string
176
-     */
177
-    protected function getSuggestURL()
178
-    {
179
-        return Controller::join_links($this->Link(), 'suggest');
180
-    }
181
-
182
-    /**
183
-     * @return ArrayList
184
-     */
185
-    protected function getOptions()
186
-    {
187
-        $options = ArrayList::create();
188
-
189
-        $source = $this->getSource();
190
-
191
-        if ($source instanceof Iterator) {
192
-            $source = iterator_to_array($source);
193
-        }
194
-
195
-        $values = $this->Value();
196
-
197
-        foreach ($source as $value) {
198
-            $options->push(
199
-                ArrayData::create(array(
200
-                    'Title' => $value,
201
-                    'Value' => $value,
202
-                    'Selected' => in_array($value, $values),
203
-                ))
204
-            );
205
-        }
206
-
207
-        return $options;
208
-    }
209
-
210
-    /**
211
-     * {@inheritdoc}
212
-     */
213
-    public function setValue($value, $source = null)
214
-    {
215
-        if (is_string($value)) {
216
-            $value = explode(',', $value);
217
-        }
218
-
219
-        if ($source instanceof DataObject) {
220
-            $name = $this->getName();
221
-            $value = explode(',', $source->$name);
222
-        }
223
-
224
-        if ($source instanceof SS_List) {
225
-            $value = $source->column('ID');
226
-        }
227
-
228
-        if (is_null($value)) {
229
-            $value = array();
230
-        }
231
-
232
-        return parent::setValue(array_filter($value));
233
-    }
234
-
235
-    /**
236
-     * {@inheritdoc}
237
-     */
238
-    public function getAttributes()
239
-    {
240
-        return array_merge(
241
-            parent::getAttributes(),
242
-            array('name' => $this->getName() . '[]')
243
-        );
244
-    }
245
-
246
-    /**
247
-     * {@inheritdoc}
248
-     */
249
-    public function saveInto(DataObjectInterface $record)
250
-    {
251
-        parent::saveInto($record);
252
-
253
-        $name = $this->getName();
254
-
255
-        $record->$name = join(',', $this->Value());
256
-        $record->write();
257
-    }
258
-
259
-    /**
260
-     * Returns a JSON string of tags, for lazy loading.
261
-     *
262
-     * @param SS_HTTPRequest $request
263
-     *
264
-     * @return SS_HTTPResponse
265
-     */
266
-    public function suggest(SS_HTTPRequest $request)
267
-    {
268
-        $responseBody = Convert::raw2json(
269
-            array('items' => array())
270
-        );
271
-
272
-        $response = new SS_HTTPResponse();
273
-        $response->addHeader('Content-Type', 'application/json');
274
-
275
-        if ($record = $this->getRecord()) {
276
-            $tags = array();
277
-            $term = $request->getVar('term');
278
-
279
-            if ($record->hasField($this->getName())) {
280
-                $tags = $this->getTags($term);
281
-            }
282
-
283
-            $responseBody = Convert::raw2json(
284
-                array('items' => $tags)
285
-            );
286
-        }
287
-
288
-        $response->setBody($responseBody);
289
-
290
-        return $response;
291
-    }
292
-
293
-    /**
294
-     * Returns array of arrays representing tags.
295
-     *
296
-     * @param string $term
297
-     *
298
-     * @return array
299
-     */
300
-    protected function getTags($term)
301
-    {
302
-        $record = $this->getRecord();
303
-
304
-        if (!$record) {
305
-            return array();
306
-        }
307
-
308
-        $fieldName = $this->getName();
309
-        $className = $record->getClassName();
310
-
311
-        $term = Convert::raw2sql($term);
312
-
313
-        $query = $className::get()
314
-            ->filter($fieldName . ':PartialMatch:nocase', $term)
315
-            ->limit($this->getLazyLoadItemLimit());
316
-
317
-        $items = array();
318
-
319
-        foreach ($query->column($fieldName) as $tags) {
320
-            $tags = explode(',', $tags);
321
-
322
-            foreach ($tags as $i => $tag) {
323
-                if (stripos($tag, $term) !== false && !in_array($tag, $items)) {
324
-                    $items[] = array(
325
-                        'id' => $tag,
326
-                        'text' => $tag
327
-                    );
328
-                }
329
-            }
330
-        }
331
-
332
-        return $items;
333
-    }
334
-
335
-    /**
336
-     * DropdownField assumes value will be a scalar so we must
337
-     * override validate. This only applies to Silverstripe 3.2+
338
-     *
339
-     * @param Validator $validator
340
-     * @return bool
341
-     */
342
-    public function validate($validator)
343
-    {
344
-        return true;
345
-    }
14
+	/**
15
+	 * @var array
16
+	 */
17
+	public static $allowed_actions = array(
18
+		'suggest',
19
+	);
20
+
21
+	/**
22
+	 * @var bool
23
+	 */
24
+	protected $shouldLazyLoad = false;
25
+
26
+	/**
27
+	 * @var int
28
+	 */
29
+	protected $lazyLoadItemLimit = 10;
30
+
31
+	/**
32
+	 * @var null|DataObject
33
+	 */
34
+	protected $record;
35
+
36
+	/**
37
+	 * @var bool
38
+	 */
39
+	protected $isMultiple = true;
40
+
41
+	/**
42
+	 * @param string $name
43
+	 * @param string $title
44
+	 * @param array|SS_List $source
45
+	 * @param array|SS_List $value
46
+	 */
47
+	public function __construct($name, $title = '', $source = array(), $value = array())
48
+	{
49
+		parent::__construct($name, $title, $source, $value);
50
+	}
51
+
52
+	/**
53
+	 * @return bool
54
+	 */
55
+	public function getShouldLazyLoad()
56
+	{
57
+		return $this->shouldLazyLoad;
58
+	}
59
+
60
+	/**
61
+	 * @param bool $shouldLazyLoad
62
+	 *
63
+	 * @return static
64
+	 */
65
+	public function setShouldLazyLoad($shouldLazyLoad)
66
+	{
67
+		$this->shouldLazyLoad = $shouldLazyLoad;
68
+
69
+		return $this;
70
+	}
71
+
72
+	/**
73
+	 * @return int
74
+	 */
75
+	public function getLazyLoadItemLimit()
76
+	{
77
+		return $this->lazyLoadItemLimit;
78
+	}
79
+
80
+	/**
81
+	 * @param int $lazyLoadItemLimit
82
+	 *
83
+	 * @return static
84
+	 */
85
+	public function setLazyLoadItemLimit($lazyLoadItemLimit)
86
+	{
87
+		$this->lazyLoadItemLimit = $lazyLoadItemLimit;
88
+
89
+		return $this;
90
+	}
91
+
92
+	/**
93
+	 * @return bool
94
+	 */
95
+	public function getIsMultiple()
96
+	{
97
+		return $this->isMultiple;
98
+	}
99
+
100
+	/**
101
+	 * @param bool $isMultiple
102
+	 *
103
+	 * @return static
104
+	 */
105
+	public function setIsMultiple($isMultiple)
106
+	{
107
+		$this->isMultiple = $isMultiple;
108
+
109
+		return $this;
110
+	}
111
+
112
+	/**
113
+	 * @return null|DataObject
114
+	 */
115
+	public function getRecord()
116
+	{
117
+		if ($this->record) {
118
+			return $this->record;
119
+		}
120
+
121
+		if ($form = $this->getForm()) {
122
+			return $form->getRecord();
123
+		}
124
+
125
+		return null;
126
+	}
127
+
128
+	/**
129
+	 * @param DataObject $record
130
+	 *
131
+	 * @return $this
132
+	 */
133
+	public function setRecord(DataObject $record)
134
+	{
135
+		$this->record = $record;
136
+
137
+		return $this;
138
+	}
139
+
140
+	/**
141
+	 * {@inheritdoc}
142
+	 */
143
+	public function Field($properties = array())
144
+	{
145
+		Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
146
+		Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
147
+
148
+		Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
149
+		Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
150
+		Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
151
+		Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
152
+
153
+		$this->addExtraClass('ss-tag-field');
154
+
155
+		if ($this->getIsMultiple()) {
156
+			$this->setAttribute('multiple', 'multiple');
157
+		}
158
+
159
+		if ($this->getShouldLazyLoad()) {
160
+			$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
161
+		} else {
162
+			$properties = array_merge($properties, array(
163
+				'Options' => $this->getOptions()
164
+			));
165
+		}
166
+
167
+		$this->setAttribute('data-can-create', (int) $this->getCanCreate());
168
+
169
+		return $this
170
+			->customise($properties)
171
+			->renderWith(array("templates/TagField"));
172
+	}
173
+
174
+	/**
175
+	 * @return string
176
+	 */
177
+	protected function getSuggestURL()
178
+	{
179
+		return Controller::join_links($this->Link(), 'suggest');
180
+	}
181
+
182
+	/**
183
+	 * @return ArrayList
184
+	 */
185
+	protected function getOptions()
186
+	{
187
+		$options = ArrayList::create();
188
+
189
+		$source = $this->getSource();
190
+
191
+		if ($source instanceof Iterator) {
192
+			$source = iterator_to_array($source);
193
+		}
194
+
195
+		$values = $this->Value();
196
+
197
+		foreach ($source as $value) {
198
+			$options->push(
199
+				ArrayData::create(array(
200
+					'Title' => $value,
201
+					'Value' => $value,
202
+					'Selected' => in_array($value, $values),
203
+				))
204
+			);
205
+		}
206
+
207
+		return $options;
208
+	}
209
+
210
+	/**
211
+	 * {@inheritdoc}
212
+	 */
213
+	public function setValue($value, $source = null)
214
+	{
215
+		if (is_string($value)) {
216
+			$value = explode(',', $value);
217
+		}
218
+
219
+		if ($source instanceof DataObject) {
220
+			$name = $this->getName();
221
+			$value = explode(',', $source->$name);
222
+		}
223
+
224
+		if ($source instanceof SS_List) {
225
+			$value = $source->column('ID');
226
+		}
227
+
228
+		if (is_null($value)) {
229
+			$value = array();
230
+		}
231
+
232
+		return parent::setValue(array_filter($value));
233
+	}
234
+
235
+	/**
236
+	 * {@inheritdoc}
237
+	 */
238
+	public function getAttributes()
239
+	{
240
+		return array_merge(
241
+			parent::getAttributes(),
242
+			array('name' => $this->getName() . '[]')
243
+		);
244
+	}
245
+
246
+	/**
247
+	 * {@inheritdoc}
248
+	 */
249
+	public function saveInto(DataObjectInterface $record)
250
+	{
251
+		parent::saveInto($record);
252
+
253
+		$name = $this->getName();
254
+
255
+		$record->$name = join(',', $this->Value());
256
+		$record->write();
257
+	}
258
+
259
+	/**
260
+	 * Returns a JSON string of tags, for lazy loading.
261
+	 *
262
+	 * @param SS_HTTPRequest $request
263
+	 *
264
+	 * @return SS_HTTPResponse
265
+	 */
266
+	public function suggest(SS_HTTPRequest $request)
267
+	{
268
+		$responseBody = Convert::raw2json(
269
+			array('items' => array())
270
+		);
271
+
272
+		$response = new SS_HTTPResponse();
273
+		$response->addHeader('Content-Type', 'application/json');
274
+
275
+		if ($record = $this->getRecord()) {
276
+			$tags = array();
277
+			$term = $request->getVar('term');
278
+
279
+			if ($record->hasField($this->getName())) {
280
+				$tags = $this->getTags($term);
281
+			}
282
+
283
+			$responseBody = Convert::raw2json(
284
+				array('items' => $tags)
285
+			);
286
+		}
287
+
288
+		$response->setBody($responseBody);
289
+
290
+		return $response;
291
+	}
292
+
293
+	/**
294
+	 * Returns array of arrays representing tags.
295
+	 *
296
+	 * @param string $term
297
+	 *
298
+	 * @return array
299
+	 */
300
+	protected function getTags($term)
301
+	{
302
+		$record = $this->getRecord();
303
+
304
+		if (!$record) {
305
+			return array();
306
+		}
307
+
308
+		$fieldName = $this->getName();
309
+		$className = $record->getClassName();
310
+
311
+		$term = Convert::raw2sql($term);
312
+
313
+		$query = $className::get()
314
+			->filter($fieldName . ':PartialMatch:nocase', $term)
315
+			->limit($this->getLazyLoadItemLimit());
316
+
317
+		$items = array();
318
+
319
+		foreach ($query->column($fieldName) as $tags) {
320
+			$tags = explode(',', $tags);
321
+
322
+			foreach ($tags as $i => $tag) {
323
+				if (stripos($tag, $term) !== false && !in_array($tag, $items)) {
324
+					$items[] = array(
325
+						'id' => $tag,
326
+						'text' => $tag
327
+					);
328
+				}
329
+			}
330
+		}
331
+
332
+		return $items;
333
+	}
334
+
335
+	/**
336
+	 * DropdownField assumes value will be a scalar so we must
337
+	 * override validate. This only applies to Silverstripe 3.2+
338
+	 *
339
+	 * @param Validator $validator
340
+	 * @return bool
341
+	 */
342
+	public function validate($validator)
343
+	{
344
+		return true;
345
+	}
346 346
 }
Please login to merge, or discard this patch.
code/TagField.php 3 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -236,6 +236,7 @@  discard block
 block discarded – undo
236 236
 
237 237
     /**
238 238
      * {@inheritdoc}
239
+     * @param string[] $value
239 240
      */
240 241
     public function setValue($value, $source = null)
241 242
     {
@@ -434,7 +435,7 @@  discard block
 block discarded – undo
434 435
      * Render the readonly field as HTML.
435 436
      *
436 437
      * @param array $properties
437
-     * @return HTMLText
438
+     * @return string
438 439
      */
439 440
     public function Field($properties = array())
440 441
     {
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -156,13 +156,13 @@  discard block
 block discarded – undo
156 156
      */
157 157
     public function Field($properties = array())
158 158
     {
159
-        Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
160
-        Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
159
+        Requirements::css(TAG_FIELD_DIR.'/css/select2.min.css');
160
+        Requirements::css(TAG_FIELD_DIR.'/css/TagField.css');
161 161
 
162
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
163
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
164
-        Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
165
-        Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
162
+        Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
163
+        Requirements::javascript(THIRDPARTY_DIR.'/jquery-entwine/dist/jquery.entwine-dist.js');
164
+        Requirements::javascript(TAG_FIELD_DIR.'/js/select2.js');
165
+        Requirements::javascript(TAG_FIELD_DIR.'/js/TagField.js');
166 166
 
167 167
         $this->addExtraClass('ss-tag-field');
168 168
 
@@ -211,9 +211,9 @@  discard block
 block discarded – undo
211 211
         // Mark selected tags while still returning a full list of possible options
212 212
         $ids = array(); // empty fallback array for comparing
213 213
         $values = $this->Value();
214
-        if($values){
214
+        if ($values) {
215 215
             // @TODO conversion from array to DataList to array...(?)
216
-            if(is_array($values)) {
216
+            if (is_array($values)) {
217 217
                 $values = DataList::create($dataClass)->filter('ID', $values);
218 218
             }
219 219
             $ids = $values->column('ID');
@@ -263,7 +263,7 @@  discard block
 block discarded – undo
263 263
     {
264 264
         return array_merge(
265 265
             parent::getAttributes(),
266
-            array('name' => $this->getName() . '[]')
266
+            array('name' => $this->getName().'[]')
267 267
         );
268 268
     }
269 269
 
@@ -378,7 +378,7 @@  discard block
 block discarded – undo
378 378
         $titleField = $this->getTitleField();
379 379
 
380 380
         $query = $source
381
-            ->filter($titleField . ':PartialMatch:nocase', $term)
381
+            ->filter($titleField.':PartialMatch:nocase', $term)
382 382
             ->sort($titleField)
383 383
             ->limit($this->getLazyLoadItemLimit());
384 384
 
Please login to merge, or discard this patch.
Indentation   +434 added lines, -434 removed lines patch added patch discarded remove patch
@@ -8,418 +8,418 @@  discard block
 block discarded – undo
8 8
  */
9 9
 class TagField extends DropdownField
10 10
 {
11
-    /**
12
-     * @var array
13
-     */
14
-    public static $allowed_actions = array(
15
-        'suggest',
16
-    );
17
-
18
-    /**
19
-     * @var bool
20
-     */
21
-    protected $shouldLazyLoad = false;
22
-
23
-    /**
24
-     * @var int
25
-     */
26
-    protected $lazyLoadItemLimit = 10;
27
-
28
-    /**
29
-     * @var bool
30
-     */
31
-    protected $canCreate = true;
32
-
33
-    /**
34
-     * @var string
35
-     */
36
-    protected $titleField = 'Title';
37
-
38
-    /**
39
-     * @var bool
40
-     */
41
-    protected $isMultiple = true;
42
-
43
-    /**
44
-     * @param string $name
45
-     * @param string $title
46
-     * @param null|DataList $source
47
-     * @param null|DataList $value
48
-     */
49
-    public function __construct($name, $title = '', $source = null, $value = null)
50
-    {
51
-        parent::__construct($name, $title, $source, $value);
52
-    }
53
-
54
-    /**
55
-     * @return bool
56
-     */
57
-    public function getShouldLazyLoad()
58
-    {
59
-        return $this->shouldLazyLoad;
60
-    }
61
-
62
-    /**
63
-     * @param bool $shouldLazyLoad
64
-     *
65
-     * @return static
66
-     */
67
-    public function setShouldLazyLoad($shouldLazyLoad)
68
-    {
69
-        $this->shouldLazyLoad = $shouldLazyLoad;
70
-
71
-        return $this;
72
-    }
73
-
74
-    /**
75
-     * @return int
76
-     */
77
-    public function getLazyLoadItemLimit()
78
-    {
79
-        return $this->lazyLoadItemLimit;
80
-    }
81
-
82
-    /**
83
-     * @param int $lazyLoadItemLimit
84
-     *
85
-     * @return static
86
-     */
87
-    public function setLazyLoadItemLimit($lazyLoadItemLimit)
88
-    {
89
-        $this->lazyLoadItemLimit = $lazyLoadItemLimit;
90
-
91
-        return $this;
92
-    }
93
-
94
-    /**
95
-     * @return bool
96
-     */
97
-    public function getIsMultiple()
98
-    {
99
-        return $this->isMultiple;
100
-    }
101
-
102
-    /**
103
-     * @param bool $isMultiple
104
-     *
105
-     * @return static
106
-     */
107
-    public function setIsMultiple($isMultiple)
108
-    {
109
-        $this->isMultiple = $isMultiple;
110
-
111
-        return $this;
112
-    }
113
-
114
-    /**
115
-     * @return bool
116
-     */
117
-    public function getCanCreate()
118
-    {
119
-        return $this->canCreate;
120
-    }
121
-
122
-    /**
123
-     * @param bool $canCreate
124
-     *
125
-     * @return static
126
-     */
127
-    public function setCanCreate($canCreate)
128
-    {
129
-        $this->canCreate = $canCreate;
130
-
131
-        return $this;
132
-    }
133
-
134
-    /**
135
-     * @return string
136
-     */
137
-    public function getTitleField()
138
-    {
139
-        return $this->titleField;
140
-    }
141
-
142
-    /**
143
-     * @param string $titleField
144
-     *
145
-     * @return $this
146
-     */
147
-    public function setTitleField($titleField)
148
-    {
149
-        $this->titleField = $titleField;
150
-
151
-        return $this;
152
-    }
153
-
154
-    /**
155
-     * {@inheritdoc}
156
-     */
157
-    public function Field($properties = array())
158
-    {
159
-        Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
160
-        Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
161
-
162
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
163
-        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
164
-        Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
165
-        Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
166
-
167
-        $this->addExtraClass('ss-tag-field');
168
-
169
-        if ($this->getIsMultiple()) {
170
-            $this->setAttribute('multiple', 'multiple');
171
-        }
172
-
173
-        if ($this->shouldLazyLoad) {
174
-            $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
175
-        } else {
176
-            $properties = array_merge($properties, array(
177
-                'Options' => $this->getOptions()
178
-            ));
179
-        }
180
-
181
-        $this->setAttribute('data-can-create', (int) $this->getCanCreate());
182
-
183
-        return $this
184
-            ->customise($properties)
185
-            ->renderWith(array("templates/TagField"));
186
-    }
187
-
188
-    /**
189
-     * @return string
190
-     */
191
-    protected function getSuggestURL()
192
-    {
193
-        return Controller::join_links($this->Link(), 'suggest');
194
-    }
195
-
196
-    /**
197
-     * @return ArrayList
198
-     */
199
-    protected function getOptions()
200
-    {
201
-        $options = ArrayList::create();
202
-
203
-        $source = $this->getSource();
204
-
205
-        if (!$source) {
206
-            $source = new ArrayList();
207
-        }
208
-
209
-        $dataClass = $source->dataClass();
210
-
211
-        $values = $this->Value();
212
-
213
-        // Mark selected tags while still returning a full list of possible options
214
-        $ids = array(); // empty fallback array for comparing
215
-        $values = $this->Value();
216
-        if($values){
217
-            // @TODO conversion from array to DataList to array...(?)
218
-            if(is_array($values)) {
219
-                $values = DataList::create($dataClass)->filter('ID', $values);
220
-            }
221
-            $ids = $values->column('ID');
222
-        }
223
-
224
-        $titleField = $this->getTitleField();
225
-
226
-        foreach ($source as $object) {
227
-            $options->push(
228
-                ArrayData::create(array(
229
-                    'Title' => $object->$titleField,
230
-                    'Value' => $object->ID,
231
-                    'Selected' => in_array($object->ID, $ids),
232
-                ))
233
-            );
234
-        }
235
-
236
-        return $options;
237
-    }
238
-
239
-    /**
240
-     * {@inheritdoc}
241
-     */
242
-    public function setValue($value, $source = null)
243
-    {
244
-        if ($source instanceof DataObject) {
245
-            $name = $this->getName();
246
-
247
-            if ($source->hasMethod($name)) {
248
-                $value = $source->$name()->getIDList();
249
-            }
250
-        } elseif ($value instanceof SS_List) {
251
-            $value = $value->column('ID');
252
-        }
253
-
254
-        if (!is_array($value)) {
255
-            return parent::setValue($value);
256
-        }
257
-
258
-        return parent::setValue(array_filter($value));
259
-    }
260
-
261
-    /**
262
-     * {@inheritdoc}
263
-     */
264
-    public function getAttributes()
265
-    {
266
-        return array_merge(
267
-            parent::getAttributes(),
268
-            array('name' => $this->getName() . '[]')
269
-        );
270
-    }
271
-
272
-    /**
273
-     * {@inheritdoc}
274
-     */
275
-    public function saveInto(DataObjectInterface $record)
276
-    {
277
-        parent::saveInto($record);
278
-
279
-        $name = $this->getName();
280
-        $titleField = $this->getTitleField();
281
-
282
-        $source = $this->getSource();
283
-
284
-        $values = $this->Value();
285
-
286
-        if (!$values) {
287
-            $values = array();
288
-        }
289
-
290
-        if (empty($record) || empty($source) || empty($titleField)) {
291
-            return;
292
-        }
293
-
294
-        if (!$record->hasMethod($name)) {
295
-            throw new Exception(
296
-                sprintf("%s does not have a %s method", get_class($record), $name)
297
-            );
298
-        }
299
-
300
-        $relation = $record->$name();
301
-
302
-        foreach ($values as $i => $value) {
303
-            if (!is_numeric($value)) {
304
-                if (!$this->getCanCreate()) {
305
-                    unset($values[$i]);
306
-                    continue;
307
-                }
308
-
309
-                // Get or create record
310
-                $record = $this->getOrCreateTag($value);
311
-                $values[$i] = $record->ID;
312
-            }
313
-        }
314
-
315
-        if ($values instanceof SS_List) {
316
-            $values = iterator_to_array($values);
317
-        }
318
-
319
-        $relation->setByIDList(array_filter($values));
320
-    }
321
-
322
-    /**
323
-     * Get or create tag with the given value
324
-     *
325
-     * @param string $term
326
-     * @return DataObject
327
-     */
328
-    protected function getOrCreateTag($term)
329
-    {
330
-        // Check if existing record can be found
331
-        $source = $this->getSource();
332
-        $titleField = $this->getTitleField();
333
-        $record = $source
334
-            ->filter($titleField, $term)
335
-            ->first();
336
-        if ($record) {
337
-            return $record;
338
-        }
339
-
340
-        // Create new instance if not yet saved
341
-        $dataClass = $source->dataClass();
342
-        $record = Injector::inst()->create($dataClass);
343
-        $record->{$titleField} = $term;
344
-        $record->write();
345
-        return $record;
346
-    }
347
-
348
-    /**
349
-     * Returns a JSON string of tags, for lazy loading.
350
-     *
351
-     * @param SS_HTTPRequest $request
352
-     *
353
-     * @return SS_HTTPResponse
354
-     */
355
-    public function suggest(SS_HTTPRequest $request)
356
-    {
357
-        $tags = $this->getTags($request->getVar('term'));
358
-
359
-        $response = new SS_HTTPResponse();
360
-        $response->addHeader('Content-Type', 'application/json');
361
-        $response->setBody(json_encode(array('items' => $tags)));
362
-
363
-        return $response;
364
-    }
365
-
366
-    /**
367
-     * Returns array of arrays representing tags.
368
-     *
369
-     * @param string $term
370
-     *
371
-     * @return array
372
-     */
373
-    protected function getTags($term)
374
-    {
375
-        /**
376
-         * @var DataList $source
377
-         */
378
-        $source = $this->getSource();
379
-
380
-        $titleField = $this->getTitleField();
381
-
382
-        $query = $source
383
-            ->filter($titleField . ':PartialMatch:nocase', $term)
384
-            ->sort($titleField)
385
-            ->limit($this->getLazyLoadItemLimit());
386
-
387
-        // Map into a distinct list
388
-        $items = array();
389
-        $titleField = $this->getTitleField();
390
-        foreach ($query->map('ID', $titleField) as $id => $title) {
391
-            $items[$title] = array(
392
-                'id' => $id,
393
-                'text' => $title
394
-            );
395
-        }
396
-
397
-        return array_values($items);
398
-    }
399
-
400
-    /**
401
-     * DropdownField assumes value will be a scalar so we must
402
-     * override validate. This only applies to Silverstripe 3.2+
403
-     *
404
-     * @param Validator $validator
405
-     * @return bool
406
-     */
407
-    public function validate($validator)
408
-    {
409
-        return true;
410
-    }
411
-
412
-    /**
413
-     * Converts the field to a readonly variant.
414
-     *
415
-     * @return TagField_Readonly
416
-     */
417
-    public function performReadonlyTransformation()
418
-    {
419
-        $copy = $this->castedCopy('TagField_Readonly');
420
-        $copy->setSource($this->getSource());
421
-        return $copy;
422
-    }
11
+	/**
12
+	 * @var array
13
+	 */
14
+	public static $allowed_actions = array(
15
+		'suggest',
16
+	);
17
+
18
+	/**
19
+	 * @var bool
20
+	 */
21
+	protected $shouldLazyLoad = false;
22
+
23
+	/**
24
+	 * @var int
25
+	 */
26
+	protected $lazyLoadItemLimit = 10;
27
+
28
+	/**
29
+	 * @var bool
30
+	 */
31
+	protected $canCreate = true;
32
+
33
+	/**
34
+	 * @var string
35
+	 */
36
+	protected $titleField = 'Title';
37
+
38
+	/**
39
+	 * @var bool
40
+	 */
41
+	protected $isMultiple = true;
42
+
43
+	/**
44
+	 * @param string $name
45
+	 * @param string $title
46
+	 * @param null|DataList $source
47
+	 * @param null|DataList $value
48
+	 */
49
+	public function __construct($name, $title = '', $source = null, $value = null)
50
+	{
51
+		parent::__construct($name, $title, $source, $value);
52
+	}
53
+
54
+	/**
55
+	 * @return bool
56
+	 */
57
+	public function getShouldLazyLoad()
58
+	{
59
+		return $this->shouldLazyLoad;
60
+	}
61
+
62
+	/**
63
+	 * @param bool $shouldLazyLoad
64
+	 *
65
+	 * @return static
66
+	 */
67
+	public function setShouldLazyLoad($shouldLazyLoad)
68
+	{
69
+		$this->shouldLazyLoad = $shouldLazyLoad;
70
+
71
+		return $this;
72
+	}
73
+
74
+	/**
75
+	 * @return int
76
+	 */
77
+	public function getLazyLoadItemLimit()
78
+	{
79
+		return $this->lazyLoadItemLimit;
80
+	}
81
+
82
+	/**
83
+	 * @param int $lazyLoadItemLimit
84
+	 *
85
+	 * @return static
86
+	 */
87
+	public function setLazyLoadItemLimit($lazyLoadItemLimit)
88
+	{
89
+		$this->lazyLoadItemLimit = $lazyLoadItemLimit;
90
+
91
+		return $this;
92
+	}
93
+
94
+	/**
95
+	 * @return bool
96
+	 */
97
+	public function getIsMultiple()
98
+	{
99
+		return $this->isMultiple;
100
+	}
101
+
102
+	/**
103
+	 * @param bool $isMultiple
104
+	 *
105
+	 * @return static
106
+	 */
107
+	public function setIsMultiple($isMultiple)
108
+	{
109
+		$this->isMultiple = $isMultiple;
110
+
111
+		return $this;
112
+	}
113
+
114
+	/**
115
+	 * @return bool
116
+	 */
117
+	public function getCanCreate()
118
+	{
119
+		return $this->canCreate;
120
+	}
121
+
122
+	/**
123
+	 * @param bool $canCreate
124
+	 *
125
+	 * @return static
126
+	 */
127
+	public function setCanCreate($canCreate)
128
+	{
129
+		$this->canCreate = $canCreate;
130
+
131
+		return $this;
132
+	}
133
+
134
+	/**
135
+	 * @return string
136
+	 */
137
+	public function getTitleField()
138
+	{
139
+		return $this->titleField;
140
+	}
141
+
142
+	/**
143
+	 * @param string $titleField
144
+	 *
145
+	 * @return $this
146
+	 */
147
+	public function setTitleField($titleField)
148
+	{
149
+		$this->titleField = $titleField;
150
+
151
+		return $this;
152
+	}
153
+
154
+	/**
155
+	 * {@inheritdoc}
156
+	 */
157
+	public function Field($properties = array())
158
+	{
159
+		Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
160
+		Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
161
+
162
+		Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
163
+		Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
164
+		Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
165
+		Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
166
+
167
+		$this->addExtraClass('ss-tag-field');
168
+
169
+		if ($this->getIsMultiple()) {
170
+			$this->setAttribute('multiple', 'multiple');
171
+		}
172
+
173
+		if ($this->shouldLazyLoad) {
174
+			$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
175
+		} else {
176
+			$properties = array_merge($properties, array(
177
+				'Options' => $this->getOptions()
178
+			));
179
+		}
180
+
181
+		$this->setAttribute('data-can-create', (int) $this->getCanCreate());
182
+
183
+		return $this
184
+			->customise($properties)
185
+			->renderWith(array("templates/TagField"));
186
+	}
187
+
188
+	/**
189
+	 * @return string
190
+	 */
191
+	protected function getSuggestURL()
192
+	{
193
+		return Controller::join_links($this->Link(), 'suggest');
194
+	}
195
+
196
+	/**
197
+	 * @return ArrayList
198
+	 */
199
+	protected function getOptions()
200
+	{
201
+		$options = ArrayList::create();
202
+
203
+		$source = $this->getSource();
204
+
205
+		if (!$source) {
206
+			$source = new ArrayList();
207
+		}
208
+
209
+		$dataClass = $source->dataClass();
210
+
211
+		$values = $this->Value();
212
+
213
+		// Mark selected tags while still returning a full list of possible options
214
+		$ids = array(); // empty fallback array for comparing
215
+		$values = $this->Value();
216
+		if($values){
217
+			// @TODO conversion from array to DataList to array...(?)
218
+			if(is_array($values)) {
219
+				$values = DataList::create($dataClass)->filter('ID', $values);
220
+			}
221
+			$ids = $values->column('ID');
222
+		}
223
+
224
+		$titleField = $this->getTitleField();
225
+
226
+		foreach ($source as $object) {
227
+			$options->push(
228
+				ArrayData::create(array(
229
+					'Title' => $object->$titleField,
230
+					'Value' => $object->ID,
231
+					'Selected' => in_array($object->ID, $ids),
232
+				))
233
+			);
234
+		}
235
+
236
+		return $options;
237
+	}
238
+
239
+	/**
240
+	 * {@inheritdoc}
241
+	 */
242
+	public function setValue($value, $source = null)
243
+	{
244
+		if ($source instanceof DataObject) {
245
+			$name = $this->getName();
246
+
247
+			if ($source->hasMethod($name)) {
248
+				$value = $source->$name()->getIDList();
249
+			}
250
+		} elseif ($value instanceof SS_List) {
251
+			$value = $value->column('ID');
252
+		}
253
+
254
+		if (!is_array($value)) {
255
+			return parent::setValue($value);
256
+		}
257
+
258
+		return parent::setValue(array_filter($value));
259
+	}
260
+
261
+	/**
262
+	 * {@inheritdoc}
263
+	 */
264
+	public function getAttributes()
265
+	{
266
+		return array_merge(
267
+			parent::getAttributes(),
268
+			array('name' => $this->getName() . '[]')
269
+		);
270
+	}
271
+
272
+	/**
273
+	 * {@inheritdoc}
274
+	 */
275
+	public function saveInto(DataObjectInterface $record)
276
+	{
277
+		parent::saveInto($record);
278
+
279
+		$name = $this->getName();
280
+		$titleField = $this->getTitleField();
281
+
282
+		$source = $this->getSource();
283
+
284
+		$values = $this->Value();
285
+
286
+		if (!$values) {
287
+			$values = array();
288
+		}
289
+
290
+		if (empty($record) || empty($source) || empty($titleField)) {
291
+			return;
292
+		}
293
+
294
+		if (!$record->hasMethod($name)) {
295
+			throw new Exception(
296
+				sprintf("%s does not have a %s method", get_class($record), $name)
297
+			);
298
+		}
299
+
300
+		$relation = $record->$name();
301
+
302
+		foreach ($values as $i => $value) {
303
+			if (!is_numeric($value)) {
304
+				if (!$this->getCanCreate()) {
305
+					unset($values[$i]);
306
+					continue;
307
+				}
308
+
309
+				// Get or create record
310
+				$record = $this->getOrCreateTag($value);
311
+				$values[$i] = $record->ID;
312
+			}
313
+		}
314
+
315
+		if ($values instanceof SS_List) {
316
+			$values = iterator_to_array($values);
317
+		}
318
+
319
+		$relation->setByIDList(array_filter($values));
320
+	}
321
+
322
+	/**
323
+	 * Get or create tag with the given value
324
+	 *
325
+	 * @param string $term
326
+	 * @return DataObject
327
+	 */
328
+	protected function getOrCreateTag($term)
329
+	{
330
+		// Check if existing record can be found
331
+		$source = $this->getSource();
332
+		$titleField = $this->getTitleField();
333
+		$record = $source
334
+			->filter($titleField, $term)
335
+			->first();
336
+		if ($record) {
337
+			return $record;
338
+		}
339
+
340
+		// Create new instance if not yet saved
341
+		$dataClass = $source->dataClass();
342
+		$record = Injector::inst()->create($dataClass);
343
+		$record->{$titleField} = $term;
344
+		$record->write();
345
+		return $record;
346
+	}
347
+
348
+	/**
349
+	 * Returns a JSON string of tags, for lazy loading.
350
+	 *
351
+	 * @param SS_HTTPRequest $request
352
+	 *
353
+	 * @return SS_HTTPResponse
354
+	 */
355
+	public function suggest(SS_HTTPRequest $request)
356
+	{
357
+		$tags = $this->getTags($request->getVar('term'));
358
+
359
+		$response = new SS_HTTPResponse();
360
+		$response->addHeader('Content-Type', 'application/json');
361
+		$response->setBody(json_encode(array('items' => $tags)));
362
+
363
+		return $response;
364
+	}
365
+
366
+	/**
367
+	 * Returns array of arrays representing tags.
368
+	 *
369
+	 * @param string $term
370
+	 *
371
+	 * @return array
372
+	 */
373
+	protected function getTags($term)
374
+	{
375
+		/**
376
+		 * @var DataList $source
377
+		 */
378
+		$source = $this->getSource();
379
+
380
+		$titleField = $this->getTitleField();
381
+
382
+		$query = $source
383
+			->filter($titleField . ':PartialMatch:nocase', $term)
384
+			->sort($titleField)
385
+			->limit($this->getLazyLoadItemLimit());
386
+
387
+		// Map into a distinct list
388
+		$items = array();
389
+		$titleField = $this->getTitleField();
390
+		foreach ($query->map('ID', $titleField) as $id => $title) {
391
+			$items[$title] = array(
392
+				'id' => $id,
393
+				'text' => $title
394
+			);
395
+		}
396
+
397
+		return array_values($items);
398
+	}
399
+
400
+	/**
401
+	 * DropdownField assumes value will be a scalar so we must
402
+	 * override validate. This only applies to Silverstripe 3.2+
403
+	 *
404
+	 * @param Validator $validator
405
+	 * @return bool
406
+	 */
407
+	public function validate($validator)
408
+	{
409
+		return true;
410
+	}
411
+
412
+	/**
413
+	 * Converts the field to a readonly variant.
414
+	 *
415
+	 * @return TagField_Readonly
416
+	 */
417
+	public function performReadonlyTransformation()
418
+	{
419
+		$copy = $this->castedCopy('TagField_Readonly');
420
+		$copy->setSource($this->getSource());
421
+		return $copy;
422
+	}
423 423
 }
424 424
 
425 425
 /**
@@ -430,26 +430,26 @@  discard block
 block discarded – undo
430 430
  */
431 431
 class TagField_Readonly extends TagField
432 432
 {
433
-    protected $readonly = true;
434
-
435
-    /**
436
-     * Render the readonly field as HTML.
437
-     *
438
-     * @param array $properties
439
-     * @return HTMLText
440
-     */
441
-    public function Field($properties = array())
442
-    {
443
-        $options = array();
444
-
445
-        foreach ($this->getOptions()->filter('Selected', true) as $option) {
446
-            $options[] = $option->Title;
447
-        }
448
-
449
-        $field = ReadonlyField::create($this->name.'_Readonly', $this->title);
450
-
451
-        $field->setForm($this->form);
452
-        $field->setValue(implode(', ', $options));
453
-        return $field->Field();
454
-    }
433
+	protected $readonly = true;
434
+
435
+	/**
436
+	 * Render the readonly field as HTML.
437
+	 *
438
+	 * @param array $properties
439
+	 * @return HTMLText
440
+	 */
441
+	public function Field($properties = array())
442
+	{
443
+		$options = array();
444
+
445
+		foreach ($this->getOptions()->filter('Selected', true) as $option) {
446
+			$options[] = $option->Title;
447
+		}
448
+
449
+		$field = ReadonlyField::create($this->name.'_Readonly', $this->title);
450
+
451
+		$field->setForm($this->form);
452
+		$field->setValue(implode(', ', $options));
453
+		return $field->Field();
454
+	}
455 455
 }
Please login to merge, or discard this patch.