1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CMS\Controllers; |
4
|
|
|
|
5
|
|
|
use NestedController; |
6
|
|
|
use SilverStripe\ORM\DataModel; |
7
|
|
|
use SilverStripe\ORM\DB; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
use Controller; |
10
|
|
|
|
11
|
|
|
use ClassInfo; |
12
|
|
|
use Injector; |
13
|
|
|
use SS_HTTPRequest; |
14
|
|
|
use Director; |
15
|
|
|
use RequestHandler; |
16
|
|
|
use SS_HTTPResponse; |
17
|
|
|
use SS_HTTPResponse_Exception; |
18
|
|
|
use Exception; |
19
|
|
|
use Translatable; |
20
|
|
|
use Debug; |
21
|
|
|
use Deprecation; |
22
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController} |
27
|
|
|
* pair, which are then used to handle the request. |
28
|
|
|
* |
29
|
|
|
* @package cms |
30
|
|
|
* @subpackage control |
31
|
|
|
*/ |
32
|
|
|
class ModelAsController extends Controller implements NestedController { |
33
|
|
|
|
34
|
|
|
private static $extensions = array('SilverStripe\\CMS\\Controllers\\OldPageRedirector'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and |
38
|
|
|
* return it. |
39
|
|
|
* |
40
|
|
|
* @param SiteTree $sitetree |
41
|
|
|
* @param string $action |
42
|
|
|
* @return ContentController |
43
|
|
|
*/ |
44
|
|
|
public static function controller_for(SiteTree $sitetree, $action = null) { |
45
|
|
|
if ($sitetree->class == 'SilverStripe\\CMS\\Model\\SiteTree') { |
46
|
|
|
$controller = "SilverStripe\\CMS\\Controllers\\ContentController"; |
47
|
|
|
} else { |
48
|
|
|
$ancestry = ClassInfo::ancestry($sitetree->class); |
49
|
|
|
while ($class = array_pop($ancestry)) { |
50
|
|
|
if (class_exists($class . "_Controller")) break; |
51
|
|
|
} |
52
|
|
|
$controller = ($class !== null) ? "{$class}_Controller" : "SilverStripe\\CMS\\Controllers\\ContentController"; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if($action && class_exists($controller . '_' . ucfirst($action))) { |
|
|
|
|
56
|
|
|
$controller = $controller . '_' . ucfirst($action); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function init() { |
63
|
|
|
singleton('SilverStripe\\CMS\\Model\\SiteTree')->extend('modelascontrollerInit', $this); |
64
|
|
|
parent::init(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
protected function beforeHandleRequest(SS_HTTPRequest $request, DataModel $model) { |
|
|
|
|
68
|
|
|
parent::beforeHandleRequest($request, $model); |
69
|
|
|
// If the database has not yet been created, redirect to the build page. |
70
|
|
|
/** @skipUpgrade */ |
71
|
|
|
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { |
72
|
|
|
$this->getResponse()->redirect(Controller::join_links( |
73
|
|
|
Director::absoluteBaseURL(), |
74
|
|
|
'dev/build', |
75
|
|
|
'?' . http_build_query(array( |
76
|
|
|
'returnURL' => isset($_GET['url']) ? $_GET['url'] : null, |
77
|
|
|
)) |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @uses ModelAsController::getNestedController() |
84
|
|
|
* @param SS_HTTPRequest $request |
85
|
|
|
* @param DataModel $model |
86
|
|
|
* @return SS_HTTPResponse |
87
|
|
|
*/ |
88
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
89
|
|
|
$this->beforeHandleRequest($request, $model); |
90
|
|
|
|
91
|
|
|
// If we had a redirection or something, halt processing. |
92
|
|
|
if($this->getResponse()->isFinished()) { |
93
|
|
|
$this->popCurrent(); |
94
|
|
|
return $this->getResponse(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// If the database has not yet been created, redirect to the build page. |
98
|
|
|
/** @skipUpgrade */ |
99
|
|
View Code Duplication |
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { |
|
|
|
|
100
|
|
|
$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)); |
101
|
|
|
$this->popCurrent(); |
102
|
|
|
|
103
|
|
|
return $this->getResponse(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$result = $this->getNestedController(); |
108
|
|
|
|
109
|
|
|
if($result instanceof RequestHandler) { |
110
|
|
|
$result = $result->handleRequest($this->getRequest(), $model); |
111
|
|
|
} else if(!($result instanceof SS_HTTPResponse)) { |
112
|
|
|
user_error("ModelAsController::getNestedController() returned bad object type '" . |
113
|
|
|
get_class($result)."'", E_USER_WARNING); |
114
|
|
|
} |
115
|
|
|
} catch(SS_HTTPResponse_Exception $responseException) { |
116
|
|
|
$result = $responseException->getResponse(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->popCurrent(); |
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return ContentController |
125
|
|
|
* @throws Exception If URLSegment not passed in as a request parameter. |
126
|
|
|
*/ |
127
|
|
|
public function getNestedController() { |
128
|
|
|
$request = $this->getRequest(); |
129
|
|
|
|
130
|
|
|
if(!$URLSegment = $request->param('URLSegment')) { |
131
|
|
|
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Find page by link, regardless of current locale settings |
135
|
|
|
if(class_exists('Translatable')) Translatable::disable_locale_filter(); |
136
|
|
|
|
137
|
|
|
// Select child page |
138
|
|
|
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); |
139
|
|
|
if(SiteTree::config()->nested_urls) { |
140
|
|
|
$conditions[] = array('"SiteTree"."ParentID"' => 0); |
141
|
|
|
} |
142
|
|
|
/** @var SiteTree $sitetree */ |
143
|
|
|
$sitetree = DataObject::get_one('SilverStripe\\CMS\\Model\\SiteTree', $conditions); |
144
|
|
|
|
145
|
|
|
// Check translation module |
146
|
|
|
// @todo Refactor out module specific code |
147
|
|
|
if(class_exists('Translatable')) Translatable::enable_locale_filter(); |
148
|
|
|
|
149
|
|
|
if(!$sitetree) { |
150
|
|
|
$this->httpError(404, 'The requested page could not be found.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Enforce current locale setting to the loaded SiteTree object |
154
|
|
|
if(class_exists('Translatable') && $sitetree->Locale) Translatable::set_current_locale($sitetree->Locale); |
|
|
|
|
155
|
|
|
|
156
|
|
|
if(isset($_REQUEST['debug'])) { |
157
|
|
|
Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return self::controller_for($sitetree, $this->getRequest()->param('Action')); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @deprecated 4.0 Use OldPageRedirector::find_old_page instead |
165
|
|
|
* |
166
|
|
|
* @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. |
167
|
|
|
* @param int $parent The ID of the parent of the page the URLSegment belongs to. |
168
|
|
|
* @param bool $ignoreNestedURLs |
169
|
|
|
* @return SiteTree |
170
|
|
|
*/ |
171
|
|
|
static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { |
172
|
|
|
Deprecation::notice('4.0', 'Use OldPageRedirector::find_old_page instead'); |
173
|
|
|
if ($parent) { |
|
|
|
|
174
|
|
|
$parent = SiteTree::get()->byID($parent); |
175
|
|
|
} |
176
|
|
|
$url = OldPageRedirector::find_old_page(array($URLSegment), $parent); |
|
|
|
|
177
|
|
|
return SiteTree::get_by_link($url); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|