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