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

Revision 21514, 14.3 KB checked in by Seasoft, 12 years ago (diff)

#1625 (typo修正・ソース整形・ソースコメントの改善)

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