| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: controller.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Base controller class.
|
|
|
5 |
*
|
|
|
6 |
* PHP versions 4 and 5
|
|
|
7 |
*
|
|
|
8 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
9 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
10 |
*
|
|
|
11 |
* Licensed under The MIT License
|
|
|
12 |
* Redistributions of files must retain the above copyright notice.
|
|
|
13 |
*
|
|
|
14 |
* @filesource
|
|
|
15 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
16 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
17 |
* @package cake
|
|
|
18 |
* @subpackage cake.cake.libs.controller
|
|
|
19 |
* @since CakePHP(tm) v 0.2.9
|
|
|
20 |
* @version $Revision: 7945 $
|
|
|
21 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
22 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
23 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
24 |
*/
|
|
|
25 |
/**
|
|
|
26 |
* Include files
|
|
|
27 |
*/
|
|
|
28 |
App::import('Core', array('Component', 'View'));
|
|
|
29 |
/**
|
|
|
30 |
* Controller
|
|
|
31 |
*
|
|
|
32 |
* Application controller class for organization of business logic.
|
|
|
33 |
* Provides basic functionality, such as rendering views inside layouts,
|
|
|
34 |
* automatic model availability, redirection, callbacks, and more.
|
|
|
35 |
*
|
|
|
36 |
* @package cake
|
|
|
37 |
* @subpackage cake.cake.libs.controller
|
|
|
38 |
* @link http://book.cakephp.org/view/49/Controllers
|
|
|
39 |
*
|
|
|
40 |
*/
|
|
|
41 |
class Controller extends Object {
|
|
|
42 |
/**
|
|
|
43 |
* The name of this controller. Controller names are plural, named after the model they manipulate.
|
|
|
44 |
*
|
|
|
45 |
* @var string
|
|
|
46 |
* @access public
|
|
|
47 |
* @link http://book.cakephp.org/view/52/name
|
|
|
48 |
*/
|
|
|
49 |
var $name = null;
|
|
|
50 |
/**
|
|
|
51 |
* Stores the current URL, relative to the webroot of the application.
|
|
|
52 |
*
|
|
|
53 |
* @var string
|
|
|
54 |
* @access public
|
|
|
55 |
*/
|
|
|
56 |
var $here = null;
|
|
|
57 |
/**
|
|
|
58 |
* The webroot of the application. Helpful if your application is placed in a folder under the current domain name.
|
|
|
59 |
*
|
|
|
60 |
* @var string
|
|
|
61 |
* @access public
|
|
|
62 |
*/
|
|
|
63 |
var $webroot = null;
|
|
|
64 |
/**
|
|
|
65 |
* The name of the currently requested controller action.
|
|
|
66 |
*
|
|
|
67 |
* @var string
|
|
|
68 |
* @access public
|
|
|
69 |
*/
|
|
|
70 |
var $action = null;
|
|
|
71 |
/**
|
|
|
72 |
* An array containing the class names of models this controller uses.
|
|
|
73 |
*
|
|
|
74 |
* Example: var $uses = array('Product', 'Post', 'Comment');
|
|
|
75 |
*
|
|
|
76 |
* @var mixed A single name as a string or a list of names as an array.
|
|
|
77 |
* @access protected
|
|
|
78 |
* @link http://book.cakephp.org/view/53/components-helpers-and-uses
|
|
|
79 |
*/
|
|
|
80 |
var $uses = false;
|
|
|
81 |
/**
|
|
|
82 |
* An array containing the names of helpers this controller uses. The array elements should
|
|
|
83 |
* not contain the "Helper" part of the classname.
|
|
|
84 |
*
|
|
|
85 |
* Example: var $helpers = array('Html', 'Javascript', 'Time', 'Ajax');
|
|
|
86 |
*
|
|
|
87 |
* @var mixed A single name as a string or a list of names as an array.
|
|
|
88 |
* @access protected
|
|
|
89 |
* @link http://book.cakephp.org/view/53/components-helpers-and-uses
|
|
|
90 |
*/
|
|
|
91 |
var $helpers = array('Html', 'Form');
|
|
|
92 |
/**
|
|
|
93 |
* Parameters received in the current request: GET and POST data, information
|
|
|
94 |
* about the request, etc.
|
|
|
95 |
*
|
|
|
96 |
* @var array
|
|
|
97 |
* @access public
|
|
|
98 |
* @link http://book.cakephp.org/view/55/The-Parameters-Attribute-params
|
|
|
99 |
*/
|
|
|
100 |
var $params = array();
|
|
|
101 |
/**
|
|
|
102 |
* Data POSTed to the controller using the HtmlHelper. Data here is accessible
|
|
|
103 |
* using the $this->data['ModelName']['fieldName'] pattern.
|
|
|
104 |
*
|
|
|
105 |
* @var array
|
|
|
106 |
* @access public
|
|
|
107 |
*/
|
|
|
108 |
var $data = array();
|
|
|
109 |
/**
|
|
|
110 |
* Holds pagination defaults for controller actions. The keys that can be included
|
|
|
111 |
* in this array are: 'conditions', 'fields', 'order', 'limit', 'page', and 'recursive',
|
|
|
112 |
* similar to the keys in the second parameter of Model::find().
|
|
|
113 |
*
|
|
|
114 |
* Pagination defaults can also be supplied in a model-by-model basis by using
|
|
|
115 |
* the name of the model as a key for a pagination array:
|
|
|
116 |
*
|
|
|
117 |
* var $paginate = array(
|
|
|
118 |
* 'Post' => array(...),
|
|
|
119 |
* 'Comment' => array(...)
|
|
|
120 |
* );
|
|
|
121 |
*
|
|
|
122 |
* @var array
|
|
|
123 |
* @access public
|
|
|
124 |
* @link http://book.cakephp.org/view/164/Pagination
|
|
|
125 |
*/
|
|
|
126 |
var $paginate = array('limit' => 20, 'page' => 1);
|
|
|
127 |
/**
|
|
|
128 |
* The name of the views subfolder containing views for this controller.
|
|
|
129 |
*
|
|
|
130 |
* @var string
|
|
|
131 |
* @access public
|
|
|
132 |
*/
|
|
|
133 |
var $viewPath = null;
|
|
|
134 |
/**
|
|
|
135 |
* The name of the layouts subfolder containing layouts for this controller.
|
|
|
136 |
*
|
|
|
137 |
* @var string
|
|
|
138 |
* @access public
|
|
|
139 |
*/
|
|
|
140 |
var $layoutPath = null;
|
|
|
141 |
/**
|
|
|
142 |
* Contains variables to be handed to the view.
|
|
|
143 |
*
|
|
|
144 |
* @var array
|
|
|
145 |
* @access public
|
|
|
146 |
*/
|
|
|
147 |
var $viewVars = array();
|
|
|
148 |
/**
|
|
|
149 |
* Text to be used for the $title_for_layout layout variable (usually
|
|
|
150 |
* placed inside <title> tags.)
|
|
|
151 |
*
|
|
|
152 |
* @var boolean
|
|
|
153 |
* @access public
|
|
|
154 |
* @link http://book.cakephp.org/view/54/Page-related-Attributes-layout-and-pageTitle
|
|
|
155 |
*/
|
|
|
156 |
var $pageTitle = false;
|
|
|
157 |
/**
|
|
|
158 |
* An array containing the class names of the models this controller uses.
|
|
|
159 |
*
|
|
|
160 |
* @var array Array of model objects.
|
|
|
161 |
* @access public
|
|
|
162 |
*/
|
|
|
163 |
var $modelNames = array();
|
|
|
164 |
/**
|
|
|
165 |
* Base URL path.
|
|
|
166 |
*
|
|
|
167 |
* @var string
|
|
|
168 |
* @access public
|
|
|
169 |
*/
|
|
|
170 |
var $base = null;
|
|
|
171 |
/**
|
|
|
172 |
* The name of the layout file to render the view inside of. The name specified
|
|
|
173 |
* is the filename of the layout in /app/views/layouts without the .ctp
|
|
|
174 |
* extension.
|
|
|
175 |
*
|
|
|
176 |
* @var string
|
|
|
177 |
* @access public
|
|
|
178 |
* @link http://book.cakephp.org/view/54/Page-related-Attributes-layout-and-pageTitle
|
|
|
179 |
*/
|
|
|
180 |
var $layout = 'default';
|
|
|
181 |
/**
|
|
|
182 |
* Set to true to automatically render the view
|
|
|
183 |
* after action logic.
|
|
|
184 |
*
|
|
|
185 |
* @var boolean
|
|
|
186 |
* @access public
|
|
|
187 |
*/
|
|
|
188 |
var $autoRender = true;
|
|
|
189 |
/**
|
|
|
190 |
* Set to true to automatically render the layout around views.
|
|
|
191 |
*
|
|
|
192 |
* @var boolean
|
|
|
193 |
* @access public
|
|
|
194 |
*/
|
|
|
195 |
var $autoLayout = true;
|
|
|
196 |
/**
|
|
|
197 |
* Instance of Component used to handle callbacks.
|
|
|
198 |
*
|
|
|
199 |
* @var string
|
|
|
200 |
* @access public
|
|
|
201 |
*/
|
|
|
202 |
var $Component = null;
|
|
|
203 |
/**
|
|
|
204 |
* Array containing the names of components this controller uses. Component names
|
|
|
205 |
* should not contain the "Component" portion of the classname.
|
|
|
206 |
*
|
|
|
207 |
* Example: var $components = array('Session', 'RequestHandler', 'Acl');
|
|
|
208 |
*
|
|
|
209 |
* @var array
|
|
|
210 |
* @access public
|
|
|
211 |
* @link http://book.cakephp.org/view/53/components-helpers-and-uses
|
|
|
212 |
*/
|
|
|
213 |
var $components = array();
|
|
|
214 |
/**
|
|
|
215 |
* The name of the View class this controller sends output to.
|
|
|
216 |
*
|
|
|
217 |
* @var string
|
|
|
218 |
* @access public
|
|
|
219 |
*/
|
|
|
220 |
var $view = 'View';
|
|
|
221 |
/**
|
|
|
222 |
* File extension for view templates. Defaults to Cake's conventional ".ctp".
|
|
|
223 |
*
|
|
|
224 |
* @var string
|
|
|
225 |
* @access public
|
|
|
226 |
*/
|
|
|
227 |
var $ext = '.ctp';
|
|
|
228 |
/**
|
|
|
229 |
* The output of the requested action. Contains either a variable
|
|
|
230 |
* returned from the action, or the data of the rendered view;
|
|
|
231 |
* You can use this var in child controllers' afterFilter() callbacks to alter output.
|
|
|
232 |
*
|
|
|
233 |
* @var string
|
|
|
234 |
* @access public
|
|
|
235 |
*/
|
|
|
236 |
var $output = null;
|
|
|
237 |
/**
|
|
|
238 |
* Automatically set to the name of a plugin.
|
|
|
239 |
*
|
|
|
240 |
* @var string
|
|
|
241 |
* @access public
|
|
|
242 |
*/
|
|
|
243 |
var $plugin = null;
|
|
|
244 |
/**
|
|
|
245 |
* Used to define methods a controller that will be cached. To cache a
|
|
|
246 |
* single action, the value is set to an array containing keys that match
|
|
|
247 |
* action names and values that denote cache expiration times (in seconds).
|
|
|
248 |
*
|
|
|
249 |
* Example: var $cacheAction = array(
|
|
|
250 |
* 'view/23/' => 21600,
|
|
|
251 |
* 'recalled/' => 86400
|
|
|
252 |
* );
|
|
|
253 |
*
|
|
|
254 |
* $cacheAction can also be set to a strtotime() compatible string. This
|
|
|
255 |
* marks all the actions in the controller for view caching.
|
|
|
256 |
*
|
|
|
257 |
* @var mixed
|
|
|
258 |
* @access public
|
|
|
259 |
* @link http://book.cakephp.org/view/346/Caching-in-the-Controller
|
|
|
260 |
*/
|
|
|
261 |
var $cacheAction = false;
|
|
|
262 |
/**
|
|
|
263 |
* Used to create cached instances of models a controller uses.
|
|
|
264 |
* When set to true, all models related to the controller will be cached.
|
|
|
265 |
* This can increase performance in many cases.
|
|
|
266 |
*
|
|
|
267 |
* @var boolean
|
|
|
268 |
* @access public
|
|
|
269 |
*/
|
|
|
270 |
var $persistModel = false;
|
|
|
271 |
/**
|
|
|
272 |
* Holds all params passed and named.
|
|
|
273 |
*
|
|
|
274 |
* @var mixed
|
|
|
275 |
* @access public
|
|
|
276 |
*/
|
|
|
277 |
var $passedArgs = array();
|
|
|
278 |
/**
|
|
|
279 |
* Triggers Scaffolding
|
|
|
280 |
*
|
|
|
281 |
* @var mixed
|
|
|
282 |
* @access public
|
|
|
283 |
* @link http://book.cakephp.org/view/105/Scaffolding
|
|
|
284 |
*/
|
|
|
285 |
var $scaffold = false;
|
|
|
286 |
/**
|
|
|
287 |
* Holds current methods of the controller
|
|
|
288 |
*
|
|
|
289 |
* @var array
|
|
|
290 |
* @access public
|
|
|
291 |
* @link
|
|
|
292 |
*/
|
|
|
293 |
var $methods = array();
|
|
|
294 |
/**
|
|
|
295 |
* Constructor.
|
|
|
296 |
*
|
|
|
297 |
*/
|
|
|
298 |
function __construct() {
|
|
|
299 |
if ($this->name === null) {
|
|
|
300 |
$r = null;
|
|
|
301 |
if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
|
|
|
302 |
die (__("Controller::__construct() : Can not get or parse my own class name, exiting."));
|
|
|
303 |
}
|
|
|
304 |
$this->name = $r[1];
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
if ($this->viewPath == null) {
|
|
|
308 |
$this->viewPath = Inflector::underscore($this->name);
|
|
|
309 |
}
|
|
|
310 |
$this->modelClass = Inflector::classify($this->name);
|
|
|
311 |
$this->modelKey = Inflector::underscore($this->modelClass);
|
|
|
312 |
$this->Component =& new Component();
|
|
|
313 |
|
|
|
314 |
$childMethods = get_class_methods($this);
|
|
|
315 |
$parentMethods = get_class_methods('Controller');
|
|
|
316 |
|
|
|
317 |
foreach ($childMethods as $key => $value) {
|
|
|
318 |
$childMethods[$key] = strtolower($value);
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
foreach ($parentMethods as $key => $value) {
|
|
|
322 |
$parentMethods[$key] = strtolower($value);
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
$this->methods = array_diff($childMethods, $parentMethods);
|
|
|
326 |
parent::__construct();
|
|
|
327 |
}
|
|
|
328 |
/**
|
|
|
329 |
* Merge components, helpers, and uses vars from AppController and PluginAppController.
|
|
|
330 |
*
|
|
|
331 |
* @return void
|
|
|
332 |
* @access protected
|
|
|
333 |
*/
|
|
|
334 |
function __mergeVars() {
|
|
|
335 |
$pluginName = Inflector::camelize($this->plugin);
|
|
|
336 |
$pluginController = $pluginName . 'AppController';
|
|
|
337 |
|
|
|
338 |
if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
|
|
|
339 |
$appVars = get_class_vars('AppController');
|
|
|
340 |
$uses = $appVars['uses'];
|
|
|
341 |
$merge = array('components', 'helpers');
|
|
|
342 |
$plugin = null;
|
|
|
343 |
|
|
|
344 |
if (!empty($this->plugin)) {
|
|
|
345 |
$plugin = $pluginName . '.';
|
|
|
346 |
if (!is_subclass_of($this, $pluginController)) {
|
|
|
347 |
$pluginController = null;
|
|
|
348 |
}
|
|
|
349 |
} else {
|
|
|
350 |
$pluginController = null;
|
|
|
351 |
}
|
|
|
352 |
|
|
|
353 |
if ($uses == $this->uses && !empty($this->uses)) {
|
|
|
354 |
if (!in_array($plugin . $this->modelClass, $this->uses)) {
|
|
|
355 |
array_unshift($this->uses, $plugin . $this->modelClass);
|
|
|
356 |
} elseif ($this->uses[0] !== $plugin . $this->modelClass) {
|
|
|
357 |
$this->uses = array_flip($this->uses);
|
|
|
358 |
unset($this->uses[$plugin . $this->modelClass]);
|
|
|
359 |
$this->uses = array_flip($this->uses);
|
|
|
360 |
array_unshift($this->uses, $plugin . $this->modelClass);
|
|
|
361 |
}
|
|
|
362 |
} elseif ($this->uses !== null || $this->uses !== false) {
|
|
|
363 |
$merge[] = 'uses';
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
foreach ($merge as $var) {
|
|
|
367 |
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
|
|
368 |
if ($var === 'components') {
|
|
|
369 |
$normal = Set::normalize($this->{$var});
|
|
|
370 |
$app = Set::normalize($appVars[$var]);
|
|
|
371 |
$this->{$var} = Set::merge($normal, $app);
|
|
|
372 |
} else {
|
|
|
373 |
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
|
|
374 |
}
|
|
|
375 |
}
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
if ($pluginController) {
|
|
|
380 |
$appVars = get_class_vars($pluginController);
|
|
|
381 |
$uses = $appVars['uses'];
|
|
|
382 |
$merge = array('components', 'helpers');
|
|
|
383 |
|
|
|
384 |
if ($this->uses !== null || $this->uses !== false) {
|
|
|
385 |
$merge[] = 'uses';
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
foreach ($merge as $var) {
|
|
|
389 |
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
|
|
390 |
if ($var === 'components') {
|
|
|
391 |
$normal = Set::normalize($this->{$var});
|
|
|
392 |
$app = Set::normalize($appVars[$var]);
|
|
|
393 |
$this->{$var} = Set::merge($normal, array_diff_assoc($app, $normal));
|
|
|
394 |
} else {
|
|
|
395 |
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
|
|
396 |
}
|
|
|
397 |
}
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
/**
|
|
|
402 |
* Loads Model classes based on the the uses property
|
|
|
403 |
* see Controller::loadModel(); for more info.
|
|
|
404 |
* Loads Components and prepares them for initialization.
|
|
|
405 |
*
|
|
|
406 |
* @return mixed true if models found and instance created, or cakeError if models not found.
|
|
|
407 |
* @access public
|
|
|
408 |
* @see Controller::loadModel()
|
|
|
409 |
* @link http://book.cakephp.org/view/429/constructClasses
|
|
|
410 |
*/
|
|
|
411 |
function constructClasses() {
|
|
|
412 |
$this->__mergeVars();
|
|
|
413 |
$this->Component->init($this);
|
|
|
414 |
|
|
|
415 |
if ($this->uses !== null || ($this->uses !== array())) {
|
|
|
416 |
if (empty($this->passedArgs) || !isset($this->passedArgs['0'])) {
|
|
|
417 |
$id = false;
|
|
|
418 |
} else {
|
|
|
419 |
$id = $this->passedArgs['0'];
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
if ($this->uses === false) {
|
|
|
423 |
$this->loadModel($this->modelClass, $id);
|
|
|
424 |
} elseif ($this->uses) {
|
|
|
425 |
$uses = is_array($this->uses) ? $this->uses : array($this->uses);
|
|
|
426 |
$modelClassName = $uses[0];
|
|
|
427 |
if (strpos($uses[0], '.') !== false) {
|
|
|
428 |
list($plugin, $modelClassName) = explode('.', $uses[0]);
|
|
|
429 |
}
|
|
|
430 |
$this->modelClass = $modelClassName;
|
|
|
431 |
foreach ($uses as $modelClass) {
|
|
|
432 |
$this->loadModel($modelClass);
|
|
|
433 |
}
|
|
|
434 |
}
|
|
|
435 |
}
|
|
|
436 |
return true;
|
|
|
437 |
}
|
|
|
438 |
/**
|
|
|
439 |
* Loads and instantiates models required by this controller.
|
|
|
440 |
* If Controller::persistModel; is true, controller will create cached model instances on first request,
|
|
|
441 |
* additional request will used cached models.
|
|
|
442 |
* If the model is non existent, it will throw a missing database table error, as Cake generates
|
|
|
443 |
* dynamic models for the time being.
|
|
|
444 |
*
|
|
|
445 |
* @param string $modelClass Name of model class to load
|
|
|
446 |
* @param mixed $id Initial ID the instanced model class should have
|
|
|
447 |
* @return mixed true when single model found and instance created error returned if models not found.
|
|
|
448 |
* @access public
|
|
|
449 |
*/
|
|
|
450 |
function loadModel($modelClass = null, $id = null) {
|
|
|
451 |
if ($modelClass === null) {
|
|
|
452 |
$modelClass = $this->modelClass;
|
|
|
453 |
}
|
|
|
454 |
$cached = false;
|
|
|
455 |
$object = null;
|
|
|
456 |
$plugin = null;
|
|
|
457 |
if ($this->uses === false) {
|
|
|
458 |
if ($this->plugin) {
|
|
|
459 |
$plugin = $this->plugin . '.';
|
|
|
460 |
}
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
if (strpos($modelClass, '.') !== false) {
|
|
|
464 |
list($plugin, $modelClass) = explode('.', $modelClass);
|
|
|
465 |
$plugin = $plugin . '.';
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
if ($this->persistModel === true) {
|
|
|
469 |
$cached = $this->_persist($modelClass, null, $object);
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
if (($cached === false)) {
|
|
|
473 |
$this->modelNames[] = $modelClass;
|
|
|
474 |
|
|
|
475 |
if (!PHP5) {
|
|
|
476 |
$this->{$modelClass} =& ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
|
|
|
477 |
} else {
|
|
|
478 |
$this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
if (!$this->{$modelClass}) {
|
|
|
482 |
return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base)));
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
if ($this->persistModel === true) {
|
|
|
486 |
$this->_persist($modelClass, true, $this->{$modelClass});
|
|
|
487 |
$registry = ClassRegistry::getInstance();
|
|
|
488 |
$this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry');
|
|
|
489 |
}
|
|
|
490 |
} else {
|
|
|
491 |
$this->_persist($modelClass . 'registry', true, $object, 'registry');
|
|
|
492 |
$this->_persist($modelClass, true, $object);
|
|
|
493 |
$this->modelNames[] = $modelClass;
|
|
|
494 |
}
|
|
|
495 |
}
|
|
|
496 |
/**
|
|
|
497 |
* Redirects to given $url, after turning off $this->autoRender.
|
|
|
498 |
* Script execution is halted after the redirect.
|
|
|
499 |
*
|
|
|
500 |
* @param mixed $url A string or array-based URL pointing to another location within the app, or an absolute URL
|
|
|
501 |
* @param integer $status Optional HTTP status code (eg: 404)
|
|
|
502 |
* @param boolean $exit If true, exit() will be called after the redirect
|
|
|
503 |
* @return mixed void if $exit = false. Terminates script if $exit = true
|
|
|
504 |
* @access public
|
|
|
505 |
* @link http://book.cakephp.org/view/425/redirect
|
|
|
506 |
*/
|
|
|
507 |
function redirect($url, $status = null, $exit = true) {
|
|
|
508 |
$this->autoRender = false;
|
|
|
509 |
|
|
|
510 |
if (is_array($status)) {
|
|
|
511 |
extract($status, EXTR_OVERWRITE);
|
|
|
512 |
}
|
|
|
513 |
$response = $this->Component->beforeRedirect($this, $url, $status, $exit);
|
|
|
514 |
|
|
|
515 |
if ($response === false) {
|
|
|
516 |
return;
|
|
|
517 |
}
|
|
|
518 |
if (is_array($response)) {
|
|
|
519 |
foreach ($response as $resp) {
|
|
|
520 |
if (is_array($resp) && isset($resp['url'])) {
|
|
|
521 |
extract($resp, EXTR_OVERWRITE);
|
|
|
522 |
} elseif ($resp !== null) {
|
|
|
523 |
$url = $resp;
|
|
|
524 |
}
|
|
|
525 |
}
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
if (function_exists('session_write_close')) {
|
|
|
529 |
session_write_close();
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
if (!empty($status)) {
|
|
|
533 |
$codes = array(
|
|
|
534 |
100 => 'Continue',
|
|
|
535 |
101 => 'Switching Protocols',
|
|
|
536 |
200 => 'OK',
|
|
|
537 |
201 => 'Created',
|
|
|
538 |
202 => 'Accepted',
|
|
|
539 |
203 => 'Non-Authoritative Information',
|
|
|
540 |
204 => 'No Content',
|
|
|
541 |
205 => 'Reset Content',
|
|
|
542 |
206 => 'Partial Content',
|
|
|
543 |
300 => 'Multiple Choices',
|
|
|
544 |
301 => 'Moved Permanently',
|
|
|
545 |
302 => 'Found',
|
|
|
546 |
303 => 'See Other',
|
|
|
547 |
304 => 'Not Modified',
|
|
|
548 |
305 => 'Use Proxy',
|
|
|
549 |
307 => 'Temporary Redirect',
|
|
|
550 |
400 => 'Bad Request',
|
|
|
551 |
401 => 'Unauthorized',
|
|
|
552 |
402 => 'Payment Required',
|
|
|
553 |
403 => 'Forbidden',
|
|
|
554 |
404 => 'Not Found',
|
|
|
555 |
405 => 'Method Not Allowed',
|
|
|
556 |
406 => 'Not Acceptable',
|
|
|
557 |
407 => 'Proxy Authentication Required',
|
|
|
558 |
408 => 'Request Time-out',
|
|
|
559 |
409 => 'Conflict',
|
|
|
560 |
410 => 'Gone',
|
|
|
561 |
411 => 'Length Required',
|
|
|
562 |
412 => 'Precondition Failed',
|
|
|
563 |
413 => 'Request Entity Too Large',
|
|
|
564 |
414 => 'Request-URI Too Large',
|
|
|
565 |
415 => 'Unsupported Media Type',
|
|
|
566 |
416 => 'Requested range not satisfiable',
|
|
|
567 |
417 => 'Expectation Failed',
|
|
|
568 |
500 => 'Internal Server Error',
|
|
|
569 |
501 => 'Not Implemented',
|
|
|
570 |
502 => 'Bad Gateway',
|
|
|
571 |
503 => 'Service Unavailable',
|
|
|
572 |
504 => 'Gateway Time-out'
|
|
|
573 |
);
|
|
|
574 |
if (is_string($status)) {
|
|
|
575 |
$codes = array_combine(array_values($codes), array_keys($codes));
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
if (isset($codes[$status])) {
|
|
|
579 |
$code = $msg = $codes[$status];
|
|
|
580 |
if (is_numeric($status)) {
|
|
|
581 |
$code = $status;
|
|
|
582 |
}
|
|
|
583 |
if (is_string($status)) {
|
|
|
584 |
$msg = $status;
|
|
|
585 |
}
|
|
|
586 |
$status = "HTTP/1.1 {$code} {$msg}";
|
|
|
587 |
} else {
|
|
|
588 |
$status = null;
|
|
|
589 |
}
|
|
|
590 |
}
|
|
|
591 |
|
|
|
592 |
if (!empty($status)) {
|
|
|
593 |
$this->header($status);
|
|
|
594 |
}
|
|
|
595 |
if ($url !== null) {
|
|
|
596 |
$this->header('Location: ' . Router::url($url, true));
|
|
|
597 |
}
|
|
|
598 |
|
|
|
599 |
if (!empty($status) && ($status >= 300 && $status < 400)) {
|
|
|
600 |
$this->header($status);
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
if ($exit) {
|
|
|
604 |
$this->_stop();
|
|
|
605 |
}
|
|
|
606 |
}
|
|
|
607 |
/**
|
|
|
608 |
* Convenience method for header()
|
|
|
609 |
*
|
|
|
610 |
* @param string $status
|
|
|
611 |
* @return void
|
|
|
612 |
* @access public
|
|
|
613 |
*/
|
|
|
614 |
function header($status) {
|
|
|
615 |
header($status);
|
|
|
616 |
}
|
|
|
617 |
/**
|
|
|
618 |
* Saves a variable for use inside a view template.
|
|
|
619 |
*
|
|
|
620 |
* @param mixed $one A string or an array of data.
|
|
|
621 |
* @param mixed $two Value in case $one is a string (which then works as the key).
|
|
|
622 |
* Unused if $one is an associative array, otherwise serves as the values to $one's keys.
|
|
|
623 |
* @return void
|
|
|
624 |
* @access public
|
|
|
625 |
* @link http://book.cakephp.org/view/427/set
|
|
|
626 |
*/
|
|
|
627 |
function set($one, $two = null) {
|
|
|
628 |
$data = array();
|
|
|
629 |
|
|
|
630 |
if (is_array($one)) {
|
|
|
631 |
if (is_array($two)) {
|
|
|
632 |
$data = array_combine($one, $two);
|
|
|
633 |
} else {
|
|
|
634 |
$data = $one;
|
|
|
635 |
}
|
|
|
636 |
} else {
|
|
|
637 |
$data = array($one => $two);
|
|
|
638 |
}
|
|
|
639 |
|
|
|
640 |
foreach ($data as $name => $value) {
|
|
|
641 |
if ($name === 'title') {
|
|
|
642 |
$this->pageTitle = $value;
|
|
|
643 |
} else {
|
|
|
644 |
if ($two === null && is_array($one)) {
|
|
|
645 |
$this->viewVars[Inflector::variable($name)] = $value;
|
|
|
646 |
} else {
|
|
|
647 |
$this->viewVars[$name] = $value;
|
|
|
648 |
}
|
|
|
649 |
}
|
|
|
650 |
}
|
|
|
651 |
}
|
|
|
652 |
/**
|
|
|
653 |
* Internally redirects one action to another. Examples:
|
|
|
654 |
*
|
|
|
655 |
* setAction('another_action');
|
|
|
656 |
* setAction('action_with_parameters', $parameter1);
|
|
|
657 |
*
|
|
|
658 |
* @param string $action The new action to be redirected to
|
|
|
659 |
* @param mixed Any other parameters passed to this method will be passed as
|
|
|
660 |
* parameters to the new action.
|
|
|
661 |
* @return mixed Returns the return value of the called action
|
|
|
662 |
* @access public
|
|
|
663 |
*/
|
|
|
664 |
function setAction($action) {
|
|
|
665 |
$this->action = $action;
|
|
|
666 |
$args = func_get_args();
|
|
|
667 |
unset($args[0]);
|
|
|
668 |
return call_user_func_array(array(&$this, $action), $args);
|
|
|
669 |
}
|
|
|
670 |
/**
|
|
|
671 |
* Controller callback to tie into Auth component. Only called when AuthComponent::authorize is set to 'controller'.
|
|
|
672 |
*
|
|
|
673 |
* @return bool true if authorized, false otherwise
|
|
|
674 |
* @access public
|
|
|
675 |
* @link http://book.cakephp.org/view/396/authorize
|
|
|
676 |
*/
|
|
|
677 |
function isAuthorized() {
|
|
|
678 |
trigger_error(sprintf(__('%s::isAuthorized() is not defined.', true), $this->name), E_USER_WARNING);
|
|
|
679 |
return false;
|
|
|
680 |
}
|
|
|
681 |
/**
|
|
|
682 |
* Returns number of errors in a submitted FORM.
|
|
|
683 |
*
|
|
|
684 |
* @return integer Number of errors
|
|
|
685 |
* @access public
|
|
|
686 |
*/
|
|
|
687 |
function validate() {
|
|
|
688 |
$args = func_get_args();
|
|
|
689 |
$errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
|
|
|
690 |
|
|
|
691 |
if ($errors === false) {
|
|
|
692 |
return 0;
|
|
|
693 |
}
|
|
|
694 |
return count($errors);
|
|
|
695 |
}
|
|
|
696 |
/**
|
|
|
697 |
* Validates models passed by parameters. Example:
|
|
|
698 |
*
|
|
|
699 |
* $errors = $this->validateErrors($this->Article, $this->User);
|
|
|
700 |
*
|
|
|
701 |
* @param mixed A list of models as a variable argument
|
|
|
702 |
* @return array Validation errors, or false if none
|
|
|
703 |
* @access public
|
|
|
704 |
*/
|
|
|
705 |
function validateErrors() {
|
|
|
706 |
$objects = func_get_args();
|
|
|
707 |
|
|
|
708 |
if (!count($objects)) {
|
|
|
709 |
return false;
|
|
|
710 |
}
|
|
|
711 |
|
|
|
712 |
$errors = array();
|
|
|
713 |
foreach ($objects as $object) {
|
|
|
714 |
$this->{$object->alias}->set($object->data);
|
|
|
715 |
$errors = array_merge($errors, $this->{$object->alias}->invalidFields());
|
|
|
716 |
}
|
|
|
717 |
|
|
|
718 |
return $this->validationErrors = (count($errors) ? $errors : false);
|
|
|
719 |
}
|
|
|
720 |
/**
|
|
|
721 |
* Instantiates the correct view class, hands it its data, and uses it to render the view output.
|
|
|
722 |
*
|
|
|
723 |
* @param string $action Action name to render
|
|
|
724 |
* @param string $layout Layout to use
|
|
|
725 |
* @param string $file File to use for rendering
|
|
|
726 |
* @return string Full output string of view contents
|
|
|
727 |
* @access public
|
|
|
728 |
* @link http://book.cakephp.org/view/428/render
|
|
|
729 |
*/
|
|
|
730 |
function render($action = null, $layout = null, $file = null) {
|
|
|
731 |
$this->beforeRender();
|
|
|
732 |
|
|
|
733 |
$viewClass = $this->view;
|
|
|
734 |
if ($this->view != 'View') {
|
|
|
735 |
if (strpos($viewClass, '.') !== false) {
|
|
|
736 |
list($plugin, $viewClass) = explode('.', $viewClass);
|
|
|
737 |
}
|
|
|
738 |
$viewClass = $viewClass . 'View';
|
|
|
739 |
App::import('View', $this->view);
|
|
|
740 |
}
|
|
|
741 |
|
|
|
742 |
$this->Component->beforeRender($this);
|
|
|
743 |
|
|
|
744 |
$this->params['models'] = $this->modelNames;
|
|
|
745 |
|
|
|
746 |
if (Configure::read() > 2) {
|
|
|
747 |
$this->set('cakeDebug', $this);
|
|
|
748 |
}
|
|
|
749 |
|
|
|
750 |
$View =& new $viewClass($this);
|
|
|
751 |
|
|
|
752 |
if (!empty($this->modelNames)) {
|
|
|
753 |
$models = array();
|
|
|
754 |
foreach ($this->modelNames as $currentModel) {
|
|
|
755 |
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
|
|
|
756 |
$models[] = Inflector::underscore($currentModel);
|
|
|
757 |
}
|
|
|
758 |
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model') && !empty($this->$currentModel->validationErrors)) {
|
|
|
759 |
$View->validationErrors[Inflector::camelize($currentModel)] =& $this->$currentModel->validationErrors;
|
|
|
760 |
}
|
|
|
761 |
}
|
|
|
762 |
$models = array_diff(ClassRegistry::keys(), $models);
|
|
|
763 |
foreach ($models as $currentModel) {
|
|
|
764 |
if (ClassRegistry::isKeySet($currentModel)) {
|
|
|
765 |
$currentObject =& ClassRegistry::getObject($currentModel);
|
|
|
766 |
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
|
|
767 |
$View->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
|
|
|
768 |
}
|
|
|
769 |
}
|
|
|
770 |
}
|
|
|
771 |
}
|
|
|
772 |
|
|
|
773 |
$this->autoRender = false;
|
|
|
774 |
$this->output .= $View->render($action, $layout, $file);
|
|
|
775 |
|
|
|
776 |
return $this->output;
|
|
|
777 |
}
|
|
|
778 |
/**
|
|
|
779 |
* Returns the referring URL for this request.
|
|
|
780 |
*
|
|
|
781 |
* @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
|
|
|
782 |
* @param boolean $local If true, restrict referring URLs to local server
|
|
|
783 |
* @return string Referring URL
|
|
|
784 |
* @access public
|
|
|
785 |
* @link http://book.cakephp.org/view/430/referer
|
|
|
786 |
*/
|
|
|
787 |
function referer($default = null, $local = false) {
|
|
|
788 |
$ref = env('HTTP_REFERER');
|
|
|
789 |
if (!empty($ref) && defined('FULL_BASE_URL')) {
|
|
|
790 |
$base = FULL_BASE_URL . $this->webroot;
|
|
|
791 |
if (strpos($ref, $base) === 0) {
|
|
|
792 |
$return = substr($ref, strlen($base));
|
|
|
793 |
if ($return[0] != '/') {
|
|
|
794 |
$return = '/'.$return;
|
|
|
795 |
}
|
|
|
796 |
return $return;
|
|
|
797 |
} elseif (!$local) {
|
|
|
798 |
return $ref;
|
|
|
799 |
}
|
|
|
800 |
}
|
|
|
801 |
|
|
|
802 |
if ($default != null) {
|
|
|
803 |
return $default;
|
|
|
804 |
}
|
|
|
805 |
return '/';
|
|
|
806 |
}
|
|
|
807 |
/**
|
|
|
808 |
* Forces the user's browser not to cache the results of the current request.
|
|
|
809 |
*
|
|
|
810 |
* @return void
|
|
|
811 |
* @access public
|
|
|
812 |
* @link http://book.cakephp.org/view/431/disableCache
|
|
|
813 |
*/
|
|
|
814 |
function disableCache() {
|
|
|
815 |
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
|
|
|
816 |
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
|
|
817 |
header("Cache-Control: no-store, no-cache, must-revalidate");
|
|
|
818 |
header("Cache-Control: post-check=0, pre-check=0", false);
|
|
|
819 |
header("Pragma: no-cache");
|
|
|
820 |
}
|
|
|
821 |
/**
|
|
|
822 |
* Shows a message to the user for $pause seconds, then redirects to $url.
|
|
|
823 |
* Uses flash.ctp as the default layout for the message.
|
|
|
824 |
* Does not work if the current debug level is higher than 0.
|
|
|
825 |
*
|
|
|
826 |
* @param string $message Message to display to the user
|
|
|
827 |
* @param string $url Relative URL to redirect to after the time expires
|
|
|
828 |
* @param integer $pause Time to show the message
|
|
|
829 |
* @return void Renders flash layout
|
|
|
830 |
* @access public
|
|
|
831 |
* @link http://book.cakephp.org/view/426/flash
|
|
|
832 |
*/
|
|
|
833 |
function flash($message, $url, $pause = 1) {
|
|
|
834 |
$this->autoRender = false;
|
|
|
835 |
$this->set('url', Router::url($url));
|
|
|
836 |
$this->set('message', $message);
|
|
|
837 |
$this->set('pause', $pause);
|
|
|
838 |
$this->set('page_title', $message);
|
|
|
839 |
$this->render(false, 'flash');
|
|
|
840 |
}
|
|
|
841 |
/**
|
|
|
842 |
* Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
|
|
|
843 |
*
|
|
|
844 |
* @param array $data POST'ed data organized by model and field
|
|
|
845 |
* @param mixed $op A string containing an SQL comparison operator, or an array matching operators to fields
|
|
|
846 |
* @param string $bool SQL boolean operator: AND, OR, XOR, etc.
|
|
|
847 |
* @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be included in the returned conditions
|
|
|
848 |
* @return array An array of model conditions
|
|
|
849 |
* @access public
|
|
|
850 |
* @link http://book.cakephp.org/view/432/postConditions
|
|
|
851 |
*/
|
|
|
852 |
function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
|
|
|
853 |
if (!is_array($data) || empty($data)) {
|
|
|
854 |
if (!empty($this->data)) {
|
|
|
855 |
$data = $this->data;
|
|
|
856 |
} else {
|
|
|
857 |
return null;
|
|
|
858 |
}
|
|
|
859 |
}
|
|
|
860 |
$cond = array();
|
|
|
861 |
|
|
|
862 |
if ($op === null) {
|
|
|
863 |
$op = '';
|
|
|
864 |
}
|
|
|
865 |
|
|
|
866 |
foreach ($data as $model => $fields) {
|
|
|
867 |
foreach ($fields as $field => $value) {
|
|
|
868 |
$key = $model.'.'.$field;
|
|
|
869 |
$fieldOp = $op;
|
|
|
870 |
if (is_array($op) && array_key_exists($key, $op)) {
|
|
|
871 |
$fieldOp = $op[$key];
|
|
|
872 |
} elseif (is_array($op) && array_key_exists($field, $op)) {
|
|
|
873 |
$fieldOp = $op[$field];
|
|
|
874 |
} elseif (is_array($op)) {
|
|
|
875 |
$fieldOp = false;
|
|
|
876 |
}
|
|
|
877 |
if ($exclusive && $fieldOp === false) {
|
|
|
878 |
continue;
|
|
|
879 |
}
|
|
|
880 |
$fieldOp = strtoupper(trim($fieldOp));
|
|
|
881 |
if ($fieldOp === 'LIKE') {
|
|
|
882 |
$key = $key.' LIKE';
|
|
|
883 |
$value = '%'.$value.'%';
|
|
|
884 |
} elseif ($fieldOp && $fieldOp != '=') {
|
|
|
885 |
$key = $key.' '.$fieldOp;
|
|
|
886 |
}
|
|
|
887 |
$cond[$key] = $value;
|
|
|
888 |
}
|
|
|
889 |
}
|
|
|
890 |
if ($bool != null && strtoupper($bool) != 'AND') {
|
|
|
891 |
$cond = array($bool => $cond);
|
|
|
892 |
}
|
|
|
893 |
return $cond;
|
|
|
894 |
}
|
|
|
895 |
/**
|
|
|
896 |
* Handles automatic pagination of model records.
|
|
|
897 |
*
|
|
|
898 |
* @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
|
|
|
899 |
* @param mixed $scope Conditions to use while paginating
|
|
|
900 |
* @param array $whitelist List of allowed options for paging
|
|
|
901 |
* @return array Model query results
|
|
|
902 |
* @access public
|
|
|
903 |
* @link http://book.cakephp.org/view/165/Controller-Setup
|
|
|
904 |
*/
|
|
|
905 |
function paginate($object = null, $scope = array(), $whitelist = array()) {
|
|
|
906 |
if (is_array($object)) {
|
|
|
907 |
$whitelist = $scope;
|
|
|
908 |
$scope = $object;
|
|
|
909 |
$object = null;
|
|
|
910 |
}
|
|
|
911 |
$assoc = null;
|
|
|
912 |
|
|
|
913 |
if (is_string($object)) {
|
|
|
914 |
$assoc = null;
|
|
|
915 |
|
|
|
916 |
if (strpos($object, '.') !== false) {
|
|
|
917 |
list($object, $assoc) = explode('.', $object);
|
|
|
918 |
}
|
|
|
919 |
|
|
|
920 |
if ($assoc && isset($this->{$object}->{$assoc})) {
|
|
|
921 |
$object = $this->{$object}->{$assoc};
|
|
|
922 |
} elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) {
|
|
|
923 |
$object = $this->{$this->modelClass}->{$assoc};
|
|
|
924 |
} elseif (isset($this->{$object})) {
|
|
|
925 |
$object = $this->{$object};
|
|
|
926 |
} elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) {
|
|
|
927 |
$object = $this->{$this->modelClass}->{$object};
|
|
|
928 |
}
|
|
|
929 |
} elseif (empty($object) || $object === null) {
|
|
|
930 |
if (isset($this->{$this->modelClass})) {
|
|
|
931 |
$object = $this->{$this->modelClass};
|
|
|
932 |
} else {
|
|
|
933 |
$className = null;
|
|
|
934 |
$name = $this->uses[0];
|
|
|
935 |
if (strpos($this->uses[0], '.') !== false) {
|
|
|
936 |
list($name, $className) = explode('.', $this->uses[0]);
|
|
|
937 |
}
|
|
|
938 |
if ($className) {
|
|
|
939 |
$object = $this->{$className};
|
|
|
940 |
} else {
|
|
|
941 |
$object = $this->{$name};
|
|
|
942 |
}
|
|
|
943 |
}
|
|
|
944 |
}
|
|
|
945 |
|
|
|
946 |
if (!is_object($object)) {
|
|
|
947 |
trigger_error(sprintf(__('Controller::paginate() - can\'t find model %1$s in controller %2$sController', true), $object, $this->name), E_USER_WARNING);
|
|
|
948 |
return array();
|
|
|
949 |
}
|
|
|
950 |
$options = array_merge($this->params, $this->params['url'], $this->passedArgs);
|
|
|
951 |
|
|
|
952 |
if (isset($this->paginate[$object->alias])) {
|
|
|
953 |
$defaults = $this->paginate[$object->alias];
|
|
|
954 |
} else {
|
|
|
955 |
$defaults = $this->paginate;
|
|
|
956 |
}
|
|
|
957 |
|
|
|
958 |
if (isset($options['show'])) {
|
|
|
959 |
$options['limit'] = $options['show'];
|
|
|
960 |
}
|
|
|
961 |
|
|
|
962 |
if (isset($options['sort'])) {
|
|
|
963 |
$direction = null;
|
|
|
964 |
if (isset($options['direction'])) {
|
|
|
965 |
$direction = strtolower($options['direction']);
|
|
|
966 |
}
|
|
|
967 |
if ($direction != 'asc' && $direction != 'desc') {
|
|
|
968 |
$direction = 'asc';
|
|
|
969 |
}
|
|
|
970 |
$options['order'] = array($options['sort'] => $direction);
|
|
|
971 |
}
|
|
|
972 |
|
|
|
973 |
if (!empty($options['order']) && is_array($options['order'])) {
|
|
|
974 |
$alias = $object->alias ;
|
|
|
975 |
$key = $field = key($options['order']);
|
|
|
976 |
|
|
|
977 |
if (strpos($key, '.') !== false) {
|
|
|
978 |
list($alias, $field) = explode('.', $key);
|
|
|
979 |
}
|
|
|
980 |
$value = $options['order'][$key];
|
|
|
981 |
unset($options['order'][$key]);
|
|
|
982 |
|
|
|
983 |
if (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
|
|
|
984 |
$options['order'][$alias . '.' . $field] = $value;
|
|
|
985 |
} elseif ($object->hasField($field)) {
|
|
|
986 |
$options['order'][$alias . '.' . $field] = $value;
|
|
|
987 |
}
|
|
|
988 |
}
|
|
|
989 |
$vars = array('fields', 'order', 'limit', 'page', 'recursive');
|
|
|
990 |
$keys = array_keys($options);
|
|
|
991 |
$count = count($keys);
|
|
|
992 |
|
|
|
993 |
for ($i = 0; $i < $count; $i++) {
|
|
|
994 |
if (!in_array($keys[$i], $vars, true)) {
|
|
|
995 |
unset($options[$keys[$i]]);
|
|
|
996 |
}
|
|
|
997 |
if (empty($whitelist) && ($keys[$i] === 'fields' || $keys[$i] === 'recursive')) {
|
|
|
998 |
unset($options[$keys[$i]]);
|
|
|
999 |
} elseif (!empty($whitelist) && !in_array($keys[$i], $whitelist)) {
|
|
|
1000 |
unset($options[$keys[$i]]);
|
|
|
1001 |
}
|
|
|
1002 |
}
|
|
|
1003 |
$conditions = $fields = $order = $limit = $page = $recursive = null;
|
|
|
1004 |
|
|
|
1005 |
if (!isset($defaults['conditions'])) {
|
|
|
1006 |
$defaults['conditions'] = array();
|
|
|
1007 |
}
|
|
|
1008 |
extract($options = array_merge(array('page' => 1, 'limit' => 20), $defaults, $options));
|
|
|
1009 |
|
|
|
1010 |
if (is_array($scope) && !empty($scope)) {
|
|
|
1011 |
$conditions = array_merge($conditions, $scope);
|
|
|
1012 |
} elseif (is_string($scope)) {
|
|
|
1013 |
$conditions = array($conditions, $scope);
|
|
|
1014 |
}
|
|
|
1015 |
if ($recursive === null) {
|
|
|
1016 |
$recursive = $object->recursive;
|
|
|
1017 |
}
|
|
|
1018 |
$type = 'all';
|
|
|
1019 |
|
|
|
1020 |
if (isset($defaults[0])) {
|
|
|
1021 |
$type = array_shift($defaults);
|
|
|
1022 |
}
|
|
|
1023 |
$extra = array_diff_key($defaults, compact(
|
|
|
1024 |
'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
|
|
|
1025 |
));
|
|
|
1026 |
if ($type !== 'all') {
|
|
|
1027 |
$extra['type'] = $type;
|
|
|
1028 |
}
|
|
|
1029 |
|
|
|
1030 |
if (method_exists($object, 'paginateCount')) {
|
|
|
1031 |
$count = $object->paginateCount($conditions, $recursive, $extra);
|
|
|
1032 |
} else {
|
|
|
1033 |
$parameters = compact('conditions');
|
|
|
1034 |
if ($recursive != $object->recursive) {
|
|
|
1035 |
$parameters['recursive'] = $recursive;
|
|
|
1036 |
}
|
|
|
1037 |
$count = $object->find('count', array_merge($parameters, $extra));
|
|
|
1038 |
}
|
|
|
1039 |
$pageCount = intval(ceil($count / $limit));
|
|
|
1040 |
|
|
|
1041 |
if ($page === 'last' || $page >= $pageCount) {
|
|
|
1042 |
$options['page'] = $page = $pageCount;
|
|
|
1043 |
} elseif (intval($page) < 1) {
|
|
|
1044 |
$options['page'] = $page = 1;
|
|
|
1045 |
}
|
|
|
1046 |
|
|
|
1047 |
if (method_exists($object, 'paginate')) {
|
|
|
1048 |
$results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra);
|
|
|
1049 |
} else {
|
|
|
1050 |
$parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
|
|
|
1051 |
if ($recursive != $object->recursive) {
|
|
|
1052 |
$parameters['recursive'] = $recursive;
|
|
|
1053 |
}
|
|
|
1054 |
$results = $object->find($type, array_merge($parameters, $extra));
|
|
|
1055 |
}
|
|
|
1056 |
$paging = array(
|
|
|
1057 |
'page' => $page,
|
|
|
1058 |
'current' => count($results),
|
|
|
1059 |
'count' => $count,
|
|
|
1060 |
'prevPage' => ($page > 1),
|
|
|
1061 |
'nextPage' => ($count > ($page * $limit)),
|
|
|
1062 |
'pageCount' => $pageCount,
|
|
|
1063 |
'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults),
|
|
|
1064 |
'options' => $options
|
|
|
1065 |
);
|
|
|
1066 |
$this->params['paging'][$object->alias] = $paging;
|
|
|
1067 |
|
|
|
1068 |
if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
|
|
|
1069 |
$this->helpers[] = 'Paginator';
|
|
|
1070 |
}
|
|
|
1071 |
return $results;
|
|
|
1072 |
}
|
|
|
1073 |
/**
|
|
|
1074 |
* Called before the controller action.
|
|
|
1075 |
*
|
|
|
1076 |
* @access public
|
|
|
1077 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1078 |
*/
|
|
|
1079 |
function beforeFilter() {
|
|
|
1080 |
}
|
|
|
1081 |
/**
|
|
|
1082 |
* Called after the controller action is run, but before the view is rendered.
|
|
|
1083 |
*
|
|
|
1084 |
* @access public
|
|
|
1085 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1086 |
*/
|
|
|
1087 |
function beforeRender() {
|
|
|
1088 |
}
|
|
|
1089 |
/**
|
|
|
1090 |
* Called after the controller action is run and rendered.
|
|
|
1091 |
*
|
|
|
1092 |
* @access public
|
|
|
1093 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1094 |
*/
|
|
|
1095 |
function afterFilter() {
|
|
|
1096 |
}
|
|
|
1097 |
/**
|
|
|
1098 |
* This method should be overridden in child classes.
|
|
|
1099 |
*
|
|
|
1100 |
* @param string $method name of method called example index, edit, etc.
|
|
|
1101 |
* @return boolean Success
|
|
|
1102 |
* @access protected
|
|
|
1103 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1104 |
*/
|
|
|
1105 |
function _beforeScaffold($method) {
|
|
|
1106 |
return true;
|
|
|
1107 |
}
|
|
|
1108 |
/**
|
|
|
1109 |
* This method should be overridden in child classes.
|
|
|
1110 |
*
|
|
|
1111 |
* @param string $method name of method called either edit or update.
|
|
|
1112 |
* @return boolean Success
|
|
|
1113 |
* @access protected
|
|
|
1114 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1115 |
*/
|
|
|
1116 |
function _afterScaffoldSave($method) {
|
|
|
1117 |
return true;
|
|
|
1118 |
}
|
|
|
1119 |
/**
|
|
|
1120 |
* This method should be overridden in child classes.
|
|
|
1121 |
*
|
|
|
1122 |
* @param string $method name of method called either edit or update.
|
|
|
1123 |
* @return boolean Success
|
|
|
1124 |
* @access protected
|
|
|
1125 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1126 |
*/
|
|
|
1127 |
function _afterScaffoldSaveError($method) {
|
|
|
1128 |
return true;
|
|
|
1129 |
}
|
|
|
1130 |
/**
|
|
|
1131 |
* This method should be overridden in child classes.
|
|
|
1132 |
* If not it will render a scaffold error.
|
|
|
1133 |
* Method MUST return true in child classes
|
|
|
1134 |
*
|
|
|
1135 |
* @param string $method name of method called example index, edit, etc.
|
|
|
1136 |
* @return boolean Success
|
|
|
1137 |
* @access protected
|
|
|
1138 |
* @link http://book.cakephp.org/view/60/Callbacks
|
|
|
1139 |
*/
|
|
|
1140 |
function _scaffoldError($method) {
|
|
|
1141 |
return false;
|
|
|
1142 |
}
|
|
|
1143 |
}
|
|
|
1144 |
?>
|