1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\GraphQL\Controller; |
4
|
|
|
use SilverStripe\ORM\ArrayList; |
5
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A Page type that lets you pull content through from any arbitrary |
9
|
|
|
* external content source. At some point soon, this might change to behave |
10
|
|
|
* more like a virtual page... Might be necessary in the future |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
* @author Marcus Nyeholt <[email protected]> |
14
|
|
|
* @license http://silverstripe.org/bsd-license/ |
15
|
|
|
*/ |
16
|
|
|
class ExternalContentPage extends Page |
17
|
|
|
{ |
18
|
|
|
private static $db = array( |
19
|
|
|
'ExternalContentRoot' => DBVarchar::class, |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
private static $table_name = 'ExternalContentPage'; |
23
|
|
|
|
24
|
|
|
private static $has_one = array( |
25
|
|
|
// 'ExternalContent' => 'ExternalContentSource' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
public function getCMSFields() |
29
|
|
|
{ |
30
|
|
|
$fields = parent::getCMSFields(); |
31
|
|
|
$fields->removeFieldFromTab('Root.Main', 'Content'); |
32
|
|
|
$fields->addFieldToTab('Root.Main', ExternalTreeDropdownField::create('ExternalContentRoot', _t('ExternalContentPage.CONTENT_SOURCE', 'External Content Source'), 'ExternalContentSource')); |
33
|
|
|
|
34
|
|
|
return $fields; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* When linking to this external content page, return a URL that'll let |
39
|
|
|
* you view the external content item directly |
40
|
|
|
* |
41
|
|
|
* (non-PHPdoc) |
42
|
|
|
* @see sapphire/core/model/SiteTree#Link($action) |
43
|
|
|
*/ |
44
|
|
|
public function RelativeLink($action = null) |
45
|
|
|
{ |
46
|
|
|
$remoteObject = $this->ContentItem(); |
47
|
|
|
if (!is_string($action)) { |
48
|
|
|
$action = null; |
49
|
|
|
} |
50
|
|
|
if ($remoteObject) { |
51
|
|
|
return $this->LinkFor($remoteObject, $action ? $action : 'view'); |
52
|
|
|
} |
53
|
|
|
return parent::RelativeLink($action); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function LinkFor($remoteObject, $action = null) |
57
|
|
|
{ |
58
|
|
|
$link = parent::RelativeLink(); |
59
|
|
|
$id = $remoteObject->ID; |
60
|
|
|
// otherwise, we're after $this link (view) plus id |
61
|
|
|
return Controller::join_links($link, $action, $id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Cache the requested item so that repeated calls |
66
|
|
|
* to this method doesn't make a bunch of extra requests |
67
|
|
|
* |
68
|
|
|
* @var ExternalContentItem |
69
|
|
|
*/ |
70
|
|
|
private $requestedItem; |
71
|
|
|
|
72
|
|
|
public function setRequestedItem($item) |
73
|
|
|
{ |
74
|
|
|
$this->requestedItem = $item; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the external content item |
79
|
|
|
* |
80
|
|
|
* @return DataObject |
81
|
|
|
*/ |
82
|
|
|
public function ContentItem($what='k') |
83
|
|
|
{ |
84
|
|
|
if ($this->requestedItem) { |
85
|
|
|
return $this->requestedItem; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!$this->ExternalContentRoot) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// See if an item was requested in the url directly |
93
|
|
|
$id = isset($_REQUEST['item']) ? $_REQUEST['item'] : null; |
94
|
|
|
|
95
|
|
|
if (!$id) { |
96
|
|
|
$id = $this->ExternalContentRoot; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$remoteObject = ExternalContent::getDataObjectFor($id); |
100
|
|
|
|
101
|
|
|
if ($remoteObject) { |
102
|
|
|
$this->requestedItem = $remoteObject; |
103
|
|
|
return $this->requestedItem; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the children of this external content item as my children |
111
|
|
|
* |
112
|
|
|
* @return ArrayList |
113
|
|
|
*/ |
114
|
|
|
public function Children() |
115
|
|
|
{ |
116
|
|
|
$item = $this->ContentItem(); |
117
|
|
|
return $item ? $item->stageChildren() : ArrayList::create(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
use SilverStripe\View\SSViewer; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Contains methods for interacting with external content on the frontend |
125
|
|
|
* |
126
|
|
|
* @author Marcus Nyeholt <[email protected]> |
127
|
|
|
* |
128
|
|
|
*/ |
129
|
|
|
class ExternalContentPage_Controller extends PageController |
130
|
|
|
{ |
131
|
|
|
public const URL_STUB = 'extcon'; |
132
|
|
|
|
133
|
|
|
private static $allowed_actions = array( |
134
|
|
|
'view', |
135
|
|
|
'download', |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
public function init() |
139
|
|
|
{ |
140
|
|
|
parent::init(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Display an item. |
145
|
|
|
* |
146
|
|
|
* @param HTTPRequest $request |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function view($request) |
150
|
|
|
{ |
151
|
|
|
$object = null; |
152
|
|
|
if ($id = $request->param('ID')) { |
153
|
|
|
$object = ExternalContent::getDataObjectFor($id); |
154
|
|
|
if ($object instanceof ExternalContentSource) { |
155
|
|
|
$object = $object->getRoot(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($object && ($object instanceof ExternalContentItem || $object instanceof ExternalContentSource)) { |
159
|
|
|
$this->data()->setRequestedItem($object); |
160
|
|
|
$type = $object instanceof ExternalContentItem ? $object->getType() : 'source'; |
161
|
|
|
$template = 'ExternalContent_' . get_class($object) . '_' . $type; |
162
|
|
|
|
163
|
|
|
$viewer = SSViewer::create(array($template, 'ExternalContent_' . get_class($object), 'ExternalContent', 'Page')); |
164
|
|
|
$action = 'view'; |
165
|
|
|
$this->extend('updateViewer', $action, $viewer); |
166
|
|
|
|
167
|
|
|
return $this->customise($object)->renderWith($viewer); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
echo "Template not found for " . ($object ? get_class($object) . ' #' . $object->ID : ''); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Called to download this content item and stream it directly to the browser |
176
|
|
|
* |
177
|
|
|
* @param HTTP_Request $request |
178
|
|
|
*/ |
179
|
|
|
public function download($request) |
180
|
|
|
{ |
181
|
|
|
if ($request->param('ID')) { |
182
|
|
|
$object = ExternalContent::getDataObjectFor($request->param('ID')); |
183
|
|
|
if ($object && $object instanceof ExternalContentItem) { |
184
|
|
|
$object->streamContent(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|