source: branches/version-2_12-multilang/data/class/pages/admin/design/LC_Page_Admin_Design_MainEdit.php @ 22100

Revision 22100, 14.5 KB checked in by pineray, 11 years ago (diff)

#163 (テキスト出力多言語対応)

グローバル化に伴い、不要となった SC_I18n を削除.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2012 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/**
28 * メイン編集 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Admin_Design_MainEdit extends LC_Page_Admin_Ex {
35
36    // }}}
37    // {{{ functions
38
39    /**
40     * Page を初期化する.
41     *
42     * @return void
43     */
44    function init() {
45        parent::init();
46        $this->tpl_mainpage = 'design/main_edit.tpl';
47        $this->text_row     = 13;
48        $this->tpl_subno = 'main_edit';
49        $this->tpl_mainno = 'design';
50        $this->tpl_maintitle = t('TPL_MAINTITLE_003');
51        $this->tpl_subtitle = t('LC_Page_Admin_Design_MainEdit_002');
52        $masterData = new SC_DB_MasterData_Ex();
53        $this->arrDeviceType = $masterData->getMasterData('mtb_device_type');
54    }
55
56    /**
57     * Page のプロセス.
58     *
59     * @return void
60     */
61    function process() {
62        $this->action();
63        $this->sendResponse();
64    }
65
66    /**
67     * Page のアクション.
68     *
69     * @return void
70     */
71    function action() {
72
73        $objLayout = new SC_Helper_PageLayout_Ex();
74        $objFormParam = new SC_FormParam_Ex();
75        $this->lfInitParam($objFormParam);
76        $objFormParam->setParam($_REQUEST);
77        $objFormParam->convParam();
78        $this->arrErr = $objFormParam->checkError();
79        $is_error = (!SC_Utils_Ex::isBlank($this->arrErr));
80
81        $this->device_type_id = $objFormParam->getValue('device_type_id', DEVICE_TYPE_PC);
82        $this->page_id = $objFormParam->getValue('page_id');
83
84        switch ($this->getMode()) {
85            // 削除
86            case 'delete':
87                if (!$is_error) {
88                    if ($objLayout->isEditablePage($this->device_type_id, $this->page_id)) {
89                        $objLayout->lfDelPageData($this->page_id, $this->device_type_id);
90
91                        SC_Response_Ex::reload(array('device_type_id' => $this->device_type_id,
92                                                     'msg' => 'on'), true);
93                        SC_Response_Ex::actionExit();
94                    }
95                }
96                break;
97
98            // 登録/編集
99            case 'confirm':
100                if (!$is_error) {
101                    $this->arrErr = $this->lfCheckError($objFormParam, $this->arrErr);
102                    if (SC_Utils_Ex::isBlank($this->arrErr)) {
103                        $result = $this->doRegister($objFormParam, $objLayout);
104                        if ($result !== false) {
105
106                            SC_Response_Ex::reload(array('device_type_id' => $this->device_type_id,
107                                                         'page_id' => $result,
108                                                         'msg' => 'on'), true);
109                        SC_Response_Ex::actionExit();
110                        }
111                    }
112                }
113                break;
114
115            default:
116                if (isset($_GET['msg']) && $_GET['msg'] == 'on') {
117                    $this->tpl_onload = "alert('" . t('ALERT_004') . "');";
118                }
119                break;
120        }
121
122        if (!$is_error) {
123            $this->arrPageList = $objLayout->getPageProperties($this->device_type_id, null);
124            // page_id が指定されている場合にはテンプレートデータの取得
125            if (!SC_Utils_Ex::isBlank($this->page_id)) {
126                $arrPageData = $this->getTplMainpage($this->device_type_id, $this->page_id, $objLayout);
127                $objFormParam->setParam($arrPageData);
128            }
129        } else {
130            // 画面にエラー表示しないため, ログ出力
131            GC_Utils_Ex::gfPrintLog('Error: ' . print_r($this->arrErr, true));
132        }
133        $this->tpl_subtitle = $this->arrDeviceType[$this->device_type_id] . '>' . $this->tpl_subtitle;
134        $this->arrForm = $objFormParam->getFormParamList();
135
136    }
137
138    /**
139     * デストラクタ.
140     *
141     * @return void
142     */
143    function destroy() {
144        parent::destroy();
145    }
146
147    /**
148     * パラメーター情報の初期化
149     *
150     * XXX URL のフィールドは, 実際は filename なので注意
151     *
152     * @param object $objFormParam SC_FormParamインスタンス
153     * @return void
154     */
155    function lfInitParam(&$objFormParam) {
156        $objFormParam->addParam(t('PARAM_LABEL_PAGE_ID'), 'page_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
157        $objFormParam->addParam(t('PARAM_LABEL_DEVICE_TYPE_ID'), 'device_type_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
158        $objFormParam->addParam(t('PARAM_LABEL_THE_NAME'), 'page_name', STEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
159        $objFormParam->addParam(t('PARAM_LABEL_URL'), 'filename', STEXT_LEN, 'a', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
160        $objFormParam->addParam(t('PARAM_LABEL_HEADER_CHK'), 'header_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
161        $objFormParam->addParam(t('PARAM_LABEL_FOOTER_CHK'), 'footer_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
162        $objFormParam->addParam(t('PARAM_LABEL_EDIT_FLG'), 'edit_flg', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
163        $objFormParam->addParam(t('PARAM_LABEL_TPL_DATA'), 'tpl_data');
164    }
165
166    /**
167     * ページデータを取得する.
168     *
169     * @param integer $device_type_id 端末種別ID
170     * @param integer $page_id ページID
171     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
172     * @return array ページデータの配列
173     */
174    function getTplMainpage($device_type_id, $page_id, &$objLayout) {
175        $arrPageData = $objLayout->getPageProperties($device_type_id, $page_id);
176
177        $templatePath = $objLayout->getTemplatePath($device_type_id);
178        $filename = $templatePath . $arrPageData[0]['filename'] . '.tpl';
179        if (file_exists($filename)) {
180            $arrPageData[0]['tpl_data'] = file_get_contents($filename);
181        }
182        // ファイル名を画面表示用に加工しておく
183        $arrPageData[0]['filename'] = preg_replace('|^' . preg_quote(USER_DIR) . '|', '', $arrPageData[0]['filename']);
184        return $arrPageData[0];
185    }
186
187    /**
188     * 登録を実行する.
189     *
190     * ファイルの作成に失敗した場合は, エラーメッセージを出力し,
191     * データベースをロールバックする.
192     *
193     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
194     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
195     * @return integer|boolean 登録が成功した場合, 登録したページID;
196     *                         失敗した場合 false
197     */
198    function doRegister(&$objFormParam, &$objLayout) {
199        $filename = $objFormParam->getValue('filename');
200        $arrParams['device_type_id'] = $objFormParam->getValue('device_type_id');
201        $arrParams['page_id'] = $objFormParam->getValue('page_id');
202        $arrParams['header_chk'] = intval($objFormParam->getValue('header_chk')) === 1 ? 1 : 2;
203        $arrParams['footer_chk'] = intval($objFormParam->getValue('footer_chk')) === 1 ? 1 : 2;
204        $arrParams['tpl_data'] = $objFormParam->getValue('tpl_data');
205        $arrParams['page_name'] = $objFormParam->getValue('page_name');
206        $arrParams['url'] = USER_DIR . $filename . '.php';
207        $arrParams['filename'] = USER_DIR . $filename;
208
209        $objQuery =& SC_Query_Ex::getSingletonInstance();
210        $objQuery->begin();
211
212        $page_id = $this->registerPage($arrParams, $objLayout);
213
214        /*
215         * 新規登録時
216         * or 編集可能な既存ページ編集時かつ, PHP ファイルが存在しない場合に,
217         * PHP ファイルを作成する.
218         */
219        if (SC_Utils_Ex::isBlank($arrParams['page_id'])
220            || $objLayout->isEditablePage($arrParams['device_type_id'], $arrParams['page_id'])) {
221            if (!$this->createPHPFile($filename)) {
222                $this->arrErr['err'] = t('LC_Page_Admin_Design_MainEdit_003');
223                $objQuery->rollback();
224                return false;
225            }
226            // 新規登録時のみ $page_id を代入
227            $arrParams['page_id'] = $page_id;
228        }
229
230        if ($objLayout->isEditablePage($arrParams['device_type_id'], $page_id)) {
231            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $arrParams['filename'] . '.tpl';
232        } else {
233            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $filename . '.tpl';
234        }
235
236        if (!SC_Helper_FileManager_Ex::sfWriteFile($tpl_path, $arrParams['tpl_data'])) {
237            $this->arrErr['err'] = t('LC_Page_Admin_Design_MainEdit_004');
238            $objQuery->rollback();
239            return false;
240        }
241
242        $objQuery->commit();
243        return $arrParams['page_id'];
244    }
245
246    /**
247     * 入力内容をデータベースに登録する.
248     *
249     * @param array $arrParams フォームパラメーターの配列
250     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
251     * @return integer ページID
252     */
253    function registerPage($arrParams, &$objLayout) {
254        $objQuery =& SC_Query_Ex::getSingletonInstance();
255
256        // ページIDが空の場合は新規登録
257        $is_new = SC_Utils_Ex::isBlank($arrParams['page_id']);
258        // 既存ページの存在チェック
259        if (!$is_new) {
260            $arrExists = $objLayout->getPageProperties($arrParams['device_type_id'], $arrParams['page_id']);
261        }
262
263        $table = 'dtb_pagelayout';
264        $arrValues = $objQuery->extractOnlyColsOf($table, $arrParams);
265        $arrValues['update_url'] = $_SERVER['HTTP_REFERER'];
266        $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
267
268        // 新規登録
269        if ($is_new || SC_Utils_Ex::isBlank($arrExists)) {
270            $objQuery->setOrder('');
271            $arrValues['page_id'] = 1 + $objQuery->max('page_id', $table, 'device_type_id = ?',
272                                                       array($arrValues['device_type_id']));
273            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
274            $objQuery->insert($table, $arrValues);
275        }
276        // 更新
277        else {
278            // 編集不可ページは更新しない
279            if (!$objLayout->isEditablePage($arrValues['device_type_id'], $arrValues['page_id'])) {
280                unset($arrValues['page_name']);
281                unset($arrValues['filename']);
282                unset($arrValues['url']);
283            }
284
285            $objQuery->update($table, $arrValues, 'page_id = ? AND device_type_id = ?',
286                              array($arrValues['page_id'], $arrValues['device_type_id']));
287        }
288        return $arrValues['page_id'];
289    }
290
291    /**
292     * エラーチェックを行う.
293     *
294     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
295     * @return array エラーメッセージの配列
296     */
297    function lfCheckError(&$objFormParam, &$arrErr) {
298        $arrParams = $objFormParam->getHashArray();
299        $objErr = new SC_CheckError_Ex($arrParams);
300        $objErr->arrErr =& $arrErr;
301        $objErr->doFunc(array(t('PARAM_LABEL_THE_NAME'), 'page_name', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
302        $objErr->doFunc(array(t('PARAM_LABEL_URL'), 'filename', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
303
304        /*
305         * URL チェック
306         * ここでチェックするのは, パスのみなので SC_CheckError::URL_CHECK()
307         * は使用しない
308         */
309        $valid_url = true;
310        foreach (explode('/', $arrParams['filename']) as $val) {
311            if (!preg_match('/^[a-zA-Z0-9:_~\.\-]+$/', $val)) {
312                $valid_url = false;
313            }
314            if ($val == '.' || $val == '..') {
315                $valid_url = false;
316            }
317        }
318        if (!$valid_url) {
319            $objErr->arrErr['filename'] = t('LC_Page_Admin_Design_MainEdit_005');
320        }
321        // 同一URLの存在チェック
322        $where = 'page_id <> 0 AND device_type_id = ? AND filename = ?';
323        $arrValues = array($arrParams['device_type_id'], USER_DIR . $arrParams['filename']);
324        // 変更の場合は自 URL を除外
325        if (!SC_Utils_Ex::isBlank($arrParams['page_id'])) {
326            $where .= ' AND page_id <> ?';
327            $arrValues[] = $arrParams['page_id'];
328        }
329
330        $objQuery =& SC_Query_Ex::getSingletonInstance();
331        $exists = $objQuery->exists('dtb_pagelayout', $where, $arrValues);
332        if ($exists) {
333            $objErr->arrErr['filename'] = t('LC_Page_Admin_Design_MainEdit_006');
334        }
335        return $objErr->arrErr;
336    }
337
338    /**
339     * PHP ファイルを生成する.
340     *
341     * 既に同名の PHP ファイルが存在する場合は何もせず true を返す.(#831)
342     *
343     * @param string $filename フォームパラメーターの filename
344     * @return boolean 作成に成功した場合 true
345     */
346    function createPHPFile($filename) {
347        $path = USER_REALDIR . $filename . '.php';
348
349        if (file_exists($path)) {
350            return true;
351        }
352
353        if (file_exists(USER_DEF_PHP_REALFILE)) {
354            $php_contents = file_get_contents(USER_DEF_PHP_REALFILE);
355        } else {
356            return false;
357        }
358
359        // require.php の PATH を書き換える
360        $defaultStrings = "exit; // Don't rewrite. This line is rewritten by EC-CUBE.";
361        $replaceStrings = "require_once '" . str_repeat('../', substr_count($filename, '/')) . "../require.php';";
362        $php_contents = str_replace($defaultStrings, $replaceStrings, $php_contents);
363
364        return SC_Helper_FileManager_Ex::sfWriteFile($path, $php_contents);
365    }
366}
Note: See TracBrowser for help on using the repository browser.