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

Revision 22567, 14.5 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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