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

Revision 19722, 17.2 KB checked in by nanasess, 13 years ago (diff)

#748(モバイル/スマートフォンのデザイン管理)

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