|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Model; |
|
4
|
|
|
|
|
5
|
|
|
use ExternalContentItem; |
|
6
|
|
|
use ExternalContentTransformer; |
|
7
|
|
|
use PhpTek\Exodus\Transform\StaticSiteFileTransformer; |
|
8
|
|
|
use PhpTek\Exodus\Transform\StaticSitePageTransformer; |
|
9
|
|
|
use PhpTek\Exodus\Tool\StaticSiteMimeProcessor; |
|
10
|
|
|
use PhpTek\Exodus\Tool\StaticSiteUtils; |
|
11
|
|
|
use SilverStripe\Core\Convert; |
|
12
|
|
|
use SilverStripe\ORM\ArrayList; |
|
13
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
14
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy; |
|
15
|
|
|
use SilverStripe\View\Requirements; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Deals-to transforming imported SiteTree and File objects |
|
19
|
|
|
* |
|
20
|
|
|
* @package phptek/silverstripe-exodus |
|
21
|
|
|
* @author Sam Minee <[email protected]> |
|
22
|
|
|
* @author Russell Michell <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class StaticSiteContentItem extends ExternalContentItem |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $table_name = 'StaticSiteContentItem'; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Default Content type, either 'sitetree', 'file' (or false to disable the default) |
|
33
|
|
|
* |
|
34
|
|
|
* @var mixed (string | boolean) |
|
35
|
|
|
*/ |
|
36
|
|
|
private $default_content_type = 'sitetree'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $extensions = [ |
|
|
|
|
|
|
42
|
|
|
'hierarchy' => Hierarchy::class, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function init() |
|
49
|
|
|
{ |
|
50
|
|
|
$url = $this->externalId; |
|
51
|
|
|
$processedURL = $this->source->urlList()->processedURL($url); |
|
52
|
|
|
$parentURL = $this->source->urlList()->parentProcessedURL($processedURL); |
|
53
|
|
|
$subURL = substr( |
|
54
|
|
|
$processedURL ? $processedURL['url'] : '', |
|
55
|
|
|
strlen($parentURL['url']) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
if ($subURL != '/') { |
|
59
|
|
|
$subURL = trim($subURL, '/'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Just default values |
|
63
|
|
|
$this->Name = $subURL; |
|
|
|
|
|
|
64
|
|
|
$this->Title = $this->Name; |
|
65
|
|
|
$this->AbsoluteURL = rtrim($this->source->BaseUrl, '/') . $this->externalId; |
|
|
|
|
|
|
66
|
|
|
$this->ProcessedURL = $processedURL['url'] ?? ''; |
|
|
|
|
|
|
67
|
|
|
$this->ProcessedMIME = $processedURL['mime'] ?? ''; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* @param boolean $showAll |
|
73
|
|
|
* @return ArrayList |
|
74
|
|
|
*/ |
|
75
|
|
|
public function stageChildren($showAll = false) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$this->source->urlList()->hasCrawled()) { |
|
78
|
|
|
return ArrayList::create(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$childrenURLs = $this->source->urlList()->getChildren($this->externalId); |
|
82
|
|
|
$children = ArrayList::create(); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($childrenURLs as $child) { |
|
85
|
|
|
$children->push($this->source->getObject($child)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $children; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @return int |
|
94
|
|
|
*/ |
|
95
|
|
|
public function numChildren(): int |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->source->urlList()->hasCrawled()) { |
|
98
|
|
|
return 0; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return count($this->source->urlList()->getChildren($this->externalId)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns the correct lowercase Silverstripe base class-name based on the current URL's Mime-Type |
|
106
|
|
|
* and directs the module to use the correct StaticSiteDataTypeTransformer subclass. |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getType(): string |
|
111
|
|
|
{ |
|
112
|
|
|
$mimeTypeProcessor = singleton(StaticSiteMimeProcessor::class); |
|
113
|
|
|
|
|
114
|
|
|
if ($mimeTypeProcessor->isOfFileOrImage($this->ProcessedMIME)) { |
|
115
|
|
|
return 'file'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($mimeTypeProcessor->isOfHtml($this->ProcessedMIME)) { |
|
119
|
|
|
return 'sitetree'; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Log everything that doesn't fit: |
|
123
|
|
|
singleton(StaticSiteUtils::class)->log('UNKNOWN Schema not configured for Mime & URL:', $this->AbsoluteURL, $this->ProcessedMIME); |
|
124
|
|
|
|
|
125
|
|
|
return $this->default_content_type; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns the correct content-object transformation class. |
|
130
|
|
|
* |
|
131
|
|
|
* @return ExternalContentTransformer |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getTransformer(): ExternalContentTransformer |
|
134
|
|
|
{ |
|
135
|
|
|
$type = $this->getType(); |
|
136
|
|
|
|
|
137
|
|
|
if ($type == 'file') { |
|
138
|
|
|
return StaticSiteFileTransformer::create(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($type == 'sitetree') { |
|
142
|
|
|
return StaticSitePageTransformer::create(); |
|
143
|
|
|
} |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return FieldList $fields |
|
|
|
|
|
|
148
|
|
|
*/ |
|
149
|
|
|
public function getCMSFields() |
|
150
|
|
|
{ |
|
151
|
|
|
$fields = parent::getCMSFields(); |
|
152
|
|
|
|
|
153
|
|
|
// Add the preview fields here, including rules used |
|
154
|
|
|
$urlField = ReadonlyField::create( |
|
155
|
|
|
"PreviewSourceURL", |
|
156
|
|
|
"Imported from", |
|
157
|
|
|
"<a href=\"$this->AbsoluteURL\">" . Convert::raw2xml($this->AbsoluteURL) . "</a>" |
|
|
|
|
|
|
158
|
|
|
); |
|
159
|
|
|
$urlField->dontEscape = true; |
|
160
|
|
|
$fields->addFieldToTab("Root.Preview", $urlField); |
|
161
|
|
|
|
|
162
|
|
|
$dataType = $this->getType(); |
|
163
|
|
|
$content = ''; |
|
164
|
|
|
|
|
165
|
|
|
if ($t = $this->getTransformer()) { |
|
166
|
|
|
$content = $t->getContentFieldsAndSelectors($this, $dataType); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (count($content) === 0) { |
|
170
|
|
|
return $fields; |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
foreach ($content as $k => $v) { |
|
174
|
|
|
$readonlyField = ReadonlyField::create("Preview$k", "$k<br>\n<em>" . $v['selector'] . "</em>", $v['content']); |
|
175
|
|
|
$readonlyField->addExtraClass('readonly-click-toggle'); |
|
176
|
|
|
$fields->addFieldToTab("Root.Preview", $readonlyField); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
Requirements::javascript('phptek/silverstripe-exodus:js/StaticSiteContentItem.js'); |
|
180
|
|
|
|
|
181
|
|
|
return $fields; |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Performs some checks on $item. If it is of the wrong type, returns false |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $type e.g. 'sitetree' |
|
188
|
|
|
* @return boolean |
|
189
|
|
|
*/ |
|
190
|
|
|
public function checkIsType(string $type): bool |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->getType() === strtolower($type); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|