| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2013 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 | /** |
|---|
| 25 | * Webページのレイアウト情報を制御するヘルパークラス. |
|---|
| 26 | * |
|---|
| 27 | * @package Helper |
|---|
| 28 | * @author LOCKON CO.,LTD. |
|---|
| 29 | * @version $Id:SC_Helper_PageLayout.php 15532 2007-08-31 14:39:46Z nanasess $ |
|---|
| 30 | */ |
|---|
| 31 | class SC_Helper_PageLayout { |
|---|
| 32 | |
|---|
| 33 | // }}} |
|---|
| 34 | // {{{ functions |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * ページのレイアウト情報を取得し, 設定する. |
|---|
| 38 | * |
|---|
| 39 | * 現在の URL に応じたページのレイアウト情報を取得し, LC_Page インスタンスに |
|---|
| 40 | * 設定する. |
|---|
| 41 | * |
|---|
| 42 | * @access public |
|---|
| 43 | * @param LC_Page $objPage LC_Page インスタンス |
|---|
| 44 | * @param boolean $preview プレビュー表示の場合 true |
|---|
| 45 | * @param string $url ページのURL($_SERVER['SCRIPT_NAME'] の情報) |
|---|
| 46 | * @param integer $device_type_id 端末種別ID |
|---|
| 47 | * @return void |
|---|
| 48 | */ |
|---|
| 49 | function sfGetPageLayout(&$objPage, $preview = false, $url = '', $device_type_id = DEVICE_TYPE_PC) { |
|---|
| 50 | |
|---|
| 51 | // URLを元にページ情報を取得 |
|---|
| 52 | if ($preview === false) { |
|---|
| 53 | $url = preg_replace('|^' . preg_quote(ROOT_URLPATH) . '|', '', $url); |
|---|
| 54 | $arrPageData = $this->getPageProperties($device_type_id, null, 'url = ?', array($url)); |
|---|
| 55 | } |
|---|
| 56 | // プレビューの場合は, プレビュー用のデータを取得 |
|---|
| 57 | else { |
|---|
| 58 | $arrPageData = $this->getPageProperties($device_type_id, 0); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $objPage->tpl_mainpage = $this->getTemplatePath($device_type_id) . $arrPageData[0]['filename'] . '.tpl'; |
|---|
| 62 | $objPage->arrPageLayout =& $arrPageData[0]; |
|---|
| 63 | |
|---|
| 64 | // ページタイトルを設定 |
|---|
| 65 | if (SC_Utils_Ex::isBlank($objPage->tpl_title)) { |
|---|
| 66 | $objPage->tpl_title = $objPage->arrPageLayout['page_name']; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | // 該当ページのブロックを取得し, 配置する |
|---|
| 70 | $masterData = new SC_DB_MasterData_Ex(); |
|---|
| 71 | $arrTarget = $masterData->getMasterData('mtb_target'); |
|---|
| 72 | $arrBlocs = $this->getBlocPositions($device_type_id, $objPage->arrPageLayout['page_id']); |
|---|
| 73 | // 無効なプラグインのブロックを取り除く. |
|---|
| 74 | $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(); |
|---|
| 75 | $arrBlocs = $objPlugin->getEnableBlocs($arrBlocs); |
|---|
| 76 | // php_path, tpl_path が存在するものを, 各ターゲットに配置 |
|---|
| 77 | foreach ($arrTarget as $target_id => $value) { |
|---|
| 78 | foreach ($arrBlocs as $arrBloc) { |
|---|
| 79 | if ($arrBloc['target_id'] != $target_id) { |
|---|
| 80 | continue; |
|---|
| 81 | } |
|---|
| 82 | if (is_file($arrBloc['php_path']) |
|---|
| 83 | || is_file($arrBloc['tpl_path'])) { |
|---|
| 84 | $objPage->arrPageLayout[$arrTarget[$target_id]][] = $arrBloc; |
|---|
| 85 | } else { |
|---|
| 86 | $error = "ブロックが見つかりません\n" |
|---|
| 87 | . 'tpl_path: ' . $arrBloc['tpl_path'] . "\n" |
|---|
| 88 | . 'php_path: ' . $arrBloc['php_path']; |
|---|
| 89 | GC_Utils_Ex::gfPrintLog($error); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | // カラム数を取得する |
|---|
| 94 | $objPage->tpl_column_num = $this->getColumnNum($objPage->arrPageLayout); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * ページの属性を取得する. |
|---|
| 99 | * |
|---|
| 100 | * この関数は, dtb_pagelayout の情報を検索する. |
|---|
| 101 | * $device_type_id は必須. デフォルト値は DEVICE_TYPE_PC. |
|---|
| 102 | * $page_id が null の場合は, $page_id が 0 以外のものを検索する. |
|---|
| 103 | * |
|---|
| 104 | * @access public |
|---|
| 105 | * @param integer $device_type_id 端末種別ID |
|---|
| 106 | * @param integer $page_id ページID; null の場合は, 0 以外を検索する. |
|---|
| 107 | * @param string $where 追加の検索条件 |
|---|
| 108 | * @param array $arrParams 追加の検索パラメーター |
|---|
| 109 | * @return array ページ属性の配列 |
|---|
| 110 | */ |
|---|
| 111 | function getPageProperties($device_type_id = DEVICE_TYPE_PC, $page_id = null, $where = '', $arrParams = array()) { |
|---|
| 112 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 113 | $where = 'device_type_id = ? ' . (SC_Utils_Ex::isBlank($where) ? $where : 'AND ' . $where); |
|---|
| 114 | if ($page_id === null) { |
|---|
| 115 | $where = 'page_id <> ? AND ' . $where; |
|---|
| 116 | $page_id = 0; |
|---|
| 117 | } else { |
|---|
| 118 | $where = 'page_id = ? AND ' . $where; |
|---|
| 119 | } |
|---|
| 120 | $objQuery->setOrder('page_id'); |
|---|
| 121 | $arrParams = array_merge(array($page_id, $device_type_id), $arrParams); |
|---|
| 122 | return $objQuery->select('*', 'dtb_pagelayout', $where, $arrParams); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * ブロック情報を取得する. |
|---|
| 127 | * |
|---|
| 128 | * @access public |
|---|
| 129 | * @param integer $device_type_id 端末種別ID |
|---|
| 130 | * @param string $where 追加の検索条件 |
|---|
| 131 | * @param array $arrParams 追加の検索パラメーター |
|---|
| 132 | * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true |
|---|
| 133 | * @return array ブロック情報の配列 |
|---|
| 134 | */ |
|---|
| 135 | function getBlocs($device_type_id = DEVICE_TYPE_PC, $where = '', $arrParams = array(), $has_realpath = true) { |
|---|
| 136 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 137 | $where = 'device_type_id = ? ' . (SC_Utils_Ex::isBlank($where) ? $where : 'AND ' . $where); |
|---|
| 138 | $arrParams = array_merge(array($device_type_id), $arrParams); |
|---|
| 139 | $objQuery->setOrder('bloc_id'); |
|---|
| 140 | $arrBlocs = $objQuery->select('*', 'dtb_bloc', $where, $arrParams); |
|---|
| 141 | if ($has_realpath) { |
|---|
| 142 | $this->setBlocPathTo($device_type_id, $arrBlocs); |
|---|
| 143 | } |
|---|
| 144 | return $arrBlocs; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** |
|---|
| 148 | * ブロック配置情報を取得する. |
|---|
| 149 | * |
|---|
| 150 | * @access public |
|---|
| 151 | * @param integer $device_type_id 端末種別ID |
|---|
| 152 | * @param integer $page_id ページID |
|---|
| 153 | * @param boolean $has_realpath php_path, tpl_path の絶対パスを含める場合 true |
|---|
| 154 | * @return array 配置情報を含めたブロックの配列 |
|---|
| 155 | */ |
|---|
| 156 | function getBlocPositions($device_type_id, $page_id, $has_realpath = true) { |
|---|
| 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 | //全ページ設定と各ページのブロックの重複を削除 |
|---|
| 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 | * |
|---|
| 188 | * XXX ファイルを確実に削除したかどうかのチェック |
|---|
| 189 | * |
|---|
| 190 | * @access public |
|---|
| 191 | * @param integer $page_id ページID |
|---|
| 192 | * @param integer $device_type_id 端末種別ID |
|---|
| 193 | * @return integer 削除数 |
|---|
| 194 | */ |
|---|
| 195 | function lfDelPageData($page_id, $device_type_id = DEVICE_TYPE_PC) { |
|---|
| 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 | * |
|---|
| 210 | * dtb_pagelayout の削除後に呼び出すこと。 |
|---|
| 211 | * |
|---|
| 212 | * @access private |
|---|
| 213 | * @param string $filename |
|---|
| 214 | * @param integer $device_type_id 端末種別ID |
|---|
| 215 | * @return void // TODO boolean にするべき? |
|---|
| 216 | */ |
|---|
| 217 | function lfDelFile($filename, $device_type_id) { |
|---|
| 218 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
|---|
| 219 | |
|---|
| 220 | /* |
|---|
| 221 | * 同名ファイルの使用件数 |
|---|
| 222 | * PHP ファイルは, 複数のデバイスで共有するため, device_type_id を条件に入れない |
|---|
| 223 | */ |
|---|
| 224 | $exists = $objQuery->exists('dtb_pagelayout', 'filename = ?', array($filename)); |
|---|
| 225 | |
|---|
| 226 | if (!$exists) { |
|---|
| 227 | // phpファイルの削除 |
|---|
| 228 | $del_php = HTML_REALDIR . $filename . '.php'; |
|---|
| 229 | if (file_exists($del_php)) { |
|---|
| 230 | unlink($del_php); |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | // tplファイルの削除 |
|---|
| 235 | $del_tpl = $this->getTemplatePath($device_type_id) . $filename . '.tpl'; |
|---|
| 236 | if (file_exists($del_tpl)) { |
|---|
| 237 | unlink($del_tpl); |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * 編集可能ページかどうか. |
|---|
| 243 | * |
|---|
| 244 | * @access public |
|---|
| 245 | * @param integer $device_type_id 端末種別ID |
|---|
| 246 | * @param integer $page_id ページID |
|---|
| 247 | * @return 編集可能ページの場合 true |
|---|
| 248 | */ |
|---|
| 249 | function isEditablePage($device_type_id, $page_id) { |
|---|
| 250 | if ($page_id == 0) { |
|---|
| 251 | return false; |
|---|
| 252 | } |
|---|
| 253 | $arrPages = $this->getPageProperties($device_type_id, $page_id); |
|---|
| 254 | if ($arrPages[0]['edit_flg'] != 2) { |
|---|
| 255 | return true; |
|---|
| 256 | } |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * テンプレートのパスを取得する. |
|---|
| 262 | * |
|---|
| 263 | * @access public |
|---|
| 264 | * @param integer $device_type_id 端末種別ID |
|---|
| 265 | * @param boolean $isUser USER_REALDIR 以下のパスを返す場合 true |
|---|
| 266 | * @return string テンプレートのパス |
|---|
| 267 | */ |
|---|
| 268 | function getTemplatePath($device_type_id = DEVICE_TYPE_PC, $isUser = false) { |
|---|
| 269 | $templateName = ''; |
|---|
| 270 | switch ($device_type_id) { |
|---|
| 271 | case DEVICE_TYPE_MOBILE: |
|---|
| 272 | $dir = MOBILE_TEMPLATE_REALDIR; |
|---|
| 273 | $templateName = MOBILE_TEMPLATE_NAME; |
|---|
| 274 | break; |
|---|
| 275 | |
|---|
| 276 | case DEVICE_TYPE_SMARTPHONE: |
|---|
| 277 | $dir = SMARTPHONE_TEMPLATE_REALDIR; |
|---|
| 278 | $templateName = SMARTPHONE_TEMPLATE_NAME; |
|---|
| 279 | break; |
|---|
| 280 | |
|---|
| 281 | case DEVICE_TYPE_PC: |
|---|
| 282 | default: |
|---|
| 283 | $dir = TEMPLATE_REALDIR; |
|---|
| 284 | $templateName = TEMPLATE_NAME; |
|---|
| 285 | break; |
|---|
| 286 | } |
|---|
| 287 | $userPath = USER_REALDIR; |
|---|
| 288 | if ($isUser) { |
|---|
| 289 | $dir = $userPath . USER_PACKAGE_DIR . $templateName . '/'; |
|---|
| 290 | } |
|---|
| 291 | return $dir; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * DocumentRoot から user_data のパスを取得する. |
|---|
| 296 | * |
|---|
| 297 | * 引数 $hasPackage を true にした場合は, user_data/packages/template_name |
|---|
| 298 | * を取得する. |
|---|
| 299 | * |
|---|
| 300 | * @access public |
|---|
| 301 | * @param integer $device_type_id 端末種別ID |
|---|
| 302 | * @param boolean $hasPackage パッケージのパスも含める場合 true |
|---|
| 303 | * @return string 端末に応じた DocumentRoot から user_data までのパス |
|---|
| 304 | */ |
|---|
| 305 | function getUserDir($device_type_id = DEVICE_TYPE_PC, $hasPackage = false) { |
|---|
| 306 | switch ($device_type_id) { |
|---|
| 307 | case DEVICE_TYPE_MOBILE: |
|---|
| 308 | $templateName = MOBILE_TEMPLATE_NAME; |
|---|
| 309 | break; |
|---|
| 310 | |
|---|
| 311 | case DEVICE_TYPE_SMARTPHONE: |
|---|
| 312 | $templateName = SMARTPHONE_TEMPLATE_NAME; |
|---|
| 313 | break; |
|---|
| 314 | |
|---|
| 315 | case DEVICE_TYPE_PC: |
|---|
| 316 | default: |
|---|
| 317 | $templateName = TEMPLATE_NAME; |
|---|
| 318 | } |
|---|
| 319 | $userDir = ROOT_URLPATH . USER_DIR; |
|---|
| 320 | if ($hasPackage) { |
|---|
| 321 | return $userDir . USER_PACKAGE_DIR . $templateName . '/'; |
|---|
| 322 | } |
|---|
| 323 | return $userDir; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | // }}} |
|---|
| 327 | // {{{ private functions |
|---|
| 328 | |
|---|
| 329 | /** |
|---|
| 330 | * ブロックの php_path, tpl_path を設定する. |
|---|
| 331 | * |
|---|
| 332 | * @access private |
|---|
| 333 | * @param integer $device_type_id 端末種別ID |
|---|
| 334 | * @param array $arrBlocs 設定するブロックの配列 |
|---|
| 335 | * @return void |
|---|
| 336 | */ |
|---|
| 337 | function setBlocPathTo($device_type_id = DEVICE_TYPE_PC, &$arrBlocs = array()) { |
|---|
| 338 | foreach ($arrBlocs as $key => $value) { |
|---|
| 339 | $arrBloc =& $arrBlocs[$key]; |
|---|
| 340 | $arrBloc['php_path'] = SC_Utils_Ex::isBlank($arrBloc['php_path']) ? '' : HTML_REALDIR . $arrBloc['php_path']; |
|---|
| 341 | $bloc_dir = $this->getTemplatePath($device_type_id) . BLOC_DIR; |
|---|
| 342 | $arrBloc['tpl_path'] = SC_Utils_Ex::isBlank($arrBloc['tpl_path']) ? '' : $bloc_dir . $arrBloc['tpl_path']; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * カラム数を取得する. |
|---|
| 348 | * |
|---|
| 349 | * @access private |
|---|
| 350 | * @param array $arrPageLayout レイアウト情報の配列 |
|---|
| 351 | * @return integer $col_num カラム数 |
|---|
| 352 | */ |
|---|
| 353 | function getColumnNum($arrPageLayout) { |
|---|
| 354 | // メインは確定 |
|---|
| 355 | $col_num = 1; |
|---|
| 356 | // LEFT NAVI |
|---|
| 357 | if (count($arrPageLayout['LeftNavi']) > 0) $col_num++; |
|---|
| 358 | // RIGHT NAVI |
|---|
| 359 | if (count($arrPageLayout['RightNavi']) > 0) $col_num++; |
|---|
| 360 | |
|---|
| 361 | return $col_num; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|