00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 App::import('Core', array('Component', 'View'));
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 class Controller extends Object {
00042
00043
00044
00045
00046
00047
00048
00049 var $name = null;
00050
00051
00052
00053
00054
00055
00056 var $here = null;
00057
00058
00059
00060
00061
00062
00063 var $webroot = null;
00064
00065
00066
00067
00068
00069
00070 var $action = null;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 var $uses = false;
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 var $helpers = array('Html', 'Form');
00092
00093
00094
00095
00096
00097
00098
00099
00100 var $params = array();
00101
00102
00103
00104
00105
00106
00107
00108 var $data = array();
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 var $paginate = array('limit' => 20, 'page' => 1);
00127
00128
00129
00130
00131
00132
00133 var $viewPath = null;
00134
00135
00136
00137
00138
00139
00140 var $layoutPath = null;
00141
00142
00143
00144
00145
00146
00147 var $viewVars = array();
00148
00149
00150
00151
00152
00153
00154
00155
00156 var $pageTitle = false;
00157
00158
00159
00160
00161
00162
00163 var $modelNames = array();
00164
00165
00166
00167
00168
00169
00170 var $base = null;
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180 var $layout = 'default';
00181
00182
00183
00184
00185
00186
00187
00188 var $autoRender = true;
00189
00190
00191
00192
00193
00194
00195 var $autoLayout = true;
00196
00197
00198
00199
00200
00201
00202 var $Component = null;
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 var $components = array();
00214
00215
00216
00217
00218
00219
00220 var $view = 'View';
00221
00222
00223
00224
00225
00226
00227 var $ext = '.ctp';
00228
00229
00230
00231
00232
00233
00234
00235
00236 var $output = null;
00237
00238
00239
00240
00241
00242
00243 var $plugin = null;
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 var $cacheAction = false;
00262
00263
00264
00265
00266
00267
00268
00269
00270 var $persistModel = false;
00271
00272
00273
00274
00275
00276
00277 var $passedArgs = array();
00278
00279
00280
00281
00282
00283
00284
00285 var $scaffold = false;
00286
00287
00288
00289
00290
00291
00292
00293 var $methods = array();
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 var $modelClass = null;
00304
00305
00306
00307
00308
00309
00310
00311
00312 var $modelKey = null;
00313
00314
00315
00316
00317
00318
00319 var $validationErrors = null;
00320
00321
00322
00323
00324 function __construct() {
00325 if ($this->name === null) {
00326 $r = null;
00327 if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
00328 die (__("Controller::__construct() : Can not get or parse my own class name, exiting."));
00329 }
00330 $this->name = $r[1];
00331 }
00332
00333 if ($this->viewPath == null) {
00334 $this->viewPath = Inflector::underscore($this->name);
00335 }
00336 $this->modelClass = Inflector::classify($this->name);
00337 $this->modelKey = Inflector::underscore($this->modelClass);
00338 $this->Component =& new Component();
00339
00340 $childMethods = get_class_methods($this);
00341 $parentMethods = get_class_methods('Controller');
00342
00343 foreach ($childMethods as $key => $value) {
00344 $childMethods[$key] = strtolower($value);
00345 }
00346
00347 foreach ($parentMethods as $key => $value) {
00348 $parentMethods[$key] = strtolower($value);
00349 }
00350 $this->methods = array_diff($childMethods, $parentMethods);
00351 parent::__construct();
00352 }
00353
00354
00355
00356
00357
00358
00359 function __mergeVars() {
00360 $pluginName = Inflector::camelize($this->plugin);
00361 $pluginController = $pluginName . 'AppController';
00362
00363 if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
00364 $appVars = get_class_vars('AppController');
00365 $uses = $appVars['uses'];
00366 $merge = array('components', 'helpers');
00367 $plugin = null;
00368
00369 if (!empty($this->plugin)) {
00370 $plugin = $pluginName . '.';
00371 if (!is_subclass_of($this, $pluginController)) {
00372 $pluginController = null;
00373 }
00374 } else {
00375 $pluginController = null;
00376 }
00377
00378 if ($uses == $this->uses && !empty($this->uses)) {
00379 if (!in_array($plugin . $this->modelClass, $this->uses)) {
00380 array_unshift($this->uses, $plugin . $this->modelClass);
00381 } elseif ($this->uses[0] !== $plugin . $this->modelClass) {
00382 $this->uses = array_flip($this->uses);
00383 unset($this->uses[$plugin . $this->modelClass]);
00384 $this->uses = array_flip($this->uses);
00385 array_unshift($this->uses, $plugin . $this->modelClass);
00386 }
00387 } elseif ($this->uses !== null || $this->uses !== false) {
00388 $merge[] = 'uses';
00389 }
00390
00391 foreach ($merge as $var) {
00392 if (!empty($appVars[$var]) && is_array($this->{$var})) {
00393 if ($var === 'components') {
00394 $normal = Set::normalize($this->{$var});
00395 $app = Set::normalize($appVars[$var]);
00396 if ($app !== $normal) {
00397 $this->{$var} = Set::merge($app, $normal);
00398 }
00399 } else {
00400 $this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
00401 }
00402 }
00403 }
00404 }
00405
00406 if ($pluginController && $pluginName != null) {
00407 $appVars = get_class_vars($pluginController);
00408 $uses = $appVars['uses'];
00409 $merge = array('components', 'helpers');
00410
00411 if ($this->uses !== null || $this->uses !== false) {
00412 $merge[] = 'uses';
00413 }
00414
00415 foreach ($merge as $var) {
00416 if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
00417 if ($var === 'components') {
00418 $normal = Set::normalize($this->{$var});
00419 $app = Set::normalize($appVars[$var]);
00420 if ($app !== $normal) {
00421 $this->{$var} = Set::merge($app, $normal);
00422 }
00423 } else {
00424 $this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
00425 }
00426 }
00427 }
00428 }
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 function constructClasses() {
00441 $this->__mergeVars();
00442 $this->Component->init($this);
00443
00444 if ($this->uses !== null || ($this->uses !== array())) {
00445 if (empty($this->passedArgs) || !isset($this->passedArgs['0'])) {
00446 $id = false;
00447 } else {
00448 $id = $this->passedArgs['0'];
00449 }
00450
00451 if ($this->uses === false) {
00452 $this->loadModel($this->modelClass, $id);
00453 } elseif ($this->uses) {
00454 $uses = is_array($this->uses) ? $this->uses : array($this->uses);
00455 $modelClassName = $uses[0];
00456 if (strpos($uses[0], '.') !== false) {
00457 list($plugin, $modelClassName) = explode('.', $uses[0]);
00458 }
00459 $this->modelClass = $modelClassName;
00460 foreach ($uses as $modelClass) {
00461 $this->loadModel($modelClass);
00462 }
00463 }
00464 }
00465 return true;
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 function loadModel($modelClass = null, $id = null) {
00480 if ($modelClass === null) {
00481 $modelClass = $this->modelClass;
00482 }
00483 $cached = false;
00484 $object = null;
00485 $plugin = null;
00486 if ($this->uses === false) {
00487 if ($this->plugin) {
00488 $plugin = $this->plugin . '.';
00489 }
00490 }
00491
00492 if (strpos($modelClass, '.') !== false) {
00493 list($plugin, $modelClass) = explode('.', $modelClass);
00494 $plugin = $plugin . '.';
00495 }
00496
00497 if ($this->persistModel === true) {
00498 $cached = $this->_persist($modelClass, null, $object);
00499 }
00500
00501 if (($cached === false)) {
00502 $this->modelNames[] = $modelClass;
00503
00504 if (!PHP5) {
00505 $this->{$modelClass} =& ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
00506 } else {
00507 $this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
00508 }
00509
00510 if (!$this->{$modelClass}) {
00511 return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base)));
00512 }
00513
00514 if ($this->persistModel === true) {
00515 $this->_persist($modelClass, true, $this->{$modelClass});
00516 $registry = ClassRegistry::getInstance();
00517 $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry');
00518 }
00519 } else {
00520 $this->_persist($modelClass . 'registry', true, $object, 'registry');
00521 $this->_persist($modelClass, true, $object);
00522 $this->modelNames[] = $modelClass;
00523 }
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 function redirect($url, $status = null, $exit = true) {
00537 $this->autoRender = false;
00538
00539 if (is_array($status)) {
00540 extract($status, EXTR_OVERWRITE);
00541 }
00542 $response = $this->Component->beforeRedirect($this, $url, $status, $exit);
00543
00544 if ($response === false) {
00545 return;
00546 }
00547 if (is_array($response)) {
00548 foreach ($response as $resp) {
00549 if (is_array($resp) && isset($resp['url'])) {
00550 extract($resp, EXTR_OVERWRITE);
00551 } elseif ($resp !== null) {
00552 $url = $resp;
00553 }
00554 }
00555 }
00556
00557 if (function_exists('session_write_close')) {
00558 session_write_close();
00559 }
00560
00561 if (!empty($status)) {
00562 $codes = array(
00563 100 => 'Continue',
00564 101 => 'Switching Protocols',
00565 200 => 'OK',
00566 201 => 'Created',
00567 202 => 'Accepted',
00568 203 => 'Non-Authoritative Information',
00569 204 => 'No Content',
00570 205 => 'Reset Content',
00571 206 => 'Partial Content',
00572 300 => 'Multiple Choices',
00573 301 => 'Moved Permanently',
00574 302 => 'Found',
00575 303 => 'See Other',
00576 304 => 'Not Modified',
00577 305 => 'Use Proxy',
00578 307 => 'Temporary Redirect',
00579 400 => 'Bad Request',
00580 401 => 'Unauthorized',
00581 402 => 'Payment Required',
00582 403 => 'Forbidden',
00583 404 => 'Not Found',
00584 405 => 'Method Not Allowed',
00585 406 => 'Not Acceptable',
00586 407 => 'Proxy Authentication Required',
00587 408 => 'Request Time-out',
00588 409 => 'Conflict',
00589 410 => 'Gone',
00590 411 => 'Length Required',
00591 412 => 'Precondition Failed',
00592 413 => 'Request Entity Too Large',
00593 414 => 'Request-URI Too Large',
00594 415 => 'Unsupported Media Type',
00595 416 => 'Requested range not satisfiable',
00596 417 => 'Expectation Failed',
00597 500 => 'Internal Server Error',
00598 501 => 'Not Implemented',
00599 502 => 'Bad Gateway',
00600 503 => 'Service Unavailable',
00601 504 => 'Gateway Time-out'
00602 );
00603 if (is_string($status)) {
00604 $codes = array_combine(array_values($codes), array_keys($codes));
00605 }
00606
00607 if (isset($codes[$status])) {
00608 $code = $msg = $codes[$status];
00609 if (is_numeric($status)) {
00610 $code = $status;
00611 }
00612 if (is_string($status)) {
00613 $msg = $status;
00614 }
00615 $status = "HTTP/1.1 {$code} {$msg}";
00616 } else {
00617 $status = null;
00618 }
00619 }
00620
00621 if (!empty($status)) {
00622 $this->header($status);
00623 }
00624 if ($url !== null) {
00625 $this->header('Location: ' . Router::url($url, true));
00626 }
00627
00628 if (!empty($status) && ($status >= 300 && $status < 400)) {
00629 $this->header($status);
00630 }
00631
00632 if ($exit) {
00633 $this->_stop();
00634 }
00635 }
00636
00637
00638
00639
00640
00641
00642
00643 function header($status) {
00644 header($status);
00645 }
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 function set($one, $two = null) {
00657 $data = array();
00658
00659 if (is_array($one)) {
00660 if (is_array($two)) {
00661 $data = array_combine($one, $two);
00662 } else {
00663 $data = $one;
00664 }
00665 } else {
00666 $data = array($one => $two);
00667 }
00668
00669 foreach ($data as $name => $value) {
00670 if ($name === 'title') {
00671 $this->pageTitle = $value;
00672 } else {
00673 if ($two === null && is_array($one)) {
00674 $this->viewVars[Inflector::variable($name)] = $value;
00675 } else {
00676 $this->viewVars[$name] = $value;
00677 }
00678 }
00679 }
00680 }
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 function setAction($action) {
00694 $this->action = $action;
00695 $args = func_get_args();
00696 unset($args[0]);
00697 return call_user_func_array(array(&$this, $action), $args);
00698 }
00699
00700
00701
00702
00703
00704
00705
00706 function isAuthorized() {
00707 trigger_error(sprintf(__('%s::isAuthorized() is not defined.', true), $this->name), E_USER_WARNING);
00708 return false;
00709 }
00710
00711
00712
00713
00714
00715
00716 function validate() {
00717 $args = func_get_args();
00718 $errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
00719
00720 if ($errors === false) {
00721 return 0;
00722 }
00723 return count($errors);
00724 }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 function validateErrors() {
00735 $objects = func_get_args();
00736
00737 if (!count($objects)) {
00738 return false;
00739 }
00740
00741 $errors = array();
00742 foreach ($objects as $object) {
00743 $this->{$object->alias}->set($object->data);
00744 $errors = array_merge($errors, $this->{$object->alias}->invalidFields());
00745 }
00746
00747 return $this->validationErrors = (count($errors) ? $errors : false);
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759 function render($action = null, $layout = null, $file = null) {
00760 $this->beforeRender();
00761
00762 $viewClass = $this->view;
00763 if ($this->view != 'View') {
00764 if (strpos($viewClass, '.') !== false) {
00765 list($plugin, $viewClass) = explode('.', $viewClass);
00766 }
00767 $viewClass = $viewClass . 'View';
00768 App::import('View', $this->view);
00769 }
00770
00771 $this->Component->beforeRender($this);
00772
00773 $this->params['models'] = $this->modelNames;
00774
00775 if (Configure::read() > 2) {
00776 $this->set('cakeDebug', $this);
00777 }
00778
00779 $View =& new $viewClass($this);
00780
00781 if (!empty($this->modelNames)) {
00782 $models = array();
00783 foreach ($this->modelNames as $currentModel) {
00784 if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
00785 $models[] = Inflector::underscore($currentModel);
00786 }
00787 if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model') && !empty($this->$currentModel->validationErrors)) {
00788 $View->validationErrors[Inflector::camelize($currentModel)] =& $this->$currentModel->validationErrors;
00789 }
00790 }
00791 $models = array_diff(ClassRegistry::keys(), $models);
00792 foreach ($models as $currentModel) {
00793 if (ClassRegistry::isKeySet($currentModel)) {
00794 $currentObject =& ClassRegistry::getObject($currentModel);
00795 if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
00796 $View->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
00797 }
00798 }
00799 }
00800 }
00801
00802 $this->autoRender = false;
00803 $this->output .= $View->render($action, $layout, $file);
00804
00805 return $this->output;
00806 }
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 function referer($default = null, $local = false) {
00817 $ref = env('HTTP_REFERER');
00818 if (!empty($ref) && defined('FULL_BASE_URL')) {
00819 $base = FULL_BASE_URL . $this->webroot;
00820 if (strpos($ref, $base) === 0) {
00821 $return = substr($ref, strlen($base));
00822 if ($return[0] != '/') {
00823 $return = '/'.$return;
00824 }
00825 return $return;
00826 } elseif (!$local) {
00827 return $ref;
00828 }
00829 }
00830
00831 if ($default != null) {
00832 return $default;
00833 }
00834 return '/';
00835 }
00836
00837
00838
00839
00840
00841
00842
00843 function disableCache() {
00844 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00845 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00846 header("Cache-Control: no-store, no-cache, must-revalidate");
00847 header("Cache-Control: post-check=0, pre-check=0", false);
00848 header("Pragma: no-cache");
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862 function flash($message, $url, $pause = 1) {
00863 $this->autoRender = false;
00864 $this->set('url', Router::url($url));
00865 $this->set('message', $message);
00866 $this->set('pause', $pause);
00867 $this->set('page_title', $message);
00868 $this->render(false, 'flash');
00869 }
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881 function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
00882 if (!is_array($data) || empty($data)) {
00883 if (!empty($this->data)) {
00884 $data = $this->data;
00885 } else {
00886 return null;
00887 }
00888 }
00889 $cond = array();
00890
00891 if ($op === null) {
00892 $op = '';
00893 }
00894
00895 foreach ($data as $model => $fields) {
00896 foreach ($fields as $field => $value) {
00897 $key = $model.'.'.$field;
00898 $fieldOp = $op;
00899 if (is_array($op) && array_key_exists($key, $op)) {
00900 $fieldOp = $op[$key];
00901 } elseif (is_array($op) && array_key_exists($field, $op)) {
00902 $fieldOp = $op[$field];
00903 } elseif (is_array($op)) {
00904 $fieldOp = false;
00905 }
00906 if ($exclusive && $fieldOp === false) {
00907 continue;
00908 }
00909 $fieldOp = strtoupper(trim($fieldOp));
00910 if ($fieldOp === 'LIKE') {
00911 $key = $key.' LIKE';
00912 $value = '%'.$value.'%';
00913 } elseif ($fieldOp && $fieldOp != '=') {
00914 $key = $key.' '.$fieldOp;
00915 }
00916 $cond[$key] = $value;
00917 }
00918 }
00919 if ($bool != null && strtoupper($bool) != 'AND') {
00920 $cond = array($bool => $cond);
00921 }
00922 return $cond;
00923 }
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934 function paginate($object = null, $scope = array(), $whitelist = array()) {
00935 if (is_array($object)) {
00936 $whitelist = $scope;
00937 $scope = $object;
00938 $object = null;
00939 }
00940 $assoc = null;
00941
00942 if (is_string($object)) {
00943 $assoc = null;
00944
00945 if (strpos($object, '.') !== false) {
00946 list($object, $assoc) = explode('.', $object);
00947 }
00948
00949 if ($assoc && isset($this->{$object}->{$assoc})) {
00950 $object =& $this->{$object}->{$assoc};
00951 } elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) {
00952 $object =& $this->{$this->modelClass}->{$assoc};
00953 } elseif (isset($this->{$object})) {
00954 $object =& $this->{$object};
00955 } elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) {
00956 $object =& $this->{$this->modelClass}->{$object};
00957 }
00958 } elseif (empty($object) || $object === null) {
00959 if (isset($this->{$this->modelClass})) {
00960 $object =& $this->{$this->modelClass};
00961 } else {
00962 $className = null;
00963 $name = $this->uses[0];
00964 if (strpos($this->uses[0], '.') !== false) {
00965 list($name, $className) = explode('.', $this->uses[0]);
00966 }
00967 if ($className) {
00968 $object =& $this->{$className};
00969 } else {
00970 $object =& $this->{$name};
00971 }
00972 }
00973 }
00974
00975 if (!is_object($object)) {
00976 trigger_error(sprintf(__('Controller::paginate() - can\'t find model %1$s in controller %2$sController', true), $object, $this->name), E_USER_WARNING);
00977 return array();
00978 }
00979 $options = array_merge($this->params, $this->params['url'], $this->passedArgs);
00980
00981 if (isset($this->paginate[$object->alias])) {
00982 $defaults = $this->paginate[$object->alias];
00983 } else {
00984 $defaults = $this->paginate;
00985 }
00986
00987 if (isset($options['show'])) {
00988 $options['limit'] = $options['show'];
00989 }
00990
00991 if (isset($options['sort'])) {
00992 $direction = null;
00993 if (isset($options['direction'])) {
00994 $direction = strtolower($options['direction']);
00995 }
00996 if ($direction != 'asc' && $direction != 'desc') {
00997 $direction = 'asc';
00998 }
00999 $options['order'] = array($options['sort'] => $direction);
01000 }
01001
01002 if (!empty($options['order']) && is_array($options['order'])) {
01003 $alias = $object->alias ;
01004 $key = $field = key($options['order']);
01005
01006 if (strpos($key, '.') !== false) {
01007 list($alias, $field) = explode('.', $key);
01008 }
01009 $value = $options['order'][$key];
01010 unset($options['order'][$key]);
01011
01012 if (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
01013 $options['order'][$alias . '.' . $field] = $value;
01014 } elseif ($object->hasField($field)) {
01015 $options['order'][$alias . '.' . $field] = $value;
01016 }
01017 }
01018 $vars = array('fields', 'order', 'limit', 'page', 'recursive');
01019 $keys = array_keys($options);
01020 $count = count($keys);
01021
01022 for ($i = 0; $i < $count; $i++) {
01023 if (!in_array($keys[$i], $vars, true)) {
01024 unset($options[$keys[$i]]);
01025 }
01026 if (empty($whitelist) && ($keys[$i] === 'fields' || $keys[$i] === 'recursive')) {
01027 unset($options[$keys[$i]]);
01028 } elseif (!empty($whitelist) && !in_array($keys[$i], $whitelist)) {
01029 unset($options[$keys[$i]]);
01030 }
01031 }
01032 $conditions = $fields = $order = $limit = $page = $recursive = null;
01033
01034 if (!isset($defaults['conditions'])) {
01035 $defaults['conditions'] = array();
01036 }
01037
01038 $type = 'all';
01039
01040 if (isset($defaults[0])) {
01041 $type = $defaults[0];
01042 unset($defaults[0]);
01043 }
01044
01045 extract($options = array_merge(array('page' => 1, 'limit' => 20), $defaults, $options));
01046
01047 if (is_array($scope) && !empty($scope)) {
01048 $conditions = array_merge($conditions, $scope);
01049 } elseif (is_string($scope)) {
01050 $conditions = array($conditions, $scope);
01051 }
01052 if ($recursive === null) {
01053 $recursive = $object->recursive;
01054 }
01055
01056 $extra = array_diff_key($defaults, compact(
01057 'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
01058 ));
01059 if ($type !== 'all') {
01060 $extra['type'] = $type;
01061 }
01062
01063 if (method_exists($object, 'paginateCount')) {
01064 $count = $object->paginateCount($conditions, $recursive, $extra);
01065 } else {
01066 $parameters = compact('conditions');
01067 if ($recursive != $object->recursive) {
01068 $parameters['recursive'] = $recursive;
01069 }
01070 $count = $object->find('count', array_merge($parameters, $extra));
01071 }
01072 $pageCount = intval(ceil($count / $limit));
01073
01074 if ($page === 'last' || $page >= $pageCount) {
01075 $options['page'] = $page = $pageCount;
01076 } elseif (intval($page) < 1) {
01077 $options['page'] = $page = 1;
01078 }
01079 $page = $options['page'] = (integer)$page;
01080
01081 if (method_exists($object, 'paginate')) {
01082 $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra);
01083 } else {
01084 $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
01085 if ($recursive != $object->recursive) {
01086 $parameters['recursive'] = $recursive;
01087 }
01088 $results = $object->find($type, array_merge($parameters, $extra));
01089 }
01090 $paging = array(
01091 'page' => $page,
01092 'current' => count($results),
01093 'count' => $count,
01094 'prevPage' => ($page > 1),
01095 'nextPage' => ($count > ($page * $limit)),
01096 'pageCount' => $pageCount,
01097 'defaults' => array_merge(array('limit' => 20, 'step' => 1), $defaults),
01098 'options' => $options
01099 );
01100 $this->params['paging'][$object->alias] = $paging;
01101
01102 if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
01103 $this->helpers[] = 'Paginator';
01104 }
01105 return $results;
01106 }
01107
01108
01109
01110
01111
01112
01113 function beforeFilter() {
01114 }
01115
01116
01117
01118
01119
01120
01121 function beforeRender() {
01122 }
01123
01124
01125
01126
01127
01128
01129 function afterFilter() {
01130 }
01131
01132
01133
01134
01135
01136
01137
01138
01139 function _beforeScaffold($method) {
01140 return true;
01141 }
01142
01143
01144
01145
01146
01147
01148
01149
01150 function _afterScaffoldSave($method) {
01151 return true;
01152 }
01153
01154
01155
01156
01157
01158
01159
01160
01161 function _afterScaffoldSaveError($method) {
01162 return true;
01163 }
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174 function _scaffoldError($method) {
01175 return false;
01176 }
01177 }
01178 ?>