PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 3ms app.codeStats 0ms

/cake/libs/view/view.php

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