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