1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2011 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 | require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php'; |
---|
25 | |
---|
26 | /** |
---|
27 | * テンプレート設定 のページクラス. |
---|
28 | * |
---|
29 | * @package Page |
---|
30 | * @author LOCKON CO.,LTD. |
---|
31 | * @version $Id$ |
---|
32 | */ |
---|
33 | class LC_Page_Admin_Design_Template extends LC_Page_Admin_Ex { |
---|
34 | |
---|
35 | // }}} |
---|
36 | // {{{ functions |
---|
37 | |
---|
38 | /** |
---|
39 | * Page を初期化する. |
---|
40 | * |
---|
41 | * @return void |
---|
42 | */ |
---|
43 | function init() { |
---|
44 | parent::init(); |
---|
45 | $this->tpl_mainpage = 'design/template.tpl'; |
---|
46 | $this->tpl_subno = 'template'; |
---|
47 | $this->tpl_mainno = 'design'; |
---|
48 | $this->tpl_maintitle = 'デザイン管理'; |
---|
49 | $this->tpl_subtitle = 'テンプレート設定'; |
---|
50 | $this->arrErr = array(); |
---|
51 | $this->arrForm = array(); |
---|
52 | ini_set('max_execution_time', 300); |
---|
53 | $masterData = new SC_DB_MasterData_Ex(); |
---|
54 | $this->arrDeviceType = $masterData->getMasterData('mtb_device_type'); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Page のプロセス. |
---|
59 | * |
---|
60 | * @return void |
---|
61 | */ |
---|
62 | function process() { |
---|
63 | $this->action(); |
---|
64 | $this->sendResponse(); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Page のアクション. |
---|
69 | * |
---|
70 | * @return void |
---|
71 | */ |
---|
72 | function action() { |
---|
73 | // フックポイント. |
---|
74 | $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(); |
---|
75 | $objPlugin->doAction('lc_page_admin_design_template_action_start', array($this)); |
---|
76 | |
---|
77 | $objFormParam = new SC_FormParam_Ex(); |
---|
78 | $this->lfInitParam($objFormParam); |
---|
79 | $objFormParam->setParam($_REQUEST); |
---|
80 | $objFormParam->convParam(); |
---|
81 | |
---|
82 | $this->device_type_id = $objFormParam->getValue('device_type_id', DEVICE_TYPE_PC); |
---|
83 | $this->tpl_select = $this->getTemplateName($this->device_type_id); |
---|
84 | $template_code = $objFormParam->getValue('template_code'); |
---|
85 | |
---|
86 | switch ($this->getMode()) { |
---|
87 | |
---|
88 | // 登録ボタン押下時 |
---|
89 | case 'register': |
---|
90 | $this->arrErr = $objFormParam->checkError(); |
---|
91 | if (SC_Utils_Ex::isBlank($this->arrErr)) { |
---|
92 | if ($this->doRegister($template_code, $this->device_type_id)) { |
---|
93 | $this->tpl_select = $template_code; |
---|
94 | $this->tpl_onload = "alert('登録が完了しました。');"; |
---|
95 | } |
---|
96 | } |
---|
97 | break; |
---|
98 | |
---|
99 | // 削除ボタン押下時 |
---|
100 | case 'delete': |
---|
101 | if ($objFormParam->checkError()) { |
---|
102 | SC_Utils_Ex::sfDispError(''); |
---|
103 | } |
---|
104 | $this->arrErr = $objFormParam->checkError(); |
---|
105 | if (SC_Utils_Ex::isBlank($this->arrErr)) { |
---|
106 | if ($this->doDelete($template_code, $this->device_type_id)) { |
---|
107 | $this->tpl_onload = "alert('削除が完了しました。');"; |
---|
108 | } |
---|
109 | } |
---|
110 | break; |
---|
111 | |
---|
112 | // downloadボタン押下時 |
---|
113 | case 'download': |
---|
114 | $this->arrErr = $objFormParam->checkError(); |
---|
115 | if (SC_Utils_Ex::isBlank($this->arrErr)) { |
---|
116 | // フックポイント. |
---|
117 | $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(); |
---|
118 | $objPlugin->doAction('lc_page_admin_design_template_action_download', array($this)); |
---|
119 | |
---|
120 | if ($this->doDownload($template_code) !== false) { |
---|
121 | // ブラウザに出力し, 終了する |
---|
122 | exit; |
---|
123 | } |
---|
124 | } |
---|
125 | break; |
---|
126 | |
---|
127 | default: |
---|
128 | break; |
---|
129 | } |
---|
130 | |
---|
131 | if (!$is_error) { |
---|
132 | $this->templates = $this->getAllTemplates($this->device_type_id); |
---|
133 | } else { |
---|
134 | // 画面にエラー表示しないため, ログ出力 |
---|
135 | GC_Utils_Ex::gfPrintLog('Error: ' . print_r($this->arrErr, true)); |
---|
136 | } |
---|
137 | $this->tpl_subtitle = $this->arrDeviceType[$this->device_type_id] . '>' . $this->tpl_subtitle; |
---|
138 | |
---|
139 | // フックポイント. |
---|
140 | $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(); |
---|
141 | $objPlugin->doAction('lc_page_admin_design_template_action_end', array($this)); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * デストラクタ. |
---|
146 | * |
---|
147 | * @return void |
---|
148 | */ |
---|
149 | function destroy() { |
---|
150 | parent::destroy(); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * パラメーター情報の初期化 |
---|
155 | * |
---|
156 | * @param object $objFormParam SC_FormParamインスタンス |
---|
157 | * @return void |
---|
158 | */ |
---|
159 | function lfInitParam(&$objFormParam) { |
---|
160 | $objFormParam->addParam('端末種別ID', 'device_type_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK')); |
---|
161 | $objFormParam->addParam('template_code', 'template_code', STEXT_LEN, 'a', array('EXIST_CHECK', 'SPTAB_CHECK','MAX_LENGTH_CHECK', 'ALNUM_CHECK')); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * 使用するテンプレートを設定する. |
---|
166 | * |
---|
167 | * テンプレートをマスターデータに登録する. |
---|
168 | * |
---|
169 | * @param string $template_code テンプレートコード |
---|
170 | * @param integer $device_type_id 端末種別ID |
---|
171 | * @return void |
---|
172 | */ |
---|
173 | function doUpdateMasterData($template_code, $device_type_id) { |
---|
174 | $masterData = new SC_DB_MasterData_Ex(); |
---|
175 | |
---|
176 | $defineName = 'TEMPLATE_NAME'; |
---|
177 | switch ($device_type_id) { |
---|
178 | case DEVICE_TYPE_MOBILE: |
---|
179 | $defineName = 'MOBILE_' . $defineName; |
---|
180 | break; |
---|
181 | |
---|
182 | case DEVICE_TYPE_SMARTPHONE: |
---|
183 | $defineName = 'SMARTPHONE_' . $defineName; |
---|
184 | break; |
---|
185 | |
---|
186 | case DEVICE_TYPE_PC: |
---|
187 | default: |
---|
188 | break; |
---|
189 | } |
---|
190 | |
---|
191 | // DBのデータを更新 |
---|
192 | $arrData = array($defineName => var_export($template_code, true)); |
---|
193 | $masterData->updateMasterData('mtb_constants', array(), $arrData); |
---|
194 | |
---|
195 | // キャッシュを生成 |
---|
196 | $masterData->createCache('mtb_constants', array(), true, array('id', 'remarks')); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * ブロック位置の更新. |
---|
201 | * |
---|
202 | * ブロック位置を更新する SQL を実行する. |
---|
203 | * この SQL は, 各端末に合わせて実行する必要がある |
---|
204 | * |
---|
205 | * @param string $filepath SQLのファイルパス |
---|
206 | * @return void |
---|
207 | */ |
---|
208 | function updateBloc($filepath) { |
---|
209 | $sql = file_get_contents($filepath); |
---|
210 | if ($sql !== false) { |
---|
211 | // 改行、タブを1スペースに変換 |
---|
212 | $sql = preg_replace("/[\r\n\t]/", ' ' ,$sql); |
---|
213 | $sql_split = explode(';', $sql); |
---|
214 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
215 | foreach ($sql_split as $val) { |
---|
216 | if (trim($val) != '') { |
---|
217 | $objQuery->query($val); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * テンプレートパッケージの削除. |
---|
225 | * |
---|
226 | * @param string $template_code テンプレートコード |
---|
227 | * @param integer $device_type_id 端末種別ID |
---|
228 | * @return boolean 成功した場合 true; 失敗した場合 false |
---|
229 | */ |
---|
230 | function doDelete($template_code, $device_type_id) { |
---|
231 | if ($template_code == $this->getTemplateName($device_type_id) |
---|
232 | || $template_code == $this->getTemplateName($device_type_id, true)) { |
---|
233 | $this->arrErr['err'] = '※ デフォルトテンプレートと、選択中のテンプレートは削除出来ません<br />'; |
---|
234 | return false; |
---|
235 | } else { |
---|
236 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
237 | $objQuery->begin(); |
---|
238 | $objQuery->delete('dtb_templates', 'template_code = ? AND device_type_id = ?', |
---|
239 | array($template_code, $device_type_id)); |
---|
240 | |
---|
241 | $error = '※ テンプレートの削除ができませんでした<br />'; |
---|
242 | // テンプレート削除 |
---|
243 | $templates_dir = SMARTY_TEMPLATES_REALDIR . $template_code. '/'; |
---|
244 | if (SC_Utils_Ex::sfDelFile($templates_dir) === false) { |
---|
245 | $this->arrErr['err'] = $error; |
---|
246 | $objQuery->rollback(); |
---|
247 | return false; |
---|
248 | } |
---|
249 | // ユーザーデータ削除 |
---|
250 | $user_dir = USER_TEMPLATE_REALDIR. $template_code. '/'; |
---|
251 | if (SC_Utils_Ex::sfDelFile($user_dir) === false) { |
---|
252 | $this->arrErr['err'] = $error; |
---|
253 | $objQuery->rollback(); |
---|
254 | return false; |
---|
255 | } |
---|
256 | |
---|
257 | // コンパイル削除 |
---|
258 | $templates_c_dir = DATA_REALDIR. 'Smarty/templates_c/'. $template_code. '/'; |
---|
259 | if (SC_Utils_Ex::sfDelFile($templates_c_dir) === false) { |
---|
260 | $this->arrErr['err'] = $error; |
---|
261 | $objQuery->rollback(); |
---|
262 | return false; |
---|
263 | } |
---|
264 | $objQuery->commit(); |
---|
265 | return true; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * 登録を実行する. |
---|
271 | * |
---|
272 | * 失敗した場合は, エラーメッセージを出力し, false を返す. |
---|
273 | * |
---|
274 | * @param string $template_code テンプレートコード |
---|
275 | * @param integer $device_type_id 端末種別ID |
---|
276 | * @return boolean 成功した場合 true; 失敗した場合 false |
---|
277 | */ |
---|
278 | function doRegister($template_code, $device_type_id) { |
---|
279 | |
---|
280 | $tpl_dir = USER_TEMPLATE_REALDIR . $template_code . '/'; |
---|
281 | if (!is_dir($tpl_dir)) { |
---|
282 | $this->arrErr['err'] = '※ ' . $tpl_dir . 'が見つかりません<br />'; |
---|
283 | return false; |
---|
284 | } |
---|
285 | |
---|
286 | // 更新SQLファイルが存在する場合はブロック位置を更新 |
---|
287 | $sql_file = $tpl_dir . 'sql/update_bloc.sql'; |
---|
288 | if (file_exists($sql_file)) { |
---|
289 | $this->updateBloc($sql_file); |
---|
290 | } |
---|
291 | |
---|
292 | $this->doUpdateMasterData($template_code, $device_type_id); |
---|
293 | // コンパイルファイルのクリア処理 |
---|
294 | $objView = new SC_AdminView_Ex(); |
---|
295 | $objView->_smarty->clear_compiled_tpl(); |
---|
296 | return true; |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * ダウンロードを実行する. |
---|
301 | * |
---|
302 | * 指定のテンプレートをアーカイブし, ブラウザに出力する. |
---|
303 | * 失敗した場合は, エラーメッセージを出力し, false を返す. |
---|
304 | * |
---|
305 | * @param string $template_code テンプレートコード |
---|
306 | * @return boolean 成功した場合 true; 失敗した場合 false |
---|
307 | */ |
---|
308 | function doDownload($template_code) { |
---|
309 | $from_dir = USER_TEMPLATE_REALDIR . $template_code . '/'; |
---|
310 | $to_dir = SMARTY_TEMPLATES_REALDIR . $template_code . '/_packages/'; |
---|
311 | if (SC_Utils_Ex::recursiveMkdir($to_dir) === false) { |
---|
312 | $this->arrErr['err'] = '※ ディレクトリの作成に失敗しました<br />'; |
---|
313 | return false; |
---|
314 | } |
---|
315 | SC_Utils_Ex::sfCopyDir($from_dir, $to_dir); |
---|
316 | if (SC_Helper_FileManager_Ex::downloadArchiveFiles(SMARTY_TEMPLATES_REALDIR . $template_code, $template_code) === false) { |
---|
317 | $this->arrErr['err'] = '※ アーカイブファイルの作成に失敗しました<br />'; |
---|
318 | return false; |
---|
319 | } |
---|
320 | return true; |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * テンプレート情報を取得する. |
---|
325 | * |
---|
326 | * @param integer $device_type_id 端末種別ID |
---|
327 | * @return array テンプレート情報の配列 |
---|
328 | */ |
---|
329 | function getAllTemplates($device_type_id) { |
---|
330 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
331 | return $objQuery->select('*', 'dtb_templates', 'device_type_id = ?', array($device_type_id)); |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | * テンプレート名を返す. |
---|
336 | * |
---|
337 | * @param integer $device_type_id 端末種別ID |
---|
338 | * @param boolean $isDefault デフォルトテンプレート名を返す場合 true |
---|
339 | * @return string テンプレート名 |
---|
340 | */ |
---|
341 | function getTemplateName($device_type_id, $isDefault = false) { |
---|
342 | switch ($device_type_id) { |
---|
343 | case DEVICE_TYPE_MOBILE: |
---|
344 | return $isDefault ? MOBILE_DEFAULT_TEMPLATE_NAME : MOBILE_TEMPLATE_NAME; |
---|
345 | |
---|
346 | case DEVICE_TYPE_SMARTPHONE: |
---|
347 | return $isDefault ? SMARTPHONE_DEFAULT_TEMPLATE_NAME : SMARTPHONE_TEMPLATE_NAME; |
---|
348 | |
---|
349 | case DEVICE_TYPE_PC: |
---|
350 | default: |
---|
351 | break; |
---|
352 | } |
---|
353 | return $isDefault ? DEFAULT_TEMPLATE_NAME : TEMPLATE_NAME; |
---|
354 | } |
---|
355 | } |
---|