view.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: libs_2view_2view_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 
00004 /**
00005  * Methods for displaying presentation data in the view.
00006  *
00007  * PHP versions 4 and 5
00008  *
00009  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00010  * Copyright 2005-2008, Cake Software Foundation, Inc.
00011  *                              1785 E. Sahara Avenue, Suite 490-204
00012  *                              Las Vegas, Nevada 89104
00013  *
00014  * Licensed under The MIT License
00015  * Redistributions of files must retain the above copyright notice.
00016  *
00017  * @filesource
00018  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00019  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00020  * @package         cake
00021  * @subpackage      cake.cake.libs.view
00022  * @since           CakePHP(tm) v 0.10.0.1076
00023  * @version         $Revision: 580 $
00024  * @modifiedby      $LastChangedBy: gwoo $
00025  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00026  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00027  */
00028 /**
00029  * Included libraries.
00030  */
00031 App::import('Core', array('Helper', 'ClassRegistry'));
00032 /**
00033  * View, the V in the MVC triad.
00034  *
00035  * Class holding methods for displaying presentation data.
00036  *
00037  * @package         cake
00038  * @subpackage      cake.cake.libs.view
00039  */
00040 class View extends Object {
00041 /**
00042  * Path parts for creating links in views.
00043  *
00044  * @var string Base URL
00045  * @access public
00046  */
00047     var $base = null;
00048 /**
00049  * Stores the current URL (for links etc.)
00050  *
00051  * @var string Current URL
00052  */
00053     var $here = null;
00054 /**
00055  * Name of the plugin.
00056  *
00057  * @link http://manual.cakephp.org/chapter/plugins
00058  * @var string
00059  */
00060     var $plugin = null;
00061 /**
00062  * Name of the controller.
00063  *
00064  * @var string Name of controller
00065  * @access public
00066  */
00067     var $name = null;
00068 /**
00069  * Action to be performed.
00070  *
00071  * @var string Name of action
00072  * @access public
00073  */
00074     var $action = null;
00075 /**
00076  * Array of parameter data
00077  *
00078  * @var array Parameter data
00079  */
00080     var $params = array();
00081 /**
00082  * Current passed params
00083  *
00084  * @var mixed
00085  */
00086     var $passedArgs = array();
00087 /**
00088  * Array of data
00089  *
00090  * @var array Parameter data
00091  */
00092     var $data = array();
00093 /**
00094  * An array of names of built-in helpers to include.
00095  *
00096  * @var mixed A single name as a string or a list of names as an array.
00097  * @access public
00098  */
00099     var $helpers = array('Html');
00100 /**
00101  * Path to View.
00102  *
00103  * @var string Path to View
00104  */
00105     var $viewPath = null;
00106 /**
00107  * Variables for the view
00108  *
00109  * @var array
00110  * @access public
00111  */
00112     var $viewVars = array();
00113 /**
00114  * Name of layout to use with this View.
00115  *
00116  * @var string
00117  * @access public
00118  */
00119     var $layout = 'default';
00120 /**
00121  * Path to Layout.
00122  *
00123  * @var string Path to Layout
00124  */
00125     var $layoutPath = null;
00126 /**
00127  * Title HTML element of this View.
00128  *
00129  * @var string
00130  * @access public
00131  */
00132     var $pageTitle = false;
00133 /**
00134  * Turns on or off Cake's conventional mode of rendering views. On by default.
00135  *
00136  * @var boolean
00137  * @access public
00138  */
00139     var $autoRender = true;
00140 /**
00141  * Turns on or off Cake's conventional mode of finding layout files. On by default.
00142  *
00143  * @var boolean
00144  * @access public
00145  */
00146     var $autoLayout = true;
00147 /**
00148  * File extension. Defaults to Cake's template ".ctp".
00149  *
00150  * @var string
00151  */
00152     var $ext = '.ctp';
00153 /**
00154  * Sub-directory for this view file.
00155  *
00156  * @var string
00157  */
00158     var $subDir = null;
00159 /**
00160  * Theme name.
00161  *
00162  * @var string
00163  */
00164     var $themeWeb = null;
00165 /**
00166  * Used to define methods a controller that will be cached.
00167  *
00168  * @see Controller::$cacheAction
00169  * @var mixed
00170  * @access public
00171  */
00172     var $cacheAction = false;
00173 /**
00174  * holds current errors for the model validation
00175  *
00176  * @var array
00177  */
00178     var $validationErrors = array();
00179 /**
00180  * True when the view has been rendered.
00181  *
00182  * @var boolean
00183  */
00184     var $hasRendered = false;
00185 /**
00186  * Array of loaded view helpers.
00187  *
00188  * @var array
00189  */
00190     var $loaded = array();
00191 /**
00192  * True if in scope of model-specific region
00193  *
00194  * @var boolean
00195  */
00196     var $modelScope = false;
00197 /**
00198  * Name of current model this view context is attached to
00199  *
00200  * @var string
00201  */
00202     var $model = null;
00203 /**
00204  * Name of association model this view context is attached to
00205  *
00206  * @var string
00207  */
00208     var $association = null;
00209 /**
00210  * Name of current model field this view context is attached to
00211  *
00212  * @var string
00213  */
00214     var $field = null;
00215 /**
00216  * Suffix of current field this view context is attached to
00217  *
00218  * @var string
00219  */
00220     var $fieldSuffix = null;
00221 /**
00222  * The current model ID this view context is attached to
00223  *
00224  * @var mixed
00225  */
00226     var $modelId = null;
00227 /**
00228  * List of generated DOM UUIDs
00229  *
00230  * @var array
00231  */
00232     var $uuids = array();
00233 /**
00234  * Holds View output.
00235  *
00236  * @var string
00237  **/
00238     var $output = false;
00239 /**
00240  * List of variables to collect from the associated controller
00241  *
00242  * @var array
00243  * @access protected
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  * Scripts (and/or other <head /> tags) for the layout
00248  *
00249  * @var array
00250  * @access private
00251  */
00252     var $__scripts = array();
00253 /**
00254  * Holds an array of paths.
00255  *
00256  * @var array
00257  */
00258     var $__paths = array();
00259 /**
00260  * Constructor
00261  *
00262  * @return View
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  * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
00280  *
00281  * This realizes the concept of Elements, (or "partial layouts")
00282  * and the $params array is used to send data to be used in the
00283  * Element.
00284  *
00285  * @param string $name Name of template file in the/app/views/elements/ folder
00286  * @param array $params Array of data to be made available to the for rendered view (i.e. the Element)
00287  * @return string Rendered Element
00288  * @access public
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  * Renders view for given action and layout. If $file is given, that is used
00349  * for a view filename (e.g. customFunkyView.ctp).
00350  *
00351  * @param string $action Name of action to render for
00352  * @param string $layout Layout to use
00353  * @param string $file Custom filename for view
00354  * @return string Rendered Element
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  * Renders a layout. Returns output from _render(). Returns false on error.
00395  *
00396  * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
00397  * @return mixed Rendered output, or false on error
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  * Render cached view
00468  *
00469  * @param string $filename the cache file to include
00470  * @param string $timeStart the page render start time
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  * Returns a list of variables available in the current View context
00498  *
00499  * @return array
00500  * @access public
00501  */
00502     function getVars() {
00503         return array_keys($this->viewVars);
00504     }
00505 /**
00506  * Returns the contents of the given View variable(s)
00507  *
00508  * @return array
00509  * @access public
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  * Adds a script block or other element to be inserted in $scripts_for_layout in
00520  * the <head /> of a document layout
00521  *
00522  * @param string $name
00523  * @param string $content
00524  * @access public
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  * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
00537  *
00538  * @param string $object Type of object, i.e. 'form' or 'link'
00539  * @param string $url The object's target URL
00540  * @return string
00541  * @access public
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  * Returns the entity reference of the current context as an array of identity parts
00556  *
00557  * @return array An array containing the identity elements of an entity
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  * Allows a template or element to set a variable that will be available in
00567  * a layout or other element. Analagous to Controller::set.
00568  *
00569  * @param mixed $one A string or an array of data.
00570  * @param mixed $two Value in case $one is a string (which then works as the key).
00571  *              Unused if $one is an associative array, otherwise serves as the values to $one's keys.
00572  * @return unknown
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  * Displays an error page to the user. Uses layouts/error.ctp to render the page.
00600  *
00601  * @param integer $code HTTP Error code (for instance: 404)
00602  * @param string $name Name of the error (for instance: Not Found)
00603  * @param string $message Error message as a web page
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  * Renders and returns output for given view filename with its
00614  * array of data.
00615  *
00616  * @param string $___viewFn Filename of the view
00617  * @param array $___dataForView Data to include in rendered view
00618  * @return string Rendered output
00619  * @access protected
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  * Loads helpers, with their dependencies.
00679  *
00680  * @param array $loaded List of helpers that are already loaded.
00681  * @param array $helpers List of helpers to load.
00682  * @param string $parent holds name of helper, if loaded helper has helpers
00683  * @return array
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  * Returns filename of given action's template file (.ctp) as a string.
00751  * CamelCased action names will be under_scored! This means that you can have
00752  * LongActionNames that refer to long_action_names.ctp views.
00753  *
00754  * @param string $action Controller action to find template filename for
00755  * @return string Template filename
00756  * @access protected
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  * Returns layout filename for this template as a string.
00812  *
00813  * @return string Filename for layout file (.ctp).
00814  * @access protected
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  * Return a misssing view error message
00841  *
00842  * @param string $viewFileName the filename that should exist
00843  * @return cakeError
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  * Return all possible paths to find view files in order
00867  *
00868  * @param string $plugin
00869  * @return array paths
00870  * @access protected
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  * @deprecated
00900  * @see View::element
00901  */
00902     function renderElement($name, $params = array(), $loadHelpers = false) {
00903         return $this->element($name, $params, $loadHelpers);
00904     }
00905 }
00906 ?>