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

Revision 20507, 16.2 KB checked in by shutta, 13 years ago (diff)

SC_Queryクラスのclass_extends対応

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