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

Revision 21693, 15.4 KB checked in by h_yoshimoto, 12 years ago (diff)

#1692 フックポイント名を変更

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