View.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <?php
  2. /**
  3. * Methods for displaying presentation data in the view.
  4. *
  5. * PHP 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.libs.view
  16. * @since CakePHP(tm) v 0.10.0.1076
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Included libraries.
  21. */
  22. App::uses('HelperCollection', 'View');
  23. App::uses('Router', 'Routing');
  24. /**
  25. * View, the V in the MVC triad. View interacts with Helpers and view variables passed
  26. * in from the controller to render the results of the controller action. Often this is HTML,
  27. * but can also take the form of JSON, XML, PDF's or streaming files.
  28. *
  29. * CakePHP uses a two-step-view pattern. This means that the view content is rendered first,
  30. * and then inserted into the selected layout. A special `$content_for_layout` variable is available
  31. * in the layout, and it contains the rendered view. This also means you can pass data from the view to the
  32. * layout using `$this->set()`
  33. *
  34. * @package cake.libs.view
  35. */
  36. class View extends Object {
  37. /**
  38. * Helpers collection
  39. *
  40. * @var HelperCollection
  41. */
  42. public $Helpers;
  43. /**
  44. * Name of the plugin.
  45. *
  46. * @link http://manual.cakephp.org/chapter/plugins
  47. * @var string
  48. */
  49. public $plugin = null;
  50. /**
  51. * Name of the controller.
  52. *
  53. * @var string Name of controller
  54. */
  55. public $name = null;
  56. /**
  57. * Current passed params
  58. *
  59. * @var mixed
  60. */
  61. public $passedArgs = array();
  62. /**
  63. * An array of names of built-in helpers to include.
  64. *
  65. * @var mixed A single name as a string or a list of names as an array.
  66. */
  67. public $helpers = array('Html');
  68. /**
  69. * Path to View.
  70. *
  71. * @var string Path to View
  72. */
  73. public $viewPath = null;
  74. /**
  75. * Variables for the view
  76. *
  77. * @var array
  78. */
  79. public $viewVars = array();
  80. /**
  81. * Name of view to use with this View.
  82. *
  83. * @var string
  84. */
  85. public $view = null;
  86. /**
  87. * Name of layout to use with this View.
  88. *
  89. * @var string
  90. */
  91. public $layout = 'default';
  92. /**
  93. * Path to Layout.
  94. *
  95. * @var string Path to Layout
  96. */
  97. public $layoutPath = null;
  98. /**
  99. * Turns on or off Cake's conventional mode of applying layout files. On by default.
  100. * Setting to off means that layouts will not be automatically applied to rendered views.
  101. *
  102. * @var boolean
  103. */
  104. public $autoLayout = true;
  105. /**
  106. * File extension. Defaults to Cake's template ".ctp".
  107. *
  108. * @var string
  109. */
  110. public $ext = '.ctp';
  111. /**
  112. * Sub-directory for this view file. This is often used for extension based routing.
  113. * for example with an `xml` extension, $subDir would be `xml/`
  114. *
  115. * @var string
  116. */
  117. public $subDir = null;
  118. /**
  119. * Theme name. If you are using themes, you should remember to use ThemeView as well.
  120. *
  121. * @var string
  122. */
  123. public $theme = null;
  124. /**
  125. * Used to define methods a controller that will be cached.
  126. *
  127. * @see Controller::$cacheAction
  128. * @var mixed
  129. */
  130. public $cacheAction = false;
  131. /**
  132. * holds current errors for the model validation
  133. *
  134. * @var array
  135. */
  136. public $validationErrors = array();
  137. /**
  138. * True when the view has been rendered.
  139. *
  140. * @var boolean
  141. */
  142. public $hasRendered = false;
  143. /**
  144. * True if in scope of model-specific region
  145. *
  146. * @var boolean
  147. */
  148. public $modelScope = false;
  149. /**
  150. * Name of current model this view context is attached to
  151. *
  152. * @var string
  153. */
  154. public $model = null;
  155. /**
  156. * Name of association model this view context is attached to
  157. *
  158. * @var string
  159. */
  160. public $association = null;
  161. /**
  162. * Name of current model field this view context is attached to
  163. *
  164. * @var string
  165. */
  166. public $field = null;
  167. /**
  168. * Suffix of current field this view context is attached to
  169. *
  170. * @var string
  171. */
  172. public $fieldSuffix = null;
  173. /**
  174. * The current model ID this view context is attached to
  175. *
  176. * @var mixed
  177. */
  178. public $modelId = null;
  179. /**
  180. * List of generated DOM UUIDs
  181. *
  182. * @var array
  183. */
  184. public $uuids = array();
  185. /**
  186. * Holds View output.
  187. *
  188. * @var string
  189. */
  190. public $output = false;
  191. /**
  192. * An instance of a CakeRequest object that contains information about the current request.
  193. * This object contains all the information about a request and several methods for reading
  194. * additional information about the request.
  195. *
  196. * @var CakeRequest
  197. */
  198. public $request;
  199. /**
  200. * The Cache configuration View will use to store cached elements. Changing this will change
  201. * the default configuration elements are stored under. You can also choose a cache config
  202. * per element.
  203. *
  204. * @var string
  205. * @see View::element()
  206. */
  207. public $elementCache = 'default';
  208. /**
  209. * List of variables to collect from the associated controller
  210. *
  211. * @var array
  212. */
  213. private $__passedVars = array(
  214. 'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name',
  215. 'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
  216. );
  217. /**
  218. * Scripts (and/or other <head /> tags) for the layout
  219. *
  220. * @var array
  221. */
  222. protected $_scripts = array();
  223. /**
  224. * Holds an array of paths.
  225. *
  226. * @var array
  227. */
  228. private $__paths = array();
  229. /**
  230. * boolean to indicate that helpers have been loaded.
  231. *
  232. * @var boolean
  233. */
  234. protected $_helpersLoaded = false;
  235. /**
  236. * Constructor
  237. *
  238. * @param Controller $controller A controller object to pull View::__passedArgs from.
  239. */
  240. function __construct($controller) {
  241. if (is_object($controller)) {
  242. $count = count($this->__passedVars);
  243. for ($j = 0; $j < $count; $j++) {
  244. $var = $this->__passedVars[$j];
  245. $this->{$var} = $controller->{$var};
  246. }
  247. }
  248. $this->Helpers = new HelperCollection($this);
  249. parent::__construct();
  250. }
  251. /**
  252. * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
  253. *
  254. * This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
  255. * data to be used in the element. Elements can be cached improving performance by using the `cache` option.
  256. *
  257. * ### Special params
  258. *
  259. * - `cache` - Can either be `true`, to enable caching using the config in View::$elementCache. Or an array
  260. * If an array, the following keys can be used:
  261. * - `config` - Used to store the cached element in a custom cache configuration.
  262. * - `key` - Used to define the key used in the Cache::write(). It will be prefixed with `element_`
  263. * - `plugin` - Load an element from a specific plugin.
  264. *
  265. * @param string $name Name of template file in the/app/views/elements/ folder
  266. * @param array $params Array of data to be made available to the for rendered
  267. * view (i.e. the Element)
  268. * @param boolean $callbacks Set to true to fire beforeRender and afterRender helper callbacks for this element.
  269. * Defaults to false.
  270. * @return string Rendered Element
  271. */
  272. public function element($name, $params = array(), $callbacks = false) {
  273. $file = $plugin = $key = null;
  274. if (isset($params['plugin'])) {
  275. $plugin = $params['plugin'];
  276. }
  277. if (isset($this->plugin) && !$plugin) {
  278. $plugin = $this->plugin;
  279. }
  280. if (isset($params['cache'])) {
  281. $keys = array_merge(array($plugin, $name), array_keys($params));
  282. $caching = array(
  283. 'config' => $this->elementCache,
  284. 'key' => implode('_', $keys)
  285. );
  286. if (is_array($params['cache'])) {
  287. $defaults = array(
  288. 'config' => $this->elementCache,
  289. 'key' => $caching['key']
  290. );
  291. $caching = array_merge($defaults, $params['cache']);
  292. }
  293. $key = 'element_' . $caching['key'];
  294. $contents = Cache::read($key, $caching['config']);
  295. if ($contents !== false) {
  296. return $contents;
  297. }
  298. }
  299. $file = $this->_getElementFilename($name, $plugin);
  300. if ($file) {
  301. if (!$this->_helpersLoaded) {
  302. $this->loadHelpers();
  303. }
  304. if ($callbacks) {
  305. $this->Helpers->trigger('beforeRender', array($file));
  306. }
  307. $element = $this->_render($file, array_merge($this->viewVars, $params));
  308. if ($callbacks) {
  309. $this->Helpers->trigger('afterRender', array($file, $element));
  310. }
  311. if (isset($params['cache'])) {
  312. Cache::write($key, $element, $caching['config']);
  313. }
  314. return $element;
  315. }
  316. $file = 'elements' . DS . $name . $this->ext;
  317. if (Configure::read('debug') > 0) {
  318. return "Element Not Found: " . $file;
  319. }
  320. }
  321. /**
  322. * Renders view for given view file and layout.
  323. *
  324. * Render triggers helper callbacks, which are fired before and after the view are rendered,
  325. * as well as before and after the layout. The helper callbacks are called
  326. *
  327. * - `beforeRender`
  328. * - `afterRender`
  329. * - `beforeLayout`
  330. * - `afterLayout`
  331. *
  332. * If View::$autoRender is false and no `$layout` is provided, the view will be returned bare.
  333. *
  334. * @param string $view Name of view file to use
  335. * @param string $layout Layout to use.
  336. * @return string Rendered Element
  337. * @throws CakeException if there is an error in the view.
  338. */
  339. public function render($view = null, $layout = null) {
  340. if ($this->hasRendered) {
  341. return true;
  342. }
  343. if (!$this->_helpersLoaded) {
  344. $this->loadHelpers();
  345. }
  346. $this->output = null;
  347. if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
  348. $this->Helpers->trigger('beforeRender', array($viewFileName));
  349. $this->output = $this->_render($viewFileName);
  350. $this->Helpers->trigger('afterRender', array($viewFileName));
  351. }
  352. if ($layout === null) {
  353. $layout = $this->layout;
  354. }
  355. if ($this->output === false) {
  356. throw new CakeException(__("Error in view %s, got no content.", $viewFileName));
  357. }
  358. if ($layout && $this->autoLayout) {
  359. $this->output = $this->renderLayout($this->output, $layout);
  360. }
  361. $this->hasRendered = true;
  362. return $this->output;
  363. }
  364. /**
  365. * Renders a layout. Returns output from _render(). Returns false on error.
  366. * Several variables are created for use in layout.
  367. *
  368. * - `title_for_layout` - A backwards compatible place holder, you should set this value if you want more control.
  369. * - `content_for_layout` - contains rendered view file
  370. * - `scripts_for_layout` - contains scripts added to header
  371. *
  372. * @param string $content_for_layout Content to render in a view, wrapped by the surrounding layout.
  373. * @return mixed Rendered output, or false on error
  374. * @throws CakeException if there is an error in the view.
  375. */
  376. public function renderLayout($content_for_layout, $layout = null) {
  377. $layoutFileName = $this->_getLayoutFileName($layout);
  378. if (empty($layoutFileName)) {
  379. return $this->output;
  380. }
  381. if (!$this->_helpersLoaded) {
  382. $this->loadHelpers();
  383. }
  384. $this->Helpers->trigger('beforeLayout', array($layoutFileName));
  385. $this->viewVars = array_merge($this->viewVars, array(
  386. 'content_for_layout' => $content_for_layout,
  387. 'scripts_for_layout' => implode("\n\t", $this->_scripts),
  388. ));
  389. if (!isset($this->viewVars['title_for_layout'])) {
  390. $this->viewVars['title_for_layout'] = Inflector::humanize($this->viewPath);
  391. }
  392. $this->output = $this->_render($layoutFileName);
  393. if ($this->output === false) {
  394. throw new CakeException(__("Error in layout %s, got no content.", $layoutFileName));
  395. }
  396. $this->Helpers->trigger('afterLayout', array($layoutFileName));
  397. return $this->output;
  398. }
  399. /**
  400. * Render cached view. Works in concert with CacheHelper and Dispatcher to
  401. * render cached view files.
  402. *
  403. * @param string $filename the cache file to include
  404. * @param string $timeStart the page render start time
  405. * @return boolean Success of rendering the cached file.
  406. */
  407. public function renderCache($filename, $timeStart) {
  408. ob_start();
  409. include ($filename);
  410. if (Configure::read('debug') > 0 && $this->layout != 'xml') {
  411. echo "<!-- Cached Render Time: " . round(microtime(true) - $timeStart, 4) . "s -->";
  412. }
  413. $out = ob_get_clean();
  414. if (preg_match('/^<!--cachetime:(\\d+)-->/', $out, $match)) {
  415. if (time() >= $match['1']) {
  416. @unlink($filename);
  417. unset ($out);
  418. return false;
  419. } else {
  420. if ($this->layout === 'xml') {
  421. header('Content-type: text/xml');
  422. }
  423. $commentLength = strlen('<!--cachetime:' . $match['1'] . '-->');
  424. echo substr($out, $commentLength);
  425. return true;
  426. }
  427. }
  428. }
  429. /**
  430. * Returns a list of variables available in the current View context
  431. *
  432. * @return array Array of the set view variable names.
  433. */
  434. public function getVars() {
  435. return array_keys($this->viewVars);
  436. }
  437. /**
  438. * Returns the contents of the given View variable(s)
  439. *
  440. * @param string $var The view var you want the contents of.
  441. * @return mixed The content of the named var if its set, otherwise null.
  442. */
  443. public function getVar($var) {
  444. if (!isset($this->viewVars[$var])) {
  445. return null;
  446. } else {
  447. return $this->viewVars[$var];
  448. }
  449. }
  450. /**
  451. * Adds a script block or other element to be inserted in $scripts_for_layout in
  452. * the `<head />` of a document layout
  453. *
  454. * @param string $name Either the key name for the script, or the script content. Name can be used to
  455. * update/replace a script element.
  456. * @param string $content The content of the script being added, optional.
  457. * @return void
  458. */
  459. public function addScript($name, $content = null) {
  460. if (empty($content)) {
  461. if (!in_array($name, array_values($this->_scripts))) {
  462. $this->_scripts[] = $name;
  463. }
  464. } else {
  465. $this->_scripts[$name] = $content;
  466. }
  467. }
  468. /**
  469. * Generates a unique, non-random DOM ID for an object, based on the object type and the target URL.
  470. *
  471. * @param string $object Type of object, i.e. 'form' or 'link'
  472. * @param string $url The object's target URL
  473. * @return string
  474. */
  475. public function uuid($object, $url) {
  476. $c = 1;
  477. $url = Router::url($url);
  478. $hash = $object . substr(md5($object . $url), 0, 10);
  479. while (in_array($hash, $this->uuids)) {
  480. $hash = $object . substr(md5($object . $url . $c), 0, 10);
  481. $c++;
  482. }
  483. $this->uuids[] = $hash;
  484. return $hash;
  485. }
  486. /**
  487. * Returns the entity reference of the current context as an array of identity parts
  488. *
  489. * @return array An array containing the identity elements of an entity
  490. */
  491. public function entity() {
  492. $assoc = ($this->association) ? $this->association : $this->model;
  493. if (!empty($this->entityPath)) {
  494. $path = explode('.', $this->entityPath);
  495. $count = count($path);
  496. if (
  497. ($count == 1 && !empty($this->association)) ||
  498. ($count == 1 && $this->model != $this->entityPath) ||
  499. ($count == 1 && empty($this->association) && !empty($this->field)) ||
  500. ($count == 2 && !empty($this->fieldSuffix)) ||
  501. is_numeric($path[0]) && !empty($assoc)
  502. ) {
  503. array_unshift($path, $assoc);
  504. }
  505. return Set::filter($path);
  506. }
  507. return array_values(Set::filter(
  508. array($assoc, $this->modelId, $this->field, $this->fieldSuffix)
  509. ));
  510. }
  511. /**
  512. * Allows a template or element to set a variable that will be available in
  513. * a layout or other element. Analagous to Controller::set().
  514. *
  515. * @param mixed $one A string or an array of data.
  516. * @param mixed $two Value in case $one is a string (which then works as the key).
  517. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  518. * @return void
  519. */
  520. public function set($one, $two = null) {
  521. $data = null;
  522. if (is_array($one)) {
  523. if (is_array($two)) {
  524. $data = array_combine($one, $two);
  525. } else {
  526. $data = $one;
  527. }
  528. } else {
  529. $data = array($one => $two);
  530. }
  531. if ($data == null) {
  532. return false;
  533. }
  534. $this->viewVars = $data + $this->viewVars;
  535. }
  536. /**
  537. * Magic accessor for helpers. Provides access to attributes that were deprecated.
  538. *
  539. * @param string $name Name of the attribute to get.
  540. * @return mixed
  541. */
  542. public function __get($name) {
  543. if (isset($this->Helpers->{$name})) {
  544. return $this->Helpers->{$name};
  545. }
  546. switch ($name) {
  547. case 'base':
  548. case 'here':
  549. case 'webroot':
  550. case 'data':
  551. return $this->request->{$name};
  552. case 'action':
  553. return isset($this->request->params['action']) ? $this->request->params['action'] : '';
  554. case 'params':
  555. return $this->request;
  556. }
  557. return null;
  558. }
  559. /**
  560. * Interact with the HelperCollection to load all the helpers.
  561. *
  562. * @return void
  563. */
  564. public function loadHelpers() {
  565. $helpers = HelperCollection::normalizeObjectArray($this->helpers);
  566. foreach ($helpers as $name => $properties) {
  567. list($plugin, $class) = pluginSplit($properties['class']);
  568. $this->{$class} = $this->Helpers->load($properties['class'], $properties['settings']);
  569. }
  570. $this->_helpersLoaded = true;
  571. }
  572. /**
  573. * Renders and returns output for given view filename with its
  574. * array of data.
  575. *
  576. * @param string $___viewFn Filename of the view
  577. * @param array $___dataForView Data to include in rendered view. If empty the current View::$viewVars will be used.
  578. * @return string Rendered output
  579. */
  580. protected function _render($___viewFn, $___dataForView = array()) {
  581. if (empty($___dataForView)) {
  582. $___dataForView = $this->viewVars;
  583. }
  584. extract($___dataForView, EXTR_SKIP);
  585. ob_start();
  586. include $___viewFn;
  587. return ob_get_clean();
  588. }
  589. /**
  590. * Loads a helper. Delegates to the `HelperCollection::load()` to load the helper
  591. *
  592. * @param string $helperName Name of the helper to load.
  593. * @param array $settings Settings for the helper
  594. * @return Helper a constructed helper object.
  595. * @see HelperCollection::load()
  596. */
  597. public function loadHelper($helperName, $settings = array()) {
  598. return $this->Helpers->load($helperName, $settings);
  599. }
  600. /**
  601. * Returns filename of given action's template file (.ctp) as a string.
  602. * CamelCased action names will be under_scored! This means that you can have
  603. * LongActionNames that refer to long_action_names.ctp views.
  604. *
  605. * @param string $name Controller action to find template filename for
  606. * @return string Template filename
  607. * @throws MissingViewException when a view file could not be found.
  608. */
  609. protected function _getViewFileName($name = null) {
  610. $subDir = null;
  611. if (!is_null($this->subDir)) {
  612. $subDir = $this->subDir . DS;
  613. }
  614. if ($name === null) {
  615. $name = $this->view;
  616. }
  617. $name = str_replace('/', DS, $name);
  618. if (strpos($name, DS) === false && $name[0] !== '.') {
  619. $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
  620. } elseif (strpos($name, DS) !== false) {
  621. if ($name[0] === DS || $name[1] === ':') {
  622. if (is_file($name)) {
  623. return $name;
  624. }
  625. $name = trim($name, DS);
  626. } else if ($name[0] === '.') {
  627. $name = substr($name, 3);
  628. } else {
  629. $name = $this->viewPath . DS . $subDir . $name;
  630. }
  631. }
  632. $paths = $this->_paths($this->plugin);
  633. $exts = $this->_getExtensions();
  634. foreach ($exts as $ext) {
  635. foreach ($paths as $path) {
  636. if (file_exists($path . $name . $ext)) {
  637. return $path . $name . $ext;
  638. }
  639. }
  640. }
  641. $defaultPath = $paths[0];
  642. if ($this->plugin) {
  643. $pluginPaths = App::path('plugins');
  644. foreach ($paths as $path) {
  645. if (strpos($path, $pluginPaths[0]) === 0) {
  646. $defaultPath = $path;
  647. break;
  648. }
  649. }
  650. }
  651. throw new MissingViewException(array('file' => $defaultPath . $name . $this->ext));
  652. }
  653. /**
  654. * Returns layout filename for this template as a string.
  655. *
  656. * @param string $name The name of the layout to find.
  657. * @return string Filename for layout file (.ctp).
  658. * @throws MissingLayoutException when a layout cannot be located
  659. */
  660. protected function _getLayoutFileName($name = null) {
  661. if ($name === null) {
  662. $name = $this->layout;
  663. }
  664. $subDir = null;
  665. if (!is_null($this->layoutPath)) {
  666. $subDir = $this->layoutPath . DS;
  667. }
  668. $paths = $this->_paths($this->plugin);
  669. $file = 'layouts' . DS . $subDir . $name;
  670. $exts = $this->_getExtensions();
  671. foreach ($exts as $ext) {
  672. foreach ($paths as $path) {
  673. if (file_exists($path . $file . $ext)) {
  674. return $path . $file . $ext;
  675. }
  676. }
  677. }
  678. throw new MissingLayoutException(array('file' => $paths[0] . $file . $this->ext));
  679. }
  680. /**
  681. * Get the extensions that view files can use.
  682. *
  683. * @return array Array of extensions view files use.
  684. * @access protected
  685. */
  686. function _getExtensions() {
  687. $exts = array($this->ext);
  688. if ($this->ext !== '.ctp') {
  689. array_push($exts, '.ctp');
  690. }
  691. return $exts;
  692. }
  693. /**
  694. * Finds an element filename, returns false on failure.
  695. *
  696. * @param string $name The name of the element to find.
  697. * @param string $plugin The plugin name the element is in.
  698. * @return mixed Either a string to the element filename or false when one can't be found.
  699. */
  700. protected function _getElementFileName($name, $plugin = null) {
  701. $paths = $this->_paths($plugin);
  702. $exts = $this->_getExtensions();
  703. foreach ($exts as $ext) {
  704. foreach ($paths as $path) {
  705. if (file_exists($path . 'elements' . DS . $name . $ext)) {
  706. return $path . 'elements' . DS . $name . $ext;
  707. }
  708. }
  709. }
  710. return false;
  711. }
  712. /**
  713. * Return all possible paths to find view files in order
  714. *
  715. * @param string $plugin Optional plugin name to scan for view files.
  716. * @param boolean $cached Set to true to force a refresh of view paths.
  717. * @return array paths
  718. */
  719. protected function _paths($plugin = null, $cached = true) {
  720. if ($plugin === null && $cached === true && !empty($this->__paths)) {
  721. return $this->__paths;
  722. }
  723. $paths = array();
  724. $viewPaths = App::path('View');
  725. $corePaths = array_flip(App::core('View'));
  726. if (!empty($plugin)) {
  727. $count = count($viewPaths);
  728. for ($i = 0; $i < $count; $i++) {
  729. if (!isset($corePaths[$viewPaths[$i]])) {
  730. $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
  731. }
  732. }
  733. $paths = array_merge($paths, App::path('View', $plugin));
  734. }
  735. $this->__paths = array_unique(array_merge($paths, $viewPaths, array_keys($corePaths)));
  736. return $this->__paths;
  737. }
  738. }