2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Base controller class.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-05-01 21:05:46 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.libs.controller
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-05-01 21:05:46 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Include files
|
|
|
|
*/
|
2010-12-04 07:15:22 +00:00
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-12-04 07:27:20 +00:00
|
|
|
App::uses('ClassRegistry', 'Utility');
|
2010-12-03 21:39:50 +00:00
|
|
|
App::uses('ComponentCollection', 'Controller');
|
|
|
|
App::uses('View', 'View');
|
2010-05-03 03:28:09 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Application controller class for organization of business logic.
|
|
|
|
* Provides basic functionality, such as rendering views inside layouts,
|
|
|
|
* automatic model availability, redirection, callbacks, and more.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-22 03:27:17 +00:00
|
|
|
* Controllers should provide a number of 'action' methods. These are public methods on the controller
|
2011-02-12 20:54:39 +00:00
|
|
|
* that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for
|
2010-12-22 03:27:17 +00:00
|
|
|
* performing a specific action on a resource or collection of resources. For example adding or editing a new
|
|
|
|
* object, or listing a set of objects.
|
|
|
|
*
|
|
|
|
* You can access request parameters, using `$this->request`. The request object contains all the POST, GET and FILES
|
2011-02-12 20:54:39 +00:00
|
|
|
* that were part of the request.
|
2010-12-22 03:27:17 +00:00
|
|
|
*
|
|
|
|
* After performing the required actions, controllers are responsible for creating a response. This usually
|
|
|
|
* takes the form of a generated View, or possibly a redirection to another controller action. In either case
|
|
|
|
* `$this->response` allows you to manipulate all aspects of the response.
|
|
|
|
*
|
|
|
|
* Controllers are created by Dispatcher based on request parameters and routing. By default controllers and actions
|
|
|
|
* use conventional names. For example `/posts/index` maps to `PostsController::index()`. You can re-map urls
|
|
|
|
* using Router::connect().
|
|
|
|
*
|
|
|
|
* @package cake.libs.controller
|
|
|
|
* @link http://book.cakephp.org/view/956/Introduction
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Controller extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-05-31 03:54:22 +00:00
|
|
|
* The name of this controller. Controller names are plural, named after the model they manipulate.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/959/Controller-Attributes
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* An array containing the class names of models this controller uses.
|
|
|
|
*
|
2010-04-04 07:14:00 +00:00
|
|
|
* Example: `public $uses = array('Product', 'Post', 'Comment');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-12-13 22:48:01 +00:00
|
|
|
* Can be set to array() to use no models. Can be set to false to
|
2009-10-31 18:25:33 +00:00
|
|
|
* use no models and prevent the merging of $uses with AppController
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @var mixed A single name as a string or a list of names as an array.
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/961/components-helpers-and-uses
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* An array containing the names of helpers this controller uses. The array elements should
|
2008-10-30 19:30:17 +00:00
|
|
|
* not contain the "Helper" part of the classname.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-04-04 07:14:00 +00:00
|
|
|
* Example: `public $helpers = array('Html', 'Javascript', 'Time', 'Ajax');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var mixed A single name as a string or a list of names as an array.
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/961/components-helpers-and-uses
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $helpers = array('Session', 'Html', 'Form');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-08 03:46:02 +00:00
|
|
|
/**
|
|
|
|
* An instance of a CakeRequest object that contains information about the current request.
|
2010-09-11 03:15:32 +00:00
|
|
|
* This object contains all the information about a request and several methods for reading
|
2011-02-12 20:54:39 +00:00
|
|
|
* additional information about the request.
|
2010-05-08 03:46:02 +00:00
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
*/
|
|
|
|
public $request;
|
|
|
|
|
2010-08-22 16:31:55 +00:00
|
|
|
/**
|
|
|
|
* An instance of a CakeResponse object that contains information about the impending response
|
|
|
|
*
|
|
|
|
* @var CakeResponse
|
|
|
|
*/
|
|
|
|
public $response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The classname to use for creating the response object.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_responseClass = 'CakeResponse';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The name of the views subfolder containing views for this controller.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $viewPath = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* The name of the layouts subfolder containing layouts for this controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $layoutPath = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Contains variables to be handed to the view.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $viewVars = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* An array containing the class names of the models this controller uses.
|
|
|
|
*
|
|
|
|
* @var array Array of model objects.
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $modelNames = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-02-12 20:54:39 +00:00
|
|
|
/**
|
|
|
|
* The name of the view file to render. The name specified
|
|
|
|
* is the filename in /app/views/<sub_folder> without the .ctp extension.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @link http://book.cakephp.org/view/962/Page-related-Attributes-layout-and-pageTitle
|
|
|
|
*/
|
|
|
|
public $view = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* The name of the layout file to render the view inside of. The name specified
|
2008-05-30 11:40:08 +00:00
|
|
|
* is the filename of the layout in /app/views/layouts without the .ctp
|
|
|
|
* extension.
|
|
|
|
*
|
|
|
|
* @var string
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/962/Page-related-Attributes-layout-and-pageTitle
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $layout = 'default';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Set to true to automatically render the view
|
|
|
|
* after action logic.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $autoRender = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Set to true to automatically render the layout around views.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $autoLayout = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
2010-12-22 03:27:17 +00:00
|
|
|
* Instance of ComponentCollection used to handle callbacks.
|
2008-05-31 03:54:22 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-07-04 19:13:13 +00:00
|
|
|
public $Components = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Array containing the names of components this controller uses. Component names
|
2008-10-30 19:30:17 +00:00
|
|
|
* should not contain the "Component" portion of the classname.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-04-04 07:14:00 +00:00
|
|
|
* Example: `public $components = array('Session', 'RequestHandler', 'Acl');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/961/components-helpers-and-uses
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $components = array('Session');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* The name of the View class this controller sends output to.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-02-12 20:54:39 +00:00
|
|
|
public $viewClass = 'View';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-15 03:35:58 +00:00
|
|
|
/**
|
|
|
|
* Instance of the View created during rendering. Won't be set until after Controller::render() is called.
|
|
|
|
*
|
|
|
|
* @var View
|
|
|
|
*/
|
|
|
|
public $View;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* File extension for view templates. Defaults to Cake's conventional ".ctp".
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $ext = '.ctp';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Automatically set to the name of a plugin.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $plugin = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to define methods a controller that will be cached. To cache a
|
|
|
|
* single action, the value is set to an array containing keys that match
|
|
|
|
* action names and values that denote cache expiration times (in seconds).
|
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* {{{
|
2010-04-04 07:14:00 +00:00
|
|
|
* public $cacheAction = array(
|
2008-05-30 11:40:08 +00:00
|
|
|
* 'view/23/' => 21600,
|
|
|
|
* 'recalled/' => 86400
|
|
|
|
* );
|
2010-03-28 15:28:03 +00:00
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* $cacheAction can also be set to a strtotime() compatible string. This
|
|
|
|
* marks all the actions in the controller for view caching.
|
|
|
|
*
|
|
|
|
* @var mixed
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/1380/Caching-in-the-Controller
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $cacheAction = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to create cached instances of models a controller uses.
|
|
|
|
* When set to true, all models related to the controller will be cached.
|
|
|
|
* This can increase performance in many cases.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $persistModel = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds all params passed and named.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $passedArgs = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-26 20:09:20 +00:00
|
|
|
/**
|
|
|
|
* Triggers Scaffolding
|
|
|
|
*
|
|
|
|
* @var mixed
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/1103/Scaffolding
|
2008-07-26 20:09:20 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $scaffold = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-11 16:34:05 +00:00
|
|
|
/**
|
2010-12-22 03:27:17 +00:00
|
|
|
* Holds current methods of the controller. This is a list of all the methods reachable
|
|
|
|
* via url. Modifying this array, will allow you to change which methods can be reached.
|
2008-11-11 16:34:05 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $methods = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
2009-05-01 21:05:46 +00:00
|
|
|
* This controller's primary model class name, the Inflector::classify()'ed version of
|
2009-03-19 21:10:13 +00:00
|
|
|
* the controller's $name property.
|
|
|
|
*
|
|
|
|
* Example: For a controller named 'Comments', the modelClass would be 'Comment'
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $modelClass = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* This controller's model key name, an underscored version of the controller's $modelClass property.
|
|
|
|
*
|
|
|
|
* Example: For a controller named 'ArticleComments', the modelKey would be 'article_comment'
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $modelKey = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
/**
|
|
|
|
* Holds any validation errors produced by the last call of the validateErrors() method/
|
|
|
|
*
|
|
|
|
* @var array Validation errors, or false if none
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $validationErrors = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-16 04:44:43 +00:00
|
|
|
/**
|
|
|
|
* The class name of the parent class you wish to merge with.
|
|
|
|
* Typically this is AppController, but you may wish to merge vars with a different
|
|
|
|
* parent class.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_mergeParent = 'AppController';
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2010-05-07 03:18:50 +00:00
|
|
|
* @param CakeRequest $request Request object for this controller can be null for testing.
|
|
|
|
* But expect that features that use the params will not work.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-22 16:31:55 +00:00
|
|
|
public function __construct($request = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->name === null) {
|
2010-12-06 14:08:06 +00:00
|
|
|
$this->name = substr(get_class($this), 0, strlen(get_class($this)) -10);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->viewPath == null) {
|
|
|
|
$this->viewPath = Inflector::underscore($this->name);
|
|
|
|
}
|
2011-02-23 04:06:53 +00:00
|
|
|
|
|
|
|
$this->modelClass = Inflector::singularize($this->name);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->modelKey = Inflector::underscore($this->modelClass);
|
2010-07-04 19:13:13 +00:00
|
|
|
$this->Components = new ComponentCollection();
|
2008-11-11 16:34:05 +00:00
|
|
|
|
2011-02-22 04:43:35 +00:00
|
|
|
$childMethods = get_class_methods($this);
|
|
|
|
$parentMethods = get_class_methods('Controller');
|
|
|
|
|
|
|
|
$this->methods = array_diff($childMethods, $parentMethods);
|
|
|
|
|
2010-05-07 03:18:50 +00:00
|
|
|
if ($request instanceof CakeRequest) {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->setRequest($request);
|
2010-05-07 03:18:50 +00:00
|
|
|
}
|
2010-08-22 16:31:55 +00:00
|
|
|
$this->getResponse();
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-11-01 00:02:26 +00:00
|
|
|
/**
|
2011-02-23 04:06:53 +00:00
|
|
|
* Provides backwards compatibility to avoid problems with empty and isset to alias properties.
|
2011-04-27 04:14:32 +00:00
|
|
|
* Lazy loads models using the loadModel() method if declared in $uses
|
2010-11-01 00:02:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __isset($name) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
case 'action':
|
|
|
|
case 'params':
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-27 04:14:32 +00:00
|
|
|
|
|
|
|
if (is_array($this->uses)) {
|
|
|
|
foreach ($this->uses as $modelClass) {
|
|
|
|
list($plugin, $class) = pluginSplit($modelClass, true);
|
|
|
|
if ($name === $class) {
|
|
|
|
if (!$plugin) {
|
|
|
|
$plugin = $this->plugin ? $this->plugin . '.' : null;
|
|
|
|
}
|
|
|
|
return $this->loadModel($modelClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name === $this->modelClass) {
|
|
|
|
list($plugin, $class) = pluginSplit($name, true);
|
|
|
|
if (!$plugin) {
|
|
|
|
$plugin = $this->plugin ? $this->plugin . '.' : null;
|
|
|
|
}
|
|
|
|
return $this->loadModel($plugin . $this->modelClass);
|
|
|
|
}
|
|
|
|
|
2010-11-01 00:02:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-11 03:15:32 +00:00
|
|
|
/**
|
2011-04-17 06:13:03 +00:00
|
|
|
* Provides backwards compatibility access to the request object properties.
|
2010-09-11 03:15:32 +00:00
|
|
|
* Also provides the params alias.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
return $this->request->{$name};
|
|
|
|
case 'action':
|
2010-09-11 03:55:42 +00:00
|
|
|
return isset($this->request->params['action']) ? $this->request->params['action'] : '';
|
2010-09-11 03:15:32 +00:00
|
|
|
case 'params':
|
|
|
|
return $this->request;
|
2010-12-15 02:21:39 +00:00
|
|
|
case 'paginate':
|
|
|
|
return $this->Components->load('Paginator')->settings;
|
2010-09-11 03:15:32 +00:00
|
|
|
}
|
2011-04-27 04:14:32 +00:00
|
|
|
|
|
|
|
if (isset($this->{$name})) {
|
|
|
|
return $this->{$name};
|
|
|
|
}
|
|
|
|
|
2010-09-11 03:15:32 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-09-11 03:37:15 +00:00
|
|
|
/**
|
2011-04-17 06:13:03 +00:00
|
|
|
* Provides backwards compatibility access for setting values to the request object.
|
2010-09-11 03:37:15 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __set($name, $value) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
return $this->request->{$name} = $value;
|
|
|
|
case 'action':
|
|
|
|
return $this->request->params['action'] = $value;
|
|
|
|
case 'params':
|
|
|
|
return $this->request->params = $value;
|
2010-12-15 02:21:39 +00:00
|
|
|
case 'paginate':
|
|
|
|
return $this->Components->load('Paginator')->settings = $value;
|
2010-09-11 03:37:15 +00:00
|
|
|
}
|
|
|
|
return $this->{$name} = $value;
|
|
|
|
}
|
|
|
|
|
2010-05-07 03:18:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the request objects and configures a number of controller properties
|
2011-03-09 12:31:11 +00:00
|
|
|
* based on the contents of the request. The properties that get set are
|
|
|
|
*
|
|
|
|
* - $this->request - To the $request parameter
|
|
|
|
* - $this->plugin - To the $request->params['plugin']
|
|
|
|
* - $this->view - To the $request->params['action']
|
|
|
|
* - $this->autoLayout - To the false if $request->params['bare']; is set.
|
|
|
|
* - $this->autoRender - To false if $request->params['return'] == 1
|
|
|
|
* - $this->passedArgs - The the combined results of params['named'] and params['pass]
|
2010-05-07 03:18:50 +00:00
|
|
|
*
|
|
|
|
* @param CakeRequest $request
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-15 03:58:27 +00:00
|
|
|
public function setRequest(CakeRequest $request) {
|
2010-09-11 03:15:32 +00:00
|
|
|
$this->request = $request;
|
2010-05-07 03:18:50 +00:00
|
|
|
$this->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
|
2011-03-09 12:31:11 +00:00
|
|
|
$this->view = isset($request->params['action']) ? $request->params['action'] : null;
|
2010-05-08 04:00:19 +00:00
|
|
|
if (isset($request->params['pass']) && isset($request->params['named'])) {
|
|
|
|
$this->passedArgs = array_merge($request->params['pass'], $request->params['named']);
|
|
|
|
}
|
2010-05-07 03:18:50 +00:00
|
|
|
|
|
|
|
if (array_key_exists('return', $request->params) && $request->params['return'] == 1) {
|
|
|
|
$this->autoRender = false;
|
|
|
|
}
|
|
|
|
if (!empty($request->params['bare'])) {
|
|
|
|
$this->autoLayout = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-16 04:44:43 +00:00
|
|
|
* Merge components, helpers, and uses vars from Controller::$_mergeParent and PluginAppController.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function __mergeVars() {
|
2010-11-20 18:20:09 +00:00
|
|
|
$pluginName = $pluginController = $plugin = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-20 18:11:26 +00:00
|
|
|
if (!empty($this->plugin)) {
|
|
|
|
$pluginName = Inflector::camelize($this->plugin);
|
|
|
|
$pluginController = $pluginName . 'AppController';
|
|
|
|
if (!is_subclass_of($this, $pluginController)) {
|
|
|
|
$pluginController = null;
|
|
|
|
}
|
|
|
|
$plugin = $pluginName . '.';
|
|
|
|
}
|
2011-02-12 20:54:39 +00:00
|
|
|
|
2010-12-16 04:44:43 +00:00
|
|
|
if (is_subclass_of($this, $this->_mergeParent) || !empty($pluginController)) {
|
|
|
|
$appVars = get_class_vars($this->_mergeParent);
|
2008-05-30 11:40:08 +00:00
|
|
|
$uses = $appVars['uses'];
|
|
|
|
$merge = array('components', 'helpers');
|
|
|
|
|
|
|
|
if ($uses == $this->uses && !empty($this->uses)) {
|
|
|
|
if (!in_array($plugin . $this->modelClass, $this->uses)) {
|
|
|
|
array_unshift($this->uses, $plugin . $this->modelClass);
|
|
|
|
} elseif ($this->uses[0] !== $plugin . $this->modelClass) {
|
|
|
|
$this->uses = array_flip($this->uses);
|
|
|
|
unset($this->uses[$plugin . $this->modelClass]);
|
|
|
|
$this->uses = array_flip($this->uses);
|
|
|
|
array_unshift($this->uses, $plugin . $this->modelClass);
|
|
|
|
}
|
2010-12-11 04:17:42 +00:00
|
|
|
} elseif (
|
2011-02-12 20:54:39 +00:00
|
|
|
($this->uses !== null || $this->uses !== false) &&
|
2010-12-11 04:17:42 +00:00
|
|
|
is_array($this->uses) && !empty($appVars['uses'])
|
|
|
|
) {
|
|
|
|
$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-12-16 04:44:43 +00:00
|
|
|
$this->_mergeVars($merge, $this->_mergeParent, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-02-12 13:36:20 +00:00
|
|
|
if ($pluginController && $pluginName != null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$merge = array('components', 'helpers');
|
2010-12-11 04:17:42 +00:00
|
|
|
$appVars = get_class_vars($pluginController);
|
|
|
|
if (
|
2011-02-12 20:54:39 +00:00
|
|
|
($this->uses !== null || $this->uses !== false) &&
|
2010-12-11 04:17:42 +00:00
|
|
|
is_array($this->uses) && !empty($appVars['uses'])
|
|
|
|
) {
|
|
|
|
$this->uses = array_merge($this->uses, array_diff($appVars['uses'], $this->uses));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-11-20 18:11:26 +00:00
|
|
|
$this->_mergeVars($merge, $pluginController);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-19 22:27:06 +00:00
|
|
|
* Loads Model classes based on the uses property
|
2008-10-30 19:30:17 +00:00
|
|
|
* see Controller::loadModel(); for more info.
|
|
|
|
* Loads Components and prepares them for initialization.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-02 20:38:19 +00:00
|
|
|
* @return mixed true if models found and instance created.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @see Controller::loadModel()
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/977/Controller-Methods#constructClasses-986
|
2010-09-02 20:38:19 +00:00
|
|
|
* @throws MissingModelException
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function constructClasses() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__mergeVars();
|
2010-07-04 19:13:13 +00:00
|
|
|
$this->Components->init($this);
|
2011-04-27 04:14:32 +00:00
|
|
|
if ($this->uses) {
|
2011-04-27 04:45:42 +00:00
|
|
|
$this->uses = (array) $this->uses;
|
|
|
|
list(, $this->modelClass) = pluginSplit(current($this->uses));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-22 16:31:55 +00:00
|
|
|
/**
|
|
|
|
* Gets the response object for this controller. Will construct the response if it has not already been built.
|
|
|
|
*
|
|
|
|
* @return CakeResponse
|
|
|
|
*/
|
|
|
|
public function getResponse() {
|
|
|
|
if (empty($this->response)) {
|
|
|
|
$this->response = new $this->_responseClass(array('charset' => Configure::read('App.encoding')));
|
|
|
|
}
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2010-02-28 03:01:08 +00:00
|
|
|
/**
|
|
|
|
* Perform the startup process for this controller.
|
2010-07-04 19:13:13 +00:00
|
|
|
* Fire the Components and Controller callbacks in the correct order.
|
2010-02-28 03:01:08 +00:00
|
|
|
*
|
2010-02-28 06:55:23 +00:00
|
|
|
* - Initializes components, which fires their `initialize` callback
|
|
|
|
* - Calls the controller `beforeFilter`.
|
|
|
|
* - triggers Component `startup` methods.
|
|
|
|
*
|
2010-02-28 03:01:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function startupProcess() {
|
2010-09-15 03:02:38 +00:00
|
|
|
$this->Components->trigger('initialize', array(&$this));
|
2010-02-28 03:01:08 +00:00
|
|
|
$this->beforeFilter();
|
2010-07-04 19:13:13 +00:00
|
|
|
$this->Components->trigger('startup', array(&$this));
|
2010-02-28 03:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the various shutdown processes for this controller.
|
2010-07-04 19:13:13 +00:00
|
|
|
* Fire the Components and Controller callbacks in the correct order.
|
2010-02-28 03:01:08 +00:00
|
|
|
*
|
2010-02-28 06:55:23 +00:00
|
|
|
* - triggers the component `shutdown` callback.
|
|
|
|
* - calls the Controller's `afterFilter` method.
|
|
|
|
*
|
2010-02-28 03:01:08 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function shutdownProcess() {
|
2010-07-04 19:13:13 +00:00
|
|
|
$this->Components->trigger('shutdown', array(&$this));
|
2010-02-28 03:01:08 +00:00
|
|
|
$this->afterFilter();
|
|
|
|
}
|
|
|
|
|
2009-12-13 22:48:01 +00:00
|
|
|
/**
|
|
|
|
* Queries & sets valid HTTP response codes & messages.
|
|
|
|
*
|
|
|
|
* @param mixed $code If $code is an integer, then the corresponding code/message is
|
|
|
|
* returned if it exists, null if it does not exist. If $code is an array,
|
|
|
|
* then the 'code' and 'message' keys of each nested array are added to the default
|
|
|
|
* HTTP codes. Example:
|
|
|
|
*
|
|
|
|
* httpCodes(404); // returns array(404 => 'Not Found')
|
|
|
|
*
|
|
|
|
* httpCodes(array(
|
|
|
|
* 701 => 'Unicorn Moved',
|
|
|
|
* 800 => 'Unexpected Minotaur'
|
|
|
|
* )); // sets these new values, and returns true
|
|
|
|
*
|
|
|
|
* @return mixed Associative array of the HTTP codes as keys, and the message
|
2010-03-28 15:28:03 +00:00
|
|
|
* strings as values, or null of the given $code does not exist.
|
2010-12-22 03:27:17 +00:00
|
|
|
* @deprecated Use CakeResponse::httpCodes();
|
2009-12-13 22:48:01 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function httpCodes($code = null) {
|
2010-08-01 04:39:09 +00:00
|
|
|
return $this->response->httpCodes($code);
|
2009-12-13 22:48:01 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Loads and instantiates models required by this controller.
|
2010-03-28 15:28:03 +00:00
|
|
|
* If Controller::$persistModel; is true, controller will cache model instances on first request,
|
2008-09-23 12:04:02 +00:00
|
|
|
* additional request will used cached models.
|
|
|
|
* If the model is non existent, it will throw a missing database table error, as Cake generates
|
|
|
|
* dynamic models for the time being.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $modelClass Name of model class to load
|
|
|
|
* @param mixed $id Initial ID the instanced model class should have
|
2010-05-02 00:02:53 +00:00
|
|
|
* @return mixed true when single model found and instance created, error returned if model not found.
|
2010-08-28 03:04:35 +00:00
|
|
|
* @throws MissingModelException if the model class cannot be found.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function loadModel($modelClass = null, $id = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($modelClass === null) {
|
|
|
|
$modelClass = $this->modelClass;
|
|
|
|
}
|
|
|
|
$cached = false;
|
|
|
|
$object = null;
|
2010-07-14 00:19:31 +00:00
|
|
|
list($plugin, $modelClass) = pluginSplit($modelClass, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($this->persistModel === true) {
|
|
|
|
$cached = $this->_persist($modelClass, null, $object);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($cached === false)) {
|
|
|
|
$this->modelNames[] = $modelClass;
|
|
|
|
|
2010-05-03 03:36:32 +00:00
|
|
|
$this->{$modelClass} = ClassRegistry::init(array(
|
|
|
|
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!$this->{$modelClass}) {
|
2010-08-28 03:04:35 +00:00
|
|
|
throw new MissingModelException($modelClass);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->persistModel === true) {
|
|
|
|
$this->_persist($modelClass, true, $this->{$modelClass});
|
2010-11-13 04:05:44 +00:00
|
|
|
$registry = ClassRegistry::getInstance();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->_persist($modelClass . 'registry', true, $object, 'registry');
|
|
|
|
$this->_persist($modelClass, true, $object);
|
|
|
|
$this->modelNames[] = $modelClass;
|
|
|
|
}
|
2010-05-02 00:02:53 +00:00
|
|
|
|
|
|
|
return true;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Redirects to given $url, after turning off $this->autoRender.
|
2008-10-30 19:30:17 +00:00
|
|
|
* Script execution is halted after the redirect.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-05-01 21:05:46 +00:00
|
|
|
* @param mixed $url A string or array-based URL pointing to another location within the app,
|
2010-03-28 15:28:03 +00:00
|
|
|
* or an absolute URL
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $status Optional HTTP status code (eg: 404)
|
|
|
|
* @param boolean $exit If true, exit() will be called after the redirect
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return mixed void if $exit = false. Terminates script if $exit = true
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/982/redirect
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function redirect($url, $status = null, $exit = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->autoRender = false;
|
|
|
|
|
|
|
|
if (is_array($status)) {
|
|
|
|
extract($status, EXTR_OVERWRITE);
|
|
|
|
}
|
2010-07-04 19:13:13 +00:00
|
|
|
$response = $this->Components->trigger(
|
2011-02-12 20:54:39 +00:00
|
|
|
'beforeRedirect',
|
2010-07-04 19:13:13 +00:00
|
|
|
array(&$this, $url, $status, $exit),
|
2010-07-04 21:09:44 +00:00
|
|
|
array('break' => true, 'breakOn' => false, 'collectReturn' => true)
|
2010-07-04 19:13:13 +00:00
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
if ($response === false) {
|
|
|
|
return;
|
|
|
|
}
|
2011-01-20 00:46:31 +00:00
|
|
|
extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);
|
|
|
|
|
|
|
|
$response = $this->beforeRedirect($url, $status, $exit);
|
|
|
|
if ($response === false) {
|
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-01-20 00:46:31 +00:00
|
|
|
extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (function_exists('session_write_close')) {
|
|
|
|
session_write_close();
|
|
|
|
}
|
|
|
|
|
2010-08-01 04:40:46 +00:00
|
|
|
if (!empty($status) && is_string($status)) {
|
|
|
|
$codes = array_flip($this->response->httpCodes());
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($codes[$status])) {
|
2010-08-01 04:40:46 +00:00
|
|
|
$status = $codes[$status];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-13 22:48:01 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($url !== null) {
|
2010-08-01 04:40:46 +00:00
|
|
|
$this->response->header('Location', Router::url($url, true));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($status) && ($status >= 300 && $status < 400)) {
|
2010-08-01 04:40:46 +00:00
|
|
|
$this->response->statusCode($status);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($exit) {
|
2010-08-01 04:40:46 +00:00
|
|
|
$this->response->send();
|
2008-06-04 19:04:58 +00:00
|
|
|
$this->_stop();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-01-20 00:46:31 +00:00
|
|
|
/**
|
|
|
|
* Parse beforeRedirect Response
|
|
|
|
*
|
|
|
|
* @param mixed $response Response from beforeRedirect callback
|
|
|
|
* @param mixed $url The same value of beforeRedirect
|
|
|
|
* @param integer $status The same value of beforeRedirect
|
|
|
|
* @param boolean $exit The same value of beforeRedirect
|
|
|
|
* @return array Array with keys url, status and exit
|
|
|
|
*/
|
|
|
|
protected function _parseBeforeRedirect($response, $url, $status, $exit) {
|
|
|
|
if (is_array($response)) {
|
|
|
|
foreach ($response as $resp) {
|
|
|
|
if (is_array($resp) && isset($resp['url'])) {
|
|
|
|
extract($resp, EXTR_OVERWRITE);
|
|
|
|
} elseif ($resp !== null) {
|
|
|
|
$url = $resp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return compact('url', 'status', 'exit');
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-08-01 04:43:00 +00:00
|
|
|
* Convenience and object wrapper method for CakeResponse::header().
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @param string $status The header message that is being set.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return void
|
2010-12-22 03:27:17 +00:00
|
|
|
* @deprecated Use CakeResponse::header()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function header($status) {
|
2010-08-01 04:43:00 +00:00
|
|
|
$this->response->header($status);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Saves a variable for use inside a view template.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param mixed $one A string or an array of data.
|
|
|
|
* @param mixed $two Value in case $one is a string (which then works as the key).
|
2009-02-04 05:00:59 +00:00
|
|
|
* Unused if $one is an associative array, otherwise serves as the values to $one's keys.
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/979/set
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function set($one, $two = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($one)) {
|
|
|
|
if (is_array($two)) {
|
|
|
|
$data = array_combine($one, $two);
|
|
|
|
} else {
|
|
|
|
$data = $one;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = array($one => $two);
|
|
|
|
}
|
2010-09-18 04:13:03 +00:00
|
|
|
$this->viewVars = $data + $this->viewVars;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 15:28:03 +00:00
|
|
|
* Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
|
|
|
|
*
|
|
|
|
* Examples:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* {{{
|
2008-05-30 11:40:08 +00:00
|
|
|
* setAction('another_action');
|
|
|
|
* setAction('action_with_parameters', $parameter1);
|
2010-03-28 15:28:03 +00:00
|
|
|
* }}}
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @param string $action The new action to be 'redirected' to
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param mixed Any other parameters passed to this method will be passed as
|
2010-03-28 15:28:03 +00:00
|
|
|
* parameters to the new action.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed Returns the return value of the called action
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setAction($action) {
|
2010-09-11 03:37:15 +00:00
|
|
|
$this->request->action = $action;
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
unset($args[0]);
|
|
|
|
return call_user_func_array(array(&$this, $action), $args);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns number of errors in a submitted FORM.
|
|
|
|
*
|
|
|
|
* @return integer Number of errors
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function validate() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
$errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
|
|
|
|
|
|
|
|
if ($errors === false) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return count($errors);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Validates models passed by parameters. Example:
|
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* `$errors = $this->validateErrors($this->Article, $this->User);`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param mixed A list of models as a variable argument
|
|
|
|
* @return array Validation errors, or false if none
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function validateErrors() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$objects = func_get_args();
|
|
|
|
|
2009-11-21 20:14:21 +00:00
|
|
|
if (empty($objects)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
foreach ($objects as $object) {
|
2010-06-21 01:47:34 +00:00
|
|
|
if (isset($this->{$object->alias})) {
|
2010-07-10 04:32:35 +00:00
|
|
|
$object = $this->{$object->alias};
|
2010-06-21 01:47:34 +00:00
|
|
|
}
|
|
|
|
$object->set($object->data);
|
|
|
|
$errors = array_merge($errors, $object->invalidFields());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-21 20:14:21 +00:00
|
|
|
return $this->validationErrors = (!empty($errors) ? $errors : false);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Instantiates the correct view class, hands it its data, and uses it to render the view output.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-02-12 20:54:39 +00:00
|
|
|
* @param string $view View to use for rendering
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $layout Layout to use
|
2008-06-27 03:12:45 +00:00
|
|
|
* @return string Full output string of view contents
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/980/render
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-12 20:54:39 +00:00
|
|
|
public function render($view = null, $layout = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->beforeRender();
|
2010-07-31 19:41:02 +00:00
|
|
|
$this->Components->trigger('beforeRender', array(&$this));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-02-12 20:54:39 +00:00
|
|
|
$viewClass = $this->viewClass;
|
|
|
|
if ($this->viewClass != 'View') {
|
2010-12-09 03:45:18 +00:00
|
|
|
list($plugin, $viewClass) = pluginSplit($viewClass, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
$viewClass = $viewClass . 'View';
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses($viewClass, $plugin . 'View');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 03:37:15 +00:00
|
|
|
$this->request->params['models'] = $this->modelNames;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-10 04:32:35 +00:00
|
|
|
$View = new $viewClass($this);
|
2008-05-31 03:54:22 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($this->modelNames)) {
|
|
|
|
$models = array();
|
|
|
|
foreach ($this->modelNames as $currentModel) {
|
|
|
|
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
|
|
|
|
$models[] = Inflector::underscore($currentModel);
|
|
|
|
}
|
2009-05-01 21:05:46 +00:00
|
|
|
$isValidModel = (
|
|
|
|
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
|
|
|
|
!empty($this->$currentModel->validationErrors)
|
|
|
|
);
|
|
|
|
if ($isValidModel) {
|
|
|
|
$View->validationErrors[Inflector::camelize($currentModel)] =&
|
|
|
|
$this->$currentModel->validationErrors;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$models = array_diff(ClassRegistry::keys(), $models);
|
|
|
|
foreach ($models as $currentModel) {
|
|
|
|
if (ClassRegistry::isKeySet($currentModel)) {
|
2010-07-10 04:32:35 +00:00
|
|
|
$currentObject = ClassRegistry::getObject($currentModel);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
2009-05-01 21:05:46 +00:00
|
|
|
$View->validationErrors[Inflector::camelize($currentModel)] =&
|
|
|
|
$currentObject->validationErrors;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->autoRender = false;
|
2010-07-15 03:35:58 +00:00
|
|
|
$this->View = $View;
|
2011-02-12 20:54:39 +00:00
|
|
|
return $this->response->body($View->render($view, $layout));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Returns the referring URL for this request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
|
|
|
|
* @param boolean $local If true, restrict referring URLs to local server
|
|
|
|
* @return string Referring URL
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/987/referer
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function referer($default = null, $local = false) {
|
2010-05-08 04:06:13 +00:00
|
|
|
if ($this->request) {
|
2010-05-15 04:53:15 +00:00
|
|
|
$referer = $this->request->referer($local);
|
|
|
|
if ($referer == '/' && $default != null) {
|
|
|
|
return Router::url($default, true);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-15 04:53:15 +00:00
|
|
|
return $referer;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-09-08 20:33:48 +00:00
|
|
|
return '/';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Forces the user's browser not to cache the results of the current request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/988/disableCache
|
2010-12-22 03:27:17 +00:00
|
|
|
* @deprecated Use CakeResponse::disableCache()
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function disableCache() {
|
2010-08-01 04:46:50 +00:00
|
|
|
$this->response->disableCache();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Shows a message to the user for $pause seconds, then redirects to $url.
|
|
|
|
* Uses flash.ctp as the default layout for the message.
|
|
|
|
* Does not work if the current debug level is higher than 0.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $message Message to display to the user
|
2009-03-09 01:11:38 +00:00
|
|
|
* @param mixed $url Relative string or array-based URL to redirect to after the time expires
|
2008-08-26 18:35:44 +00:00
|
|
|
* @param integer $pause Time to show the message
|
2009-08-28 03:40:36 +00:00
|
|
|
* @param string $layout Layout you want to use, defaults to 'flash'
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void Renders flash layout
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/983/flash
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function flash($message, $url, $pause = 1, $layout = 'flash') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->autoRender = false;
|
|
|
|
$this->set('url', Router::url($url));
|
|
|
|
$this->set('message', $message);
|
|
|
|
$this->set('pause', $pause);
|
|
|
|
$this->set('page_title', $message);
|
2010-08-01 05:43:41 +00:00
|
|
|
$this->response->body($this->render(false, $layout));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $data POST'ed data organized by model and field
|
2009-05-01 21:05:46 +00:00
|
|
|
* @param mixed $op A string containing an SQL comparison operator, or an array matching operators
|
|
|
|
* to fields
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $bool SQL boolean operator: AND, OR, XOR, etc.
|
2009-05-01 21:05:46 +00:00
|
|
|
* @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be
|
|
|
|
* included in the returned conditions
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return array An array of model conditions
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/989/postConditions
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_array($data) || empty($data)) {
|
2010-09-11 03:37:15 +00:00
|
|
|
if (!empty($this->request->data)) {
|
|
|
|
$data = $this->request->data;
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cond = array();
|
|
|
|
|
|
|
|
if ($op === null) {
|
|
|
|
$op = '';
|
|
|
|
}
|
|
|
|
|
2009-11-23 17:52:38 +00:00
|
|
|
$arrayOp = is_array($op);
|
2008-05-30 11:40:08 +00:00
|
|
|
foreach ($data as $model => $fields) {
|
|
|
|
foreach ($fields as $field => $value) {
|
2008-06-09 08:29:56 +00:00
|
|
|
$key = $model.'.'.$field;
|
|
|
|
$fieldOp = $op;
|
2009-11-23 17:52:38 +00:00
|
|
|
if ($arrayOp) {
|
2009-11-19 22:21:14 +00:00
|
|
|
if (array_key_exists($key, $op)) {
|
|
|
|
$fieldOp = $op[$key];
|
|
|
|
} elseif (array_key_exists($field, $op)) {
|
|
|
|
$fieldOp = $op[$field];
|
|
|
|
} else {
|
|
|
|
$fieldOp = false;
|
|
|
|
}
|
2008-06-09 08:29:56 +00:00
|
|
|
}
|
|
|
|
if ($exclusive && $fieldOp === false) {
|
|
|
|
continue;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-06-09 08:29:56 +00:00
|
|
|
$fieldOp = strtoupper(trim($fieldOp));
|
2008-09-14 17:17:49 +00:00
|
|
|
if ($fieldOp === 'LIKE') {
|
2008-06-09 08:29:56 +00:00
|
|
|
$key = $key.' LIKE';
|
|
|
|
$value = '%'.$value.'%';
|
|
|
|
} elseif ($fieldOp && $fieldOp != '=') {
|
|
|
|
$key = $key.' '.$fieldOp;
|
|
|
|
}
|
|
|
|
$cond[$key] = $value;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($bool != null && strtoupper($bool) != 'AND') {
|
|
|
|
$cond = array($bool => $cond);
|
|
|
|
}
|
|
|
|
return $cond;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Handles automatic pagination of model records.
|
|
|
|
*
|
|
|
|
* @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
|
|
|
|
* @param mixed $scope Conditions to use while paginating
|
|
|
|
* @param array $whitelist List of allowed options for paging
|
|
|
|
* @return array Model query results
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/1232/Controller-Setup
|
2010-12-15 02:21:39 +00:00
|
|
|
* @deprecated Use PaginatorComponent instead
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function paginate($object = null, $scope = array(), $whitelist = array()) {
|
2010-12-15 02:21:39 +00:00
|
|
|
return $this->Components->load('Paginator', $this->paginate)->paginate($object, $scope, $whitelist);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-22 03:27:17 +00:00
|
|
|
* Called before the controller action. You can use this method to configure and customize components
|
|
|
|
* or perform logic that needs to happen before each controller action.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function beforeFilter() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-12-22 03:27:17 +00:00
|
|
|
* Called after the controller action is run, but before the view is rendered. You can use this method
|
|
|
|
* to perform logic or set view variables that are required on every request.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function beforeRender() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-01-20 00:46:31 +00:00
|
|
|
/**
|
|
|
|
* The beforeRedirect method is invoked when the controller's redirect method is called but before any
|
|
|
|
* further action. If this method returns false the controller will not continue on to redirect the request.
|
|
|
|
* The $url, $status and $exit variables have same meaning as for the controller's method. You can also
|
|
|
|
* return a string which will be interpreted as the url to redirect to or return associative array with
|
|
|
|
* key 'url' and optionally 'status' and 'exit'.
|
|
|
|
*
|
|
|
|
* @param mixed $url A string or array-based URL pointing to another location within the app,
|
|
|
|
* or an absolute URL
|
|
|
|
* @param integer $status Optional HTTP status code (eg: 404)
|
|
|
|
* @param boolean $exit If true, exit() will be called after the redirect
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function beforeRedirect($url, $status = null, $exit = true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:30:17 +00:00
|
|
|
* Called after the controller action is run and rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function afterFilter() {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This method should be overridden in child classes.
|
|
|
|
*
|
|
|
|
* @param string $method name of method called example index, edit, etc.
|
|
|
|
* @return boolean Success
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function _beforeScaffold($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This method should be overridden in child classes.
|
|
|
|
*
|
|
|
|
* @param string $method name of method called either edit or update.
|
|
|
|
* @return boolean Success
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function _afterScaffoldSave($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This method should be overridden in child classes.
|
|
|
|
*
|
|
|
|
* @param string $method name of method called either edit or update.
|
|
|
|
* @return boolean Success
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function _afterScaffoldSaveError($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* This method should be overridden in child classes.
|
|
|
|
* If not it will render a scaffold error.
|
|
|
|
* Method MUST return true in child classes
|
|
|
|
*
|
|
|
|
* @param string $method name of method called example index, edit, etc.
|
|
|
|
* @return boolean Success
|
2010-03-28 15:28:03 +00:00
|
|
|
* @link http://book.cakephp.org/view/984/Callbacks
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-05-03 03:28:09 +00:00
|
|
|
public function _scaffoldError($method) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|