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

Revision 19832, 16.4 KB checked in by Seasoft, 13 years ago (diff)

#714(パス指定によるリダイレクトの記述を簡潔にする) 共通処理実装、個別処理の一部を実装
#869(create_date, update_date 列の定義が、表やDBによるバラツキがある)

  • NOT NULL 制約により実装漏れに気づいたので修正

#893(SC_Response#reload を使うべきであろう箇所で SC_Response#sendRedirect を利用している)
#628(未使用処理・定義などの削除)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
  • 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-2010 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_REALDIR . "pages/admin/LC_Page_Admin.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 {
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->tpl_subnavi  = 'design/subnavi.tpl';
48        $this->user_URL     = USER_URL;
49        $this->text_row     = 13;
50        $this->tpl_subno = "main_edit";
51        $this->tpl_mainno = "design";
52        $this->tpl_subtitle = 'ページ詳細設定';
53    }
54
55    /**
56     * Page のプロセス.
57     *
58     * @return void
59     */
60    function process() {
61        $this->action();
62        $this->sendResponse();
63    }
64
65    /**
66     * Page のアクション.
67     *
68     * @return void
69     */
70    function action() {
71        $objView = new SC_AdminView();
72        $this->objLayout = new SC_Helper_PageLayout_Ex();
73
74        // 認証可否の判定
75        $objSess = new SC_Session();
76        SC_Utils_Ex::sfIsSuccess($objSess);
77
78        // ページIDを取得
79        if (isset($_REQUEST['page_id']) && is_numeric($_REQUEST['page_id'])) {
80            $page_id = $_REQUEST['page_id'];
81        }
82
83        $this->page_id = $page_id;
84
85        // 端末種別IDを取得
86        if (isset($_REQUEST['device_type_id'])
87            && is_numeric($_REQUEST['device_type_id'])) {
88            $device_type_id = $_REQUEST['device_type_id'];
89        } else {
90            $device_type_id = DEVICE_TYPE_PC;
91        }
92
93        // ページ一覧を取得
94        $this->arrPageList = $this->objLayout->lfGetPageData("page_id <> 0 AND device_type_id = ?",
95                                                             array($device_type_id));
96
97        // メッセージ表示
98        if (isset($_GET['msg']) && $_GET['msg'] == "on"){
99            $this->tpl_onload="alert('登録が完了しました。');";
100        }
101
102        // page_id が指定されている場合にはテンプレートデータの取得
103        if (is_numeric($page_id) && $page_id != '') {
104            $this->arrPageData = $this->lfGetPageData($page_id, $device_type_id, $objView);
105        }
106
107        if (!isset($_POST['mode'])) $_POST['mode'] = "";
108
109        switch ($_POST['mode']) {
110        case 'preview':
111            $this->lfPreviewPageData($page_id, $device_type_id);
112            exit;
113            break;
114
115        case 'delete':
116            if (!$this->objLayout->lfCheckBaseData($page_id, $device_type_id)) {
117                $this->lfDeletePageData($page_id, $device_type_id);
118                exit;
119            }
120            break;
121
122        case 'confirm':
123            $this->lfConfirmPageData($page_id, $device_type_id);
124        default:
125        }
126        $this->device_type_id = $device_type_id;
127    }
128
129    /**
130     * デストラクタ.
131     *
132     * @return void
133     */
134    function destroy() {
135        parent::destroy();
136    }
137
138    /**
139     * ページデータを取得する.
140     *
141     * @param integer $page_id ページID
142     * @param integer $device_type_id 端末種別ID
143     * @param object $objView ビューオブジェクト
144     * @return void
145     */
146    function lfGetPageData($page_id, $device_type_id, $objView){
147        $arrPageData = $this->objLayout->lfGetPageData("page_id = ? AND device_type_id = ?",
148                                                       array($page_id, $device_type_id));
149
150        if (strlen($arrPageData[0]['filename']) == 0) {
151            $this->arrErr['page_id_err'] = "※ 指定されたページは編集できません。";
152            // 画面の表示
153            $objView->assignobj($this);
154            $objView->display(MAIN_FRAME);
155            exit;
156        }
157
158        // テンプレートを読み込む
159        $templatePath = $this->objLayout->getTemplatePath($device_type_id);
160        $arrPageData[0]['tpl_data'] = file_get_contents($templatePath . $arrPageData[0]['tpl_dir'] . $arrPageData[0]['filename'] . ".tpl");
161
162        // チェックボックスの値変更
163        $arrPageData[0]['header_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['header_chk'], true);
164        $arrPageData[0]['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['footer_chk'], true);
165
166        // ディレクトリを画面表示用に編集
167        $arrPageData[0]['directory'] = str_replace(USER_DIR, '', $arrPageData[0]['php_dir']);
168
169        return $arrPageData[0];
170    }
171
172    /**
173     * プレビュー画面を表示する.
174     *
175     * @param integer $page_id_old 元のページID
176     * @param integer $device_type_id 端末種別ID
177     * @return void
178     */
179    function lfPreviewPageData($page_id_old, $device_type_id) {
180
181        // プレビューの場合ページIDを0にセットする。
182        $page_id = '0';
183        $url = 'preview';
184
185        $tmpPost = $_POST;
186        $tmpPost['page_id'] = $page_id;
187        $tmpPost['url'] = $url;
188        $tmpPost['tpl_dir'] = USER_REALDIR . "templates/preview/";
189
190        $arrPreData = $this->objLayout->lfGetPageData("page_id = ? AND device_type_id = ?",
191                                                      array($page_id, $device_type_id));
192
193        // DBへデータを更新する
194        $this->lfEntryPageData($tmpPost, $device_type_id);
195
196        // TPLファイル作成
197        $cre_tpl = $this->objLayout->getTemplatePath($device_type_id) . $url . '.tpl';
198        $this->lfCreateFile($cre_tpl, $_POST['tpl_data']);
199       
200        // blocposition を削除
201        $objQuery = new SC_Query(); // DB操作オブジェクト
202        $ret = $objQuery->delete('dtb_blocposition', 'page_id = 0 AND device_type_id = ?', array($device_type_id));
203
204        if ($page_id_old != "") {
205            // 登録データを取得
206            $sql = 'SELECT target_id, bloc_id, bloc_row FROM dtb_blocposition WHERE page_id = ? AND device_type_id = ?';
207            $ret = $objQuery->getAll($sql, array($page_id_old, $device_type_id));
208
209            // blocposition を複製
210            foreach($ret as $row){
211                $row['page_id'] = $page_id;
212                $row['device_type_id'] = $device_type_id;
213                $objQuery->insert('dtb_blocposition', $row);
214            }
215        }
216        $_SESSION['preview'] = "ON";
217        SC_Response_Ex::sendRedirect('/preview/' . DIR_INDEX_URL, array("filename" => $arrPageData[0]["filename"]));
218    }
219
220    /**
221     * データ登録処理.
222     *
223     * @param integer $page_id ページID
224     * @param integer $device_type_id 端末種別ID
225     * @return void
226     */
227    function lfConfirmPageData($page_id, $device_type_id) {
228        // エラーチェック
229        $this->arrErr = $this->lfErrorCheck($_POST, $device_type_id);
230
231        // エラーがなければ更新処理を行う
232        if (count($this->arrErr) == 0) {
233            // DBへデータを更新する
234            $arrTmp = $this->lfEntryPageData($_POST, $device_type_id);
235            $page_id = $arrTmp['page_id'];
236
237            $arrTmp = $this->objLayout->lfGetPageData('page_id = ? AND device_type_id = ?', array($page_id, $device_type_id));
238            $arrData = $arrTmp[0];
239
240            // ベースデータでなければファイルを削除し、PHPファイルを作成する
241            if (!$this->objLayout->lfCheckBaseData($arrData['page_id'], $device_type_id)) {
242                // PHPファイル作成
243                $this->lfCreatePHPFile($_POST['url'], $device_type_id);
244            }
245
246            // TPLファイル作成
247            $cre_tpl = $this->objLayout->getTemplatePath($device_type_id) . $arrData['tpl_dir'] . $arrData['filename'] . '.tpl';
248            $this->lfCreateFile($cre_tpl, $_POST['tpl_data']);
249
250            $arrQueryString = array(
251                "page_id" => $arrData['page_id'],
252                "device_type_id" => $device_type_id,
253                "msg"     => "on",
254            );
255            $this->objDisplay->reload($arrQueryString, true);
256            exit;
257        } else {
258            // エラーがあれば入力時のデータを表示する
259            $this->arrPageData = $_POST;
260            $this->arrPageData['header_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['header_chk']), true);
261            $this->arrPageData['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['footer_chk']), true);
262            $this->arrPageData['directory'] = '';
263            $this->arrPageData['filename'] = $_POST['url'];
264        }
265    }
266
267    /**
268     * ブロック情報を更新する.
269     *
270     * @param array $arrData 基となる更新データ
271     * @return array 実際に使用した更新データ
272     */
273    function lfEntryPageData($arrData, $device_type_id){
274        $objQuery = new SC_Query();
275        $arrChk = array();          // 排他チェック用
276
277        // 更新データの変換
278        $sqlval = $this->lfGetUpdData($arrData, $device_type_id);
279
280        // データが存在しているかチェックを行う
281        if($arrData['page_id'] !== ''){
282            $arrChk = $this->objLayout->lfGetPageData("page_id = ? AND device_type_id = ?",
283                                                      array($arrData['page_id'], $device_type_id));
284        }
285
286        // page_id が空 若しくは データが存在していない場合にはINSERTを行う
287        if ($arrData['page_id'] === '' || !isset($arrChk[0])) {
288            // FIXME device_type_id ごとの連番にする
289            $sqlval['page_id'] = $objQuery->nextVal('dtb_pagelayout_page_id');
290            $sqlval['device_type_id'] = $device_type_id;
291            $sqlval['create_date'] = 'now()';
292            $objQuery->insert('dtb_pagelayout', $sqlval);
293        }
294        // データが存在してる場合にはアップデートを行う
295        else {
296            $objQuery->update('dtb_pagelayout', $sqlval, 'page_id = ? AND device_type_id = ?',
297                              array($arrData['page_id'], $device_type_id));
298            // 戻り値用
299            $sqlval['page_id'] = $arrData['page_id'];
300        }
301        return $sqlval;
302    }
303
304    /**
305     * DBへ更新を行うデータを生成する.
306     *
307     * @param array $arrData 更新データ
308     * @return array 更新データ
309     */
310    function lfGetUpdData($arrData, $device_type_id) {
311        $arrUpdData = array(
312            'header_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['header_chk']),   // ヘッダー使用
313            'footer_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['footer_chk']),   // フッター使用
314            'update_url'    => $_SERVER['HTTP_REFERER'],                                // 更新URL
315            'update_date'   => 'now()',
316        );
317
318        // ベースデータの場合には変更しない。
319        if (!$this->objLayout->lfCheckBaseData($arrData['page_id'], $device_type_id)) {
320            $arrUpdData['page_name']    = $arrData['page_name'] ;
321            $arrUpdData['url']          = preg_replace('|^' . preg_quote(URL_PATH) . '|', '',
322                                                       $this->objLayout->getUserDir($device_type_id) . $arrData['url'] . '.php');
323            $arrUpdData['php_dir']      = dirname($arrUpdData['url']);
324            if ($arrUpdData['php_dir'] == '.') {
325                $arrUpdData['php_dir'] = '';
326            } else {
327                $arrUpdData['php_dir'] .= '/';
328            }
329            $arrUpdData['tpl_dir']      = $arrUpdData['php_dir'];
330            $arrUpdData['filename']     = basename($arrData['url']); // 拡張子を付加しない
331        }
332
333        return $arrUpdData;
334    }
335
336    /**
337     * ページデータを削除する.
338     *
339     * @param integer $page_id ページID
340     * @return void
341     */
342    function lfDeletePageData($page_id, $device_type_id){
343        $this->objLayout->lfDelPageData($page_id, $device_type_id);
344        $this->objDisplay->reload(array("device_type_id" => $device_type_id), true);
345    }
346
347    /**
348     * 入力項目のエラーチェックを行う.
349     *
350     * @param array $arrData 入力データ
351     * @param integer $device_type_id 端末種別ID
352     * @return array エラー情報
353     */
354    function lfErrorCheck($array, $device_type_id) {
355        $objErr = new SC_CheckError($array);
356        $objErr->doFunc(array("名称", "page_name", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
357        $objErr->doFunc(array("URL", "url", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
358
359        // URLチェック
360        $okUrl = true;
361        foreach (explode('/', $array['url']) as $url_part) {
362            if (!ereg( '^[a-zA-Z0-9:_~\.-]+$', $url_part)) {
363                $okUrl = false;
364            }
365            if ($url_part == '.' || $url_part == '..') {
366                $okUrl = false;
367            }
368        }
369        if (!$okUrl) {
370            $objErr->arrErr['url'] = "※ URLを正しく入力してください。<br />";
371        }
372
373        // 同一のURLが存在している場合にはエラー
374        $params = array();
375       
376        $sqlWhere = 'url = ?';
377        $params[] = $this->objLayout->getUserDir($device_type_id) . $array['url'] . '.php';
378       
379        // プレビュー用のレコードは除外
380        $sqlWhere .= ' AND page_id <> 0';
381       
382        // 変更の場合、自身のレコードは除外
383        if (strlen($array['page_id']) != 0) {
384            $sqlWhere .= ' AND page_id <> ?';
385            $params[] = $array['page_id'];
386        }
387       
388        $arrChk = $this->objLayout->lfgetPageData($sqlWhere , $params);
389       
390        if (count($arrChk) >= 1) {
391            $objErr->arrErr['url'] = '※ 同じURLのデータが存在しています。別のURLを付けてください。<br />';
392        }
393       
394        return $objErr->arrErr;
395    }
396
397    /**
398     * ファイルを作成する.
399     *
400     * @param string $path テンプレートファイルのパス
401     * @param string $data テンプレートの内容
402     * @return void
403     */
404    function lfCreateFile($path, $data){
405
406        // ディレクトリが存在していなければ作成する
407        if (!is_dir(dirname($path))) {
408            mkdir(dirname($path), 0777, true); // FIXME (PHP4)
409        }
410
411        // ファイル作成
412        $fp = fopen($path,"w");
413        if ($fp === false) {
414            SC_Utils_Ex::sfDispException();
415        }
416        $ret = fwrite($fp, $data);
417        if ($ret === false) {
418            SC_Utils_Ex::sfDispException();
419        }
420        fclose($fp);
421    }
422
423    /**
424     * PHPファイルを作成する.
425     *
426     * @param string $path PHPファイルのパス
427     * @return void
428     */
429    function lfCreatePHPFile($url, $device_type_id){
430
431        $path = $this->objLayout->getUserPath($device_type_id) . $url . ".php";
432
433        // カスタマイズを考慮し、上書きしない。(#831)
434        if (file_exists($path)) {
435            return;
436        }
437
438        // php保存先ディレクトリが存在していなければ作成する
439        if (!is_dir(dirname($path))) {
440            mkdir(dirname($path), 0777, true); // FIXME (PHP4)
441        }
442
443        // ベースとなるPHPファイルの読み込み
444        if (file_exists(USER_DEF_PHP_REALFILE)){
445            $php_data = file_get_contents(USER_DEF_PHP_REALFILE);
446        }
447
448        // require.phpの場所を書き換える
449        $defaultStrings = "exit; // Don't rewrite. This line is rewritten by EC-CUBE.";
450        $replaceStrings = "require_once '" . str_repeat('../', substr_count($url, '/')) . "../require.php';";
451        $php_data = str_replace($defaultStrings, $replaceStrings, $php_data);
452
453        // phpファイルの作成
454        $fp = fopen($path,"w");
455        fwrite($fp, $php_data);
456        fclose($fp);
457    }
458
459}
460?>
Note: See TracBrowser for help on using the repository browser.