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
00029
00030
00031 App::import('Core', array('Helper', 'ClassRegistry'));
00032
00033
00034
00035
00036
00037
00038
00039
00040 class View extends Object {
00041
00042
00043
00044
00045
00046
00047 var $base = null;
00048
00049
00050
00051
00052
00053 var $here = null;
00054
00055
00056
00057
00058
00059
00060 var $plugin = null;
00061
00062
00063
00064
00065
00066
00067 var $name = null;
00068
00069
00070
00071
00072
00073
00074 var $action = null;
00075
00076
00077
00078
00079
00080 var $params = array();
00081
00082
00083
00084
00085
00086 var $passedArgs = array();
00087
00088
00089
00090
00091
00092 var $data = array();
00093
00094
00095
00096
00097
00098
00099 var $helpers = array('Html');
00100
00101
00102
00103
00104
00105 var $viewPath = null;
00106
00107
00108
00109
00110
00111
00112 var $viewVars = array();
00113
00114
00115
00116
00117
00118
00119 var $layout = 'default';
00120
00121
00122
00123
00124
00125 var $layoutPath = null;
00126
00127
00128
00129
00130
00131
00132 var $pageTitle = false;
00133
00134
00135
00136
00137
00138
00139 var $autoRender = true;
00140
00141
00142
00143
00144
00145
00146 var $autoLayout = true;
00147
00148
00149
00150
00151
00152 var $ext = '.ctp';
00153
00154
00155
00156
00157
00158 var $subDir = null;
00159
00160
00161
00162
00163
00164 var $themeWeb = null;
00165
00166
00167
00168
00169
00170
00171
00172 var $cacheAction = false;
00173
00174
00175
00176
00177
00178 var $validationErrors = array();
00179
00180
00181
00182
00183
00184 var $hasRendered = false;
00185
00186
00187
00188
00189
00190 var $loaded = array();
00191
00192
00193
00194
00195
00196 var $modelScope = false;
00197
00198
00199
00200
00201
00202 var $model = null;
00203
00204
00205
00206
00207
00208 var $association = null;
00209
00210
00211
00212
00213
00214 var $field = null;
00215
00216
00217
00218
00219
00220 var $fieldSuffix = null;
00221
00222
00223
00224
00225
00226 var $modelId = null;
00227
00228
00229
00230
00231
00232 var $uuids = array();
00233
00234
00235
00236
00237
00238 var $output = false;
00239
00240
00241
00242
00243
00244
00245 var $__passedVars = array('viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot', 'helpers', 'here', 'layout', 'name', 'pageTitle', 'layoutPath', 'viewPath', 'params', 'data', 'plugin', 'passedArgs', 'cacheAction');
00246
00247
00248
00249
00250
00251
00252 var $__scripts = array();
00253
00254
00255
00256
00257
00258 var $__paths = array();
00259
00260
00261
00262
00263
00264 function __construct(&$controller, $register = true) {
00265 if (is_object($controller)) {
00266 $count = count($this->__passedVars);
00267 for ($j = 0; $j < $count; $j++) {
00268 $var = $this->__passedVars[$j];
00269 $this->{$var} = $controller->{$var};
00270 }
00271 }
00272 parent::__construct();
00273
00274 if ($register) {
00275 ClassRegistry::addObject('view', $this);
00276 }
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 function element($name, $params = array(), $loadHelpers = false) {
00291 $file = $plugin = $key = null;
00292
00293 if (isset($params['plugin'])) {
00294 $plugin = $params['plugin'];
00295 }
00296
00297 if (isset($this->plugin) && !$plugin) {
00298 $plugin = $this->plugin;
00299 }
00300
00301 if (isset($params['cache'])) {
00302 $expires = '+1 day';
00303
00304 if (is_array($params['cache'])) {
00305 $expires = $params['cache']['time'];
00306 $key = Inflector::slug($params['cache']['key']);
00307 } elseif ($params['cache'] !== true) {
00308 $expires = $params['cache'];
00309 $key = implode('_', array_keys($params));
00310 }
00311
00312 if ($expires) {
00313 $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
00314 $cache = cache('views' . DS . $cacheFile, null, $expires);
00315
00316 if (is_string($cache)) {
00317 return $cache;
00318 }
00319 }
00320 }
00321 $paths = $this->_paths($plugin);
00322
00323 foreach ($paths as $path) {
00324 if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
00325 $file = $path . 'elements' . DS . $name . $this->ext;
00326 break;
00327 } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
00328 $file = $path . 'elements' . DS . $name . '.thtml';
00329 break;
00330 }
00331 }
00332
00333 if (is_file($file)) {
00334 $params = array_merge_recursive($params, $this->loaded);
00335 $element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
00336 if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
00337 cache('views' . DS . $cacheFile, $element, $expires);
00338 }
00339 return $element;
00340 }
00341 $file = $paths[0] . 'elements' . DS . $name . $this->ext;
00342
00343 if (Configure::read() > 0) {
00344 return "Not Found: " . $file;
00345 }
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 function render($action = null, $layout = null, $file = null) {
00357 if ($this->hasRendered) {
00358 return true;
00359 }
00360 $out = null;
00361
00362 if ($file != null) {
00363 $action = $file;
00364 }
00365
00366 if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
00367 if (substr($viewFileName, -3) === 'ctp' || substr($viewFileName, -5) === 'thtml') {
00368 $out = View::_render($viewFileName, $this->viewVars);
00369 } else {
00370 $out = $this->_render($viewFileName, $this->viewVars);
00371 }
00372 }
00373
00374 if ($layout === null) {
00375 $layout = $this->layout;
00376 }
00377
00378 if ($out !== false) {
00379 if ($layout && $this->autoLayout) {
00380 $out = $this->renderLayout($out, $layout);
00381 if (isset($this->loaded['cache']) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)) {
00382 $replace = array('<cake:nocache>', '</cake:nocache>');
00383 $out = str_replace($replace, '', $out);
00384 }
00385 }
00386 $this->hasRendered = true;
00387 } else {
00388 $out = $this->_render($viewFileName, $this->viewVars);
00389 trigger_error(sprintf(__("Error in view %s, got: <blockquote>%s</blockquote>", true), $viewFileName, $out), E_USER_ERROR);
00390 }
00391 return $out;
00392 }
00393
00394
00395
00396
00397
00398
00399 function renderLayout($content_for_layout, $layout = null) {
00400 $layout_fn = $this->_getLayoutFileName($layout);
00401 $debug = '';
00402
00403 if (isset($this->viewVars['cakeDebug']) && Configure::read() > 2) {
00404 $debug = View::element('dump', array('controller' => $this->viewVars['cakeDebug']), false);
00405 unset($this->viewVars['cakeDebug']);
00406 }
00407
00408 if ($this->pageTitle !== false) {
00409 $pageTitle = $this->pageTitle;
00410 } else {
00411 $pageTitle = Inflector::humanize($this->viewPath);
00412 }
00413 $data_for_layout = array_merge($this->viewVars,
00414 array(
00415 'title_for_layout' => $pageTitle,
00416 'content_for_layout' => $content_for_layout,
00417 'scripts_for_layout' => join("\n\t", $this->__scripts),
00418 'cakeDebug' => $debug
00419 )
00420 );
00421
00422 if (empty($this->loaded) && !empty($this->helpers)) {
00423 $loadHelpers = true;
00424 } else {
00425 $loadHelpers = false;
00426 $data_for_layout = array_merge($data_for_layout, $this->loaded);
00427 }
00428
00429 if (!empty($this->loaded)) {
00430 $helpers = array_keys($this->loaded);
00431 foreach ($helpers as $helperName) {
00432 $helper =& $this->loaded[$helperName];
00433 if (is_object($helper)) {
00434 if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) {
00435 $helper->beforeLayout();
00436 }
00437 }
00438 }
00439 }
00440
00441 if (substr($layout_fn, -3) === 'ctp' || substr($layout_fn, -5) === 'thtml') {
00442 $this->output = View::_render($layout_fn, $data_for_layout, $loadHelpers, true);
00443 } else {
00444 $this->output = $this->_render($layout_fn, $data_for_layout, $loadHelpers);
00445 }
00446
00447 if ($this->output === false) {
00448 $this->output = $this->_render($layout_fn, $data_for_layout);
00449 trigger_error(sprintf(__("Error in layout %s, got: <blockquote>%s</blockquote>", true), $layout_fn, $this->output), E_USER_ERROR);
00450 return false;
00451 }
00452
00453 if (!empty($this->loaded)) {
00454 $helpers = array_keys($this->loaded);
00455 foreach ($helpers as $helperName) {
00456 $helper =& $this->loaded[$helperName];
00457 if (is_object($helper)) {
00458 if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) {
00459 $helper->afterLayout();
00460 }
00461 }
00462 }
00463 }
00464 return $this->output;
00465 }
00466
00467
00468
00469
00470
00471
00472 function renderCache($filename, $timeStart) {
00473 ob_start();
00474 include ($filename);
00475
00476 if (Configure::read() > 0 && $this->layout != 'xml') {
00477 echo "<!-- Cached Render Time: " . round(getMicrotime() - $timeStart, 4) . "s -->";
00478 }
00479 $out = ob_get_clean();
00480
00481 if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
00482 if (time() >= $match['1']) {
00483 @unlink($filename);
00484 unset ($out);
00485 return false;
00486 } else {
00487 if ($this->layout === 'xml') {
00488 header('Content-type: text/xml');
00489 }
00490 $out = str_replace('<!--cachetime:'.$match['1'].'-->', '', $out);
00491 echo $out;
00492 return true;
00493 }
00494 }
00495 }
00496
00497
00498
00499
00500
00501
00502 function getVars() {
00503 return array_keys($this->viewVars);
00504 }
00505
00506
00507
00508
00509
00510
00511 function getVar($var) {
00512 if (!isset($this->viewVars[$var])) {
00513 return null;
00514 } else {
00515 return $this->viewVars[$var];
00516 }
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526 function addScript($name, $content = null) {
00527 if (empty($content)) {
00528 if (!in_array($name, array_values($this->__scripts))) {
00529 $this->__scripts[] = $name;
00530 }
00531 } else {
00532 $this->__scripts[$name] = $content;
00533 }
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543 function uuid($object, $url) {
00544 $c = 1;
00545 $url = Router::url($url);
00546 $hash = $object . substr(md5($object . $url), 0, 10);
00547 while (in_array($hash, $this->uuids)) {
00548 $hash = $object . substr(md5($object . $url . $c), 0, 10);
00549 $c++;
00550 }
00551 $this->uuids[] = $hash;
00552 return $hash;
00553 }
00554
00555
00556
00557
00558
00559 function entity() {
00560 return array_values(Set::filter(array(
00561 ife($this->association, $this->association, $this->model),
00562 $this->modelId, $this->field, $this->fieldSuffix
00563 )));
00564 }
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 function set($one, $two = null) {
00575 $data = null;
00576 if (is_array($one)) {
00577 if (is_array($two)) {
00578 $data = array_combine($one, $two);
00579 } else {
00580 $data = $one;
00581 }
00582 } else {
00583 $data = array($one => $two);
00584 }
00585
00586 if ($data == null) {
00587 return false;
00588 }
00589
00590 foreach ($data as $name => $value) {
00591 if ($name == 'title') {
00592 $this->pageTitle = $value;
00593 } else {
00594 $this->viewVars[$name] = $value;
00595 }
00596 }
00597 }
00598
00599
00600
00601
00602
00603
00604
00605 function error($code, $name, $message) {
00606 header ("HTTP/1.1 {$code} {$name}");
00607 print ($this->_render(
00608 $this->_getLayoutFileName('error'),
00609 array('code' => $code, 'name' => $name, 'message' => $message)
00610 ));
00611 }
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
00622 $loadedHelpers = array();
00623
00624 if ($this->helpers != false && $loadHelpers === true) {
00625 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
00626
00627 foreach (array_keys($loadedHelpers) as $helper) {
00628 $camelBackedHelper = Inflector::variable($helper);
00629 ${$camelBackedHelper} =& $loadedHelpers[$helper];
00630 $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
00631 }
00632
00633 foreach ($loadedHelpers as $helper) {
00634 if (is_object($helper)) {
00635 if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) {
00636 $helper->beforeRender();
00637 }
00638 }
00639 }
00640 }
00641
00642 extract($___dataForView, EXTR_SKIP);
00643 ob_start();
00644
00645 if (Configure::read() > 0) {
00646 include ($___viewFn);
00647 } else {
00648 @include ($___viewFn);
00649 }
00650
00651 if (!empty($loadedHelpers)) {
00652 foreach ($loadedHelpers as $helper) {
00653 if (is_object($helper)) {
00654 if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) {
00655 $helper->afterRender();
00656 }
00657 }
00658 }
00659 }
00660 $out = ob_get_clean();
00661
00662 if (isset($this->loaded['cache']) && (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)) {
00663 if (is_a($this->loaded['cache'], 'CacheHelper')) {
00664 $cache =& $this->loaded['cache'];
00665 $cache->base = $this->base;
00666 $cache->here = $this->here;
00667 $cache->helpers = $this->helpers;
00668 $cache->action = $this->action;
00669 $cache->controllerName = $this->name;
00670 $cache->layout = $this->layout;
00671 $cache->cacheAction = $this->cacheAction;
00672 $cache->cache($___viewFn, $out, $cached);
00673 }
00674 }
00675 return $out;
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685 function &_loadHelpers(&$loaded, $helpers, $parent = null) {
00686 if (empty($loaded)) {
00687 $helpers[] = 'Session';
00688 }
00689
00690 foreach ($helpers as $i => $helper) {
00691 $options = array();
00692
00693 if (!is_int($i)) {
00694 $options = $helper;
00695 $helper = $i;
00696 }
00697 $parts = preg_split('/\/|\./', $helper);
00698
00699 if (count($parts) === 1) {
00700 $plugin = $this->plugin;
00701 } else {
00702 $plugin = Inflector::underscore($parts['0']);
00703 $helper = $parts[count($parts) - 1];
00704 }
00705 $helperCn = $helper . 'Helper';
00706
00707 if (in_array($helper, array_keys($loaded)) !== true) {
00708 if (!class_exists($helperCn)) {
00709 if (is_null($plugin) || !App::import('Helper', $plugin . '.' . $helper)) {
00710 if (!App::import('Helper', $helper)) {
00711 $this->cakeError('missingHelperFile', array(array(
00712 'helper' => $helper,
00713 'file' => Inflector::underscore($helper) . '.php',
00714 'base' => $this->base
00715 )));
00716 return false;
00717 }
00718 }
00719 if (!class_exists($helperCn)) {
00720 $this->cakeError('missingHelperClass', array(array(
00721 'helper' => $helper,
00722 'file' => Inflector::underscore($helper) . '.php',
00723 'base' => $this->base
00724 )));
00725 return false;
00726 }
00727 }
00728 $loaded[$helper] =& new $helperCn($options);
00729 $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'themeWeb', 'plugin');
00730 $c = count($vars);
00731
00732 for ($j = 0; $j < $c; $j++) {
00733 $loaded[$helper]->{$vars[$j]} = $this->{$vars[$j]};
00734 }
00735
00736 if (!empty($this->validationErrors)) {
00737 $loaded[$helper]->validationErrors = $this->validationErrors;
00738 }
00739 if (is_array($loaded[$helper]->helpers) && !empty($loaded[$helper]->helpers)) {
00740 $loaded =& $this->_loadHelpers($loaded, $loaded[$helper]->helpers, $helper);
00741 }
00742 }
00743 if (isset($loaded[$parent])) {
00744 $loaded[$parent]->{$helper} =& $loaded[$helper];
00745 }
00746 }
00747 return $loaded;
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758 function _getViewFileName($name = null) {
00759 $subDir = null;
00760
00761 if (!is_null($this->subDir)) {
00762 $subDir = $this->subDir . DS;
00763 }
00764
00765 if ($name === null) {
00766 $name = $this->action;
00767 }
00768 $name = str_replace('/', DS, $name);
00769
00770 if (strpos($name, DS) === false && $name[0] !== '.') {
00771 $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
00772 } elseif (strpos($name, DS) !== false) {
00773 if ($name{0} === DS || $name{1} === ':') {
00774 if (is_file($name)) {
00775 return $name;
00776 }
00777 $name = trim($name, DS);
00778 } else if ($name[0] === '.') {
00779 $name = substr($name, 3);
00780 } else {
00781 $name = $this->viewPath . DS . $subDir . $name;
00782 }
00783 }
00784
00785 $paths = $this->_paths($this->plugin);
00786
00787 foreach ($paths as $path) {
00788 if (file_exists($path . $name . $this->ext)) {
00789 return $path . $name . $this->ext;
00790 } elseif (file_exists($path . $name . '.ctp')) {
00791 return $path . $name . '.ctp';
00792 } elseif (file_exists($path . $name . '.thtml')) {
00793 return $path . $name . '.thtml';
00794 }
00795 }
00796 $defaultPath = $paths[0];
00797
00798 if ($this->plugin) {
00799 $pluginPaths = Configure::read('pluginPaths');
00800 foreach ($paths as $path) {
00801 if (strpos($path, $pluginPaths[0]) === 0) {
00802 $defaultPath = $path;
00803 break;
00804 }
00805 }
00806 }
00807 return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
00808 }
00809
00810
00811
00812
00813
00814
00815
00816 function _getLayoutFileName($name = null) {
00817 if ($name === null) {
00818 $name = $this->layout;
00819 }
00820 $subDir = null;
00821
00822 if (!is_null($this->layoutPath)) {
00823 $subDir = $this->layoutPath . DS;
00824 }
00825 $paths = $this->_paths($this->plugin);
00826 $file = 'layouts' . DS . $subDir . $name;
00827
00828 foreach ($paths as $path) {
00829 if (file_exists($path . $file . $this->ext)) {
00830 return $path . $file . $this->ext;
00831 } elseif (file_exists($path . $file . '.ctp')) {
00832 return $path . $file . '.ctp';
00833 } elseif (file_exists($path . $file . '.thtml')) {
00834 return $path . $file . '.thtml';
00835 }
00836 }
00837 return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
00838 }
00839
00840
00841
00842
00843
00844
00845 function _missingView($file, $error = 'missingView') {
00846
00847 if ($error === 'missingView') {
00848 $this->cakeError('missingView', array(
00849 'className' => $this->name,
00850 'action' => $this->action,
00851 'file' => $file,
00852 'base' => $this->base
00853 ));
00854 return false;
00855 } elseif ($error === 'missingLayout') {
00856 $this->cakeError('missingLayout', array(
00857 'layout' => $this->layout,
00858 'file' => $file,
00859 'base' => $this->base
00860 ));
00861 return false;
00862 }
00863
00864 }
00865
00866
00867
00868
00869
00870
00871
00872 function _paths($plugin = null, $cached = true) {
00873 if ($plugin === null && $cached === true && !empty($this->__paths)) {
00874 return $this->__paths;
00875 }
00876 $paths = array();
00877 $viewPaths = Configure::read('viewPaths');
00878
00879 if ($plugin !== null) {
00880 $count = count($viewPaths);
00881 for ($i = 0; $i < $count; $i++) {
00882 $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
00883 }
00884 $pluginPaths = Configure::read('pluginPaths');
00885 $count = count($pluginPaths);
00886
00887 for ($i = 0; $i < $count; $i++) {
00888 $paths[] = $pluginPaths[$i] . $plugin . DS . 'views' . DS;
00889 }
00890 }
00891 $paths = array_merge($paths, $viewPaths);
00892
00893 if (empty($this->__paths)) {
00894 $this->__paths = $paths;
00895 }
00896 return $paths;
00897 }
00898
00899
00900
00901
00902 function renderElement($name, $params = array(), $loadHelpers = false) {
00903 return $this->element($name, $params, $loadHelpers);
00904 }
00905 }
00906 ?>