1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Model; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\Forms\DropdownField; |
8
|
|
|
use SilverStripe\Forms\FormField; |
9
|
|
|
use SilverStripe\Forms\HiddenField; |
10
|
|
|
use SilverStripe\Forms\ListboxField; |
11
|
|
|
use SilverStripe\Forms\TextField; |
12
|
|
|
use SilverStripe\i18n\i18n; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\ORM\ValidationResult; |
15
|
|
|
|
16
|
|
|
class ResourceFilter extends DataObject |
17
|
|
|
{ |
18
|
|
|
private static $filter_types = [ |
|
|
|
|
19
|
|
|
TextField::class => 'Text', |
20
|
|
|
DropdownField::class => 'Select one from list', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
private static $table_name = 'CKANFilter'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $db = [ |
|
|
|
|
26
|
|
|
'Name' => 'Varchar', |
27
|
|
|
'Type' => 'Varchar', |
28
|
|
|
'AllFields' => 'Boolean', |
29
|
|
|
'TypeOptions' => 'Text', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
private static $has_one = [ |
|
|
|
|
33
|
|
|
'FilterFor' => Resource::class, |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function getCMSFields() |
37
|
|
|
{ |
38
|
|
|
$fields = parent::getCMSFields(); |
39
|
|
|
$typeTitle = $fields->dataFieldByName('Type')->Title(); |
40
|
|
|
$fields->replaceField('Type', DropdownField::create( |
41
|
|
|
'Type', |
42
|
|
|
$typeTitle, |
43
|
|
|
$this->config()->get('filter_types') |
44
|
|
|
)); |
45
|
|
|
$fields->replaceField('TypeOptions', HiddenField::create('TypeOptions')); |
46
|
|
|
$fields->push( |
47
|
|
|
ListboxField::create('Fields', 'Columns', $this->FilterFor()->Fields()->map('Name', 'ReadableName')) |
|
|
|
|
48
|
|
|
); |
49
|
|
|
$fields->removeByName('FilterForID'); |
50
|
|
|
return $fields; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function forTemplate() |
54
|
|
|
{ |
55
|
|
|
$options = json_decode($this->TypeOptions, true); |
|
|
|
|
56
|
|
|
$field = Injector::inst()->createWithArgs($this->Type, $options); |
|
|
|
|
57
|
|
|
if ($field instanceof FormField) { |
58
|
|
|
throw new InvalidArgumentException("$this->Type is not a FormField"); |
59
|
|
|
} |
60
|
|
|
return $field; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|