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

Revision 20830, 13.7 KB checked in by nanasess, 13 years ago (diff)

#972 (リファクタリング開発:[管理画面]デザイン管理)

  • LC_Page_Admin_Design.php
  • SC_Helper_PageLayout もリファクタリング

#1254 (SC_FormParam::getValue() の第二引数が無効)

  • SC_Utils::isBlank() でチェックするよう修正
  • 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
RevLine 
[15682]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[20764]5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
[15682]6 *
7 * http://www.lockon.co.jp/
[16582]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.
[15682]22 */
23
24// {{{ requires
[20534]25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
[15682]26
27/**
28 * メイン編集 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
[20345]34class LC_Page_Admin_Design_MainEdit extends LC_Page_Admin_Ex {
[15682]35
36    // }}}
37    // {{{ functions
38
39    /**
40     * Page を初期化する.
41     *
42     * @return void
43     */
44    function init() {
45        parent::init();
[16565]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";
[20538]50        $this->tpl_mainno = 'design';
[16565]51        $this->tpl_subtitle = 'ページ詳細設定';
[20560]52        $masterData = new SC_DB_MasterData_Ex();
53        $this->arrDeviceType = $masterData->getMasterData('mtb_device_type');
[15682]54    }
55
56    /**
57     * Page のプロセス.
58     *
59     * @return void
60     */
61    function process() {
[19661]62        $this->action();
63        $this->sendResponse();
64    }
65
66    /**
67     * Page のアクション.
68     *
69     * @return void
70     */
71    function action() {
[20306]72        $objView = new SC_AdminView_Ex();
[15682]73        $this->objLayout = new SC_Helper_PageLayout_Ex();
74
[19713]75        // ページIDを取得
76        if (isset($_REQUEST['page_id']) && is_numeric($_REQUEST['page_id'])) {
77            $page_id = $_REQUEST['page_id'];
[15682]78        }
79
80        $this->page_id = $page_id;
81
[19713]82        // 端末種別IDを取得
83        if (isset($_REQUEST['device_type_id'])
84            && is_numeric($_REQUEST['device_type_id'])) {
85            $device_type_id = $_REQUEST['device_type_id'];
86        } else {
87            $device_type_id = DEVICE_TYPE_PC;
88        }
89
[20560]90        //サブタイトルの追加
91        $this->tpl_subtitle .= ' - ' . $this->arrDeviceType[$device_type_id];
92
[19713]93        // ページ一覧を取得
[20830]94        $this->arrPageList = $this->objLayout->getPageProperties($device_type_id, null);
[19713]95
[15682]96        // メッセージ表示
[20538]97        if (isset($_GET['msg']) && $_GET['msg'] == 'on'){
[15682]98            $this->tpl_onload="alert('登録が完了しました。');";
99        }
100
101        // page_id が指定されている場合にはテンプレートデータの取得
[19737]102        if (is_numeric($page_id) && $page_id != '') {
[19722]103            $this->arrPageData = $this->lfGetPageData($page_id, $device_type_id, $objView);
[18344]104        }
[15682]105
[20041]106        switch ($this->getMode()) {
[19713]107        case 'delete':
[20830]108            if ($this->objLayout->isEditablePage($device_type_id, $page_id)) {
[19722]109                $this->lfDeletePageData($page_id, $device_type_id);
[19713]110                exit;
111            }
112            break;
113
114        case 'confirm':
[19722]115            $this->lfConfirmPageData($page_id, $device_type_id);
[19713]116        default:
[18344]117        }
[19722]118        $this->device_type_id = $device_type_id;
[18344]119    }
[15682]120
[18344]121    /**
122     * デストラクタ.
123     *
124     * @return void
125     */
126    function destroy() {
127        parent::destroy();
128    }
[15682]129
[18344]130    /**
131     * ページデータを取得する.
132     *
133     * @param integer $page_id ページID
[19713]134     * @param integer $device_type_id 端末種別ID
[18344]135     * @param object $objView ビューオブジェクト
136     * @return void
137     */
[19713]138    function lfGetPageData($page_id, $device_type_id, $objView){
[20830]139        $arrPageData = $this->objLayout->getPageProperties($device_type_id, $page_id);
[15682]140
[18344]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        }
[15682]148
[19768]149        // テンプレートを読み込む
150        $templatePath = $this->objLayout->getTemplatePath($device_type_id);
[20055]151        $arrPageData[0]['tpl_data'] = file_get_contents($templatePath . $arrPageData[0]['filename'] . ".tpl");
[19725]152
[18344]153        // ディレクトリを画面表示用に編集
[20055]154        $arrPageData[0]['filename'] = preg_replace('|^' . preg_quote(USER_DIR) . '|', '', $arrPageData[0]['filename']);
[15682]155
[19722]156        return $arrPageData[0];
[18344]157    }
[15682]158
[18344]159    /**
160     * データ登録処理.
[15682]161     *
[18344]162     * @param integer $page_id ページID
[19713]163     * @param integer $device_type_id 端末種別ID
[15682]164     * @return void
165     */
[19713]166    function lfConfirmPageData($page_id, $device_type_id) {
[18344]167        // エラーチェック
[19737]168        $this->arrErr = $this->lfErrorCheck($_POST, $device_type_id);
[18344]169
170        // エラーがなければ更新処理を行う
171        if (count($this->arrErr) == 0) {
172            // DBへデータを更新する
[20055]173            $arrTmp = $this->lfEntryPageData(
174                $device_type_id,
175                $page_id,
176                $_POST['page_name'],
177                USER_DIR . $_POST['url'],
[20775]178                intval($_POST['header_chk']) === 1 ? 1 : 2,
179                intval($_POST['footer_chk']) === 1 ? 1 : 2
[20055]180            );
[19804]181            $page_id = $arrTmp['page_id'];
[18344]182
[20830]183            $arrTmp = $this->objLayout->getPageProperties($device_type_id, $page_id);
[19804]184            $arrData = $arrTmp[0];
185
[18344]186            // ベースデータでなければファイルを削除し、PHPファイルを作成する
[20830]187            if ($this->objLayout->isEditablePage($device_type_id, $arrData['page_id'])) {
[18344]188                // PHPファイル作成
[19722]189                $this->lfCreatePHPFile($_POST['url'], $device_type_id);
[18344]190            }
191
192            // TPLファイル作成
[20055]193            $cre_tpl = $this->objLayout->getTemplatePath($device_type_id) . $arrData['filename'] . '.tpl';
[18344]194            $this->lfCreateFile($cre_tpl, $_POST['tpl_data']);
195
[19832]196            $arrQueryString = array(
197                "page_id" => $arrData['page_id'],
198                "device_type_id" => $device_type_id,
[20538]199                'msg'     => 'on',
[19832]200            );
201            $this->objDisplay->reload($arrQueryString, true);
[18344]202            exit;
203        } else {
204            // エラーがあれば入力時のデータを表示する
205            $this->arrPageData = $_POST;
206            $this->arrPageData['directory'] = '';
207            $this->arrPageData['filename'] = $_POST['url'];
208        }
[15682]209    }
210
211    /**
212     * ブロック情報を更新する.
213     *
[20055]214     * @param integer $device_type_id
215     * @param integer $page_id
216     * @param string $page_name
217     * @param string $filename
218     * @param integer $header_chk
219     * @param integer $footer_chk
[19774]220     * @return array 実際に使用した更新データ
[15682]221     */
[20055]222    function lfEntryPageData($device_type_id, $page_id, $page_name, $filename, $header_chk, $footer_chk) {
[20507]223        $objQuery = new SC_Query_Ex();
[18803]224        $arrChk = array();          // 排他チェック用
[15682]225
[20055]226        // 更新用データの変換
227        $sqlval = $this->lfGetUpdData($device_type_id, $page_id, $page_name, $filename, $header_chk, $footer_chk);
[15682]228
229        // データが存在しているかチェックを行う
[20055]230        if ($page_id !== ''){
[20830]231            $arrChk = $this->objLayout->getPageProperties($device_type_id, $page_id);
[15682]232        }
[18803]233
[15682]234        // page_id が空 若しくは データが存在していない場合にはINSERTを行う
[20055]235        if ($page_id === '' || !isset($arrChk[0])) {
[19713]236            // FIXME device_type_id ごとの連番にする
[18803]237            $sqlval['page_id'] = $objQuery->nextVal('dtb_pagelayout_page_id');
[19713]238            $sqlval['device_type_id'] = $device_type_id;
[18803]239            $sqlval['create_date'] = 'now()';
240            $objQuery->insert('dtb_pagelayout', $sqlval);
[15682]241        }
[18803]242        // データが存在してる場合にはアップデートを行う
243        else {
[19713]244            $objQuery->update('dtb_pagelayout', $sqlval, 'page_id = ? AND device_type_id = ?',
[20055]245                              array($page_id, $device_type_id));
[19804]246            // 戻り値用
[20055]247            $sqlval['page_id'] = $page_id;
[18803]248        }
[19774]249        return $sqlval;
[15682]250    }
251
252    /**
253     * DBへ更新を行うデータを生成する.
254     *
[20055]255     * @param integer $device_type_id
256     * @param integer $page_id
257     * @param string $page_name
258     * @param string $filename
259     * @param integer $header_chk
260     * @param integer $footer_chk
[15682]261     * @return array 更新データ
262     */
[20055]263    function lfGetUpdData($device_type_id, $page_id, $page_name, $filename, $header_chk, $footer_chk) {
[18803]264        $arrUpdData = array(
[20775]265            'header_chk'    => $header_chk,  // ヘッダー使用
266            'footer_chk'    => $footer_chk,  // フッター使用
[20055]267            'update_url'    => $_SERVER['HTTP_REFERER'],                    // 更新URL
[18803]268            'update_date'   => 'now()',
269        );
270
[15682]271        // ベースデータの場合には変更しない。
[20830]272        if ($this->objLayout->isEditablePage($device_type_id, $page_id)) {
[20055]273            $arrUpdData['page_name']    = $page_name;
274            $arrUpdData['url']          = $filename . '.php';
275            $arrUpdData['filename']     = $filename; // 拡張子を付加しない
[15682]276        }
277
278        return $arrUpdData;
279    }
280
281    /**
[18344]282     * ページデータを削除する.
283     *
284     * @param integer $page_id ページID
285     * @return void
286     */
[19713]287    function lfDeletePageData($page_id, $device_type_id){
288        $this->objLayout->lfDelPageData($page_id, $device_type_id);
[19832]289        $this->objDisplay->reload(array("device_type_id" => $device_type_id), true);
[18344]290    }
291
292    /**
[15682]293     * 入力項目のエラーチェックを行う.
294     *
[20830]295     * XXX $device_type_id が dtb_pagelayout の検索条件に入ってない
296     *
[15682]297     * @param array $arrData 入力データ
[19737]298     * @param integer $device_type_id 端末種別ID
[15682]299     * @return array エラー情報
300     */
[19737]301    function lfErrorCheck($array, $device_type_id) {
[20503]302        $objErr = new SC_CheckError_Ex($array);
[15682]303        $objErr->doFunc(array("名称", "page_name", STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
[20538]304        $objErr->doFunc(array('URL', 'url', STEXT_LEN), array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
[19713]305
[15682]306        // URLチェック
[17416]307        $okUrl = true;
308        foreach (explode('/', $array['url']) as $url_part) {
309            if (!ereg( '^[a-zA-Z0-9:_~\.-]+$', $url_part)) {
310                $okUrl = false;
311            }
312            if ($url_part == '.' || $url_part == '..') {
313                $okUrl = false;
314            }
[15682]315        }
[17416]316        if (!$okUrl) {
[15682]317            $objErr->arrErr['url'] = "※ URLを正しく入力してください。<br />";
318        }
[19713]319
[15682]320        // 同一のURLが存在している場合にはエラー
[17416]321        $params = array();
[20041]322
[18847]323        $sqlWhere = 'url = ?';
[19737]324        $params[] = $this->objLayout->getUserDir($device_type_id) . $array['url'] . '.php';
[20041]325
[17416]326        // プレビュー用のレコードは除外
327        $sqlWhere .= ' AND page_id <> 0';
[20041]328
[17416]329        // 変更の場合、自身のレコードは除外
330        if (strlen($array['page_id']) != 0) {
331            $sqlWhere .= ' AND page_id <> ?';
332            $params[] = $array['page_id'];
[15682]333        }
[20041]334
[20830]335        $objQuery =& SC_Query_Ex::getSingletonInstance();
336        $arrChk = $objQuery->select('*', 'dtb_pagelayout', $sqlWhere, $params);
[20041]337
[17416]338        if (count($arrChk) >= 1) {
339            $objErr->arrErr['url'] = '※ 同じURLのデータが存在しています。別のURLを付けてください。<br />';
340        }
[20041]341
[15682]342        return $objErr->arrErr;
343    }
344
345    /**
346     * ファイルを作成する.
347     *
348     * @param string $path テンプレートファイルのパス
[18344]349     * @param string $data テンプレートの内容
[15682]350     * @return void
351     */
[18344]352    function lfCreateFile($path, $data){
[15682]353
354        // ディレクトリが存在していなければ作成する
355        if (!is_dir(dirname($path))) {
[19806]356            mkdir(dirname($path), 0777, true); // FIXME (PHP4)
[15682]357        }
358
359        // ファイル作成
[20538]360        $fp = fopen($path,'w');
[19768]361        if ($fp === false) {
362            SC_Utils_Ex::sfDispException();
363        }
364        $ret = fwrite($fp, $data);
365        if ($ret === false) {
366            SC_Utils_Ex::sfDispException();
367        }
[15682]368        fclose($fp);
369    }
370
371    /**
372     * PHPファイルを作成する.
373     *
374     * @param string $path PHPファイルのパス
375     * @return void
376     */
[19722]377    function lfCreatePHPFile($url, $device_type_id){
[15682]378
[20462]379        $path = USER_REALDIR . $url . ".php";
[18861]380
381        // カスタマイズを考慮し、上書きしない。(#831)
382        if (file_exists($path)) {
383            return;
384        }
385
[15682]386        // php保存先ディレクトリが存在していなければ作成する
387        if (!is_dir(dirname($path))) {
[19806]388            mkdir(dirname($path), 0777, true); // FIXME (PHP4)
[15682]389        }
390
391        // ベースとなるPHPファイルの読み込み
[19807]392        if (file_exists(USER_DEF_PHP_REALFILE)){
393            $php_data = file_get_contents(USER_DEF_PHP_REALFILE);
[15682]394        }
395
396        // require.phpの場所を書き換える
[19777]397        $defaultStrings = "exit; // Don't rewrite. This line is rewritten by EC-CUBE.";
398        $replaceStrings = "require_once '" . str_repeat('../', substr_count($url, '/')) . "../require.php';";
399        $php_data = str_replace($defaultStrings, $replaceStrings, $php_data);
[15682]400
401        // phpファイルの作成
[20538]402        $fp = fopen($path,'w');
[15682]403        fwrite($fp, $php_data);
404        fclose($fp);
405    }
406
407}
408?>
Note: See TracBrowser for help on using the repository browser.