1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Forms\ReadonlyField; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBText; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBBoolean; |
11
|
|
|
use SilverStripe\ORM\DataObjectSchema; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A single import rule that forms part of an ImportSchema |
15
|
|
|
*/ |
16
|
|
|
class StaticSiteContentSourceImportRule extends DataObject |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private static $db = [ |
|
|
|
|
23
|
|
|
"FieldName" => DBVarchar::class, |
24
|
|
|
"CSSSelector" => DBVarchar::class, |
25
|
|
|
"ExcludeCSSSelector" => DBText::class, |
26
|
|
|
"Attribute" => DBVarchar::class, |
27
|
|
|
"PlainText" => DBBoolean::class, |
28
|
|
|
"OuterHTML" => DBBoolean::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private static $table_name = 'StaticSiteContentSourceImportRule'; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $summary_fields = [ |
|
|
|
|
41
|
|
|
"FieldName", |
42
|
|
|
"PlainText", |
43
|
|
|
"OuterHTML", |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private static $field_labels = [ |
|
|
|
|
51
|
|
|
"FieldName" => "Target Field Name", |
52
|
|
|
"CSSSelector" => "CSS Selector", |
53
|
|
|
'ExcludeCSSSelector' => 'Excluded CSS Selector(s)', |
54
|
|
|
"Attribute" => "Element Attribute", |
55
|
|
|
"PlainText" => "Plain Text", |
56
|
|
|
"OuterHTML" => "Outer HTML", |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private static $has_one = [ |
|
|
|
|
64
|
|
|
"Schema" => StaticSiteContentSourceImportSchema::class, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getTitle() |
72
|
|
|
{ |
73
|
|
|
return $this->FieldName ?: $this->ID; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getAbsoluteURL() |
81
|
|
|
{ |
82
|
|
|
return $this->URLSegment ?: $this->Filename; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
* @return FieldList |
|
|
|
|
88
|
|
|
*/ |
89
|
|
|
public function getCMSFields() |
90
|
|
|
{ |
91
|
|
|
$fields = parent::getCMSFields(); |
92
|
|
|
$dataType = $this->Schema()->DataType; |
|
|
|
|
93
|
|
|
|
94
|
|
|
if ($dataType) { |
95
|
|
|
$fieldList = singleton(DataObjectSchema::class) |
96
|
|
|
->fieldSpecs($dataType, DataObjectSchema::DB_ONLY); |
97
|
|
|
$fieldList = array_combine(array_keys($fieldList), array_keys($fieldList)); |
98
|
|
|
foreach ( |
99
|
|
|
array_merge( |
100
|
|
|
array_keys(DataObject::config()->get('fixed_fields')), |
101
|
|
|
array_filter($fieldList, function ($item) { |
102
|
|
|
return preg_match('#(^Static|ID$)#i', $item); |
103
|
|
|
}), |
104
|
|
|
['Version'] |
105
|
|
|
) as $exclusion |
106
|
|
|
) { |
107
|
|
|
unset($fieldList[$exclusion]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
natsort($fieldList); |
111
|
|
|
|
112
|
|
|
$fieldNameField = DropdownField::create("FieldName", 'Target Field', $fieldList) |
113
|
|
|
->setEmptyString("(choose)") |
114
|
|
|
->setDescription('Remote content matched by the CSS selector below is written to this field.'); |
115
|
|
|
$fields->insertBefore($fieldNameField, 'CSSSelector'); |
|
|
|
|
116
|
|
|
$fields->dataFieldByName('CSSSelector') |
117
|
|
|
->setDescription('A valid CSS selector whose content is written to the "Target Field" above.') |
118
|
|
|
->setAttribute('style', 'width: 300px;'); |
119
|
|
|
$fields->dataFieldByName('ExcludeCSSSelector') |
120
|
|
|
->setDescription('A list of valid CSS selectors whose content' |
121
|
|
|
. ' should be ignored. This is useful for fine-tuning what is returned in an import.' |
122
|
|
|
. ' Separate multiple exclusions with a newline.'); |
123
|
|
|
$fields->dataFieldByName('OuterHTML') |
124
|
|
|
->setDescription('Use outer HTML (Fetches parent element and content and that of its children)'); |
125
|
|
|
$fields->dataFieldByName('PlainText') |
126
|
|
|
->setDescription('Convert to plain text (Removes markup)'); |
127
|
|
|
} else { |
128
|
|
|
$fields->replaceField('FieldName', $fieldName = ReadonlyField::create("FieldName", "Field Name")); |
129
|
|
|
$fieldName->setDescription('Save this rule before being able to add a field name'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$fields->dataFieldByName('Attribute')->setDescription('Add the element attribute where the desired text' |
133
|
|
|
. ' can be found (such as "alt" or "title") if not found as the selected element\'s text itself.'); |
134
|
|
|
|
135
|
|
|
return $fields; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|