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

/data/class/helper/SC_Helper_PageLayout.php

https://gitlab.com/raku.takayama/eccube-2_13
PHP | 385 lines | 197 code | 33 blank | 155 comment | 35 complexity | 3fd83e71f753551b2dfae5ab30033839 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of EC-CUBE
  4. *
  5. * Copyright(c) 2000-2014 LOCKON CO.,LTD. All Rights Reserved.
  6. *
  7. * http://www.lockon.co.jp/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. /**
  24. * Webページのレイアウト情報を制御するヘルパークラス.
  25. *
  26. * @package Helper
  27. * @author LOCKON CO.,LTD.
  28. * @version $Id:SC_Helper_PageLayout.php 15532 2007-08-31 14:39:46Z nanasess $
  29. */
  30. class SC_Helper_PageLayout
  31. {
  32. /**
  33. * ページのレイアウト情報を取得し, 設定する.
  34. *
  35. * 現在の URL に応じたページのレイアウト情報を取得し, LC_Page インスタンスに
  36. * 設定する.
  37. *
  38. * @access public
  39. * @param LC_Page $objPage LC_Page インスタンス
  40. * @param boolean $preview プレビュー表示の場合 true
  41. * @param string $url ページのURL($_SERVER['SCRIPT_NAME'] の情報)
  42. * @param integer $device_type_id 端末種別ID
  43. * @return void
  44. */
  45. public function sfGetPageLayout(&$objPage, $preview = false, $url = '', $device_type_id = DEVICE_TYPE_PC)
  46. {
  47. // URLを元にページ情報を取得
  48. if ($preview === false) {
  49. $url = preg_replace('|^' . preg_quote(ROOT_URLPATH) . '|', '', $url);
  50. $arrPageData = $this->getPageProperties($device_type_id, null, 'url = ?', array($url));
  51. // プレビューの場合は, プレビュー用のデータを取得
  52. } else {
  53. $arrPageData = $this->getPageProperties($device_type_id, 0);
  54. }
  55. if (empty($arrPageData)) {
  56. trigger_error('ページ情報を取得できませんでした。', E_USER_WARNING);
  57. }
  58. $objPage->tpl_mainpage = $this->getTemplatePath($device_type_id) . $arrPageData[0]['filename'] . '.tpl';
  59. if (!file_exists($objPage->tpl_mainpage)) {
  60. $msg = 'メイン部のテンプレートが存在しません。[' . $objPage->tpl_mainpage . ']';
  61. trigger_error($msg, E_USER_WARNING);
  62. }
  63. $objPage->arrPageLayout =& $arrPageData[0];
  64. if (strlen($objPage->arrPageLayout['author']) === 0) {
  65. $arrInfo = SC_Helper_DB_Ex::sfGetBasisData();
  66. $objPage->arrPageLayout['author'] = $arrInfo['company_name'];
  67. }
  68. // ページタイトルを設定
  69. if (SC_Utils_Ex::isBlank($objPage->tpl_title)) {
  70. $objPage->tpl_title = $objPage->arrPageLayout['page_name'];
  71. }
  72. // 該当ページのブロックを取得し, 配置する
  73. $masterData = new SC_DB_MasterData_Ex();
  74. $arrTarget = $masterData->getMasterData('mtb_target');
  75. $arrBlocs = $this->getBlocPositions($device_type_id, $objPage->arrPageLayout['page_id']);
  76. // 無効なプラグインのブロックを取り除く.
  77. $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
  78. $arrBlocs = $objPlugin->getEnableBlocs($arrBlocs);
  79. // php_path, tpl_path が存在するものを, 各ターゲットに配置
  80. foreach ($arrTarget as $target_id => $value) {
  81. foreach ($arrBlocs as $arrBloc) {
  82. if ($arrBloc['target_id'] != $target_id) {
  83. continue;
  84. }
  85. if (is_file($arrBloc['php_path'])
  86. || is_file($arrBloc['tpl_path'])) {
  87. $objPage->arrPageLayout[$arrTarget[$target_id]][] = $arrBloc;
  88. } else {
  89. $error = "ブロックが見つかりません\n"
  90. . 'tpl_path: ' . $arrBloc['tpl_path'] . "\n"
  91. . 'php_path: ' . $arrBloc['php_path'];
  92. trigger_error($error, E_USER_WARNING);
  93. }
  94. }
  95. }
  96. // カラム数を取得する
  97. $objPage->tpl_column_num = $this->getColumnNum($objPage->arrPageLayout);
  98. }
  99. /**
  100. * ページの属性を取得する.
  101. *
  102. * この関数は, dtb_pagelayout の情報を検索する.
  103. * $device_type_id は必須. デフォルト値は DEVICE_TYPE_PC.
  104. * $page_id が null の場合は, $page_id が 0 以外のものを検索する.
  105. *
  106. * @access public
  107. * @param integer $device_type_id 端末種別ID
  108. * @param integer $page_id ページID; null の場合は, 0 以外を検索する.
  109. * @param string $where 追加の検索条件
  110. * @param string[] $arrParams 追加の検索パラメーター
  111. * @return array ページ属性の配列
  112. */
  113. public function getPageProperties($device_type_id = DEVICE_TYPE_PC, $page_id = null, $where = '', $arrParams = array())
  114. {
  115. $objQuery =& SC_Query_Ex::getSingletonInstance();
  116. $where = 'device_type_id = ? ' . (SC_Utils_Ex::isBlank($where) ? $where : 'AND ' . $where);
  117. if ($page_id === null) {
  118. $where = 'page_id <> ? AND ' . $where;
  119. $page_id = 0;
  120. } else {
  121. $where = 'page_id = ? AND ' . $where;
  122. }
  123. $objQuery->setOrder('page_id');
  124. $arrParams = array_merge(array($page_id, $device_type_id), $arrParams);
  125. return $objQuery->select('*', 'dtb_pagelayout', $where, $arrParams);
  126. }
  127. /**
  128. * ブロック情報を取得する.
  129. *
  130. * @access public
  131. * @param integer $device_type_id 端末種別ID
  132. * @param string $where 追加の検索条件
  133. * @param array $arrParams 追加の検索パラメーター
  134. * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
  135. * @return array ブロック情報の配列
  136. */
  137. public function getBlocs($device_type_id = DEVICE_TYPE_PC, $where = '', $arrParams = array(), $has_realpath = true)
  138. {
  139. $objBloc = new SC_Helper_Bloc_Ex($device_type_id);
  140. $arrBlocs = $objBloc->getWhere($where, $arrParams);
  141. if ($has_realpath) {
  142. $this->setBlocPathTo($device_type_id, $arrBlocs);
  143. }
  144. return $arrBlocs;
  145. }
  146. /**
  147. * ブロック配置情報を取得する.
  148. *
  149. * @access public
  150. * @param integer $device_type_id 端末種別ID
  151. * @param integer $page_id ページID
  152. * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true
  153. * @return array 配置情報を含めたブロックの配列
  154. */
  155. public function getBlocPositions($device_type_id, $page_id, $has_realpath = true)
  156. {
  157. $objQuery =& SC_Query_Ex::getSingletonInstance();
  158. $table = <<< __EOF__
  159. dtb_blocposition AS pos
  160. JOIN dtb_bloc AS bloc
  161. ON bloc.bloc_id = pos.bloc_id
  162. AND bloc.device_type_id = pos.device_type_id
  163. __EOF__;
  164. $where = 'bloc.device_type_id = ? AND ((anywhere = 1 AND pos.page_id != 0) OR pos.page_id = ?)';
  165. $objQuery->setOrder('target_id, bloc_row');
  166. $arrBlocs = $objQuery->select('*', $table, $where, array($device_type_id, $page_id));
  167. if ($has_realpath) {
  168. $this->setBlocPathTo($device_type_id, $arrBlocs);
  169. }
  170. //全ページ設定と各ページのブロックの重複を削除
  171. $arrUniqBlocIds = array();
  172. foreach ($arrBlocs as $index => $arrBloc) {
  173. if ($arrBloc['anywhere'] == 1) {
  174. $arrUniqBlocIds[] = $arrBloc['bloc_id'];
  175. }
  176. }
  177. foreach ($arrBlocs as $bloc_index => $arrBlocData) {
  178. if (in_array($arrBlocData['bloc_id'], $arrUniqBlocIds) && $arrBlocData['anywhere'] == 0) {
  179. unset($arrBlocs[$bloc_index]);
  180. }
  181. }
  182. return $arrBlocs;
  183. }
  184. /**
  185. * ページ情報を削除する.
  186. *
  187. * XXX ファイルを確実に削除したかどうかのチェック
  188. *
  189. * @access public
  190. * @param integer $page_id ページID
  191. * @param integer $device_type_id 端末種別ID
  192. * @return integer 削除数
  193. */
  194. public function lfDelPageData($page_id, $device_type_id = DEVICE_TYPE_PC)
  195. {
  196. $objQuery =& SC_Query_Ex::getSingletonInstance();
  197. // page_id が空でない場合にはdeleteを実行
  198. if ($page_id != '') {
  199. $arrPageData = $this->getPageProperties($device_type_id, $page_id);
  200. $ret = $objQuery->delete('dtb_pagelayout', 'page_id = ? AND device_type_id = ?', array($page_id, $device_type_id));
  201. // ファイルの削除
  202. $this->lfDelFile($arrPageData[0]['filename'], $device_type_id);
  203. }
  204. return $ret;
  205. }
  206. /**
  207. * ページのファイルを削除する.
  208. *
  209. * dtb_pagelayout の削除後に呼び出すこと。
  210. *
  211. * @access private
  212. * @param string $filename
  213. * @param integer $device_type_id 端末種別ID
  214. * @return void // TODO boolean にするべき?
  215. */
  216. public function lfDelFile($filename, $device_type_id)
  217. {
  218. $objQuery =& SC_Query_Ex::getSingletonInstance();
  219. /*
  220. * 同名ファイルの使用件数
  221. * PHP ファイルは, 複数のデバイスで共有するため, device_type_id を条件に入れない
  222. */
  223. $exists = $objQuery->exists('dtb_pagelayout', 'filename = ?', array($filename));
  224. if (!$exists) {
  225. // phpファイルの削除
  226. $del_php = HTML_REALDIR . $filename . '.php';
  227. if (file_exists($del_php)) {
  228. unlink($del_php);
  229. }
  230. }
  231. // tplファイルの削除
  232. $del_tpl = $this->getTemplatePath($device_type_id) . $filename . '.tpl';
  233. if (file_exists($del_tpl)) {
  234. unlink($del_tpl);
  235. }
  236. }
  237. /**
  238. * 編集可能ページかどうか.
  239. *
  240. * @access public
  241. * @param integer $device_type_id 端末種別ID
  242. * @param integer $page_id ページID
  243. * @return boolean �合 true
  244. */
  245. public function isEditablePage($device_type_id, $page_id)
  246. {
  247. if ($page_id == 0) {
  248. return false;
  249. }
  250. $arrPages = $this->getPageProperties($device_type_id, $page_id);
  251. if ($arrPages[0]['edit_flg'] != 2) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. /**
  257. * テンプレートのパスを取得する.
  258. *
  259. * @access public
  260. * @param integer $device_type_id 端末種別ID
  261. * @param boolean $isUser USER_REALDIR 以下のパスを返す場合 true
  262. * @return string テンプレートのパス
  263. */
  264. public function getTemplatePath($device_type_id = DEVICE_TYPE_PC, $isUser = false)
  265. {
  266. $templateName = '';
  267. switch ($device_type_id) {
  268. case DEVICE_TYPE_MOBILE:
  269. $dir = MOBILE_TEMPLATE_REALDIR;
  270. $templateName = MOBILE_TEMPLATE_NAME;
  271. break;
  272. case DEVICE_TYPE_SMARTPHONE:
  273. $dir = SMARTPHONE_TEMPLATE_REALDIR;
  274. $templateName = SMARTPHONE_TEMPLATE_NAME;
  275. break;
  276. case DEVICE_TYPE_PC:
  277. default:
  278. $dir = TEMPLATE_REALDIR;
  279. $templateName = TEMPLATE_NAME;
  280. break;
  281. }
  282. $userPath = USER_REALDIR;
  283. if ($isUser) {
  284. $dir = $userPath . USER_PACKAGE_DIR . $templateName . '/';
  285. }
  286. return $dir;
  287. }
  288. /**
  289. * DocumentRoot から user_data のパスを取得する.
  290. *
  291. * 引数 $hasPackage を true にした場合は, user_data/packages/template_name
  292. * を取得する.
  293. *
  294. * @access public
  295. * @param integer $device_type_id 端末種別ID
  296. * @param boolean $hasPackage パッケージのパスも含める場合 true
  297. * @return string 端末に応じた DocumentRoot から user_data までのパス
  298. */
  299. public function getUserDir($device_type_id = DEVICE_TYPE_PC, $hasPackage = false)
  300. {
  301. switch ($device_type_id) {
  302. case DEVICE_TYPE_MOBILE:
  303. $templateName = MOBILE_TEMPLATE_NAME;
  304. break;
  305. case DEVICE_TYPE_SMARTPHONE:
  306. $templateName = SMARTPHONE_TEMPLATE_NAME;
  307. break;
  308. case DEVICE_TYPE_PC:
  309. default:
  310. $templateName = TEMPLATE_NAME;
  311. }
  312. $userDir = ROOT_URLPATH . USER_DIR;
  313. if ($hasPackage) {
  314. return $userDir . USER_PACKAGE_DIR . $templateName . '/';
  315. }
  316. return $userDir;
  317. }
  318. /**
  319. * ブロックの php_path, tpl_path を設定する.
  320. *
  321. * @access private
  322. * @param integer $device_type_id 端末種別ID
  323. * @param array $arrBlocs 設定するブロックの配列
  324. * @return void
  325. */
  326. public function setBlocPathTo($device_type_id = DEVICE_TYPE_PC, &$arrBlocs = array())
  327. {
  328. foreach ($arrBlocs as $key => $value) {
  329. $arrBloc =& $arrBlocs[$key];
  330. $arrBloc['php_path'] = SC_Utils_Ex::isBlank($arrBloc['php_path']) ? '' : HTML_REALDIR . $arrBloc['php_path'];
  331. $bloc_dir = $this->getTemplatePath($device_type_id) . BLOC_DIR;
  332. $arrBloc['tpl_path'] = SC_Utils_Ex::isBlank($arrBloc['tpl_path']) ? '' : $bloc_dir . $arrBloc['tpl_path'];
  333. }
  334. }
  335. /**
  336. * カラム数を取得する.
  337. *
  338. * @access private
  339. * @param array $arrPageLayout レイアウト情報の配列
  340. * @return integer $col_num カラム数
  341. */
  342. public function getColumnNum($arrPageLayout)
  343. {
  344. // メインは確定
  345. $col_num = 1;
  346. // LEFT NAVI
  347. if (count($arrPageLayout['LeftNavi']) > 0) $col_num++;
  348. // RIGHT NAVI
  349. if (count($arrPageLayout['RightNavi']) > 0) $col_num++;
  350. return $col_num;
  351. }
  352. }