1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23:
24: App::import('Core', 'ClassRegistry');
25: App::import('View', 'Helper', false);
26:
27: 28: 29: 30: 31: 32: 33: 34:
35: class View extends Object {
36:
37: 38: 39: 40: 41: 42:
43: var $base = null;
44:
45: 46: 47: 48: 49:
50: var $here = null;
51:
52: 53: 54: 55: 56: 57:
58: var $plugin = null;
59:
60: 61: 62: 63: 64: 65:
66: var $name = null;
67:
68: 69: 70: 71: 72: 73:
74: var $action = null;
75:
76: 77: 78: 79: 80:
81: var $params = array();
82:
83: 84: 85: 86: 87:
88: var $passedArgs = array();
89:
90: 91: 92: 93: 94:
95: var $data = array();
96:
97: 98: 99: 100: 101: 102:
103: var $helpers = array('Html');
104:
105: 106: 107: 108: 109:
110: var $viewPath = null;
111:
112: 113: 114: 115: 116: 117:
118: var $viewVars = array();
119:
120: 121: 122: 123: 124: 125:
126: var $layout = 'default';
127:
128: 129: 130: 131: 132:
133: var $layoutPath = null;
134:
135: 136: 137: 138: 139: 140:
141: var $autoRender = true;
142:
143: 144: 145: 146: 147: 148:
149: var $autoLayout = true;
150:
151: 152: 153: 154: 155: 156:
157: var $ext = '.ctp';
158:
159: 160: 161: 162: 163: 164:
165: var $subDir = null;
166:
167: 168: 169: 170: 171: 172:
173: var $theme = null;
174:
175: 176: 177: 178: 179: 180: 181:
182: var $cacheAction = false;
183:
184: 185: 186: 187: 188: 189:
190: var $validationErrors = array();
191:
192: 193: 194: 195: 196: 197:
198: var $hasRendered = false;
199:
200: 201: 202: 203: 204: 205:
206: var $loaded = array();
207:
208: 209: 210: 211: 212: 213:
214: var $modelScope = false;
215:
216: 217: 218: 219: 220: 221:
222: var $model = null;
223:
224: 225: 226: 227: 228: 229:
230: var $association = null;
231:
232: 233: 234: 235: 236: 237:
238: var $field = null;
239:
240: 241: 242: 243: 244: 245:
246: var $fieldSuffix = null;
247:
248: 249: 250: 251: 252: 253:
254: var $modelId = null;
255:
256: 257: 258: 259: 260: 261:
262: var $uuids = array();
263:
264: 265: 266: 267: 268: 269:
270: var $output = false;
271:
272: 273: 274: 275: 276: 277:
278: var $__passedVars = array(
279: 'viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot',
280: 'helpers', 'here', 'layout', 'name', 'layoutPath', 'viewPath',
281: 'params', 'data', 'plugin', 'passedArgs', 'cacheAction'
282: );
283:
284: 285: 286: 287: 288: 289:
290: var $__scripts = array();
291:
292: 293: 294: 295: 296: 297:
298: var $__paths = array();
299:
300: 301: 302: 303: 304: 305: 306:
307: function __construct(&$controller, $register = true) {
308: if (is_object($controller)) {
309: $count = count($this->__passedVars);
310: for ($j = 0; $j < $count; $j++) {
311: $var = $this->__passedVars[$j];
312: $this->{$var} = $controller->{$var};
313: }
314: }
315: parent::__construct();
316:
317: if ($register) {
318: ClassRegistry::addObject('view', $this);
319: }
320: }
321:
322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342:
343: function element($name, $params = array(), $loadHelpers = false) {
344: $file = $plugin = $key = null;
345:
346: if (isset($params['plugin'])) {
347: $plugin = $params['plugin'];
348: }
349:
350: if (isset($this->plugin) && !$plugin) {
351: $plugin = $this->plugin;
352: }
353:
354: if (isset($params['cache'])) {
355: $expires = '+1 day';
356:
357: if (is_array($params['cache'])) {
358: $expires = $params['cache']['time'];
359: $key = Inflector::slug($params['cache']['key']);
360: } else {
361: $key = implode('_', array_keys($params));
362: if ($params['cache'] !== true) {
363: $expires = $params['cache'];
364: }
365: }
366:
367: if ($expires) {
368: $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
369: $cache = cache('views' . DS . $cacheFile, null, $expires);
370:
371: if (is_string($cache)) {
372: return $cache;
373: }
374: }
375: }
376: $paths = $this->_paths($plugin);
377: $exts = $this->_getExtensions();
378: foreach ($exts as $ext) {
379: foreach ($paths as $path) {
380: if (file_exists($path . 'elements' . DS . $name . $ext)) {
381: $file = $path . 'elements' . DS . $name . $ext;
382: break;
383: }
384: }
385: if ($file) {
386: break;
387: }
388: }
389:
390: if (is_file($file)) {
391: $vars = array_merge($this->viewVars, $params);
392: foreach ($this->loaded as $name => $helper) {
393: if (!isset($vars[$name])) {
394: $vars[$name] =& $this->loaded[$name];
395: }
396: }
397: $element = $this->_render($file, $vars, $loadHelpers);
398: if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
399: cache('views' . DS . $cacheFile, $element, $expires);
400: }
401: return $element;
402: }
403: $file = $paths[0] . 'elements' . DS . $name . $this->ext;
404:
405: if (Configure::read() > 0) {
406: return "Not Found: " . $file;
407: }
408: }
409:
410: 411: 412: 413: 414: 415: 416: 417: 418: 419:
420: function render($action = null, $layout = null, $file = null) {
421: if ($this->hasRendered) {
422: return true;
423: }
424: $out = null;
425:
426: if ($file != null) {
427: $action = $file;
428: }
429:
430: if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
431: $out = $this->_render($viewFileName, $this->viewVars);
432: }
433:
434: if ($layout === null) {
435: $layout = $this->layout;
436: }
437:
438: if ($out !== false) {
439: if ($layout && $this->autoLayout) {
440: $out = $this->renderLayout($out, $layout);
441: $isCached = (
442: isset($this->loaded['cache']) ||
443: Configure::read('Cache.check') === true
444: );
445:
446: if ($isCached) {
447: $replace = array('<cake:nocache>', '</cake:nocache>');
448: $out = str_replace($replace, '', $out);
449: }
450: }
451: $this->hasRendered = true;
452: } else {
453: $out = $this->_render($viewFileName, $this->viewVars);
454: trigger_error(sprintf(__("Error in view %s, got: <blockquote>%s</blockquote>", true), $viewFileName, $out), E_USER_ERROR);
455: }
456: return $out;
457: }
458:
459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470:
471: function renderLayout($content_for_layout, $layout = null) {
472: $layoutFileName = $this->_getLayoutFileName($layout);
473: if (empty($layoutFileName)) {
474: return $this->output;
475: }
476:
477: $dataForLayout = array_merge($this->viewVars, array(
478: 'content_for_layout' => $content_for_layout,
479: 'scripts_for_layout' => implode("\n\t", $this->__scripts),
480: ));
481:
482: if (!isset($dataForLayout['title_for_layout'])) {
483: $dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
484: }
485:
486: if (empty($this->loaded) && !empty($this->helpers)) {
487: $loadHelpers = true;
488: } else {
489: $loadHelpers = false;
490: $dataForLayout = array_merge($dataForLayout, $this->loaded);
491: }
492:
493: $this->_triggerHelpers('beforeLayout');
494: $this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true);
495:
496: if ($this->output === false) {
497: $this->output = $this->_render($layoutFileName, $dataForLayout);
498: trigger_error(sprintf(__("Error in layout %s, got: <blockquote>%s</blockquote>", true), $layoutFileName, $this->output), E_USER_ERROR);
499: return false;
500: }
501:
502: $this->_triggerHelpers('afterLayout');
503:
504: return $this->output;
505: }
506:
507: 508: 509: 510: 511: 512: 513: 514:
515: function _triggerHelpers($callback) {
516: if (empty($this->loaded)) {
517: return false;
518: }
519: $helpers = array_keys($this->loaded);
520: foreach ($helpers as $helperName) {
521: $helper =& $this->loaded[$helperName];
522: if (is_object($helper)) {
523: if (is_subclass_of($helper, 'Helper')) {
524: $helper->{$callback}();
525: }
526: }
527: }
528: }
529:
530: 531: 532: 533: 534: 535: 536: 537: 538:
539: function renderCache($filename, $timeStart) {
540: ob_start();
541: include ($filename);
542:
543: if (Configure::read() > 0 && $this->layout != 'xml') {
544: echo "<!-- Cached Render Time: " . round(getMicrotime() - $timeStart, 4) . "s -->";
545: }
546: $out = ob_get_clean();
547:
548: if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
549: if (time() >= $match['1']) {
550: @unlink($filename);
551: unset ($out);
552: return false;
553: } else {
554: if ($this->layout === 'xml') {
555: header('Content-type: text/xml');
556: }
557: $commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
558: echo substr($out, $commentLength);
559: return true;
560: }
561: }
562: }
563:
564: 565: 566: 567: 568: 569:
570: function getVars() {
571: return array_keys($this->viewVars);
572: }
573:
574: 575: 576: 577: 578: 579: 580:
581: function getVar($var) {
582: if (!isset($this->viewVars[$var])) {
583: return null;
584: } else {
585: return $this->viewVars[$var];
586: }
587: }
588:
589: 590: 591: 592: 593: 594: 595: 596: 597: 598:
599: function addScript($name, $content = null) {
600: if (empty($content)) {
601: if (!in_array($name, array_values($this->__scripts))) {
602: $this->__scripts[] = $name;
603: }
604: } else {
605: $this->__scripts[$name] = $content;
606: }
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616:
617: function uuid($object, $url) {
618: $c = 1;
619: $url = Router::url($url);
620: $hash = $object . substr(md5($object . $url), 0, 10);
621: while (in_array($hash, $this->uuids)) {
622: $hash = $object . substr(md5($object . $url . $c), 0, 10);
623: $c++;
624: }
625: $this->uuids[] = $hash;
626: return $hash;
627: }
628:
629: 630: 631: 632: 633: 634:
635: function entity() {
636: $assoc = ($this->association) ? $this->association : $this->model;
637: if (!empty($this->entityPath)) {
638: $path = explode('.', $this->entityPath);
639: $count = count($path);
640: if (
641: ($count == 1 && !empty($this->association)) ||
642: ($count == 1 && $this->model != $this->entityPath) ||
643: ($count == 1 && empty($this->association) && !empty($this->field)) ||
644: ($count == 2 && !empty($this->fieldSuffix)) ||
645: is_numeric($path[0]) && !empty($assoc)
646: ) {
647: array_unshift($path, $assoc);
648: }
649: return Set::filter($path);
650: }
651: return array_values(Set::filter(
652: array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
653: ));
654: }
655:
656: 657: 658: 659: 660: 661: 662: 663: 664: 665:
666: function set($one, $two = null) {
667: $data = null;
668: if (is_array($one)) {
669: if (is_array($two)) {
670: $data = array_combine($one, $two);
671: } else {
672: $data = $one;
673: }
674: } else {
675: $data = array($one => $two);
676: }
677: if ($data == null) {
678: return false;
679: }
680: $this->viewVars = $data + $this->viewVars;
681: }
682:
683: 684: 685: 686: 687: 688: 689: 690:
691: function error($code, $name, $message) {
692: header ("HTTP/1.1 {$code} {$name}");
693: print ($this->_render(
694: $this->_getLayoutFileName('error'),
695: array('code' => $code, 'name' => $name, 'message' => $message)
696: ));
697: }
698:
699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709:
710: function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
711: $loadedHelpers = array();
712:
713: if ($this->helpers != false && $loadHelpers === true) {
714: $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
715: $helpers = array_keys($loadedHelpers);
716: $helperNames = array_map(array('Inflector', 'variable'), $helpers);
717:
718: for ($i = count($helpers) - 1; $i >= 0; $i--) {
719: $name = $helperNames[$i];
720: $helper =& $loadedHelpers[$helpers[$i]];
721:
722: if (!isset($___dataForView[$name])) {
723: ${$name} =& $helper;
724: }
725: $this->loaded[$helperNames[$i]] =& $helper;
726: $this->{$helpers[$i]} =& $helper;
727: }
728: $this->_triggerHelpers('beforeRender');
729: unset($name, $loadedHelpers, $helpers, $i, $helperNames, $helper);
730: }
731:
732: extract($___dataForView, EXTR_SKIP);
733: ob_start();
734:
735: if (Configure::read() > 0) {
736: include ($___viewFn);
737: } else {
738: @include ($___viewFn);
739: }
740:
741: if ($loadHelpers === true) {
742: $this->_triggerHelpers('afterRender');
743: }
744:
745: $out = ob_get_clean();
746: $caching = (
747: isset($this->loaded['cache']) &&
748: (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
749: );
750:
751: if ($caching) {
752: if (is_a($this->loaded['cache'], 'CacheHelper')) {
753: $cache =& $this->loaded['cache'];
754: $cache->base = $this->base;
755: $cache->here = $this->here;
756: $cache->helpers = $this->helpers;
757: $cache->action = $this->action;
758: $cache->controllerName = $this->name;
759: $cache->layout = $this->layout;
760: $cache->cacheAction = $this->cacheAction;
761: $cache->viewVars = $this->viewVars;
762: $out = $cache->cache($___viewFn, $out, $cached);
763: }
764: }
765: return $out;
766: }
767:
768: 769: 770: 771: 772: 773: 774: 775: 776:
777: function &_loadHelpers(&$loaded, $helpers, $parent = null) {
778: foreach ($helpers as $i => $helper) {
779: $options = array();
780:
781: if (!is_int($i)) {
782: $options = $helper;
783: $helper = $i;
784: }
785: list($plugin, $helper) = pluginSplit($helper, true, $this->plugin);
786: $helperCn = $helper . 'Helper';
787:
788: if (!isset($loaded[$helper])) {
789: if (!class_exists($helperCn)) {
790: $isLoaded = false;
791: if (!is_null($plugin)) {
792: $isLoaded = App::import('Helper', $plugin . $helper);
793: }
794: if (!$isLoaded) {
795: if (!App::import('Helper', $helper)) {
796: $this->cakeError('missingHelperFile', array(array(
797: 'helper' => $helper,
798: 'file' => Inflector::underscore($helper) . '.php',
799: 'base' => $this->base
800: )));
801: return false;
802: }
803: }
804: if (!class_exists($helperCn)) {
805: $this->cakeError('missingHelperClass', array(array(
806: 'helper' => $helper,
807: 'file' => Inflector::underscore($helper) . '.php',
808: 'base' => $this->base
809: )));
810: return false;
811: }
812: }
813: $loaded[$helper] =& new $helperCn($options);
814: $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
815: $c = count($vars);
816:
817: for ($j = 0; $j < $c; $j++) {
818: $loaded[$helper]->{$vars[$j]} = $this->{$vars[$j]};
819: }
820:
821: if (!empty($this->validationErrors)) {
822: $loaded[$helper]->validationErrors = $this->validationErrors;
823: }
824: if (is_array($loaded[$helper]->helpers) && !empty($loaded[$helper]->helpers)) {
825: $loaded =& $this->_loadHelpers($loaded, $loaded[$helper]->helpers, $helper);
826: }
827: }
828: if (isset($loaded[$parent])) {
829: $loaded[$parent]->{$helper} =& $loaded[$helper];
830: }
831: }
832: return $loaded;
833: }
834:
835: 836: 837: 838: 839: 840: 841: 842: 843:
844: function _getViewFileName($name = null) {
845: $subDir = null;
846:
847: if (!is_null($this->subDir)) {
848: $subDir = $this->subDir . DS;
849: }
850:
851: if ($name === null) {
852: $name = $this->action;
853: }
854: $name = str_replace('/', DS, $name);
855:
856: if (strpos($name, DS) === false && $name[0] !== '.') {
857: $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
858: } elseif (strpos($name, DS) !== false) {
859: if ($name{0} === DS || $name{1} === ':') {
860: $name = trim($name, DS);
861: } else if ($name[0] === '.') {
862: $name = substr($name, 3);
863: } else {
864: $name = $this->viewPath . DS . $subDir . $name;
865: }
866: }
867: $paths = $this->_paths(Inflector::underscore($this->plugin));
868:
869: $exts = $this->_getExtensions();
870: foreach ($exts as $ext) {
871: foreach ($paths as $path) {
872: if (file_exists($path . $name . $ext)) {
873: return $path . $name . $ext;
874: }
875: }
876: }
877: $defaultPath = $paths[0];
878:
879: if ($this->plugin) {
880: $pluginPaths = App::path('plugins');
881: foreach ($paths as $path) {
882: if (strpos($path, $pluginPaths[0]) === 0) {
883: $defaultPath = $path;
884: break;
885: }
886: }
887: }
888: return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
889: }
890:
891: 892: 893: 894: 895: 896: 897:
898: function _getLayoutFileName($name = null) {
899: if ($name === null) {
900: $name = $this->layout;
901: }
902: $subDir = null;
903:
904: if (!is_null($this->layoutPath)) {
905: $subDir = $this->layoutPath . DS;
906: }
907: $paths = $this->_paths(Inflector::underscore($this->plugin));
908: $file = 'layouts' . DS . $subDir . $name;
909:
910: $exts = $this->_getExtensions();
911: foreach ($exts as $ext) {
912: foreach ($paths as $path) {
913: if (file_exists($path . $file . $ext)) {
914: return $path . $file . $ext;
915: }
916: }
917: }
918: return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
919: }
920:
921:
922: 923: 924: 925: 926: 927:
928: function _getExtensions() {
929: $exts = array($this->ext);
930: if ($this->ext !== '.ctp') {
931: array_push($exts, '.ctp');
932: }
933: return $exts;
934: }
935:
936: 937: 938: 939: 940: 941: 942:
943: function _missingView($file, $error = 'missingView') {
944: if ($error === 'missingView') {
945: $this->cakeError('missingView', array(
946: 'className' => $this->name,
947: 'action' => $this->action,
948: 'file' => $file,
949: 'base' => $this->base
950: ));
951: return false;
952: } elseif ($error === 'missingLayout') {
953: $this->cakeError('missingLayout', array(
954: 'layout' => $this->layout,
955: 'file' => $file,
956: 'base' => $this->base
957: ));
958: return false;
959: }
960: }
961:
962: 963: 964: 965: 966: 967: 968: 969:
970: function _paths($plugin = null, $cached = true) {
971: if ($plugin === null && $cached === true && !empty($this->__paths)) {
972: return $this->__paths;
973: }
974: $paths = array();
975: $viewPaths = App::path('views');
976: $corePaths = array_flip(App::core('views'));
977:
978: if (!empty($plugin)) {
979: $count = count($viewPaths);
980: for ($i = 0; $i < $count; $i++) {
981: if (!isset($corePaths[$viewPaths[$i]])) {
982: $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
983: }
984: }
985: $paths[] = App::pluginPath($plugin) . 'views' . DS;
986: }
987: $this->__paths = array_merge($paths, $viewPaths);
988: return $this->__paths;
989: }
990: }
991: