PageRenderTime 135ms CodeModel.GetById 67ms app.highlight 55ms RepoModel.GetById 4ms app.codeStats 0ms

/cake/libs/view/view.php

https://github.com/Forbin/cakephp2x
PHP | 954 lines | 485 code | 100 blank | 369 comment | 116 complexity | 0f3a3053a6afac7fce692bc02c88ed12 MD5 | raw file
  1<?php
  2/**
  3 * Methods for displaying presentation data in the view.
  4 *
  5 * PHP Version 5.x
  6 *
  7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8 * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9 *
 10 * Licensed under The MIT License
 11 * Redistributions of files must retain the above copyright notice.
 12 *
 13 * @copyright     Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14 * @link          http://cakephp.org CakePHP(tm) Project
 15 * @package       cake
 16 * @subpackage    cake.cake.libs.view
 17 * @since         CakePHP(tm) v 0.10.0.1076
 18 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 19 */
 20
 21/**
 22 * Included libraries.
 23 */
 24App::import('Core', 'ClassRegistry');
 25App::import('View', 'Helper', false);
 26
 27/**
 28 * View, the V in the MVC triad.
 29 *
 30 * Class holding methods for displaying presentation data.
 31 *
 32 * @package       cake
 33 * @subpackage    cake.cake.libs.view
 34 */
 35class View extends Object {
 36
 37/**
 38 * Path parts for creating links in views.
 39 *
 40 * @var string Base URL
 41 * @access public
 42 */
 43	public $base = null;
 44
 45/**
 46 * Stores the current URL (for links etc.)
 47 *
 48 * @var string Current URL
 49 */
 50	public $here = null;
 51
 52/**
 53 * Name of the plugin.
 54 *
 55 * @link          http://manual.cakephp.org/chapter/plugins
 56 * @var string
 57 */
 58	public $plugin = null;
 59
 60/**
 61 * Name of the controller.
 62 *
 63 * @var string Name of controller
 64 * @access public
 65 */
 66	public $name = null;
 67
 68/**
 69 * Action to be performed.
 70 *
 71 * @var string Name of action
 72 * @access public
 73 */
 74	public $action = null;
 75
 76/**
 77 * Array of parameter data
 78 *
 79 * @var array Parameter data
 80 */
 81	public $params = array();
 82
 83/**
 84 * Current passed params
 85 *
 86 * @var mixed
 87 */
 88	public $passedArgs = array();
 89
 90/**
 91 * An array of names of built-in helpers to include.
 92 *
 93 * @var mixed A single name as a string or a list of names as an array.
 94 * @access public
 95 */
 96	public $helpers = array('Html');
 97
 98/**
 99 * Path to View.
100 *
101 * @var string Path to View
102 */
103	public $viewPath = null;
104
105/**
106 * Variables for the view
107 *
108 * @var array
109 * @access protected
110 */
111	protected $_viewVars = array();
112
113/**
114 * Name of layout to use with this View.
115 *
116 * @var string
117 * @access public
118 */
119	public $layout = 'default';
120
121/**
122 * Path to Layout.
123 *
124 * @var string Path to Layout
125 */
126	public $layoutPath = null;
127
128/**
129 * Turns on or off Cake's conventional mode of rendering views. On by default.
130 *
131 * @var boolean
132 * @access public
133 */
134	public $autoRender = true;
135
136/**
137 * Turns on or off Cake's conventional mode of finding layout files. On by default.
138 *
139 * @var boolean
140 * @access public
141 */
142	public $autoLayout = true;
143
144/**
145 * File extension. Defaults to Cake's template ".ctp".
146 *
147 * @var string
148 */
149	public $ext = '.ctp';
150
151/**
152 * Sub-directory for this view file.
153 *
154 * @var string
155 */
156	public $subDir = null;
157
158/**
159 * Theme name.
160 *
161 * @var string
162 */
163	public $theme = null;
164
165/**
166 * Used to define methods a controller that will be cached.
167 *
168 * @see Controller::$cacheAction
169 * @var mixed
170 * @access public
171 */
172	public $cacheAction = false;
173
174/**
175 * holds current errors for the model validation
176 *
177 * @var array
178 */
179	public $validationErrors = array();
180
181/**
182 * True when the view has been rendered.
183 *
184 * @var boolean
185 */
186	public $hasRendered = false;
187
188/**
189 * Array of loaded view helpers.
190 *
191 * @var array
192 */
193	public $loaded = array();
194
195/**
196 * True if in scope of model-specific region
197 *
198 * @var boolean
199 */
200	public $modelScope = false;
201
202/**
203 * Name of current model this view context is attached to
204 *
205 * @var string
206 */
207	public $model = null;
208
209/**
210 * Name of association model this view context is attached to
211 *
212 * @var string
213 */
214	public $association = null;
215
216/**
217 * Name of current model field this view context is attached to
218 *
219 * @var string
220 */
221	public $field = null;
222
223/**
224 * Suffix of current field this view context is attached to
225 *
226 * @var string
227 */
228	public $fieldSuffix = null;
229
230/**
231 * The current model ID this view context is attached to
232 *
233 * @var mixed
234 */
235	public $modelId = null;
236
237/**
238 * List of generated DOM UUIDs
239 *
240 * @var array
241 */
242	public $uuids = array();
243
244/**
245 * Holds View output.
246 *
247 * @var string
248 */
249	public $output = false;
250
251/**
252 * List of variables to collect from the associated controller
253 *
254 * @var array
255 * @access protected
256 */
257	private $__passedVars = array(
258		'viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot',
259		'helpers', 'here', 'layout', 'name', 'layoutPath', 'viewPath',
260		'params', 'data', 'plugin', 'passedArgs', 'cacheAction'
261	);
262
263/**
264 * Scripts (and/or other <head /> tags) for the layout
265 *
266 * @var array
267 * @access private
268 */
269	private $__scripts = array();
270
271/**
272 * Holds an array of paths.
273 *
274 * @var array
275 */
276	private $__paths = array();
277
278/**
279 * Constructor
280 *
281 * @return View
282 * @access public
283 */
284	public function __construct(&$controller, $register = true) {
285		if (is_object($controller)) {
286			$count = count($this->__passedVars);
287			for ($j = 0; $j < $count; $j++) {
288				$var = $this->__passedVars[$j];
289				$this->{$var} = $controller->{$var};
290			}
291		}
292		parent::__construct();
293
294		if ($register) {
295			ClassRegistry::addObject('view', $this);
296		}
297	}
298
299/**
300 * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
301 *
302 * This realizes the concept of Elements, (or "partial layouts")
303 * and the $params array is used to send data to be used in the
304 * Element.  Elements can be cached through use of the cache key.
305 *
306 * ### Special params
307 *
308 * - cache - enable caching for this element accepts boolean or strtotime compatible string.
309 *   Can also be an array. If `cache` is an array,
310 *   `time` is used to specify duration of cache.
311 *   `key` can be used to create unique cache files.
312 *
313 * @param string $name Name of template file in the/app/views/elements/ folder
314 * @param array $params Array of data to be made available to the for rendered
315 *    view (i.e. the Element)
316 * @return string Rendered Element
317 * @access public
318 */
319	public function element($name, $params = array(), $loadHelpers = false) {
320		$file = $plugin = $key = null;
321
322		if (isset($params['plugin'])) {
323			$plugin = $params['plugin'];
324		}
325
326		if (isset($this->plugin) && !$plugin) {
327			$plugin = $this->plugin;
328		}
329
330		if (isset($params['cache'])) {
331			$expires = '+1 day';
332
333			if (is_array($params['cache'])) {
334				$expires = $params['cache']['time'];
335				$key = Inflector::slug($params['cache']['key']);
336			} elseif ($params['cache'] !== true) {
337				$expires = $params['cache'];
338				$key = implode('_', array_keys($params));
339			}
340
341			if ($expires) {
342				$cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
343				$cache = cache('views' . DS . $cacheFile, null, $expires);
344
345				if (is_string($cache)) {
346					return $cache;
347				}
348			}
349		}
350		$paths = $this->_paths($plugin);
351
352		foreach ($paths as $path) {
353			if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
354				$file = $path . 'elements' . DS . $name . $this->ext;
355				break;
356			}
357		}
358
359		if (is_file($file)) {
360			$params = array_merge_recursive($params, $this->loaded);
361			$element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
362			if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
363				cache('views' . DS . $cacheFile, $element, $expires);
364			}
365			return $element;
366		}
367		$file = $paths[0] . 'elements' . DS . $name . $this->ext;
368
369		if (Configure::read() > 0) {
370			return "Not Found: " . $file;
371		}
372	}
373
374/**
375 * Renders view for given action and layout. If $file is given, that is used
376 * for a view filename (e.g. customFunkyView.ctp).
377 *
378 * @param string $action Name of action to render for
379 * @param string $layout Layout to use
380 * @param string $file Custom filename for view
381 * @return string Rendered Element
382 */
383	public function render($action = null, $layout = null, $file = null) {
384		if ($this->hasRendered) {
385			return true;
386		}
387		$out = null;
388
389		if ($file != null) {
390			$action = $file;
391		}
392
393		if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
394			$out = $this->_render($viewFileName, $this->viewVars);
395		}
396
397		if ($layout === null) {
398			$layout = $this->layout;
399		}
400
401		if ($out !== false) {
402			if ($layout && $this->autoLayout) {
403				$out = $this->renderLayout($out, $layout);
404				$isCached = (
405					isset($this->loaded['cache']) &&
406					(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
407				);
408
409				if ($isCached) {
410					$replace = array('<cake:nocache>', '</cake:nocache>');
411					$out = str_replace($replace, '', $out);
412				}
413			}
414			$this->hasRendered = true;
415		} else {
416			$out = $this->_render($viewFileName, $this->viewVars);
417			$msg = __("Error in view %s, got: <blockquote>%s</blockquote>", true);
418			trigger_error(sprintf($msg, $viewFileName, $out), E_USER_ERROR);
419		}
420		return $out;
421	}
422
423/**
424 * Renders a layout. Returns output from _render(). Returns false on error.
425 * Several variables are created for use in layout.
426 *
427 * - `title_for_layout` - contains page title
428 * - `content_for_layout` - contains rendered view file
429 * - `scripts_for_layout` - contains scripts added to header
430 *
431 * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
432 * @return mixed Rendered output, or false on error
433 * @access public
434 */
435	public function renderLayout($content_for_layout, $layout = null) {
436		$layoutFileName = $this->_getLayoutFileName($layout);
437		if (empty($layoutFileName)) {
438			return $this->output;
439		}
440
441		$dataForLayout = array_merge($this->viewVars, array(
442			'content_for_layout' => $content_for_layout,
443			'scripts_for_layout' => implode("\n\t", $this->__scripts),
444		));
445
446		if (!isset($dataForLayout['title_for_layout'])) {
447			$dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
448		}
449
450		if (empty($this->loaded) && !empty($this->helpers)) {
451			$loadHelpers = true;
452		} else {
453			$loadHelpers = false;
454			$dataForLayout = array_merge($dataForLayout, $this->loaded);
455		}
456
457		$this->_triggerHelpers('beforeLayout');
458		$this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true);
459
460		if ($this->output === false) {
461			$this->output = $this->_render($layoutFileName, $data_for_layout);
462			$msg = __("Error in layout %s, got: <blockquote>%s</blockquote>", true);
463			trigger_error(sprintf($msg, $layoutFileName, $this->output), E_USER_ERROR);
464			return false;
465		}
466
467		$this->_triggerHelpers('afterLayout');
468
469		return $this->output;
470	}
471
472/**
473 * Fire a callback on all loaded Helpers. All helpers must implement this method, 
474 * it is not checked before being called.  You can add additional helper callbacks in AppHelper.
475 *
476 * @param string $callback name of callback fire.
477 * @return void
478 * @access protected
479 */
480	protected function _triggerHelpers($callback) {
481		if (empty($this->loaded)) {
482			return false;
483		}
484		$helpers = array_keys($this->loaded);
485		foreach ($helpers as $helperName) {
486			$helper = $this->loaded[$helperName];
487			if (is_object($helper)) {
488				if (is_subclass_of($helper, 'Helper')) {
489					$helper->{$callback}();
490				}
491			}
492		}
493	}
494
495/**
496 * Render cached view
497 *
498 * @param string $filename the cache file to include
499 * @param string $timeStart the page render start time
500 * @return void
501 * @access public
502 */
503	public function renderCache($filename, $timeStart) {
504		ob_start();
505		include ($filename);
506
507		if (Configure::read() > 0 && $this->layout != 'xml') {
508			echo "<!-- Cached Render Time: " . round(microtime(true) - $timeStart, 4) . "s -->";
509		}
510		$out = ob_get_clean();
511
512		if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
513			if (time() >= $match['1']) {
514				@unlink($filename);
515				unset ($out);
516				return false;
517			} else {
518				if ($this->layout === 'xml') {
519					header('Content-type: text/xml');
520				}
521				$commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
522				echo substr($out, $commentLength);
523				return true;
524			}
525		}
526	}
527
528/**
529 * Returns a list of variables available in the current View context
530 *
531 * @return array
532 * @access public
533 */
534	public function getVars() {
535		return array_keys($this->viewVars);
536	}
537
538/**
539 * Returns the contents of the given View variable(s)
540 *
541 * @return array
542 * @access public
543 */
544	public function getVar($var) {
545		if (!isset($this->viewVars[$var])) {
546			return null;
547		} else {
548			return $this->viewVars[$var];
549		}
550	}
551
552/**
553 * Adds a script block or other element to be inserted in $scripts_for_layout in
554 * the <head /> of a document layout
555 *
556 * @param string $name
557 * @param string $content
558 * @return void
559 * @access public
560 */
561	public function addScript($name, $content = null) {
562		if (empty($content)) {
563			if (!in_array($name, array_values($this->__scripts))) {
564				$this->__scripts[] = $name;
565			}
566		} else {
567			$this->__scripts[$name] = $content;
568		}
569	}
570	
571/**
572 * Return the scripts set for this view
573 *
574 * @return array
575 * @access public
576 */
577	public function scripts() {
578		return $this->__scripts;
579	}
580
581/**
582 * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
583 *
584 * @param string $object Type of object, i.e. 'form' or 'link'
585 * @param string $url The object's target URL
586 * @return string
587 * @access public
588 */
589	public function uuid($object, $url) {
590		$c = 1;
591		$url = Router::url($url);
592		$hash = $object . substr(md5($object . $url), 0, 10);
593		while (in_array($hash, $this->uuids)) {
594			$hash = $object . substr(md5($object . $url . $c), 0, 10);
595			$c++;
596		}
597		$this->uuids[] = $hash;
598		return $hash;
599	}
600
601/**
602 * Returns the entity reference of the current context as an array of identity parts
603 *
604 * @return array An array containing the identity elements of an entity
605 * @access public
606 */
607	public function entity() {
608		$assoc = ($this->association) ? $this->association : $this->model;
609		if (!empty($this->entityPath)) {
610			$path = explode('.', $this->entityPath);
611			$count = count($path);
612			if (
613				($count == 1 && !empty($this->association)) ||
614				($count == 1 && $this->model != $this->entityPath) ||
615				($count  == 2 && !empty($this->fieldSuffix)) ||
616				is_numeric($path[0])
617			) {
618				array_unshift($path, $assoc);
619			}
620			return Set::filter($path);
621		}
622		return array_values(Set::filter(
623			array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
624		));
625	}
626
627/**
628 * Allows a template or element to set a variable that will be available in
629 * a layout or other element. Analagous to Controller::set.
630 *
631 * @param mixed $one A string or an array of data.
632 * @param mixed $two Value in case $one is a string (which then works as the key).
633 *    Unused if $one is an associative array, otherwise serves as the values to $one's keys.
634 * @return void
635 */
636	public function set($one, $two = null) {
637		$data = null;
638		if (is_array($one)) {
639			if (is_array($two)) {
640				$data = array_combine($one, $two);
641			} else {
642				$data = $one;
643			}
644		} else {
645			$data = array($one => $two);
646		}
647		if ($data == null) {
648			return false;
649		}
650		$this->viewVars = array_merge($this->viewVars, $data);
651	}
652
653/**
654 * Displays an error page to the user. Uses layouts/error.ctp to render the page.
655 *
656 * @param integer $code HTTP Error code (for instance: 404)
657 * @param string $name Name of the error (for instance: Not Found)
658 * @param string $message Error message as a web page
659 */
660	public function error($code, $name, $message) {
661		header ("HTTP/1.1 {$code} {$name}");
662		print ($this->_render(
663			$this->_getLayoutFileName('error'),
664			array('code' => $code, 'name' => $name, 'message' => $message)
665		));
666	}
667
668/**
669 * Renders and returns output for given view filename with its
670 * array of data.
671 *
672 * @param string $___viewFn Filename of the view
673 * @param array $___dataForView Data to include in rendered view
674 * @return string Rendered output
675 * @access protected
676 */
677	protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
678		$loadedHelpers = array();
679
680		if ($this->helpers != false && $loadHelpers === true) {
681			$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
682			$helpers = array_keys($loadedHelpers);
683			$helperNames = array_map(array('Inflector', 'variable'), $helpers);
684
685			for ($i = count($helpers) - 1; $i >= 0; $i--) {
686				$name = $helperNames[$i];
687				$helper = $loadedHelpers[$helpers[$i]];
688
689				if (!isset($___dataForView[$name])) {
690					${$name} = $helper;
691				}
692				$this->loaded[$helperNames[$i]] = $helper;
693				$this->{$helpers[$i]} = $helper;
694			}
695			$this->_triggerHelpers('beforeRender');
696			unset($name, $loadedHelpers, $helpers, $i, $helperNames, $helper);
697		}
698
699		extract($___dataForView, EXTR_SKIP);
700		ob_start();
701
702		if (Configure::read() > 0) {
703			include ($___viewFn);
704		} else {
705			@include ($___viewFn);
706		}
707
708		if ($loadHelpers === true) {
709			$this->_triggerHelpers('afterRender');
710		}
711
712		$out = ob_get_clean();
713		$caching = (
714			isset($this->loaded['cache']) &&
715			(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
716		);
717
718		if ($caching) {
719			if (is_a($this->loaded['cache'], 'CacheHelper')) {
720				$cache = $this->loaded['cache'];
721				$cache->base = $this->base;
722				$cache->here = $this->here;
723				$cache->helpers = $this->helpers;
724				$cache->action = $this->action;
725				$cache->controllerName = $this->name;
726				$cache->layout = $this->layout;
727				$cache->cacheAction = $this->cacheAction;
728				$cache->cache($___viewFn, $out, $cached);
729			}
730		}
731		return $out;
732	}
733
734/**
735 * Loads helpers, with their dependencies.
736 *
737 * @param array $loaded List of helpers that are already loaded.
738 * @param array $helpers List of helpers to load.
739 * @param string $parent holds name of helper, if loaded helper has helpers
740 * @return array
741 * @access protected
742 */
743	public function _loadHelpers($loaded, $helpers, $parent = null) {
744		if (empty($loaded)) {
745			$helpers[] = 'Session';
746		}
747
748		foreach ($helpers as $i => $helper) {
749			$options = array();
750
751			if (!is_int($i)) {
752				$options = $helper;
753				$helper = $i;
754			}
755			list($plugin, $helper) = pluginSplit($helper, true, $this->plugin);
756			$helperCn = $helper . 'Helper';
757
758			if (!isset($loaded[$helper])) {
759				if (!class_exists($helperCn)) {
760					$isLoaded = false;
761					if (!is_null($plugin)) {
762						$isLoaded = App::import('Helper', $plugin . $helper);
763					}
764					if (!$isLoaded) {
765						if (!App::import('Helper', $helper)) {
766							$this->cakeError('missingHelperFile', array(array(
767								'helper' => $helper,
768								'file' => Inflector::underscore($helper) . '.php',
769								'base' => $this->base
770							)));
771							return false;
772						}
773					}
774					if (!class_exists($helperCn)) {
775						$this->cakeError('missingHelperClass', array(array(
776							'helper' => $helper,
777							'file' => Inflector::underscore($helper) . '.php',
778							'base' => $this->base
779						)));
780						return false;
781					}
782				}
783				$loaded[$helper] = new $helperCn($options);
784				$vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
785				$c = count($vars);
786
787				for ($j = 0; $j < $c; $j++) {
788					$loaded[$helper]->{$vars[$j]} = $this->{$vars[$j]};
789				}
790
791				if (!empty($this->validationErrors)) {
792					$loaded[$helper]->validationErrors = $this->validationErrors;
793				}
794				if (is_array($loaded[$helper]->helpers) && !empty($loaded[$helper]->helpers)) {
795					$loaded = $this->_loadHelpers($loaded, $loaded[$helper]->helpers, $helper);
796				}
797			}
798			if (isset($loaded[$parent])) {
799				$loaded[$parent]->{$helper} = $loaded[$helper];
800			}
801		}
802		return $loaded;
803	}
804
805/**
806 * Returns filename of given action's template file (.ctp) as a string.
807 * CamelCased action names will be under_scored! This means that you can have
808 * LongActionNames that refer to long_action_names.ctp views.
809 *
810 * @param string $action Controller action to find template filename for
811 * @return string Template filename
812 * @access protected
813 */
814	protected function _getViewFileName($name = null) {
815		$subDir = null;
816
817		if (!is_null($this->subDir)) {
818			$subDir = $this->subDir . DS;
819		}
820
821		if ($name === null) {
822			$name = $this->action;
823		}
824		$name = str_replace('/', DS, $name);
825
826		if (strpos($name, DS) === false && $name[0] !== '.') {
827			$name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
828		} elseif (strpos($name, DS) !== false) {
829			if ($name{0} === DS || $name{1} === ':') {
830				if (is_file($name)) {
831					return $name;
832				}
833				$name = trim($name, DS);
834			} else if ($name[0] === '.') {
835				$name = substr($name, 3);
836			} else {
837				$name = $this->viewPath . DS . $subDir . $name;
838			}
839		}
840		$paths = $this->_paths(Inflector::underscore($this->plugin));
841		
842		$exts = array($this->ext);
843		if ($this->ext !== '.ctp') {
844			array_push($exts, '.ctp');
845		}
846		foreach ($exts as $ext) {
847			foreach ($paths as $path) {
848				if (file_exists($path . $name . $ext)) {
849					return $path . $name . $ext;
850				}
851			}
852		}
853		$defaultPath = $paths[0];
854
855		if ($this->plugin) {
856			$pluginPaths = App::path('plugins');
857			foreach ($paths as $path) {
858				if (strpos($path, $pluginPaths[0]) === 0) {
859					$defaultPath = $path;
860					break;
861				}
862			}
863		}
864		return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
865	}
866
867/**
868 * Returns layout filename for this template as a string.
869 *
870 * @return string Filename for layout file (.ctp).
871 * @access protected
872 */
873	protected function _getLayoutFileName($name = null) {
874		if ($name === null) {
875			$name = $this->layout;
876		}
877		$subDir = null;
878
879		if (!is_null($this->layoutPath)) {
880			$subDir = $this->layoutPath . DS;
881		}
882		$paths = $this->_paths(Inflector::underscore($this->plugin));
883		$file = 'layouts' . DS . $subDir . $name;
884		
885		$exts = array($this->ext);
886		if ($this->ext !== '.ctp') {
887			array_push($exts, '.ctp');
888		}
889		foreach ($exts as $ext) {
890			foreach ($paths as $path) {
891				if (file_exists($path . $file . $ext)) {
892					return $path . $file . $ext;
893				}
894			}
895		}
896		return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
897	}
898
899/**
900 * Return a misssing view error message
901 *
902 * @param string $viewFileName the filename that should exist
903 * @return cakeError
904 * @access protected
905 */
906	protected function _missingView($file, $error = 'missingView') {
907
908		if ($error === 'missingView') {
909			$this->cakeError('missingView', array(
910				'className' => $this->name,
911				'action' => $this->action,
912				'file' => $file,
913				'base' => $this->base
914			));
915			return false;
916		} elseif ($error === 'missingLayout') {
917			$this->cakeError('missingLayout', array(
918				'layout' => $this->layout,
919				'file' => $file,
920				'base' => $this->base
921			));
922			return false;
923		}
924	}
925
926/**
927 * Return all possible paths to find view files in order
928 *
929 * @param string $plugin
930 * @return array paths
931 * @access protected
932 */
933	protected function _paths($plugin = null, $cached = true) {
934		if ($plugin === null && $cached === true && !empty($this->__paths)) {
935			return $this->__paths;
936		}
937		$paths = array();
938		$viewPaths = App::path('views');
939		$corePaths = array_flip(App::core('views'));
940
941		if (!empty($plugin)) {
942			$count = count($viewPaths);
943			for ($i = 0; $i < $count; $i++) {
944				if (!isset($corePaths[$viewPaths[$i]])) {
945					$paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
946				}
947			}
948			$paths[] = App::pluginPath($plugin) . 'views' . DS;
949		}
950		$this->__paths = array_merge($paths, $viewPaths);
951		return $this->__paths;
952	}
953}
954?>