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

Revision 22857, 15.3 KB checked in by Seasoft, 11 years ago (diff)

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

  • 主に空白・空白行の調整。
  • 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
24require_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 */
33class LC_Page_Admin_Design_MainEdit extends LC_Page_Admin_Ex
34{
35    /**
36     * Page を初期化する.
37     *
38     * @return void
39     */
40    function init()
41    {
42        parent::init();
43        $this->tpl_mainpage = 'design/main_edit.tpl';
44        $this->text_row     = 13;
45        $this->tpl_subno = 'main_edit';
46        $this->tpl_mainno = 'design';
47        $this->tpl_maintitle = 'デザイン管理';
48        $this->tpl_subtitle = 'ページ詳細設定';
49        $masterData = new SC_DB_MasterData_Ex();
50        $this->arrDeviceType = $masterData->getMasterData('mtb_device_type');
51    }
52
53    /**
54     * Page のプロセス.
55     *
56     * @return void
57     */
58    function process()
59    {
60        $this->action();
61        $this->sendResponse();
62    }
63
64    /**
65     * Page のアクション.
66     *
67     * @return void
68     */
69    function action()
70    {
71        $objLayout = new SC_Helper_PageLayout_Ex();
72        $objFormParam = new SC_FormParam_Ex();
73        $this->lfInitParam($objFormParam);
74        $objFormParam->setParam($_REQUEST);
75        $objFormParam->convParam();
76        $this->arrErr = $objFormParam->checkError();
77        $is_error = (!SC_Utils_Ex::isBlank($this->arrErr));
78
79        $this->device_type_id = $objFormParam->getValue('device_type_id', DEVICE_TYPE_PC);
80        $this->page_id = $objFormParam->getValue('page_id');
81
82        switch ($this->getMode()) {
83            // 削除
84            case 'delete':
85                if (!$is_error) {
86                    if ($objLayout->isEditablePage($this->device_type_id, $this->page_id)) {
87                        $objLayout->lfDelPageData($this->page_id, $this->device_type_id);
88
89                        SC_Response_Ex::reload(array('device_type_id' => $this->device_type_id,
90                                                     'msg' => 'on'), true);
91                        SC_Response_Ex::actionExit();
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                            $arrQueryString = array(
104                                'device_type_id' => $this->device_type_id,
105                                'page_id' => $result,
106                                'msg' => 'on',
107                            );
108                            SC_Response_Ex::reload($arrQueryString, 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('登録が完了しました。');";
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     * @return void
141     */
142    function destroy()
143    {
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    {
157        $objFormParam->addParam('ページID', 'page_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
158        $objFormParam->addParam('端末種別ID', 'device_type_id', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
159        $objFormParam->addParam('名称', 'page_name', STEXT_LEN, 'KVa', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
160        $objFormParam->addParam('URL', 'filename', STEXT_LEN, 'a', array('SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
161        $objFormParam->addParam('ヘッダチェック', 'header_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
162        $objFormParam->addParam('フッタチェック', 'footer_chk', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
163        $objFormParam->addParam('修正フラグ', 'edit_flg', INT_LEN, 'n', array('NUM_CHECK', 'MAX_LENGTH_CHECK'));
164        $objFormParam->addParam('TPLデータ', 'tpl_data');
165        $objFormParam->addParam('meta タグ:author', 'author', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
166        $objFormParam->addParam('meta タグ:description', 'description', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
167        $objFormParam->addParam('meta タグ:keyword', 'keyword', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
168        $objFormParam->addParam('meta タグ:robots', 'meta_robots', MTEXT_LEN, 'KVa', array('MAX_LENGTH_CHECK'));
169    }
170
171    /**
172     * ページデータを取得する.
173     *
174     * @param integer $device_type_id 端末種別ID
175     * @param integer $page_id ページID
176     * @param SC_Helper_PageLayout $objLayout SC_Helper_PageLayout インスタンス
177     * @return array ページデータの配列
178     */
179    function getTplMainpage($device_type_id, $page_id, &$objLayout)
180    {
181        $arrPageData = $objLayout->getPageProperties($device_type_id, $page_id);
182
183        $templatePath = $objLayout->getTemplatePath($device_type_id);
184        $filename = $templatePath . $arrPageData[0]['filename'] . '.tpl';
185        if (file_exists($filename)) {
186            $arrPageData[0]['tpl_data'] = file_get_contents($filename);
187        }
188        // ファイル名を画面表示用に加工しておく
189        $arrPageData[0]['filename'] = preg_replace('|^' . preg_quote(USER_DIR) . '|', '', $arrPageData[0]['filename']);
190
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        $arrParams['author']        = $objFormParam->getValue('author');
217        $arrParams['description']   = $objFormParam->getValue('description');
218        $arrParams['keyword']       = $objFormParam->getValue('keyword');
219        $arrParams['meta_robots']   = $objFormParam->getValue('meta_robots');
220
221        $objQuery =& SC_Query_Ex::getSingletonInstance();
222        $objQuery->begin();
223
224        $page_id = $this->registerPage($arrParams, $objLayout);
225
226        /*
227         * 新規登録時
228         * or 編集可能な既存ページ編集時かつ, PHP ファイルが存在しない場合に,
229         * PHP ファイルを作成する.
230         */
231        if (SC_Utils_Ex::isBlank($arrParams['page_id'])
232            || $objLayout->isEditablePage($arrParams['device_type_id'], $arrParams['page_id'])) {
233            if (!$this->createPHPFile($filename)) {
234                $this->arrErr['err'] = '※ PHPファイルの作成に失敗しました<br />';
235                $objQuery->rollback();
236                return false;
237            }
238            // 新規登録時のみ $page_id を代入
239            $arrParams['page_id'] = $page_id;
240        }
241
242        if ($objLayout->isEditablePage($arrParams['device_type_id'], $page_id)) {
243            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $arrParams['filename'] . '.tpl';
244        } else {
245            $tpl_path = $objLayout->getTemplatePath($arrParams['device_type_id']) . $filename . '.tpl';
246        }
247
248        if (!SC_Helper_FileManager_Ex::sfWriteFile($tpl_path, $arrParams['tpl_data'])) {
249            $this->arrErr['err'] = '※ TPLファイルの書き込みに失敗しました<br />';
250            $objQuery->rollback();
251            return false;
252        }
253
254        $objQuery->commit();
255
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    {
268        $objQuery =& SC_Query_Ex::getSingletonInstance();
269
270        // ページIDが空の場合は新規登録
271        $is_new = SC_Utils_Ex::isBlank($arrParams['page_id']);
272        // 既存ページの存在チェック
273        if (!$is_new) {
274            $arrExists = $objLayout->getPageProperties($arrParams['device_type_id'], $arrParams['page_id']);
275        }
276
277        $table = 'dtb_pagelayout';
278        $arrValues = $objQuery->extractOnlyColsOf($table, $arrParams);
279        $arrValues['update_url'] = $_SERVER['HTTP_REFERER'];
280        $arrValues['update_date'] = 'CURRENT_TIMESTAMP';
281
282        // 新規登録
283        if ($is_new || SC_Utils_Ex::isBlank($arrExists)) {
284            $objQuery->setOrder('');
285            $arrValues['page_id'] = 1 + $objQuery->max('page_id', $table, 'device_type_id = ?',
286                                                       array($arrValues['device_type_id']));
287            $arrValues['create_date'] = 'CURRENT_TIMESTAMP';
288            $objQuery->insert($table, $arrValues);
289        }
290        // 更新
291        else {
292            // 編集不可ページは更新しない
293            if (!$objLayout->isEditablePage($arrValues['device_type_id'], $arrValues['page_id'])) {
294                unset($arrValues['page_name']);
295                unset($arrValues['filename']);
296                unset($arrValues['url']);
297            }
298
299            $objQuery->update($table, $arrValues, 'page_id = ? AND device_type_id = ?',
300                              array($arrValues['page_id'], $arrValues['device_type_id']));
301        }
302
303        return $arrValues['page_id'];
304    }
305
306    /**
307     * エラーチェックを行う.
308     *
309     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
310     * @return array エラーメッセージの配列
311     */
312    function lfCheckError(&$objFormParam, &$arrErr)
313    {
314        $arrParams = $objFormParam->getHashArray();
315        $objErr = new SC_CheckError_Ex($arrParams);
316        $objErr->arrErr =& $arrErr;
317        $objErr->doFunc(array('名称', 'page_name', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
318        $objErr->doFunc(array('URL', 'filename', STEXT_LEN), array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
319
320        /*
321         * URL チェック
322         * ここでチェックするのは, パスのみなので SC_CheckError::URL_CHECK()
323         * は使用しない
324         */
325        $valid_url = true;
326        foreach (explode('/', $arrParams['filename']) as $val) {
327            if (!preg_match('/^[a-zA-Z0-9:_~\.\-]+$/', $val)) {
328                $valid_url = false;
329            }
330            if ($val == '.' || $val == '..') {
331                $valid_url = false;
332            }
333        }
334        if (!$valid_url) {
335            $objErr->arrErr['filename'] = '※ URLを正しく入力してください。<br />';
336        }
337        // 同一URLの存在チェック
338        $where = 'page_id <> 0 AND device_type_id = ? AND filename = ?';
339        $arrValues = array($arrParams['device_type_id'], USER_DIR . $arrParams['filename']);
340        // 変更の場合は自 URL を除外
341        if (!SC_Utils_Ex::isBlank($arrParams['page_id'])) {
342            $where .= ' AND page_id <> ?';
343            $arrValues[] = $arrParams['page_id'];
344        }
345
346        $objQuery =& SC_Query_Ex::getSingletonInstance();
347        $exists = $objQuery->exists('dtb_pagelayout', $where, $arrValues);
348        if ($exists) {
349            $objErr->arrErr['filename'] = '※ 同じURLのデータが存在しています。別のURLを入力してください。<br />';
350        }
351
352        return $objErr->arrErr;
353    }
354
355    /**
356     * PHP ファイルを生成する.
357     *
358     * 既に同名の PHP ファイルが存在する場合は何もせず true を返す.(#831)
359     *
360     * @param string $filename フォームパラメーターの filename
361     * @return boolean 作成に成功した場合 true
362     */
363    function createPHPFile($filename)
364    {
365        $path = USER_REALDIR . $filename . '.php';
366
367        if (file_exists($path)) {
368            return true;
369        }
370
371        if (file_exists(USER_DEF_PHP_REALFILE)) {
372            $php_contents = file_get_contents(USER_DEF_PHP_REALFILE);
373        } else {
374            return false;
375        }
376
377        // require.php の PATH を書き換える
378        $defaultStrings = "exit; // Don't rewrite. This line is rewritten by EC-CUBE.";
379        $replaceStrings = "require_once '" . str_repeat('../', substr_count($filename, '/')) . "../require.php';";
380        $php_contents = str_replace($defaultStrings, $replaceStrings, $php_contents);
381
382        return SC_Helper_FileManager_Ex::sfWriteFile($path, $php_contents);
383    }
384}
Note: See TracBrowser for help on using the repository browser.