PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/view.php

https://github.com/msadouni/cakephp2x
PHP | 964 lines | 492 code | 102 blank | 370 comment | 118 complexity | 63109d0d29c85df2fa31d1760afe2c3d 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. * - `cakeDebug` - if debug is on, cake debug information is added.
  378. *
  379. * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
  380. * @return mixed Rendered output, or false on error
  381. * @access public
  382. */
  383. public function renderLayout($content_for_layout, $layout = null) {
  384. $layoutFileName = $this->_getLayoutFileName($layout);
  385. if (empty($layoutFileName)) {
  386. return $this->output;
  387. }
  388. $debug = '';
  389. if (isset($this->viewVars['cakeDebug']) && Configure::read() > 2) {
  390. $params = array('controller' => $this->viewVars['cakeDebug']);
  391. $debug = View::element('dump', $params, false);
  392. unset($this->viewVars['cakeDebug']);
  393. }
  394. $dataForLayout = array_merge($this->viewVars, array(
  395. 'content_for_layout' => $content_for_layout,
  396. 'scripts_for_layout' => implode("\n\t", $this->__scripts),
  397. 'cakeDebug' => $debug
  398. ));
  399. if (!isset($dataForLayout['title_for_layout'])) {
  400. $dataForLayout['title_for_layout'] = Inflector::humanize($this->viewPath);
  401. }
  402. if (empty($this->loaded) && !empty($this->helpers)) {
  403. $loadHelpers = true;
  404. } else {
  405. $loadHelpers = false;
  406. $dataForLayout = array_merge($dataForLayout, $this->loaded);
  407. }
  408. $this->_triggerHelpers('beforeLayout');
  409. $this->output = $this->_render($layoutFileName, $dataForLayout, $loadHelpers, true);
  410. if ($this->output === false) {
  411. $this->output = $this->_render($layoutFileName, $data_for_layout);
  412. $msg = __("Error in layout %s, got: <blockquote>%s</blockquote>", true);
  413. trigger_error(sprintf($msg, $layoutFileName, $this->output), E_USER_ERROR);
  414. return false;
  415. }
  416. $this->_triggerHelpers('afterLayout');
  417. return $this->output;
  418. }
  419. /**
  420. * Fire a callback on all loaded Helpers. All helpers must implement this method,
  421. * it is not checked before being called. You can add additional helper callbacks in AppHelper.
  422. *
  423. * @param string $callback name of callback fire.
  424. * @return void
  425. * @access protected
  426. */
  427. protected function _triggerHelpers($callback) {
  428. if (empty($this->loaded)) {
  429. return false;
  430. }
  431. $helpers = array_keys($this->loaded);
  432. foreach ($helpers as $helperName) {
  433. $helper = $this->loaded[$helperName];
  434. if (is_object($helper)) {
  435. if (is_subclass_of($helper, 'Helper')) {
  436. $helper->{$callback}();
  437. }
  438. }
  439. }
  440. }
  441. /**
  442. * Render cached view
  443. *
  444. * @param string $filename the cache file to include
  445. * @param string $timeStart the page render start time
  446. * @return void
  447. * @access public
  448. */
  449. public function renderCache($filename, $timeStart) {
  450. ob_start();
  451. include ($filename);
  452. if (Configure::read() > 0 && $this->layout != 'xml') {
  453. echo "<!-- Cached Render Time: " . round(microtime(true) - $timeStart, 4) . "s -->";
  454. }
  455. $out = ob_get_clean();
  456. if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
  457. if (time() >= $match['1']) {
  458. @unlink($filename);
  459. unset ($out);
  460. return false;
  461. } else {
  462. if ($this->layout === 'xml') {
  463. header('Content-type: text/xml');
  464. }
  465. $commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
  466. echo substr($out, $commentLength);
  467. return true;
  468. }
  469. }
  470. }
  471. /**
  472. * Returns a list of variables available in the current View context
  473. *
  474. * @return array
  475. * @access public
  476. */
  477. public function getVars() {
  478. return array_keys($this->viewVars);
  479. }
  480. /**
  481. * Returns the contents of the given View variable(s)
  482. *
  483. * @return array
  484. * @access public
  485. */
  486. public function getVar($var) {
  487. if (!isset($this->viewVars[$var])) {
  488. return null;
  489. } else {
  490. return $this->viewVars[$var];
  491. }
  492. }
  493. /**
  494. * Adds a script block or other element to be inserted in $scripts_for_layout in
  495. * the <head /> of a document layout
  496. *
  497. * @param string $name
  498. * @param string $content
  499. * @return void
  500. * @access public
  501. */
  502. public function addScript($name, $content = null) {
  503. if (empty($content)) {
  504. if (!in_array($name, array_values($this->__scripts))) {
  505. $this->__scripts[] = $name;
  506. }
  507. } else {
  508. $this->__scripts[$name] = $content;
  509. }
  510. }
  511. /**
  512. * Return the scripts set for this view
  513. *
  514. * @return array
  515. * @access public
  516. */
  517. public function scripts() {
  518. return $this->__scripts;
  519. }
  520. /**
  521. * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
  522. *
  523. * @param string $object Type of object, i.e. 'form' or 'link'
  524. * @param string $url The object's target URL
  525. * @return string
  526. * @access public
  527. */
  528. public function uuid($object, $url) {
  529. $c = 1;
  530. $url = Router::url($url);
  531. $hash = $object . substr(md5($object . $url), 0, 10);
  532. while (in_array($hash, $this->uuids)) {
  533. $hash = $object . substr(md5($object . $url . $c), 0, 10);
  534. $c++;
  535. }
  536. $this->uuids[] = $hash;
  537. return $hash;
  538. }
  539. /**
  540. * Returns the entity reference of the current context as an array of identity parts
  541. *
  542. * @return array An array containing the identity elements of an entity
  543. * @access public
  544. */
  545. public function entity() {
  546. $assoc = ($this->association) ? $this->association : $this->model;
  547. if (!empty($this->entityPath)) {
  548. $path = explode('.', $this->entityPath);
  549. $count = count($path);
  550. if (
  551. ($count == 1 && !empty($this->association)) ||
  552. ($count == 1 && $this->model != $this->entityPath) ||
  553. ($count == 2 && !empty($this->fieldSuffix)) ||
  554. is_numeric($path[0])
  555. ) {
  556. array_unshift($path, $assoc);
  557. }
  558. return Set::filter($path);
  559. }
  560. return array_values(Set::filter(
  561. array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
  562. ));
  563. }
  564. /**
  565. * Allows a template or element to set a variable that will be available in
  566. * a layout or other element. Analagous to Controller::set.
  567. *
  568. * @param mixed $one A string or an array of data.
  569. * @param mixed $two Value in case $one is a string (which then works as the key).
  570. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  571. * @return void
  572. */
  573. public function set($one, $two = null) {
  574. $data = null;
  575. if (is_array($one)) {
  576. if (is_array($two)) {
  577. $data = array_combine($one, $two);
  578. } else {
  579. $data = $one;
  580. }
  581. } else {
  582. $data = array($one => $two);
  583. }
  584. if ($data == null) {
  585. return false;
  586. }
  587. $this->viewVars = array_merge($this->viewVars, $data);
  588. }
  589. /**
  590. * Displays an error page to the user. Uses layouts/error.ctp to render the page.
  591. *
  592. * @param integer $code HTTP Error code (for instance: 404)
  593. * @param string $name Name of the error (for instance: Not Found)
  594. * @param string $message Error message as a web page
  595. */
  596. public function error($code, $name, $message) {
  597. header ("HTTP/1.1 {$code} {$name}");
  598. print ($this->_render(
  599. $this->_getLayoutFileName('error'),
  600. array('code' => $code, 'name' => $name, 'message' => $message)
  601. ));
  602. }
  603. /**
  604. * Renders and returns output for given view filename with its
  605. * array of data.
  606. *
  607. * @param string $___viewFn Filename of the view
  608. * @param array $___dataForView Data to include in rendered view
  609. * @return string Rendered output
  610. * @access protected
  611. */
  612. protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $cached = false) {
  613. $loadedHelpers = array();
  614. if ($this->helpers != false && $loadHelpers === true) {
  615. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  616. $helpers = array_keys($loadedHelpers);
  617. $helperNames = array_map(array('Inflector', 'variable'), $helpers);
  618. for ($i = count($helpers) - 1; $i >= 0; $i--) {
  619. $name = $helperNames[$i];
  620. $helper = $loadedHelpers[$helpers[$i]];
  621. if (!isset($___dataForView[$name])) {
  622. ${$name} = $helper;
  623. }
  624. $this->loaded[$helperNames[$i]] = $helper;
  625. $this->{$helpers[$i]} = $helper;
  626. }
  627. $this->_triggerHelpers('beforeRender');
  628. unset($name, $loadedHelpers, $helpers, $i, $helperNames, $helper);
  629. }
  630. extract($___dataForView, EXTR_SKIP);
  631. ob_start();
  632. if (Configure::read() > 0) {
  633. include ($___viewFn);
  634. } else {
  635. @include ($___viewFn);
  636. }
  637. if ($loadHelpers === true) {
  638. $this->_triggerHelpers('afterRender');
  639. }
  640. $out = ob_get_clean();
  641. $caching = (
  642. isset($this->loaded['cache']) &&
  643. (($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
  644. );
  645. if ($caching) {
  646. if (is_a($this->loaded['cache'], 'CacheHelper')) {
  647. $cache = $this->loaded['cache'];
  648. $cache->base = $this->base;
  649. $cache->here = $this->here;
  650. $cache->helpers = $this->helpers;
  651. $cache->action = $this->action;
  652. $cache->controllerName = $this->name;
  653. $cache->layout = $this->layout;
  654. $cache->cacheAction = $this->cacheAction;
  655. $cache->cache($___viewFn, $out, $cached);
  656. }
  657. }
  658. return $out;
  659. }
  660. /**
  661. * Loads helpers, with their dependencies.
  662. *
  663. * @param array $loaded List of helpers that are already loaded.
  664. * @param array $helpers List of helpers to load.
  665. * @param string $parent holds name of helper, if loaded helper has helpers
  666. * @return array
  667. * @access protected
  668. */
  669. public function _loadHelpers($loaded, $helpers, $parent = null) {
  670. if (empty($loaded)) {
  671. $helpers[] = 'Session';
  672. }
  673. foreach ($helpers as $i => $helper) {
  674. $options = array();
  675. if (!is_int($i)) {
  676. $options = $helper;
  677. $helper = $i;
  678. }
  679. list($plugin, $helper) = pluginSplit($helper, true, $this->plugin);
  680. $helperCn = $helper . 'Helper';
  681. if (!isset($loaded[$helper])) {
  682. if (!class_exists($helperCn)) {
  683. $isLoaded = false;
  684. if (!is_null($plugin)) {
  685. $isLoaded = App::import('Helper', $plugin . $helper);
  686. }
  687. if (!$isLoaded) {
  688. if (!App::import('Helper', $helper)) {
  689. $this->cakeError('missingHelperFile', array(array(
  690. 'helper' => $helper,
  691. 'file' => Inflector::underscore($helper) . '.php',
  692. 'base' => $this->base
  693. )));
  694. return false;
  695. }
  696. }
  697. if (!class_exists($helperCn)) {
  698. $this->cakeError('missingHelperClass', array(array(
  699. 'helper' => $helper,
  700. 'file' => Inflector::underscore($helper) . '.php',
  701. 'base' => $this->base
  702. )));
  703. return false;
  704. }
  705. }
  706. $loaded[$helper] = new $helperCn($options);
  707. $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'theme', 'plugin');
  708. $c = count($vars);
  709. for ($j = 0; $j < $c; $j++) {
  710. $loaded[$helper]->{$vars[$j]} = $this->{$vars[$j]};
  711. }
  712. if (!empty($this->validationErrors)) {
  713. $loaded[$helper]->validationErrors = $this->validationErrors;
  714. }
  715. if (is_array($loaded[$helper]->helpers) && !empty($loaded[$helper]->helpers)) {
  716. $loaded = $this->_loadHelpers($loaded, $loaded[$helper]->helpers, $helper);
  717. }
  718. }
  719. if (isset($loaded[$parent])) {
  720. $loaded[$parent]->{$helper} = $loaded[$helper];
  721. }
  722. }
  723. return $loaded;
  724. }
  725. /**
  726. * Returns filename of given action's template file (.ctp) as a string.
  727. * CamelCased action names will be under_scored! This means that you can have
  728. * LongActionNames that refer to long_action_names.ctp views.
  729. *
  730. * @param string $action Controller action to find template filename for
  731. * @return string Template filename
  732. * @access protected
  733. */
  734. protected function _getViewFileName($name = null) {
  735. $subDir = null;
  736. if (!is_null($this->subDir)) {
  737. $subDir = $this->subDir . DS;
  738. }
  739. if ($name === null) {
  740. $name = $this->action;
  741. }
  742. $name = str_replace('/', DS, $name);
  743. if (strpos($name, DS) === false && $name[0] !== '.') {
  744. $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
  745. } elseif (strpos($name, DS) !== false) {
  746. if ($name{0} === DS || $name{1} === ':') {
  747. if (is_file($name)) {
  748. return $name;
  749. }
  750. $name = trim($name, DS);
  751. } else if ($name[0] === '.') {
  752. $name = substr($name, 3);
  753. } else {
  754. $name = $this->viewPath . DS . $subDir . $name;
  755. }
  756. }
  757. $paths = $this->_paths(Inflector::underscore($this->plugin));
  758. $exts = array($this->ext);
  759. if ($this->ext !== '.ctp') {
  760. array_push($exts, '.ctp');
  761. }
  762. foreach ($exts as $ext) {
  763. foreach ($paths as $path) {
  764. if (file_exists($path . $name . $ext)) {
  765. return $path . $name . $ext;
  766. }
  767. }
  768. }
  769. $defaultPath = $paths[0];
  770. if ($this->plugin) {
  771. $pluginPaths = App::path('plugins');
  772. foreach ($paths as $path) {
  773. if (strpos($path, $pluginPaths[0]) === 0) {
  774. $defaultPath = $path;
  775. break;
  776. }
  777. }
  778. }
  779. return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
  780. }
  781. /**
  782. * Returns layout filename for this template as a string.
  783. *
  784. * @return string Filename for layout file (.ctp).
  785. * @access protected
  786. */
  787. protected function _getLayoutFileName($name = null) {
  788. if ($name === null) {
  789. $name = $this->layout;
  790. }
  791. $subDir = null;
  792. if (!is_null($this->layoutPath)) {
  793. $subDir = $this->layoutPath . DS;
  794. }
  795. $paths = $this->_paths(Inflector::underscore($this->plugin));
  796. $file = 'layouts' . DS . $subDir . $name;
  797. $exts = array($this->ext);
  798. if ($this->ext !== '.ctp') {
  799. array_push($exts, '.ctp');
  800. }
  801. foreach ($exts as $ext) {
  802. foreach ($paths as $path) {
  803. if (file_exists($path . $file . $ext)) {
  804. return $path . $file . $ext;
  805. }
  806. }
  807. }
  808. return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
  809. }
  810. /**
  811. * Return a misssing view error message
  812. *
  813. * @param string $viewFileName the filename that should exist
  814. * @return cakeError
  815. * @access protected
  816. */
  817. protected function _missingView($file, $error = 'missingView') {
  818. if ($error === 'missingView') {
  819. $this->cakeError('missingView', array(
  820. 'className' => $this->name,
  821. 'action' => $this->action,
  822. 'file' => $file,
  823. 'base' => $this->base
  824. ));
  825. return false;
  826. } elseif ($error === 'missingLayout') {
  827. $this->cakeError('missingLayout', array(
  828. 'layout' => $this->layout,
  829. 'file' => $file,
  830. 'base' => $this->base
  831. ));
  832. return false;
  833. }
  834. }
  835. /**
  836. * Return all possible paths to find view files in order
  837. *
  838. * @param string $plugin
  839. * @return array paths
  840. * @access protected
  841. */
  842. protected function _paths($plugin = null, $cached = true) {
  843. if ($plugin === null && $cached === true && !empty($this->__paths)) {
  844. return $this->__paths;
  845. }
  846. $paths = array();
  847. $viewPaths = App::path('views');
  848. $corePaths = array_flip(App::core('views'));
  849. if (!empty($plugin)) {
  850. $count = count($viewPaths);
  851. for ($i = 0; $i < $count; $i++) {
  852. if (!isset($corePaths[$viewPaths[$i]])) {
  853. $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
  854. }
  855. }
  856. $paths[] = App::pluginPath($plugin) . 'views' . DS;
  857. }
  858. $this->__paths = array_merge($paths, $viewPaths);
  859. return $this->__paths;
  860. }
  861. }
  862. ?>