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