1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11: * @link http://cakephp.org CakePHP(tm) Project
12: * @package Cake.Controller
13: * @since CakePHP(tm) v 0.2.9
14: * @license http://www.opensource.org/licenses/mit-license.php MIT License
15: */
16:
17: App::uses('CakeResponse', 'Network');
18: App::uses('ClassRegistry', 'Utility');
19: App::uses('ComponentCollection', 'Controller');
20: App::uses('View', 'View');
21: App::uses('CakeEvent', 'Event');
22: App::uses('CakeEventListener', 'Event');
23: App::uses('CakeEventManager', 'Event');
24:
25: /**
26: * Application controller class for organization of business logic.
27: * Provides basic functionality, such as rendering views inside layouts,
28: * automatic model availability, redirection, callbacks, and more.
29: *
30: * Controllers should provide a number of 'action' methods. These are public methods on the controller
31: * that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for
32: * performing a specific action on a resource or collection of resources. For example adding or editing a new
33: * object, or listing a set of objects.
34: *
35: * You can access request parameters, using `$this->request`. The request object contains all the POST, GET and FILES
36: * that were part of the request.
37: *
38: * After performing the required actions, controllers are responsible for creating a response. This usually
39: * takes the form of a generated View, or possibly a redirection to another controller action. In either case
40: * `$this->response` allows you to manipulate all aspects of the response.
41: *
42: * Controllers are created by Dispatcher based on request parameters and routing. By default controllers and actions
43: * use conventional names. For example `/posts/index` maps to `PostsController::index()`. You can re-map URLs
44: * using Router::connect().
45: *
46: * @package Cake.Controller
47: * @property AclComponent $Acl
48: * @property AuthComponent $Auth
49: * @property CookieComponent $Cookie
50: * @property EmailComponent $Email
51: * @property PaginatorComponent $Paginator
52: * @property RequestHandlerComponent $RequestHandler
53: * @property SecurityComponent $Security
54: * @property SessionComponent $Session
55: * @link http://book.cakephp.org/2.0/en/controllers.html
56: */
57: class Controller extends Object implements CakeEventListener {
58:
59: /**
60: * The name of this controller. Controller names are plural, named after the model they manipulate.
61: *
62: * @var string
63: * @link http://book.cakephp.org/2.0/en/controllers.html#controller-attributes
64: */
65: public $name = null;
66:
67: /**
68: * An array containing the class names of models this controller uses.
69: *
70: * Example: `public $uses = array('Product', 'Post', 'Comment');`
71: *
72: * Can be set to several values to express different options:
73: *
74: * - `true` Use the default inflected model name.
75: * - `array()` Use only models defined in the parent class.
76: * - `false` Use no models at all, do not merge with parent class either.
77: * - `array('Post', 'Comment')` Use only the Post and Comment models. Models
78: * Will also be merged with the parent class.
79: *
80: * The default value is `true`.
81: *
82: * @var mixed A single name as a string or a list of names as an array.
83: * @link http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
84: */
85: public $uses = true;
86:
87: /**
88: * An array containing the names of helpers this controller uses. The array elements should
89: * not contain the "Helper" part of the classname.
90: *
91: * Example: `public $helpers = array('Html', 'Javascript', 'Time', 'Ajax');`
92: *
93: * @var mixed A single name as a string or a list of names as an array.
94: * @link http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
95: */
96: public $helpers = array();
97:
98: /**
99: * An instance of a CakeRequest object that contains information about the current request.
100: * This object contains all the information about a request and several methods for reading
101: * additional information about the request.
102: *
103: * @var CakeRequest
104: * @link http://book.cakephp.org/2.0/en/controllers/request-response.html#cakerequest
105: */
106: public $request;
107:
108: /**
109: * An instance of a CakeResponse object that contains information about the impending response
110: *
111: * @var CakeResponse
112: * @link http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse
113: */
114: public $response;
115:
116: /**
117: * The classname to use for creating the response object.
118: *
119: * @var string
120: */
121: protected $_responseClass = 'CakeResponse';
122:
123: /**
124: * The name of the views subfolder containing views for this controller.
125: *
126: * @var string
127: */
128: public $viewPath = null;
129:
130: /**
131: * The name of the layouts subfolder containing layouts for this controller.
132: *
133: * @var string
134: */
135: public $layoutPath = null;
136:
137: /**
138: * Contains variables to be handed to the view.
139: *
140: * @var array
141: */
142: public $viewVars = array();
143:
144: /**
145: * The name of the view file to render. The name specified
146: * is the filename in /app/View/<SubFolder> without the .ctp extension.
147: *
148: * @var string
149: */
150: public $view = null;
151:
152: /**
153: * The name of the layout file to render the view inside of. The name specified
154: * is the filename of the layout in /app/View/Layouts without the .ctp
155: * extension.
156: *
157: * @var string
158: */
159: public $layout = 'default';
160:
161: /**
162: * Set to true to automatically render the view
163: * after action logic.
164: *
165: * @var boolean
166: */
167: public $autoRender = true;
168:
169: /**
170: * Set to true to automatically render the layout around views.
171: *
172: * @var boolean
173: */
174: public $autoLayout = true;
175:
176: /**
177: * Instance of ComponentCollection used to handle callbacks.
178: *
179: * @var ComponentCollection
180: */
181: public $Components = null;
182:
183: /**
184: * Array containing the names of components this controller uses. Component names
185: * should not contain the "Component" portion of the classname.
186: *
187: * Example: `public $components = array('Session', 'RequestHandler', 'Acl');`
188: *
189: * @var array
190: * @link http://book.cakephp.org/2.0/en/controllers/components.html
191: */
192: public $components = array('Session');
193:
194: /**
195: * The name of the View class this controller sends output to.
196: *
197: * @var string
198: */
199: public $viewClass = 'View';
200:
201: /**
202: * Instance of the View created during rendering. Won't be set until after
203: * Controller::render() is called.
204: *
205: * @var View
206: */
207: public $View;
208:
209: /**
210: * File extension for view templates. Defaults to Cake's conventional ".ctp".
211: *
212: * @var string
213: */
214: public $ext = '.ctp';
215:
216: /**
217: * Automatically set to the name of a plugin.
218: *
219: * @var string
220: */
221: public $plugin = null;
222:
223: /**
224: * Used to define methods a controller that will be cached. To cache a
225: * single action, the value is set to an array containing keys that match
226: * action names and values that denote cache expiration times (in seconds).
227: *
228: * Example:
229: *
230: * {{{
231: * public $cacheAction = array(
232: * 'view/23/' => 21600,
233: * 'recalled/' => 86400
234: * );
235: * }}}
236: *
237: * $cacheAction can also be set to a strtotime() compatible string. This
238: * marks all the actions in the controller for view caching.
239: *
240: * @var mixed
241: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html#additional-configuration-options
242: */
243: public $cacheAction = false;
244:
245: /**
246: * Holds all params passed and named.
247: *
248: * @var mixed
249: */
250: public $passedArgs = array();
251:
252: /**
253: * Triggers Scaffolding
254: *
255: * @var mixed
256: * @link http://book.cakephp.org/2.0/en/controllers/scaffolding.html
257: */
258: public $scaffold = false;
259:
260: /**
261: * Holds current methods of the controller. This is a list of all the methods reachable
262: * via URL. Modifying this array, will allow you to change which methods can be reached.
263: *
264: * @var array
265: */
266: public $methods = array();
267:
268: /**
269: * This controller's primary model class name, the Inflector::singularize()'ed version of
270: * the controller's $name property.
271: *
272: * Example: For a controller named 'Comments', the modelClass would be 'Comment'
273: *
274: * @var string
275: */
276: public $modelClass = null;
277:
278: /**
279: * This controller's model key name, an underscored version of the controller's $modelClass property.
280: *
281: * Example: For a controller named 'ArticleComments', the modelKey would be 'article_comment'
282: *
283: * @var string
284: */
285: public $modelKey = null;
286:
287: /**
288: * Holds any validation errors produced by the last call of the validateErrors() method/
289: *
290: * @var array Validation errors, or false if none
291: */
292: public $validationErrors = null;
293:
294: /**
295: * The class name of the parent class you wish to merge with.
296: * Typically this is AppController, but you may wish to merge vars with a different
297: * parent class.
298: *
299: * @var string
300: */
301: protected $_mergeParent = 'AppController';
302:
303: /**
304: * Instance of the CakeEventManager this controller is using
305: * to dispatch inner events.
306: *
307: * @var CakeEventManager
308: */
309: protected $_eventManager = null;
310:
311: /**
312: * Constructor.
313: *
314: * @param CakeRequest $request Request object for this controller. Can be null for testing,
315: * but expect that features that use the request parameters will not work.
316: * @param CakeResponse $response Response object for this controller.
317: */
318: public function __construct($request = null, $response = null) {
319: if ($this->name === null) {
320: $this->name = substr(get_class($this), 0, -10);
321: }
322:
323: if (!$this->viewPath) {
324: $this->viewPath = $this->name;
325: }
326:
327: $this->modelClass = Inflector::singularize($this->name);
328: $this->modelKey = Inflector::underscore($this->modelClass);
329: $this->Components = new ComponentCollection();
330:
331: $childMethods = get_class_methods($this);
332: $parentMethods = get_class_methods('Controller');
333:
334: $this->methods = array_diff($childMethods, $parentMethods);
335:
336: if ($request instanceof CakeRequest) {
337: $this->setRequest($request);
338: }
339: if ($response instanceof CakeResponse) {
340: $this->response = $response;
341: }
342: parent::__construct();
343: }
344:
345: /**
346: * Provides backwards compatibility to avoid problems with empty and isset to alias properties.
347: * Lazy loads models using the loadModel() method if declared in $uses
348: *
349: * @param string $name
350: * @return boolean
351: */
352: public function __isset($name) {
353: switch ($name) {
354: case 'base':
355: case 'here':
356: case 'webroot':
357: case 'data':
358: case 'action':
359: case 'params':
360: return true;
361: }
362:
363: if (is_array($this->uses)) {
364: foreach ($this->uses as $modelClass) {
365: list($plugin, $class) = pluginSplit($modelClass, true);
366: if ($name === $class) {
367: return $this->loadModel($modelClass);
368: }
369: }
370: }
371:
372: if ($name === $this->modelClass) {
373: list($plugin, $class) = pluginSplit($name, true);
374: if (!$plugin) {
375: $plugin = $this->plugin ? $this->plugin . '.' : null;
376: }
377: return $this->loadModel($plugin . $this->modelClass);
378: }
379:
380: return false;
381: }
382:
383: /**
384: * Provides backwards compatibility access to the request object properties.
385: * Also provides the params alias.
386: *
387: * @param string $name The name of the requested value
388: * @return mixed The requested value for valid variables/aliases else null
389: */
390: public function __get($name) {
391: switch ($name) {
392: case 'base':
393: case 'here':
394: case 'webroot':
395: case 'data':
396: return $this->request->{$name};
397: case 'action':
398: return isset($this->request->params['action']) ? $this->request->params['action'] : '';
399: case 'params':
400: return $this->request;
401: case 'paginate':
402: return $this->Components->load('Paginator')->settings;
403: }
404:
405: if (isset($this->{$name})) {
406: return $this->{$name};
407: }
408:
409: return null;
410: }
411:
412: /**
413: * Provides backwards compatibility access for setting values to the request object.
414: *
415: * @param string $name
416: * @param mixed $value
417: * @return void
418: */
419: public function __set($name, $value) {
420: switch ($name) {
421: case 'base':
422: case 'here':
423: case 'webroot':
424: case 'data':
425: $this->request->{$name} = $value;
426: return;
427: case 'action':
428: $this->request->params['action'] = $value;
429: return;
430: case 'params':
431: $this->request->params = $value;
432: return;
433: case 'paginate':
434: $this->Components->load('Paginator')->settings = $value;
435: return;
436: }
437: $this->{$name} = $value;
438: }
439:
440: /**
441: * Sets the request objects and configures a number of controller properties
442: * based on the contents of the request. The properties that get set are
443: *
444: * - $this->request - To the $request parameter
445: * - $this->plugin - To the $request->params['plugin']
446: * - $this->view - To the $request->params['action']
447: * - $this->autoLayout - To the false if $request->params['bare']; is set.
448: * - $this->autoRender - To false if $request->params['return'] == 1
449: * - $this->passedArgs - The the combined results of params['named'] and params['pass]
450: *
451: * @param CakeRequest $request
452: * @return void
453: */
454: public function setRequest(CakeRequest $request) {
455: $this->request = $request;
456: $this->plugin = isset($request->params['plugin']) ? Inflector::camelize($request->params['plugin']) : null;
457: $this->view = isset($request->params['action']) ? $request->params['action'] : null;
458: if (isset($request->params['pass']) && isset($request->params['named'])) {
459: $this->passedArgs = array_merge($request->params['pass'], $request->params['named']);
460: }
461:
462: if (!empty($request->params['return']) && $request->params['return'] == 1) {
463: $this->autoRender = false;
464: }
465: if (!empty($request->params['bare'])) {
466: $this->autoLayout = false;
467: }
468: }
469:
470: /**
471: * Dispatches the controller action. Checks that the action
472: * exists and isn't private.
473: *
474: * @param CakeRequest $request
475: * @return mixed The resulting response.
476: * @throws PrivateActionException When actions are not public or prefixed by _
477: * @throws MissingActionException When actions are not defined and scaffolding is
478: * not enabled.
479: */
480: public function invokeAction(CakeRequest $request) {
481: try {
482: $method = new ReflectionMethod($this, $request->params['action']);
483:
484: if ($this->_isPrivateAction($method, $request)) {
485: throw new PrivateActionException(array(
486: 'controller' => $this->name . "Controller",
487: 'action' => $request->params['action']
488: ));
489: }
490: return $method->invokeArgs($this, $request->params['pass']);
491:
492: } catch (ReflectionException $e) {
493: if ($this->scaffold !== false) {
494: return $this->_getScaffold($request);
495: }
496: throw new MissingActionException(array(
497: 'controller' => $this->name . "Controller",
498: 'action' => $request->params['action']
499: ));
500: }
501: }
502:
503: /**
504: * Check if the request's action is marked as private, with an underscore,
505: * or if the request is attempting to directly accessing a prefixed action.
506: *
507: * @param ReflectionMethod $method The method to be invoked.
508: * @param CakeRequest $request The request to check.
509: * @return boolean
510: */
511: protected function _isPrivateAction(ReflectionMethod $method, CakeRequest $request) {
512: $privateAction = (
513: $method->name[0] === '_' ||
514: !$method->isPublic() ||
515: !in_array($method->name, $this->methods)
516: );
517: $prefixes = Router::prefixes();
518:
519: if (!$privateAction && !empty($prefixes)) {
520: if (empty($request->params['prefix']) && strpos($request->params['action'], '_') > 0) {
521: list($prefix) = explode('_', $request->params['action']);
522: $privateAction = in_array($prefix, $prefixes);
523: }
524: }
525: return $privateAction;
526: }
527:
528: /**
529: * Returns a scaffold object to use for dynamically scaffolded controllers.
530: *
531: * @param CakeRequest $request
532: * @return Scaffold
533: */
534: protected function _getScaffold(CakeRequest $request) {
535: return new Scaffold($this, $request);
536: }
537:
538: /**
539: * Merge components, helpers, and uses vars from
540: * Controller::$_mergeParent and PluginAppController.
541: *
542: * @return void
543: */
544: protected function _mergeControllerVars() {
545: $pluginController = $pluginDot = null;
546: $mergeParent = is_subclass_of($this, $this->_mergeParent);
547: $pluginVars = array();
548: $appVars = array();
549:
550: if (!empty($this->plugin)) {
551: $pluginController = $this->plugin . 'AppController';
552: if (!is_subclass_of($this, $pluginController)) {
553: $pluginController = null;
554: }
555: $pluginDot = $this->plugin . '.';
556: }
557:
558: if ($pluginController) {
559: $merge = array('components', 'helpers');
560: $this->_mergeVars($merge, $pluginController);
561: }
562:
563: if ($mergeParent || !empty($pluginController)) {
564: $appVars = get_class_vars($this->_mergeParent);
565: $merge = array('components', 'helpers');
566: $this->_mergeVars($merge, $this->_mergeParent, true);
567: }
568:
569: if ($this->uses === null) {
570: $this->uses = false;
571: }
572: if ($this->uses === true) {
573: $this->uses = array($pluginDot . $this->modelClass);
574: }
575: if (isset($appVars['uses']) && $appVars['uses'] === $this->uses) {
576: array_unshift($this->uses, $pluginDot . $this->modelClass);
577: }
578: if ($pluginController) {
579: $pluginVars = get_class_vars($pluginController);
580: }
581: if ($this->uses !== false) {
582: $this->_mergeUses($pluginVars);
583: $this->_mergeUses($appVars);
584: } else {
585: $this->uses = array();
586: $this->modelClass = '';
587: }
588: }
589:
590: /**
591: * Helper method for merging the $uses property together.
592: *
593: * Merges the elements not already in $this->uses into
594: * $this->uses.
595: *
596: * @param array $merge The data to merge in.
597: * @return void
598: */
599: protected function _mergeUses($merge) {
600: if (!isset($merge['uses'])) {
601: return;
602: }
603: if ($merge['uses'] === true) {
604: return;
605: }
606: $this->uses = array_merge(
607: $this->uses,
608: array_diff($merge['uses'], $this->uses)
609: );
610: }
611:
612: /**
613: * Returns a list of all events that will fire in the controller during it's lifecycle.
614: * You can override this function to add you own listener callbacks
615: *
616: * @return array
617: */
618: public function implementedEvents() {
619: return array(
620: 'Controller.initialize' => 'beforeFilter',
621: 'Controller.beforeRender' => 'beforeRender',
622: 'Controller.beforeRedirect' => array('callable' => 'beforeRedirect', 'passParams' => true),
623: 'Controller.shutdown' => 'afterFilter'
624: );
625: }
626:
627: /**
628: * Loads Model classes based on the uses property
629: * see Controller::loadModel(); for more info.
630: * Loads Components and prepares them for initialization.
631: *
632: * @return mixed true if models found and instance created.
633: * @see Controller::loadModel()
634: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::constructClasses
635: * @throws MissingModelException
636: */
637: public function constructClasses() {
638: $this->_mergeControllerVars();
639: if ($this->uses) {
640: $this->uses = (array)$this->uses;
641: list(, $this->modelClass) = pluginSplit(reset($this->uses));
642: }
643: $this->Components->init($this);
644: return true;
645: }
646:
647: /**
648: * Returns the CakeEventManager manager instance that is handling any callbacks.
649: * You can use this instance to register any new listeners or callbacks to the
650: * controller events, or create your own events and trigger them at will.
651: *
652: * @return CakeEventManager
653: */
654: public function getEventManager() {
655: if (empty($this->_eventManager)) {
656: $this->_eventManager = new CakeEventManager();
657: $this->_eventManager->attach($this->Components);
658: $this->_eventManager->attach($this);
659: }
660: return $this->_eventManager;
661: }
662:
663: /**
664: * Perform the startup process for this controller.
665: * Fire the Components and Controller callbacks in the correct order.
666: *
667: * - Initializes components, which fires their `initialize` callback
668: * - Calls the controller `beforeFilter`.
669: * - triggers Component `startup` methods.
670: *
671: * @return void
672: */
673: public function startupProcess() {
674: $this->getEventManager()->dispatch(new CakeEvent('Controller.initialize', $this));
675: $this->getEventManager()->dispatch(new CakeEvent('Controller.startup', $this));
676: }
677:
678: /**
679: * Perform the various shutdown processes for this controller.
680: * Fire the Components and Controller callbacks in the correct order.
681: *
682: * - triggers the component `shutdown` callback.
683: * - calls the Controller's `afterFilter` method.
684: *
685: * @return void
686: */
687: public function shutdownProcess() {
688: $this->getEventManager()->dispatch(new CakeEvent('Controller.shutdown', $this));
689: }
690:
691: /**
692: * Queries & sets valid HTTP response codes & messages.
693: *
694: * @param integer|array $code If $code is an integer, then the corresponding code/message is
695: * returned if it exists, null if it does not exist. If $code is an array,
696: * then the 'code' and 'message' keys of each nested array are added to the default
697: * HTTP codes. Example:
698: *
699: * httpCodes(404); // returns array(404 => 'Not Found')
700: *
701: * httpCodes(array(
702: * 701 => 'Unicorn Moved',
703: * 800 => 'Unexpected Minotaur'
704: * )); // sets these new values, and returns true
705: *
706: * @return array Associative array of the HTTP codes as keys, and the message
707: * strings as values, or null of the given $code does not exist.
708: * @deprecated Use CakeResponse::httpCodes();
709: */
710: public function httpCodes($code = null) {
711: return $this->response->httpCodes($code);
712: }
713:
714: /**
715: * Loads and instantiates models required by this controller.
716: * If the model is non existent, it will throw a missing database table error, as Cake generates
717: * dynamic models for the time being.
718: *
719: * @param string $modelClass Name of model class to load
720: * @param integer|string $id Initial ID the instanced model class should have
721: * @return bool True if the model was found
722: * @throws MissingModelException if the model class cannot be found.
723: */
724: public function loadModel($modelClass = null, $id = null) {
725: if ($modelClass === null) {
726: $modelClass = $this->modelClass;
727: }
728:
729: $this->uses = ($this->uses) ? (array)$this->uses : array();
730: if (!in_array($modelClass, $this->uses, true)) {
731: $this->uses[] = $modelClass;
732: }
733:
734: list($plugin, $modelClass) = pluginSplit($modelClass, true);
735:
736: $this->{$modelClass} = ClassRegistry::init(array(
737: 'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
738: ));
739: if (!$this->{$modelClass}) {
740: throw new MissingModelException($modelClass);
741: }
742: return true;
743: }
744:
745: /**
746: * Redirects to given $url, after turning off $this->autoRender.
747: * Script execution is halted after the redirect.
748: *
749: * @param string|array $url A string or array-based URL pointing to another location within the app,
750: * or an absolute URL
751: * @param integer $status Optional HTTP status code (eg: 404)
752: * @param boolean $exit If true, exit() will be called after the redirect
753: * @return void
754: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
755: */
756: public function redirect($url, $status = null, $exit = true) {
757: $this->autoRender = false;
758:
759: if (is_array($status)) {
760: extract($status, EXTR_OVERWRITE);
761: }
762: $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit));
763:
764: list($event->break, $event->breakOn, $event->collectReturn) = array(true, false, true);
765: $this->getEventManager()->dispatch($event);
766:
767: if ($event->isStopped()) {
768: return;
769: }
770: $response = $event->result;
771: extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);
772:
773: if ($url !== null) {
774: $this->response->header('Location', Router::url($url, true));
775: }
776:
777: if (is_string($status)) {
778: $codes = array_flip($this->response->httpCodes());
779: if (isset($codes[$status])) {
780: $status = $codes[$status];
781: }
782: }
783:
784: if ($status) {
785: $this->response->statusCode($status);
786: }
787:
788: if ($exit) {
789: $this->response->send();
790: $this->_stop();
791: }
792: }
793:
794: /**
795: * Parse beforeRedirect Response
796: *
797: * @param mixed $response Response from beforeRedirect callback
798: * @param string|array $url The same value of beforeRedirect
799: * @param integer $status The same value of beforeRedirect
800: * @param boolean $exit The same value of beforeRedirect
801: * @return array Array with keys url, status and exit
802: */
803: protected function _parseBeforeRedirect($response, $url, $status, $exit) {
804: if (is_array($response) && array_key_exists(0, $response)) {
805: foreach ($response as $resp) {
806: if (is_array($resp) && isset($resp['url'])) {
807: extract($resp, EXTR_OVERWRITE);
808: } elseif ($resp !== null) {
809: $url = $resp;
810: }
811: }
812: } elseif (is_array($response)) {
813: extract($response, EXTR_OVERWRITE);
814: }
815: return compact('url', 'status', 'exit');
816: }
817:
818: /**
819: * Convenience and object wrapper method for CakeResponse::header().
820: *
821: * @param string $status The header message that is being set.
822: * @return void
823: * @deprecated Use CakeResponse::header()
824: */
825: public function header($status) {
826: $this->response->header($status);
827: }
828:
829: /**
830: * Saves a variable for use inside a view template.
831: *
832: * @param string|array $one A string or an array of data.
833: * @param string|array $two Value in case $one is a string (which then works as the key).
834: * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
835: * @return void
836: * @link http://book.cakephp.org/2.0/en/controllers.html#interacting-with-views
837: */
838: public function set($one, $two = null) {
839: if (is_array($one)) {
840: if (is_array($two)) {
841: $data = array_combine($one, $two);
842: } else {
843: $data = $one;
844: }
845: } else {
846: $data = array($one => $two);
847: }
848: $this->viewVars = $data + $this->viewVars;
849: }
850:
851: /**
852: * Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
853: *
854: * Examples:
855: *
856: * {{{
857: * setAction('another_action');
858: * setAction('action_with_parameters', $parameter1);
859: * }}}
860: *
861: * @param string $action The new action to be 'redirected' to.
862: * Any other parameters passed to this method will be passed as parameters to the new action.
863: * @return mixed Returns the return value of the called action
864: */
865: public function setAction($action) {
866: $this->request->params['action'] = $action;
867: $this->view = $action;
868: $args = func_get_args();
869: unset($args[0]);
870: return call_user_func_array(array(&$this, $action), $args);
871: }
872:
873: /**
874: * Returns number of errors in a submitted FORM.
875: *
876: * @return integer Number of errors
877: */
878: public function validate() {
879: $args = func_get_args();
880: $errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
881:
882: if ($errors === false) {
883: return 0;
884: }
885: return count($errors);
886: }
887:
888: /**
889: * Validates models passed by parameters. Example:
890: *
891: * `$errors = $this->validateErrors($this->Article, $this->User);`
892: *
893: * @param mixed A list of models as a variable argument
894: * @return array Validation errors, or false if none
895: */
896: public function validateErrors() {
897: $objects = func_get_args();
898:
899: if (empty($objects)) {
900: return false;
901: }
902:
903: $errors = array();
904: foreach ($objects as $object) {
905: if (isset($this->{$object->alias})) {
906: $object = $this->{$object->alias};
907: }
908: $object->set($object->data);
909: $errors = array_merge($errors, $object->invalidFields());
910: }
911:
912: return $this->validationErrors = (!empty($errors) ? $errors : false);
913: }
914:
915: /**
916: * Instantiates the correct view class, hands it its data, and uses it to render the view output.
917: *
918: * @param string $view View to use for rendering
919: * @param string $layout Layout to use
920: * @return CakeResponse A response object containing the rendered view.
921: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
922: */
923: public function render($view = null, $layout = null) {
924: $event = new CakeEvent('Controller.beforeRender', $this);
925: $this->getEventManager()->dispatch($event);
926: if ($event->isStopped()) {
927: $this->autoRender = false;
928: return $this->response;
929: }
930:
931: if (!empty($this->uses) && is_array($this->uses)) {
932: foreach ($this->uses as $model) {
933: list($plugin, $className) = pluginSplit($model);
934: $this->request->params['models'][$className] = compact('plugin', 'className');
935: }
936: }
937:
938: $this->View = $this->_getViewObject();
939:
940: $models = ClassRegistry::keys();
941: foreach ($models as $currentModel) {
942: $currentObject = ClassRegistry::getObject($currentModel);
943: if (is_a($currentObject, 'Model')) {
944: $className = get_class($currentObject);
945: list($plugin) = pluginSplit(App::location($className));
946: $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
947: $this->View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
948: }
949: }
950:
951: $this->autoRender = false;
952: $this->response->body($this->View->render($view, $layout));
953: return $this->response;
954: }
955:
956: /**
957: * Returns the referring URL for this request.
958: *
959: * @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
960: * @param boolean $local If true, restrict referring URLs to local server
961: * @return string Referring URL
962: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::referer
963: */
964: public function referer($default = null, $local = false) {
965: if (!$this->request) {
966: return '/';
967: }
968:
969: $referer = $this->request->referer($local);
970: if ($referer === '/' && $default) {
971: return Router::url($default, true);
972: }
973: return $referer;
974: }
975:
976: /**
977: * Forces the user's browser not to cache the results of the current request.
978: *
979: * @return void
980: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::disableCache
981: * @deprecated Use CakeResponse::disableCache()
982: */
983: public function disableCache() {
984: $this->response->disableCache();
985: }
986:
987: /**
988: * Shows a message to the user for $pause seconds, then redirects to $url.
989: * Uses flash.ctp as the default layout for the message.
990: * Does not work if the current debug level is higher than 0.
991: *
992: * @param string $message Message to display to the user
993: * @param string|array $url Relative string or array-based URL to redirect to after the time expires
994: * @param integer $pause Time to show the message
995: * @param string $layout Layout you want to use, defaults to 'flash'
996: * @return void
997: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::flash
998: */
999: public function flash($message, $url, $pause = 1, $layout = 'flash') {
1000: $this->autoRender = false;
1001: $this->set('url', Router::url($url));
1002: $this->set('message', $message);
1003: $this->set('pause', $pause);
1004: $this->set('page_title', $message);
1005: $this->render(false, $layout);
1006: }
1007:
1008: /**
1009: * Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
1010: *
1011: * @param array $data POST'ed data organized by model and field
1012: * @param string|array $op A string containing an SQL comparison operator, or an array matching operators
1013: * to fields
1014: * @param string $bool SQL boolean operator: AND, OR, XOR, etc.
1015: * @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be
1016: * included in the returned conditions
1017: * @return array An array of model conditions
1018: * @deprecated Will be removed in 3.0
1019: */
1020: public function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
1021: if (!is_array($data) || empty($data)) {
1022: if (!empty($this->request->data)) {
1023: $data = $this->request->data;
1024: } else {
1025: return null;
1026: }
1027: }
1028: $cond = array();
1029:
1030: if ($op === null) {
1031: $op = '';
1032: }
1033:
1034: $arrayOp = is_array($op);
1035: foreach ($data as $model => $fields) {
1036: foreach ($fields as $field => $value) {
1037: $key = $model . '.' . $field;
1038: $fieldOp = $op;
1039: if ($arrayOp) {
1040: if (array_key_exists($key, $op)) {
1041: $fieldOp = $op[$key];
1042: } elseif (array_key_exists($field, $op)) {
1043: $fieldOp = $op[$field];
1044: } else {
1045: $fieldOp = false;
1046: }
1047: }
1048: if ($exclusive && $fieldOp === false) {
1049: continue;
1050: }
1051: $fieldOp = strtoupper(trim($fieldOp));
1052: if ($fieldOp === 'LIKE') {
1053: $key = $key . ' LIKE';
1054: $value = '%' . $value . '%';
1055: } elseif ($fieldOp && $fieldOp !== '=') {
1056: $key = $key . ' ' . $fieldOp;
1057: }
1058: $cond[$key] = $value;
1059: }
1060: }
1061: if ($bool && strtoupper($bool) !== 'AND') {
1062: $cond = array($bool => $cond);
1063: }
1064: return $cond;
1065: }
1066:
1067: /**
1068: * Handles automatic pagination of model records.
1069: *
1070: * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
1071: * @param string|array $scope Conditions to use while paginating
1072: * @param array $whitelist List of allowed options for paging
1073: * @return array Model query results
1074: * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::paginate
1075: * @deprecated Use PaginatorComponent instead
1076: */
1077: public function paginate($object = null, $scope = array(), $whitelist = array()) {
1078: return $this->Components->load('Paginator', $this->paginate)->paginate($object, $scope, $whitelist);
1079: }
1080:
1081: /**
1082: * Called before the controller action. You can use this method to configure and customize components
1083: * or perform logic that needs to happen before each controller action.
1084: *
1085: * @return void
1086: * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
1087: */
1088: public function beforeFilter() {
1089: }
1090:
1091: /**
1092: * Called after the controller action is run, but before the view is rendered. You can use this method
1093: * to perform logic or set view variables that are required on every request.
1094: *
1095: * @return void
1096: * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
1097: */
1098: public function beforeRender() {
1099: }
1100:
1101: /**
1102: * The beforeRedirect method is invoked when the controller's redirect method is called but before any
1103: * further action.
1104: *
1105: * If this method returns false the controller will not continue on to redirect the request.
1106: * The $url, $status and $exit variables have same meaning as for the controller's method. You can also
1107: * return a string which will be interpreted as the url to redirect to or return associative array with
1108: * key 'url' and optionally 'status' and 'exit'.
1109: *
1110: * @param string|array $url A string or array-based URL pointing to another location within the app,
1111: * or an absolute URL
1112: * @param integer $status Optional HTTP status code (eg: 404)
1113: * @param boolean $exit If true, exit() will be called after the redirect
1114: * @return mixed
1115: * false to stop redirection event,
1116: * string controllers a new redirection url or
1117: * array with the keys url, status and exit to be used by the redirect method.
1118: * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
1119: */
1120: public function beforeRedirect($url, $status = null, $exit = true) {
1121: }
1122:
1123: /**
1124: * Called after the controller action is run and rendered.
1125: *
1126: * @return void
1127: * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
1128: */
1129: public function afterFilter() {
1130: }
1131:
1132: /**
1133: * This method should be overridden in child classes.
1134: *
1135: * @param string $method name of method called example index, edit, etc.
1136: * @return boolean Success
1137: * @link http://book.cakephp.org/2.0/en/controllers.html#callbacks
1138: */
1139: public function beforeScaffold($method) {
1140: return true;
1141: }
1142:
1143: /**
1144: * Alias to beforeScaffold()
1145: *
1146: * @param string $method
1147: * @return boolean
1148: * @see Controller::beforeScaffold()
1149: * @deprecated
1150: */
1151: protected function _beforeScaffold($method) {
1152: return $this->beforeScaffold($method);
1153: }
1154:
1155: /**
1156: * This method should be overridden in child classes.
1157: *
1158: * @param string $method name of method called either edit or update.
1159: * @return boolean Success
1160: * @link http://book.cakephp.org/2.0/en/controllers.html#callbacks
1161: */
1162: public function afterScaffoldSave($method) {
1163: return true;
1164: }
1165:
1166: /**
1167: * Alias to afterScaffoldSave()
1168: *
1169: * @param string $method
1170: * @return boolean
1171: * @see Controller::afterScaffoldSave()
1172: * @deprecated
1173: */
1174: protected function _afterScaffoldSave($method) {
1175: return $this->afterScaffoldSave($method);
1176: }
1177:
1178: /**
1179: * This method should be overridden in child classes.
1180: *
1181: * @param string $method name of method called either edit or update.
1182: * @return boolean Success
1183: * @link http://book.cakephp.org/2.0/en/controllers.html#callbacks
1184: */
1185: public function afterScaffoldSaveError($method) {
1186: return true;
1187: }
1188:
1189: /**
1190: * Alias to afterScaffoldSaveError()
1191: *
1192: * @param string $method
1193: * @return boolean
1194: * @see Controller::afterScaffoldSaveError()
1195: * @deprecated
1196: */
1197: protected function _afterScaffoldSaveError($method) {
1198: return $this->afterScaffoldSaveError($method);
1199: }
1200:
1201: /**
1202: * This method should be overridden in child classes.
1203: * If not it will render a scaffold error.
1204: * Method MUST return true in child classes
1205: *
1206: * @param string $method name of method called example index, edit, etc.
1207: * @return boolean Success
1208: * @link http://book.cakephp.org/2.0/en/controllers.html#callbacks
1209: */
1210: public function scaffoldError($method) {
1211: return false;
1212: }
1213:
1214: /**
1215: * Alias to scaffoldError()
1216: *
1217: * @param string $method
1218: * @return boolean
1219: * @see Controller::scaffoldError()
1220: * @deprecated
1221: */
1222: protected function _scaffoldError($method) {
1223: return $this->scaffoldError($method);
1224: }
1225:
1226: /**
1227: * Constructs the view class instance based on the controller property
1228: *
1229: * @return View
1230: */
1231: protected function _getViewObject() {
1232: $viewClass = $this->viewClass;
1233: if ($this->viewClass !== 'View') {
1234: list($plugin, $viewClass) = pluginSplit($viewClass, true);
1235: $viewClass = $viewClass . 'View';
1236: App::uses($viewClass, $plugin . 'View');
1237: }
1238:
1239: return new $viewClass($this);
1240: }
1241:
1242: }
1243: