PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/views/baser_app_view.php

https://github.com/hashing/basercms
PHP | 366 lines | 192 code | 33 blank | 141 comment | 52 complexity | b5f48f380efdd7569663b0e2aa1aec99 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * view 拡張クラス
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * Include files
  22. */
  23. App::import('Core','Theme');
  24. /**
  25. * view 拡張クラス
  26. *
  27. * @package baser.views
  28. */
  29. class BaserAppView extends ThemeView {
  30. /**
  31. * List of variables to collect from the associated controller
  32. *
  33. * @var array
  34. * @access protected
  35. */
  36. var $__passedVars = array(
  37. 'viewVars', 'action', 'autoLayout', 'autoRender', 'ext', 'base', 'webroot',
  38. 'helpers', 'here', 'layout', 'name', 'pageTitle', 'layoutPath', 'viewPath',
  39. 'params', 'data', 'plugin', 'passedArgs', 'cacheAction', 'subDir'
  40. );
  41. /**
  42. * Return all possible paths to find view files in order
  43. *
  44. * @param string $plugin
  45. * @return array paths
  46. * @access private
  47. */
  48. function _paths($plugin = null, $cached = true) {
  49. $paths = $this->__paths($plugin, $cached);
  50. if (!empty($this->theme)) {
  51. $count = count($paths);
  52. for ($i = 0; $i < $count; $i++) {
  53. // >>> CUSTOMIZE MODIFY 2011/03/24 ryuring
  54. // プラグインパスにテーマのパスを追加した為、
  55. // テーマのパスをさらにテーマのパスに整形しないように調整
  56. //$themePaths[] = $paths[$i] . 'themed'. DS . $this->theme . DS;
  57. // ---
  58. if(strpos($paths[$i],'themed') === false) {
  59. $themePaths[] = $paths[$i] . 'themed'. DS . $this->theme . DS;
  60. }
  61. // <<<
  62. }
  63. $paths = array_merge($themePaths, $paths);
  64. }
  65. if (empty($this->__paths)) {
  66. $this->__paths = $paths;
  67. }
  68. return $paths;
  69. }
  70. /**
  71. * Return all possible paths to find view files in order
  72. *
  73. * ※ _paths より直接呼び出されるようにする為だけに、Viewクラスより中身をコピー
  74. *
  75. * @param string $plugin
  76. * @return array paths
  77. * @access protected
  78. */
  79. function __paths($plugin = null, $cached = true) {
  80. if ($plugin === null && $cached === true && !empty($this->__paths)) {
  81. return $this->__paths;
  82. }
  83. $paths = array();
  84. $viewPaths = Configure::read('viewPaths');
  85. $corePaths = array_flip(Configure::corePaths('view'));
  86. if (!empty($plugin)) {
  87. $count = count($viewPaths);
  88. for ($i = 0; $i < $count; $i++) {
  89. if (!isset($corePaths[$viewPaths[$i]])) {
  90. $paths[] = $viewPaths[$i] . 'plugins' . DS . $plugin . DS;
  91. }
  92. }
  93. $pluginPaths = Configure::read('pluginPaths');
  94. $count = count($pluginPaths);
  95. for ($i = 0; $i < $count; $i++) {
  96. $paths[] = $pluginPaths[$i] . $plugin . DS . 'views' . DS;
  97. }
  98. }
  99. $paths = array_merge($paths, $viewPaths);
  100. if (empty($this->__paths)) {
  101. $this->__paths = $paths;
  102. }
  103. return $paths;
  104. }
  105. /**
  106. * フック処理を実行する
  107. *
  108. * @param string $out
  109. * @return mixed
  110. */
  111. function executeHook($hook, $out) {
  112. return $this->loaded['pluginHook']->{$hook}($out);
  113. }
  114. /**
  115. * Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
  116. *
  117. * This realizes the concept of Elements, (or "partial layouts")
  118. * and the $params array is used to send data to be used in the
  119. * Element. Elements can be cached through use of the cache key.
  120. *
  121. * @param string $name Name of template file in the/app/views/elements/ folder
  122. * @param array $params Array of data to be made available to the for rendered
  123. * view (i.e. the Element)
  124. * Special params:
  125. * cache - enable caching for this element accepts boolean or strtotime compatible string.
  126. * Can also be an array
  127. * if an array,'time' is used to specify duration of cache. 'key' can be used to
  128. * create unique cache files.
  129. *
  130. * @return string Rendered Element
  131. * @access public
  132. */
  133. function element($name, $params = array(), $loadHelpers = false) {
  134. $file = $plugin = $key = null;
  135. if (isset($params['plugin'])) {
  136. $plugin = $params['plugin'];
  137. }
  138. if (isset($this->plugin) && !$plugin) {
  139. $plugin = $this->plugin;
  140. }
  141. if (isset($params['cache'])) {
  142. $expires = '+1 day';
  143. if (is_array($params['cache'])) {
  144. $expires = $params['cache']['time'];
  145. $key = Inflector::slug($params['cache']['key']);
  146. } elseif ($params['cache'] !== true) {
  147. $expires = $params['cache'];
  148. $key = implode('_', array_keys($params));
  149. }
  150. if ($expires) {
  151. $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
  152. $cache = cache('views' . DS . $cacheFile, null, $expires);
  153. if (is_string($cache)) {
  154. return $cache;
  155. }
  156. }
  157. }
  158. $paths = $this->_paths($plugin);
  159. // CUSTOMIZE MODIFY 2012/04/11 ryuring
  160. // 後方互換保持の為、ファイル走査の優先順位に.ctpを追加
  161. // @deprecated .php への移行を推奨
  162. // >>>
  163. /*foreach ($paths as $path) {
  164. if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
  165. $file = $path . 'elements' . DS . $name . $this->ext;
  166. break;
  167. } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
  168. $file = $path . 'elements' . DS . $name . '.thtml';
  169. break;
  170. }
  171. }*/
  172. // ---
  173. foreach ($paths as $path) {
  174. if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
  175. $file = $path . 'elements' . DS . $name . $this->ext;
  176. break;
  177. }elseif (file_exists($path . 'elements' . DS . $name . '.ctp')) {
  178. trigger_error('エレメントテンプレートの拡張子 .ctp は非推奨です。.php を利用してください。<br />'.$path . 'elements' . DS . $name . '.ctp', E_USER_WARNING);
  179. $file = $path . 'elements' . DS . $name . '.ctp';
  180. break;
  181. } elseif (file_exists($path . 'elements' . DS . $name . '.thtml')) {
  182. $file = $path . 'elements' . DS . $name . '.thtml';
  183. break;
  184. }
  185. }
  186. // <<<
  187. if (is_file($file)) {
  188. $params = array_merge_recursive($params, $this->loaded);
  189. $element = $this->_render($file, array_merge($this->viewVars, $params), $loadHelpers);
  190. if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
  191. cache('views' . DS . $cacheFile, $element, $expires);
  192. }
  193. return $element;
  194. }
  195. $file = $paths[0] . 'elements' . DS . $name . $this->ext;
  196. if (Configure::read() > 0) {
  197. return "Not Found: " . $file;
  198. }
  199. }
  200. /**
  201. * Returns filename of given action's template file (.ctp) as a string.
  202. * CamelCased action names will be under_scored! This means that you can have
  203. * LongActionNames that refer to long_action_names.ctp views.
  204. *
  205. * @param string $action Controller action to find template filename for
  206. * @return string Template filename
  207. * @access protected
  208. */
  209. function _getViewFileName($name = null) {
  210. // CUSTOMIZE ADD 2012/04/11 ryuring
  211. // プレフィックスが設定されている場合は、プレフィックスを除外する
  212. // >>>
  213. if(!$name && isset($this->params['prefix'])) {
  214. $prefix = $this->params['prefix'];
  215. if(preg_match('/^'.$prefix.'_/', $this->action)) {
  216. $name = str_replace($prefix.'_','',$this->action);
  217. } elseif(preg_match('/^admin_/', $this->action)) {
  218. // プレフィックスをadminとしてすり替え
  219. $name = str_replace('admin_','',$this->action);
  220. }
  221. }
  222. if($this->name == 'CakeError' && $this->viewPath == 'errors') {
  223. // CakeErrorの場合はサブフォルダを除外
  224. $subDir = $this->subDir;
  225. $this->subDir = '';
  226. $fileName = parent::_getViewFileName($name);
  227. $this->subDir = $subDir;
  228. return $fileName;
  229. }
  230. // <<<
  231. $subDir = null;
  232. if (!is_null($this->subDir)) {
  233. $subDir = $this->subDir . DS;
  234. }
  235. if ($name === null) {
  236. $name = $this->action;
  237. }
  238. $name = str_replace('/', DS, $name);
  239. if (strpos($name, DS) === false && $name[0] !== '.') {
  240. $name = $this->viewPath . DS . $subDir . Inflector::underscore($name);
  241. } elseif (strpos($name, DS) !== false) {
  242. if ($name{0} === DS || $name{1} === ':') {
  243. if (is_file($name)) {
  244. return $name;
  245. }
  246. $name = trim($name, DS);
  247. } else if ($name[0] === '.') {
  248. $name = substr($name, 3);
  249. } else {
  250. $name = $this->viewPath . DS . $subDir . $name;
  251. }
  252. }
  253. $paths = $this->_paths(Inflector::underscore($this->plugin));
  254. $exts = array($this->ext, '.ctp', '.thtml');
  255. // CUSTOMIZE MODIFY 2012/04/11 ryuring
  256. // 拡張子優先順位よりもパスの優先順位を優先する仕様に変更
  257. // @deprecated .php への移行を推奨
  258. // >>>
  259. /*foreach ($exts as $ext) {
  260. foreach ($paths as $path) {
  261. if (file_exists($path . $name . $ext)) {
  262. return $path . $name . $ext;
  263. }
  264. }
  265. }*/
  266. // ---
  267. foreach ($paths as $path) {
  268. foreach ($exts as $ext) {
  269. if (file_exists($path . $name . $ext)) {
  270. if($ext == '.ctp') {
  271. trigger_error('ビューテンプレートの拡張子 .ctp は非推奨です。.php を利用してください。<br />'.$path . $name . $ext, E_USER_WARNING);
  272. }
  273. return $path . $name . $ext;
  274. }
  275. }
  276. }
  277. // <<<
  278. $defaultPath = $paths[0];
  279. if ($this->plugin) {
  280. $pluginPaths = Configure::read('pluginPaths');
  281. foreach ($paths as $path) {
  282. if (strpos($path, $pluginPaths[0]) === 0) {
  283. $defaultPath = $path;
  284. break;
  285. }
  286. }
  287. }
  288. return $this->_missingView($defaultPath . $name . $this->ext, 'missingView');
  289. }
  290. /**
  291. * Returns layout filename for this template as a string.
  292. *
  293. * @return string Filename for layout file (.ctp).
  294. * @access protected
  295. */
  296. function _getLayoutFileName($name = null) {
  297. if ($name === null) {
  298. $name = $this->layout;
  299. }
  300. $subDir = null;
  301. if (!is_null($this->layoutPath)) {
  302. $subDir = $this->layoutPath . DS;
  303. }
  304. $paths = $this->_paths(Inflector::underscore($this->plugin));
  305. $file = 'layouts' . DS . $subDir . $name;
  306. $exts = array($this->ext, '.ctp', '.thtml');
  307. // CUSTOMIZE MODIFY 2012/04/11 ryuring
  308. // 拡張子優先順位よりもパスの優先順位を優先する仕様に変更
  309. // @deprecated .php への移行を推奨
  310. // >>>
  311. /*foreach ($exts as $ext) {
  312. foreach ($paths as $path) {
  313. if (file_exists($path . $file . $ext)) {
  314. return $path . $file . $ext;
  315. }
  316. }
  317. }*/
  318. // ---
  319. foreach ($paths as $path) {
  320. foreach ($exts as $ext) {
  321. if (file_exists($path . $file . $ext)) {
  322. if($ext == '.ctp') {
  323. trigger_error('レイアウトテンプレートの拡張子 .ctp は非推奨です。.php を利用してください。<br />'.$path . $file . $ext, E_USER_WARNING);
  324. }
  325. return $path . $file . $ext;
  326. }
  327. }
  328. }
  329. // <<<
  330. return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
  331. }
  332. }
  333. ?>