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

Revision 19661, 15.4 KB checked in by nanasess, 13 years ago (diff)

source:branches/camp/camp-2_5-E のマージ

  • スマートフォン対応(#787)
  • プラグイン機能(#494)
  • 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_PATH . "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        // ページ一覧を取得
79        $this->arrPageList = $this->objLayout->lfgetPageData();
80       
81        // ブロックIDを取得
82        if (isset($_POST['page_id'])) {
83            $page_id = $_POST['page_id'];
84        }else if (isset($_GET['page_id'])){
85            $page_id = $_GET['page_id'];
86        }else{
87            $page_id = '';
88        }
89
90        $this->page_id = $page_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) and $page_id != '') {
99            $this->lfGetPageData($page_id, $objView);
100        }
101
102        if (!isset($_POST['mode'])) $_POST['mode'] = "";
103       
104        // プレビュー処理
105        if ($_POST['mode'] == 'preview') {
106            $this->lfPreviewPageData($page_id);
107            exit;
108        }
109
110        // データ登録処理
111        if ($_POST['mode'] == 'confirm') {
112            $this->lfConfirmPageData($page_id);
113        }
114
115        // データ削除処理 ベースデータでなければファイルを削除
116        if ($_POST['mode'] == 'delete' and !$this->objLayout->lfCheckBaseData($page_id)) {
117            $this->lfDeletePageData($page_id);
118            exit;
119        }
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 object $objView ビューオブジェクト
136     * @return void
137     */
138    function lfGetPageData($page_id, $objView){
139        $arrPageData = $this->objLayout->lfgetPageData(" page_id = ? " , array($page_id));
140
141        if (strlen($arrPageData[0]['filename']) == 0) {
142            $this->arrErr['page_id_err'] = "※ 指定されたページは編集できません。";
143            // 画面の表示
144            $objView->assignobj($this);
145            $objView->display(MAIN_FRAME);
146            exit;
147        }
148
149        // テンプレートファイルが存在していれば読み込む
150        $tpl_file =  USER_TEMPLATE_PATH . "/" . TEMPLATE_NAME . "/" . $arrPageData[0]['filename'] . ".tpl";
151        if (file_exists($tpl_file)){
152            $arrPageData[0]['tpl_data'] = file_get_contents($tpl_file);
153        // 存在してなければ, 指定されたテンプレートのファイルを読み込む
154        } else {
155            $arrPageData[0]['tpl_data'] = file_get_contents(TEMPLATE_DIR . $arrPageData[0]['filename'] . ".tpl");
156        }
157
158        // チェックボックスの値変更
159        $arrPageData[0]['header_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['header_chk'], true);
160        $arrPageData[0]['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox($arrPageData[0]['footer_chk'], true);
161
162        // ディレクトリを画面表示用に編集
163        $arrPageData[0]['directory'] = str_replace(USER_DIR, '', $arrPageData[0]['php_dir']);
164
165        $this->arrPageData = $arrPageData[0];
166    }
167
168    /**
169     * プレビュー画面を表示する.
170     *
171     * @param integer $page_id ページID
172     * @return void
173     */
174    function lfPreviewPageData($page_id){
175
176        $page_id_old = $page_id;
177        // プレビューの場合ページIDを0にセットする。
178        $page_id = "0";
179        $url = basename($_POST['url']);
180       
181        $tmpPost = $_POST;
182        $tmpPost['page_id'] = $page_id;
183        $tmpPost['url'] = $url;
184        $tmpPost['tpl_dir'] = USER_PATH . "templates/preview/";
185       
186        $arrPreData = $this->objLayout->lfgetPageData("page_id = ?" , array($page_id));
187       
188        // tplファイルの削除 (XXX: 処理の意図が不明。存在していると都合が悪いファイル?)
189        $del_tpl = USER_PATH . "templates/" . $arrPreData[0]['filename'] . '.tpl';
190        if (file_exists($del_tpl)){
191            unlink($del_tpl);
192        }
193
194        // DBへデータを更新する
195        $this->lfEntryPageData($tmpPost);
196
197        // TPLファイル作成
198        $preview_tpl = USER_PATH . "templates/preview/" . TEMPLATE_NAME . "/" . $url . '.tpl';
199        $this->lfCreateFile($preview_tpl, $_POST['tpl_data']);
200       
201        // blocposition を削除
202        $objQuery = new SC_Query();     // DB操作オブジェクト
203        $sql = 'delete from dtb_blocposition where page_id = 0';
204        $ret = $objQuery->query($sql);
205
206        if ($page_id_old != "") {
207            // 登録データを取得
208            $sql = "SELECT 0, target_id, bloc_id, bloc_row FROM dtb_blocposition WHERE page_id = ?";
209            $ret = $objQuery->getAll($sql,array($page_id_old));
210
211            if (count($ret) > 0) {
212
213                // blocposition を複製
214                $sql = " insert into dtb_blocposition (";
215                $sql .= "     page_id,";
216                $sql .= "     target_id,";
217                $sql .= "     bloc_id,";
218                $sql .= "     bloc_row";
219                $sql .= "     )values(?, ?, ?, ?)";
220
221                // 取得件数文INSERT実行
222                foreach($ret as $key => $val){
223                    $ret = $objQuery->query($sql,$val);
224                }
225            }
226        }
227        $_SESSION['preview'] = "ON";
228        $this->objDisplay->redirect($this->getLocation(URL_DIR . "preview/" . DIR_INDEX_URL, array("filename" => $arrPageData[0]["filename"])));
229
230    }
231
232    /**
233     * データ登録処理.
234     *
235     * @param integer $page_id ページID
236     * @return void
237     */
238    function lfConfirmPageData($page_id){
239        // エラーチェック
240        $this->arrErr = $this->lfErrorCheck($_POST);
241
242        // エラーがなければ更新処理を行う
243        if (count($this->arrErr) == 0) {
244            // DBへデータを更新する
245            $this->lfEntryPageData($_POST);
246
247            // ベースデータでなければファイルを削除し、PHPファイルを作成する
248            if (!$this->objLayout->lfCheckBaseData($page_id)) {
249                // PHPファイル作成
250                $this->lfCreatePHPFile($_POST['url']);
251            }
252
253            // TPLファイル作成
254            $cre_tpl = USER_TEMPLATE_PATH . "/" . TEMPLATE_NAME . "/" . basename($_POST['url']) . '.tpl';
255            $this->lfCreateFile($cre_tpl, $_POST['tpl_data']);
256
257            // 新規作成の場合、
258            if ($page_id == '') {
259                // ページIDを取得する
260                $arrPageData = $this->objLayout->lfgetPageData(" url = ? AND page_id <> 0" , array(USER_DIR . $_POST['url'] . '.php'));
261                $page_id = $arrPageData[0]['page_id'];
262            }
263            $this->objDisplay->redirect($this->getLocation("./main_edit.php",
264                                    array("page_id" => $page_id,
265                                          "msg"     => "on")));
266            exit;
267        } else {
268            // エラーがあれば入力時のデータを表示する
269            $this->arrPageData = $_POST;
270            $this->arrPageData['header_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['header_chk']), true);
271            $this->arrPageData['footer_chk'] = SC_Utils_Ex::sfChangeCheckBox(SC_Utils_Ex::sfChangeCheckBox($_POST['footer_chk']), true);
272            $this->arrPageData['directory'] = '';
273            $this->arrPageData['filename'] = $_POST['url'];
274        }
275    }
276
277    /**
278     * ブロック情報を更新する.
279     *
280     * @param array $arrData 更新データ
281     * @return void
282     */
283    function lfEntryPageData($arrData){
284        $objQuery = new SC_Query();
285        $arrChk = array();          // 排他チェック用
286
287        // 更新データの変換
288        $sqlval = $this->lfGetUpdData($arrData);
289
290        // データが存在しているかチェックを行う
291        if($arrData['page_id'] !== ''){
292            $arrChk = $this->objLayout->lfgetPageData("page_id = ?", array($arrData['page_id']));
293        }
294
295        // page_id が空 若しくは データが存在していない場合にはINSERTを行う
296        if ($arrData['page_id'] === '' or !isset($arrChk[0])) {
297            $sqlval['page_id'] = $objQuery->nextVal('dtb_pagelayout_page_id');
298            $sqlval['create_date'] = 'now()';
299            $objQuery->insert('dtb_pagelayout', $sqlval);
300        }
301        // データが存在してる場合にはアップデートを行う
302        else {
303            $objQuery->update('dtb_pagelayout', $sqlval, 'page_id = ?', array($arrData['page_id']));
304        }
305    }
306
307    /**
308     * DBへ更新を行うデータを生成する.
309     *
310     * @param array $arrData 更新データ
311     * @return array 更新データ
312     */
313    function lfGetUpdData($arrData){
314        $arrUpdData = array(
315            'header_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['header_chk']),   // ヘッダー使用
316            'footer_chk'    => SC_Utils_Ex::sfChangeCheckBox($arrData['footer_chk']),   // フッター使用
317            'update_url'    => $_SERVER['HTTP_REFERER'],                                // 更新URL
318            'update_date'   => 'now()',
319        );
320
321        // ベースデータの場合には変更しない。
322        if (!$this->objLayout->lfCheckBaseData($arrData['page_id'])) {
323            $arrUpdData['page_name']    = $arrData['page_name'] ;
324            $arrUpdData['url']          = USER_DIR . $arrData['url'] . '.php';
325            $arrUpdData['php_dir']      = dirname($arrUpdData['url']);
326            if ($arrUpdData['php_dir'] == '.') {
327                $arrUpdData['php_dir'] = '';
328            } else {
329                $arrUpdData['php_dir'] .= '/';
330            }
331            $arrUpdData['tpl_dir']      = substr(TPL_DIR, strlen(URL_DIR));
332            $arrUpdData['filename']     = basename($arrData['url']); // 拡張子を付加しない
333        }
334
335        return $arrUpdData;
336    }
337
338    /**
339     * ページデータを削除する.
340     *
341     * @param integer $page_id ページID
342     * @return void
343     */
344    function lfDeletePageData($page_id){
345        $this->objLayout->lfDelPageData($_POST['page_id']);
346        $this->objDisplay->redirect($this->getLocation("./main_edit.php"));
347    }
348
349    /**
350     * 入力項目のエラーチェックを行う.
351     *
352     * @param array $arrData 入力データ
353     * @return array エラー情報
354     */
355    function lfErrorCheck($array) {
356        $objErr = new SC_CheckError($array);
357        $objErr->doFunc(array("名称", "page_name", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
358        $objErr->doFunc(array("URL", "url", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
359       
360        // URLチェック
361        $okUrl = true;
362        foreach (explode('/', $array['url']) as $url_part) {
363            if (!ereg( '^[a-zA-Z0-9:_~\.-]+$', $url_part)) {
364                $okUrl = false;
365            }
366            if ($url_part == '.' || $url_part == '..') {
367                $okUrl = false;
368            }
369        }
370        if (!$okUrl) {
371            $objErr->arrErr['url'] = "※ URLを正しく入力してください。<br />";
372        }
373       
374        // 同一のURLが存在している場合にはエラー
375        $params = array();
376       
377        $sqlWhere = 'url = ?';
378        $params[] = USER_DIR . $array['url'] . '.php';
379       
380        // プレビュー用のレコードは除外
381        $sqlWhere .= ' AND page_id <> 0';
382       
383        // 変更の場合、自身のレコードは除外
384        if (strlen($array['page_id']) != 0) {
385            $sqlWhere .= ' AND page_id <> ?';
386            $params[] = $array['page_id'];
387        }
388       
389        $arrChk = $this->objLayout->lfgetPageData($sqlWhere , $params);
390       
391        if (count($arrChk) >= 1) {
392            $objErr->arrErr['url'] = '※ 同じURLのデータが存在しています。別のURLを付けてください。<br />';
393        }
394       
395        return $objErr->arrErr;
396    }
397
398    /**
399     * ファイルを作成する.
400     *
401     * @param string $path テンプレートファイルのパス
402     * @param string $data テンプレートの内容
403     * @return void
404     */
405    function lfCreateFile($path, $data){
406
407        // ディレクトリが存在していなければ作成する
408        if (!is_dir(dirname($path))) {
409            mkdir(dirname($path));
410        }
411
412        // ファイル作成
413        $fp = fopen($path,"w");
414        fwrite($fp, $data); // FIXME いきなり POST はちょっと...
415        fclose($fp);
416    }
417
418    /**
419     * PHPファイルを作成する.
420     *
421     * @param string $path PHPファイルのパス
422     * @return void
423     */
424    function lfCreatePHPFile($url){
425
426        $path = USER_PATH . $url . ".php";
427
428        // カスタマイズを考慮し、上書きしない。(#831)
429        if (file_exists($path)) {
430            return;
431        }
432
433        // php保存先ディレクトリが存在していなければ作成する
434        if (!is_dir(dirname($path))) {
435            mkdir(dirname($path));
436        }
437
438        // ベースとなるPHPファイルの読み込み
439        if (file_exists(USER_DEF_PHP)){
440            $php_data = file_get_contents(USER_DEF_PHP);
441        }
442
443        // require.phpの場所を書き換える
444        $php_data = str_replace("###require###", str_repeat('../', substr_count($url, '/')) . '../require.php', $php_data);
445
446        // phpファイルの作成
447        $fp = fopen($path,"w");
448        fwrite($fp, $php_data);
449        fclose($fp);
450    }
451
452}
453?>
Note: See TracBrowser for help on using the repository browser.