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