PageRenderTime 46ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/libs/view/view.php

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