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

Revision 20679, 15.2 KB checked in by coelacanth, 13 years ago (diff)

#1168 [管理画面]デザイン管理(レイアウト設定):プレビューボタンクリックでDBエラー

  • 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 * ターゲットID 0:未使用 1:レフトナビ 2:ライトナビ 3:イン画面上部 4:メイン画面下部  5:画面上部 6:画面下部 7:ヘッダより上 8:フッタより下 9:HEADタグ内 10:ヘッダ内部
31 * @package Page
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class LC_Page_Admin_Design extends LC_Page_Admin_Ex {
36
37    // }}}
38    // {{{ functions
39
40    /**
41     * Page を初期化する.
42     *
43     * @return void
44     */
45    function init() {
46        parent::init();
47        $this->tpl_mainpage = 'design/index.tpl';
48        $this->tpl_subnavi = 'design/subnavi.tpl';
49        $this->tpl_subno = 'layout';
50        $this->tpl_mainno = 'design';
51        $this->tpl_subtitle = 'レイアウト設定';
52        $masterData = new SC_DB_MasterData_Ex();
53        $this->arrTarget = $masterData->getMasterData("mtb_target");
54        $this->arrDeviceType = $masterData->getMasterData('mtb_device_type');
55    }
56
57    /**
58     * Page のプロセス.
59     *
60     * @return void
61     */
62    function process() {
63        $this->action();
64        $this->sendResponse();
65    }
66
67    /**
68     * Page のアクション.
69     *
70     * TODO パラメータの数値チェック
71     *
72     * @return void
73     */
74    function action() {
75        $objLayout = new SC_Helper_PageLayout_Ex();
76
77        // ページIDを取得
78        if (isset($_REQUEST['page_id']) && is_numeric($_REQUEST['page_id'])) {
79            $page_id = $_REQUEST['page_id'];
80        } else {
81            $page_id = 1; // FIXME $_REQUEST['page_id'] を受け取れない不具合時に不正処理が行なわれる原因となった
82        }
83        // 端末種別IDを取得
84        if (isset($_REQUEST['device_type_id'])
85            && is_numeric($_REQUEST['device_type_id'])) {
86            $device_type_id = $_REQUEST['device_type_id'];
87        } else {
88            $device_type_id = DEVICE_TYPE_PC;
89        }
90
91        //サブタイトルの追加
92        $this->tpl_subtitle .= ' - ' . $this->arrDeviceType[$device_type_id];
93
94        // 編集可能ページを取得
95        $this->arrEditPage = $objLayout->lfGetPageData("page_id <> 0 AND device_type_id = ?", array($device_type_id));
96
97        // レイアウト情報を取得
98        $arrBlocPos = $objLayout->lfGetNaviData($page_id, $device_type_id);
99
100        // データの存在チェックを行う
101        $arrPageData = $objLayout->lfGetPageData("page_id = ? AND device_type_id = ?", array($page_id, $device_type_id));
102
103        if (count($arrPageData) <= 0) {
104            $this->exists_page = 0;
105        }else{
106            $this->exists_page = 1;
107        }
108
109        // メッセージ表示
110        if (isset($_GET['msg']) && $_GET['msg'] == 'on') {
111            $this->complate_msg="alert('登録が完了しました。');";
112        }
113
114        // ブロックを取得
115        $objQuery = SC_Query_Ex::getSingletonInstance();
116        $arrBloc = $objQuery->select("*", "dtb_bloc", "device_type_id = ?", array($device_type_id));
117
118        switch ($this->getMode()) {
119        // 新規ブロック作成
120        case 'new_bloc':
121            SC_Response_Ex::sendRedirect('bloc.php', array("device_type_id" => $device_type_id));
122            exit;
123            break;
124
125        // 新規ページ作成
126        case 'new_page':
127            SC_Response_Ex::sendRedirect('main_edit.php', array("device_type_id" => $device_type_id));
128            exit;
129            break;
130
131        case 'confirm':
132        case 'preview':
133            //TODO 要リファクタリング(MODE if利用)
134            $page_id = $_POST['page_id'];
135            if ($this->getMode() == 'preview') {
136                $page_id = '0';
137            }
138
139            // 更新用にデータを整える
140            $arrUpdBlocData = array();
141
142            // delete実行
143            $arrRet = $objQuery->delete("dtb_blocposition",
144                                        "page_id = ? AND device_type_id = ?",
145                                        array($page_id, $device_type_id));
146
147            $arrTargetFlip = array_flip($this->arrTarget);
148
149            // POSTのデータを使いやすいように修正
150            for ($upd_cnt = 1; $upd_cnt <= $_POST['bloc_cnt']; $upd_cnt++) {
151                if (!isset($_POST['id_'.$upd_cnt])) {
152                    break;
153                }
154
155                // ブロック名称
156                $arrUpdBlocData[$upd_cnt]['name']       = $_POST['name_'.$upd_cnt];
157                // ブロックID
158                $arrUpdBlocData[$upd_cnt]['id']         = $_POST['id_'.$upd_cnt];
159                // ターゲットID
160                $arrUpdBlocData[$upd_cnt]['target_id']  = $arrTargetFlip[$_POST['target_id_'.$upd_cnt]];
161                // TOP座標
162                $arrUpdBlocData[$upd_cnt]['top']        = $_POST['top_'.$upd_cnt];
163                // 全ページ適用か
164                $arrUpdBlocData[$upd_cnt]['anywhere']   = $_POST['anywhere_'.$upd_cnt];
165                // 更新URL
166                $arrUpdBlocData[$upd_cnt]['update_url'] = $_SERVER['HTTP_REFERER'];
167            }
168
169            // ブロックの順序を取得し、更新を行う
170            foreach ($arrUpdBlocData as $key => $val) {
171                if ($arrUpdBlocData[$key]['target_id'] == TARGET_ID_UNUSED) {
172                    continue;
173                }
174
175                // ブロックの順序を取得
176                $arrUpdBlocData[$key]['bloc_row'] = $this->lfGetRowID($arrUpdBlocData, $val);
177
178                // insertデータ生成
179                $arrInsData = array('device_type_id' => $device_type_id,
180                                    'page_id' => $page_id,
181                                    'target_id' => $arrUpdBlocData[$key]['target_id'],
182                                    'bloc_id' => $arrUpdBlocData[$key]['id'],
183                                    'bloc_row' => $arrUpdBlocData[$key]['bloc_row'],
184                                    'anywhere' => $arrUpdBlocData[$key]['anywhere'] ? 1 : 0);
185                $count = $objQuery->getOne("SELECT COUNT(*) FROM dtb_blocposition WHERE anywhere = 1 AND bloc_id = ? AND device_type_id = ?",
186                                           array($arrUpdBlocData[$key]['id'], $device_type_id));
187
188                if ($arrUpdBlocData[$key]['anywhere'] == 1) {
189                    $count = $objQuery->getOne("SELECT COUNT(*) FROM dtb_blocposition WHERE anywhere = 1 AND bloc_id = ? AND device_type_id = ?",
190                                               array($arrUpdBlocData[$key]['id'], $device_type_id));
191                    if ($count != 0) {
192                        continue;
193                    }
194                } else {
195                    if ($count > 0) {
196                        $objQuery->query("DELETE FROM dtb_blocposition WHERE anywhere = 1 AND bloc_id = ? AND device_type_id = ?",
197                                         array($arrUpdBlocData[$key]['id'], $device_type_id));
198                    }
199                }
200                // SQL実行
201                $arrRet = $objQuery->insert("dtb_blocposition", $arrInsData);
202            }
203
204            // プレビュー処理 TODO 要リファクタリング(MODE if利用)
205            if ($this->getMode() == 'preview') {
206                $this->lfSetPreData($arrPageData, $objLayout);
207
208                $_SESSION['preview'] = 'ON';
209
210                SC_Response_Ex::sendRedirectFromUrlPath('preview/' . DIR_INDEX_PATH, array('filename' => $arrPageData[0]['filename']));
211                exit;
212
213            } else {
214                $arrQueryString = array("device_type_id" => $device_type_id, "page_id" => $page_id, 'msg' => 'on');
215                SC_Response_Ex::reload($arrQueryString, true);
216                exit;
217            }
218        break;
219
220        // データ削除処理
221        case 'delete':
222            //ベースデータでなければファイルを削除
223            if (!$objLayout->lfCheckBaseData($page_id, $device_type_id)) {
224                $objLayout->lfDelPageData($page_id, $device_type_id);
225                SC_Response_Ex::reload(array('device_type_id' => $device_type_id), true);
226                exit;
227            }
228        break;
229
230        default:
231        }
232
233        // ブロック情報を画面配置用に編集
234        $tpl_arrBloc = array();
235        $cnt = 0;
236        // 使用されているブロックデータを生成
237        foreach($arrBlocPos as $key => $val){
238            if ($val['page_id'] == $page_id) {
239                $tpl_arrBloc = $this->lfSetBlocData($arrBloc, $val, $tpl_arrBloc, $cnt);
240                $cnt++;
241            }
242        }
243        // 未使用のブロックデータを追加
244        foreach($arrBloc as $key => $val){
245            if (!$this->lfChkBloc($val, $tpl_arrBloc)) {
246                $val['target_id'] = TARGET_ID_UNUSED; // 未使用に追加する
247                $tpl_arrBloc = $this->lfSetBlocData($arrBloc, $val, $tpl_arrBloc, $cnt);
248                $cnt++;
249            }
250        }
251
252        $this->tpl_arrBloc = $tpl_arrBloc;
253        $this->bloc_cnt = count($tpl_arrBloc);
254        $this->page_id = $page_id;
255        $this->device_type_id = $device_type_id;
256
257        // ページ名称を取得
258        $arrPageData = $objLayout->lfGetPageData('page_id = ? AND device_type_id = ?', array($page_id, $device_type_id));
259        $this->arrPageData = $arrPageData[0];
260
261        global $GLOBAL_ERR;
262        $errCnt = 0;
263        if ($GLOBAL_ERR != "") {
264            $arrGlobalErr = explode("\n",$GLOBAL_ERR);
265            $errCnt = count($arrGlobalErr) - 8;
266            if ($errCnt < 0 ) {
267                $errCnt = 0;
268            }
269        }
270        $this->errCnt = $errCnt;
271    }
272
273    /**
274     * デストラクタ.
275     *
276     * @return void
277     */
278    function destroy() {
279        parent::destroy();
280    }
281
282    /**
283     * ブロック情報の配列を生成する.
284     *
285     * @param array $arrBloc Bloc情報
286     * @param array $tpl_arrBloc データをセットする配列
287     * @param array $val DBから取得したブロック情報
288     * @param integer $cnt 配列番号
289     * @return array データをセットした配列
290     */
291    function lfSetBlocData($arrBloc, $val, $tpl_arrBloc, $cnt) {
292
293        $tpl_arrBloc[$cnt]['target_id'] = $this->arrTarget[$val['target_id']];
294        $tpl_arrBloc[$cnt]['bloc_id'] = $val['bloc_id'];
295        $tpl_arrBloc[$cnt]['bloc_row'] =
296        isset($val['bloc_row']) ? $val['bloc_row'] : "";
297        $tpl_arrBloc[$cnt]['anywhere'] = $val['anywhere'];
298        if($val['anywhere'] == 1){
299            $tpl_arrBloc[$cnt]['anywhere_selected'] = 'checked="checked"';
300        }
301        foreach($arrBloc as $bloc_key => $bloc_val){
302            if ($bloc_val['bloc_id'] == $val['bloc_id']) {
303                $bloc_name = $bloc_val['bloc_name'];
304                break;
305            }
306        }
307        $tpl_arrBloc[$cnt]['name'] = $bloc_name;
308
309        return $tpl_arrBloc;
310    }
311
312    /**
313     * ブロックIDが配列に追加されているかのチェックを行う.
314     *
315     * @param array $arrBloc Bloc情報
316     * @param array $arrChkData チェックを行うデータ配列
317     * @return bool 存在する場合 true
318     */
319    function lfChkBloc($arrBloc, $arrChkData) {
320        foreach($arrChkData as $key => $val){
321            if ($val['bloc_id'] === $arrBloc['bloc_id'] ) {
322                // 配列に存在すればTrueを返す
323                return true;
324            }
325        }
326
327        // 配列に存在しなければFlaseを返す
328        return false;
329    }
330
331    /**
332     * ブロックIDが何番目に配置されているかを調べる.
333     *
334     * @param array $arrUpdData 更新情報
335     * @param array $arrObj チェックを行うデータ配列
336     * @return integer 順番
337     */
338    function lfGetRowID($arrUpdData, $arrObj){
339        $no = 0; // カウント用(同じデータが必ず1件あるので、初期値は0)
340
341        // 対象データが何番目に配置されているのかを取得する。
342        foreach ($arrUpdData as $key => $val) {
343            if ($val['target_id'] === $arrObj['target_id'] and $val['top'] <= $arrObj['top']){
344                $no++;
345            }
346        }
347        // 番号を返す
348        return $no;
349    }
350
351    /**
352     * プレビューするデータを DB に保存する.
353     *
354     * FIXME
355     *
356     * @param array $arrPageData ページ情報の配列
357     * @return void
358     */
359    function lfSetPreData($arrPageData, &$objLayout){
360        $objQuery = new SC_Query_Ex();     // DB操作オブジェクト
361        $sql = "";                      // データ更新SQL生成用
362        $ret = "";                      // データ更新結果格納用
363        $arrUpdData = array();          // 更新データ生成用
364        $filename = $arrPageData[0]['filename'];
365
366        $arrPreData = $objLayout->lfgetPageData(" page_id = ? " , array("0"));
367
368        // XXX tplファイルの削除
369        $del_tpl = USER_REALDIR . "templates/" . $filename . '.tpl';
370
371        if (file_exists($del_tpl)){
372            unlink($del_tpl);
373        }
374
375        // filename が空の場合にはMYページと判断
376        if($filename == ""){
377            $tplfile = TEMPLATE_REALDIR . "mypage/index";
378            $filename = 'mypage';
379        } else {
380            $tplfile = TEMPLATE_REALDIR . $filename;
381        }
382
383        // プレビュー用tplファイルのコピー
384        $copyTo = USER_REALDIR . "templates/preview/" . TEMPLATE_NAME . "/" . $filename . ".tpl";
385
386        if (!is_dir(dirname($copyTo))) {
387            mkdir(dirname($copyTo));
388        }
389
390        copy($tplfile . ".tpl", $copyTo);
391
392        // 更新データの取得
393        $sql = "select page_id,page_name, header_chk, footer_chk from dtb_pagelayout where page_id = ? OR page_id = ANY (SELECT page_id FROM dtb_blocposition WHERE anywhere = 1)" ;
394
395        $ret = $objQuery->getAll($sql, array($arrPageData[0]['page_id']));
396
397        // dbデータのコピー
398        $sql = " update dtb_pagelayout set ";
399        $sql .= "     page_name = ?";
400        $sql .= "     ,header_chk = ?";
401        $sql .= "     ,footer_chk = ?";
402        $sql .= "     ,url = ?";
403        $sql .= "     ,filename = ?";
404//      $sql .= "     ,anywhere = ?";
405        $sql .= " where page_id = 0";
406        var_dump($ret);
407                echo("####<br/>\n\n".__LINE__ ." in file:".__FILE__."<br/>\n\n ####");
408
409        $arrUpdData = array($ret[0]['page_id']
410        ,$ret[0]['page_id']
411        ,$ret[0]['page_id']
412        ,USER_DIR . "templates/" . TEMPLATE_NAME . "/"
413        ,$filename
414//      ,$ret[0]['anywhere']
415
416        );
417
418        $objQuery->query($sql,$arrUpdData);
419    }
420}
421?>
Note: See TracBrowser for help on using the repository browser.