PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/view/view.php

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