2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base controller class.
|
2012-03-24 04:38:57 +01:00
|
|
|
* Controllers are the cornerstone of all site functionality in SilverStripe. The {@link Director}
|
2007-07-19 12:40:28 +02:00
|
|
|
* selects a controller to pass control to, and then calls {@link run()}. This method will execute
|
|
|
|
* the appropriate action - either by calling the action method, or displaying the action's template.
|
2007-09-14 20:02:03 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* See {@link getTemplate()} for information on how the template is chosen.
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-02-11 03:08:39 +01:00
|
|
|
class Controller extends RequestHandler implements TemplateGlobalProvider {
|
2008-10-06 00:35:14 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2008-10-06 00:35:14 +02:00
|
|
|
* @var array $urlParams An array of arguments extracted from the URL
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $urlParams;
|
2008-10-06 00:35:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $requestParams Contains all GET and POST parameters
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* passed to the current {@link SS_HTTPRequest}.
|
|
|
|
* @uses SS_HTTPRequest->requestVars()
|
2008-10-06 00:35:14 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $requestParams;
|
2008-10-06 00:35:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string $action The URL part matched on the current controller as
|
|
|
|
* determined by the "$Action" part of the {@link $url_handlers} definition.
|
|
|
|
* Should correlate to a public method on this controller.
|
|
|
|
* Used in {@link render()} and {@link getViewer()} to determine
|
|
|
|
* action-specific templates.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $action;
|
2007-08-17 07:45:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@link Session} object for this controller
|
|
|
|
*/
|
|
|
|
protected $session;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
/**
|
|
|
|
* Stack of current controllers.
|
|
|
|
* Controller::$controller_stack[0] is the current controller.
|
|
|
|
*/
|
|
|
|
protected static $controller_stack = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
protected $basicAuthEnabled = true;
|
2007-08-17 05:09:46 +02:00
|
|
|
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @var SS_HTTPResponse $response The response object that the controller returns.
|
2008-10-05 21:45:38 +02:00
|
|
|
* Set in {@link handleRequest()}.
|
2007-08-17 05:09:46 +02:00
|
|
|
*/
|
|
|
|
protected $response;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
|
|
|
* Default URL handlers - (Action)/(ID)/(OtherID)
|
|
|
|
*/
|
|
|
|
static $url_handlers = array(
|
2008-08-09 05:29:30 +02:00
|
|
|
'$Action//$ID/$OtherID' => 'handleAction',
|
2008-08-09 05:19:54 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $allowed_actions = array(
|
|
|
|
'handleAction',
|
|
|
|
'handleIndex',
|
|
|
|
);
|
|
|
|
|
2008-10-16 13:08:51 +02:00
|
|
|
/**
|
|
|
|
* Initialisation function that is run before any action on the controller is called.
|
|
|
|
*
|
|
|
|
* @uses BasicAuth::requireLogin()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2009-11-15 22:24:58 +01:00
|
|
|
if($this->basicAuthEnabled) BasicAuth::protect_site_if_necessary();
|
2008-10-16 13:08:51 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
// Directly access the session variable just in case the Group or Member tables don't yet exist
|
2008-11-09 23:11:25 +01:00
|
|
|
if(Session::get('loggedInAs') && Security::database_is_ready()) {
|
2008-11-06 05:51:25 +01:00
|
|
|
$member = Member::currentUser();
|
2008-11-24 10:31:14 +01:00
|
|
|
if($member) {
|
2010-10-19 00:53:03 +02:00
|
|
|
if(!headers_sent()) Cookie::set("PastMember", true, 90, null, null, false, true);
|
2012-09-26 23:34:00 +02:00
|
|
|
DB::query("UPDATE \"Member\" SET \"LastVisited\" = " . DB::getConn()->now()
|
|
|
|
. " WHERE \"ID\" = $member->ID", null);
|
2008-11-24 10:31:14 +01:00
|
|
|
}
|
2008-10-16 13:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is used to test that subordinate controllers are actually calling parent::init() - a common bug
|
|
|
|
$this->baseInitCalled = true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 04:23:42 +01:00
|
|
|
/**
|
|
|
|
* Returns a link to this controller. Overload with your own Link rules if they exist.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2012-01-10 04:23:42 +01:00
|
|
|
return get_class($this) .'/';
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Executes this controller, and return an {@link SS_HTTPResponse} object with the result.
|
2009-09-07 02:14:11 +02:00
|
|
|
*
|
|
|
|
* This method first does a few set-up activities:
|
|
|
|
* - Push this controller ont to the controller stack -
|
|
|
|
* see {@link Controller::curr()} for information about this.
|
|
|
|
* - Call {@link init()}
|
|
|
|
* - Defer to {@link RequestHandler->handleRequest()} to determine which action
|
|
|
|
* should be executed
|
2008-10-04 06:45:43 +02:00
|
|
|
*
|
2009-09-07 02:14:11 +02:00
|
|
|
* Note: $requestParams['executeForm'] support was removed,
|
|
|
|
* make the following change in your URLs:
|
|
|
|
* "/?executeForm=FooBar" -> "/FooBar"
|
|
|
|
* Also make sure "FooBar" is in the $allowed_actions of your controller class.
|
2008-10-04 06:45:43 +02:00
|
|
|
*
|
2009-09-07 02:14:11 +02:00
|
|
|
* Note: You should rarely need to overload run() -
|
|
|
|
* this kind of change is only really appropriate for things like nested
|
|
|
|
* controllers - {@link ModelAsController} and {@link RootURLController}
|
|
|
|
* are two examples here. If you want to make more
|
|
|
|
* orthodox functionality, it's better to overload {@link init()} or {@link index()}.
|
|
|
|
*
|
|
|
|
* Important: If you are going to overload handleRequest,
|
|
|
|
* make sure that you start the method with $this->pushCurrent()
|
|
|
|
* and end the method with $this->popCurrent().
|
|
|
|
* Failure to do this will create weird session errors.
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param $request The {@link SS_HTTPRequest} object that is responsible
|
2009-09-07 02:14:11 +02:00
|
|
|
* for distributing request parsing.
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_HTTPResponse The response that this controller produces,
|
2009-09-07 02:14:11 +02:00
|
|
|
* including HTTP headers such as redirection info
|
2008-08-09 05:19:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
|
2008-08-11 06:48:45 +02:00
|
|
|
if(!$request) user_error("Controller::handleRequest() not passed a request!", E_USER_ERROR);
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->pushCurrent();
|
|
|
|
$this->urlParams = $request->allParams();
|
2008-10-05 21:45:38 +02:00
|
|
|
$this->request = $request;
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$this->response = new SS_HTTPResponse();
|
2012-05-01 04:43:52 +02:00
|
|
|
$this->setDataModel($model);
|
2009-05-28 02:22:07 +02:00
|
|
|
|
|
|
|
$this->extend('onBeforeInit');
|
2008-08-09 05:19:54 +02:00
|
|
|
|
|
|
|
// Init
|
|
|
|
$this->baseInitCalled = false;
|
|
|
|
$this->init();
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$this->baseInitCalled) {
|
|
|
|
user_error("init() method on class '$this->class' doesn't call Controller::init()."
|
|
|
|
. "Make sure that you have parent::init() included.", E_USER_WARNING);
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
|
2009-05-28 02:22:07 +02:00
|
|
|
$this->extend('onAfterInit');
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
// If we had a redirection or something, halt processing.
|
|
|
|
if($this->response->isFinished()) {
|
|
|
|
$this->popCurrent();
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
$body = parent::handleRequest($request, $model);
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
if($body instanceof SS_HTTPResponse) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Request handler returned SS_HTTPResponse object to $this->class controller;"
|
|
|
|
. "returning it without modification.");
|
|
|
|
}
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->response = $body;
|
2013-03-12 00:17:20 +01:00
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
} else {
|
2013-03-12 00:17:20 +01:00
|
|
|
if($body instanceof Object && $body->hasMethod('getViewer')) {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(isset($_REQUEST['debug_request'])) {
|
|
|
|
Debug::message("Request handler $body->class object to $this->class controller;"
|
|
|
|
. "rendering with template returned by $body->class::getViewer()");
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
$body = $body->getViewer($request->latestParam('Action'))->process($body);
|
2008-08-11 04:57:59 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->response->setBody($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ContentNegotiator::process($this->response);
|
|
|
|
HTTP::add_cache_headers($this->response);
|
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller's default action handler. It will call the method named in $Action, if that method exists.
|
|
|
|
* If $Action isn't given, it will use "index" as a default.
|
|
|
|
*/
|
2013-02-19 23:47:29 +01:00
|
|
|
protected function handleAction($request, $action) {
|
2008-08-11 00:49:59 +02:00
|
|
|
foreach($request->latestParams() as $k => $v) {
|
|
|
|
if($v || !isset($this->urlParams[$k])) $this->urlParams[$k] = $v;
|
|
|
|
}
|
|
|
|
|
2013-01-24 20:44:39 +01:00
|
|
|
$this->action = $action;
|
2008-08-09 05:19:54 +02:00
|
|
|
$this->requestParams = $request->requestVars();
|
2013-01-24 20:44:39 +01:00
|
|
|
|
|
|
|
if($this->hasMethod($action)) {
|
|
|
|
$result = parent::handleAction($request, $action);
|
|
|
|
|
2009-10-11 02:07:01 +02:00
|
|
|
// If the action returns an array, customise with it before rendering the template.
|
|
|
|
if(is_array($result)) {
|
2013-01-24 20:44:39 +01:00
|
|
|
return $this->getViewer($action)->process($this->customise($result));
|
2008-08-09 05:19:54 +02:00
|
|
|
} else {
|
2009-10-11 02:07:01 +02:00
|
|
|
return $result;
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-24 20:44:39 +01:00
|
|
|
return $this->getViewer($action)->process($this);
|
2008-08-09 05:19:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setURLParams($urlParams) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->urlParams = $urlParams;
|
|
|
|
}
|
|
|
|
|
2007-08-21 01:22:30 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* @return array The parameters extracted from the URL by the {@link Director}.
|
2007-08-21 01:22:30 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURLParams() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->urlParams;
|
|
|
|
}
|
2007-12-02 22:29:31 +01:00
|
|
|
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Returns the SS_HTTPResponse object that this controller is building up.
|
2007-12-02 22:29:31 +01:00
|
|
|
* Can be used to set the status code and headers
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getResponse() {
|
2007-12-02 22:29:31 +01:00
|
|
|
return $this->response;
|
|
|
|
}
|
2008-10-05 21:45:38 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
protected $baseInitCalled = false;
|
|
|
|
|
2008-03-03 09:48:52 +01:00
|
|
|
/**
|
|
|
|
* Return the object that is going to own a form that's being processed, and handle its execution.
|
|
|
|
* Note that the result needn't be an actual controller object.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFormOwner() {
|
2008-03-03 09:48:52 +01:00
|
|
|
// Get the appropraite ocntroller: sometimes we want to get a form from another controller
|
|
|
|
if(isset($this->requestParams['formController'])) {
|
|
|
|
$formController = Director::getControllerForURL($this->requestParams['formController']);
|
|
|
|
|
|
|
|
while(is_a($formController, 'NestedController')) {
|
|
|
|
$formController = $formController->getNestedController();
|
|
|
|
}
|
|
|
|
return $formController;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* This is the default action handler used if a method doesn't exist.
|
|
|
|
* It will process the controller object with the template returned by {@link getViewer()}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function defaultAction($action) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getViewer($action)->process($this);
|
|
|
|
}
|
2007-09-14 20:02:03 +02:00
|
|
|
|
2008-03-03 00:24:10 +01:00
|
|
|
/**
|
|
|
|
* Returns the action that is being executed on this controller.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAction() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->action;
|
|
|
|
}
|
2007-09-14 20:02:03 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return an SSViewer object to process the data
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return SSViewer The viewer identified being the default handler for this Controller/Action combination
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getViewer($action) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Hard-coded templates
|
|
|
|
if($this->templates[$action]) {
|
|
|
|
$templates = $this->templates[$action];
|
|
|
|
} else if($this->templates['index']) {
|
|
|
|
$templates = $this->templates['index'];
|
|
|
|
} else if($this->template) {
|
|
|
|
$templates = $this->template;
|
|
|
|
} else {
|
2008-10-06 21:25:45 +02:00
|
|
|
// Add action-specific templates for inheritance chain
|
2012-06-19 12:34:54 +02:00
|
|
|
$templates = array();
|
2008-08-14 05:35:13 +02:00
|
|
|
$parentClass = $this->class;
|
|
|
|
if($action && $action != 'index') {
|
|
|
|
$parentClass = $this->class;
|
|
|
|
while($parentClass != "Controller") {
|
|
|
|
$templates[] = strtok($parentClass,'_') . '_' . $action;
|
|
|
|
$parentClass = get_parent_class($parentClass);
|
|
|
|
}
|
|
|
|
}
|
2008-10-06 21:25:45 +02:00
|
|
|
// Add controller templates for inheritance chain
|
2007-07-19 12:40:28 +02:00
|
|
|
$parentClass = $this->class;
|
|
|
|
while($parentClass != "Controller") {
|
2008-08-14 05:35:13 +02:00
|
|
|
$templates[] = strtok($parentClass,'_');
|
2007-07-19 12:40:28 +02:00
|
|
|
$parentClass = get_parent_class($parentClass);
|
|
|
|
}
|
2008-08-14 05:35:13 +02:00
|
|
|
|
2012-06-19 12:34:54 +02:00
|
|
|
$templates[] = 'Controller';
|
|
|
|
|
2008-10-06 21:25:45 +02:00
|
|
|
// remove duplicates
|
2007-07-19 12:40:28 +02:00
|
|
|
$templates = array_unique($templates);
|
|
|
|
}
|
2012-06-19 12:34:54 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return new SSViewer($templates);
|
|
|
|
}
|
2009-10-11 02:07:23 +02:00
|
|
|
|
|
|
|
public function hasAction($action) {
|
|
|
|
return parent::hasAction($action) || $this->hasActionTemplate($action);
|
|
|
|
}
|
2012-02-27 05:27:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all the "action" part of the current URL and returns the result.
|
|
|
|
* If no action parameter is present, returns the full URL
|
|
|
|
* @static
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function removeAction($fullURL, $action = null) {
|
|
|
|
if (!$action) $action = $this->getAction(); //default to current action
|
|
|
|
$returnURL = $fullURL;
|
|
|
|
|
|
|
|
if (($pos = strpos($fullURL, $action)) !== false) {
|
|
|
|
$returnURL = substr($fullURL,0,$pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $returnURL;
|
|
|
|
}
|
2009-10-11 02:07:23 +02:00
|
|
|
|
2009-10-11 02:07:01 +02:00
|
|
|
/**
|
|
|
|
* Returns TRUE if this controller has a template that is specifically designed to handle a specific action.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-10-11 02:07:23 +02:00
|
|
|
public function hasActionTemplate($action) {
|
2009-10-11 02:07:01 +02:00
|
|
|
if(isset($this->templates[$action])) return true;
|
|
|
|
|
|
|
|
$parentClass = $this->class;
|
|
|
|
$templates = array();
|
|
|
|
|
|
|
|
while($parentClass != 'Controller') {
|
|
|
|
$templates[] = strtok($parentClass, '_') . '_' . $action;
|
|
|
|
$parentClass = get_parent_class($parentClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
return SSViewer::hasTemplate($templates);
|
|
|
|
}
|
2008-10-05 21:27:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the current controller with the templates determined
|
|
|
|
* by {@link getViewer()}.
|
|
|
|
*
|
|
|
|
* @param array $params Key-value array for custom template variables (Optional)
|
|
|
|
* @return string Parsed template content
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function render($params = null) {
|
2008-10-05 21:27:21 +02:00
|
|
|
$template = $this->getViewer($this->getAction());
|
|
|
|
|
|
|
|
// if the object is already customised (e.g. through Controller->run()), use it
|
|
|
|
$obj = ($this->customisedObj) ? $this->customisedObj : $this;
|
|
|
|
|
|
|
|
if($params) $obj = $this->customise($params);
|
2009-05-28 02:22:07 +02:00
|
|
|
|
2008-10-05 21:27:21 +02:00
|
|
|
return $template->process($obj);
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-11-15 22:24:58 +01:00
|
|
|
* Call this to disable site-wide basic authentication for a specific contoller.
|
|
|
|
* This must be called before Controller::init(). That is, you must call it in your controller's
|
|
|
|
* init method before it calls parent::init().
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function disableBasicAuth() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->basicAuthEnabled = false;
|
|
|
|
}
|
|
|
|
|
2007-08-17 05:09:46 +02:00
|
|
|
/**
|
|
|
|
* Returns the current controller
|
2007-08-21 01:22:30 +02:00
|
|
|
* @returns Controller
|
2007-08-17 05:09:46 +02:00
|
|
|
*/
|
|
|
|
public static function curr() {
|
2007-08-17 07:45:15 +02:00
|
|
|
if(Controller::$controller_stack) {
|
|
|
|
return Controller::$controller_stack[0];
|
|
|
|
} else {
|
|
|
|
user_error("No current controller available", E_USER_WARNING);
|
|
|
|
}
|
2007-08-17 05:09:46 +02:00
|
|
|
}
|
2007-08-21 00:39:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether we have a currently active controller or not
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return boolean True if there is at least 1 controller in the stack.
|
2007-08-21 00:39:44 +02:00
|
|
|
*/
|
|
|
|
public static function has_curr() {
|
|
|
|
return Controller::$controller_stack ? true : false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the member is allowed to do the given action.
|
|
|
|
* @param perm The permission to be checked, such as 'View'.
|
|
|
|
* @param member The member whose permissions need checking. Defaults to the currently logged
|
|
|
|
* in user.
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function can($perm, $member = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2011-10-29 01:09:22 +02:00
|
|
|
if(is_array($perm)) {
|
|
|
|
$perm = array_map(array($this, 'can'), $perm, array_fill(0, count($perm), $member));
|
|
|
|
return min($perm);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->hasMethod($methodName = 'can' . $perm)) {
|
|
|
|
return $this->$methodName($member);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:02:03 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------
|
2007-09-14 20:02:03 +02:00
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
/**
|
|
|
|
* Pushes this controller onto the stack of current controllers.
|
2012-09-26 23:34:00 +02:00
|
|
|
* This means that any redirection, session setting, or other things that rely on Controller::curr() will now
|
|
|
|
* write to this controller object.
|
2007-08-17 07:45:15 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function pushCurrent() {
|
2007-08-17 07:45:15 +02:00
|
|
|
array_unshift(self::$controller_stack, $this);
|
|
|
|
// Create a new session object
|
2007-08-18 01:11:43 +02:00
|
|
|
if(!$this->session) {
|
2007-10-02 06:33:18 +02:00
|
|
|
if(isset(self::$controller_stack[1])) {
|
|
|
|
$this->session = self::$controller_stack[1]->getSession();
|
|
|
|
} else {
|
|
|
|
$this->session = new Session(null);
|
|
|
|
}
|
2007-08-18 01:11:43 +02:00
|
|
|
}
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pop this controller off the top of the stack.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function popCurrent() {
|
2007-08-18 01:31:51 +02:00
|
|
|
if($this === self::$controller_stack[0]) {
|
2007-08-17 07:45:15 +02:00
|
|
|
array_shift(self::$controller_stack);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("popCurrent called on $this->class controller, but it wasn't at the top of the stack",
|
|
|
|
E_USER_WARNING);
|
2007-08-17 07:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-17 05:09:46 +02:00
|
|
|
|
|
|
|
/**
|
2012-05-23 11:48:06 +02:00
|
|
|
* Redirect to the given URL.
|
2007-08-17 05:09:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function redirect($url, $code=302) {
|
2010-10-13 06:59:22 +02:00
|
|
|
if(!$this->response) $this->response = new SS_HTTPResponse();
|
|
|
|
|
2011-08-18 13:32:40 +02:00
|
|
|
if($this->response->getHeader('Location') && $this->response->getHeader('Location') != $url) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Already directed to " . $this->response->getHeader('Location')
|
|
|
|
. "; now trying to direct to $url", E_USER_WARNING);
|
2008-04-26 08:53:13 +02:00
|
|
|
return;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
|
2007-08-17 05:09:46 +02:00
|
|
|
// Attach site-root to relative links, if they have a slash in them
|
2012-09-26 23:34:00 +02:00
|
|
|
if($url=="" || $url[0]=='?' || (substr($url,0,4) != "http" && $url[0] != "/" && strpos($url,'/') !== false)) {
|
2007-08-17 05:09:46 +02:00
|
|
|
$url = Director::baseURL() . $url;
|
|
|
|
}
|
|
|
|
|
2008-04-22 03:45:55 +02:00
|
|
|
$this->response->redirect($url, $code);
|
2007-08-17 05:09:46 +02:00
|
|
|
}
|
2007-08-17 07:45:15 +02:00
|
|
|
|
2008-10-14 21:59:01 +02:00
|
|
|
/**
|
2011-10-10 10:10:20 +02:00
|
|
|
* Redirect back. Uses either the HTTP_REFERER or a manually set request-variable called "BackURL".
|
2008-10-14 21:59:01 +02:00
|
|
|
* This variable is needed in scenarios where not HTTP-Referer is sent (
|
|
|
|
* e.g when calling a page by location.href in IE).
|
|
|
|
* If none of the two variables is available, it will redirect to the base
|
|
|
|
* URL (see {@link Director::baseURL()}).
|
|
|
|
* @uses redirect()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function redirectBack() {
|
2011-09-29 05:21:32 +02:00
|
|
|
// Don't cache the redirect back ever
|
|
|
|
HTTP::set_cache_age(0);
|
|
|
|
|
2010-10-13 06:59:22 +02:00
|
|
|
$url = null;
|
|
|
|
|
|
|
|
// In edge-cases, this will be called outside of a handleRequest() context; in that case,
|
|
|
|
// redirect to the homepage - don't break into the global state at this stage because we'll
|
|
|
|
// be calling from a test context or something else where the global state is inappropraite
|
|
|
|
if($this->request) {
|
2011-10-10 10:10:20 +02:00
|
|
|
if($this->request->requestVar('BackURL')) {
|
|
|
|
$url = $this->request->requestVar('BackURL');
|
2010-10-13 06:59:22 +02:00
|
|
|
} else if($this->request->getHeader('Referer')) {
|
|
|
|
$url = $this->request->getHeader('Referer');
|
|
|
|
}
|
2008-10-14 21:59:01 +02:00
|
|
|
}
|
2010-10-13 06:59:22 +02:00
|
|
|
|
|
|
|
if(!$url) $url = Director::baseURL();
|
2008-10-14 21:59:01 +02:00
|
|
|
|
2009-03-17 23:24:50 +01:00
|
|
|
// absolute redirection URLs not located on this site may cause phishing
|
|
|
|
if(Director::is_site_url($url)) {
|
|
|
|
return $this->redirect($url);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-14 21:59:01 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 02:26:41 +02:00
|
|
|
/**
|
|
|
|
* Tests whether a redirection has been requested.
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will
|
|
|
|
* return null;
|
2007-08-31 02:26:41 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function redirectedTo() {
|
2010-10-13 06:59:22 +02:00
|
|
|
return $this->response && $this->response->getHeader('Location');
|
2007-08-31 02:26:41 +02:00
|
|
|
}
|
|
|
|
|
2007-08-17 07:45:15 +02:00
|
|
|
/**
|
|
|
|
* Get the Session object representing this Controller's session
|
2007-08-21 01:22:30 +02:00
|
|
|
* @return Session
|
2007-08-17 07:45:15 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getSession() {
|
2007-08-17 07:45:15 +02:00
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Session object.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setSession(Session $session) {
|
2007-08-17 07:45:15 +02:00
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
/**
|
2010-04-13 05:20:59 +02:00
|
|
|
* Joins two or more link segments together, putting a slash between them if necessary.
|
|
|
|
* Use this for building the results of {@link Link()} methods.
|
|
|
|
* If either of the links have query strings,
|
|
|
|
* then they will be combined and put at the end of the resulting url.
|
|
|
|
*
|
|
|
|
* Caution: All parameters are expected to be URI-encoded already.
|
|
|
|
*
|
|
|
|
* @param String
|
|
|
|
* @return String
|
2008-08-09 05:54:55 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function join_links() {
|
2008-08-09 05:54:55 +02:00
|
|
|
$args = func_get_args();
|
2008-08-19 12:06:43 +02:00
|
|
|
$result = "";
|
|
|
|
$querystrings = array();
|
2010-10-15 02:29:29 +02:00
|
|
|
$fragmentIdentifier = null;
|
2008-08-09 05:54:55 +02:00
|
|
|
foreach($args as $arg) {
|
2010-10-15 02:29:29 +02:00
|
|
|
// Find fragment identifier - keep the last one
|
|
|
|
if(strpos($arg,'#') !== false) {
|
|
|
|
list($arg, $fragmentIdentifier) = explode('#',$arg,2);
|
|
|
|
}
|
|
|
|
// Find querystrings
|
2008-08-19 12:06:43 +02:00
|
|
|
if(strpos($arg,'?') !== false) {
|
|
|
|
list($arg, $suffix) = explode('?',$arg,2);
|
|
|
|
$querystrings[] = $suffix;
|
|
|
|
}
|
2012-03-08 21:41:17 +01:00
|
|
|
if((is_string($arg) && $arg) || is_numeric($arg)) {
|
|
|
|
$arg = (string)$arg;
|
2008-08-28 06:25:13 +02:00
|
|
|
if($result && substr($result,-1) != '/' && $arg[0] != '/') $result .= "/$arg";
|
2009-10-11 02:07:15 +02:00
|
|
|
else $result .= (substr($result, -1) == '/' && $arg[0] == '/') ? ltrim($arg, '/') : $arg;
|
2008-08-28 06:25:13 +02:00
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
2008-08-19 12:06:43 +02:00
|
|
|
if($querystrings) $result .= '?' . implode('&', $querystrings);
|
2010-10-15 02:29:29 +02:00
|
|
|
if($fragmentIdentifier) $result .= "#$fragmentIdentifier";
|
2008-08-19 12:06:43 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
return $result;
|
|
|
|
}
|
2012-02-11 03:08:39 +01:00
|
|
|
|
2012-02-21 01:36:34 +01:00
|
|
|
public static function get_template_global_variables() {
|
2012-02-11 03:08:39 +01:00
|
|
|
return array(
|
|
|
|
'CurrentPage' => 'curr',
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|