source: branches/comu-ver2/data/class/pages/admin/design/LC_Page_Admin_Design_Template.php @ 17673

Revision 17673, 14.6 KB checked in by Seasoft, 15 years ago (diff)

merge 17635,17636,17638,17646,17647,17648,17649

  • 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
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 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/LC_Page.php");
26require_once(DATA_PATH . "module/Tar.php");
27require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_FileManager_Ex.php");
28
29/**
30 * テンプレート設定 のページクラス.
31 *
32 * @package Page
33 * @author LOCKON CO.,LTD.
34 * @version $Id$
35 */
36class LC_Page_Admin_Design_Template extends LC_Page {
37
38    // }}}
39    // {{{ functions
40
41    /** テンプレートデータ種別 */
42    var $arrSubnavi = array(
43                     'title' => array(
44                                1 => 'top',
45                                2 => 'product',
46                                3 => 'detail',
47                                4 => 'mypage'
48                                             ),
49                     'name' =>array(
50                                1 => 'TOPページ',
51                                2 => '商品一覧ページ',
52                                3 => '商品詳細ページ',
53                                4 => 'MYページ'
54                              )
55                     );
56
57    /**
58     * Page を初期化する.
59     *
60     * @return void
61     */
62    function init() {
63        parent::init();
64        $this->tpl_mainpage = 'design/template.tpl';
65        $this->tpl_subnavi  = 'design/subnavi.tpl';
66        $this->tpl_subno    = 'template';
67        $this->tpl_mainno   = "design";
68        $this->tpl_subtitle = 'テンプレート設定';
69        $this->arrErr  = array();
70        $this->arrForm = array();
71        $this->tpl_select = TEMPLATE_NAME;
72        ini_set("max_execution_time", 300);
73    }
74
75    /**
76     * Page のプロセス.
77     *
78     * @return void
79     */
80    function process() {
81        // 認証可否の判定
82        $objSession = new SC_Session();
83        SC_Utils::sfIsSuccess($objSession);
84
85        // uniqidをテンプレートへ埋め込み
86        $this->uniqid = $objSession->getUniqId();
87
88        $objView = new SC_AdminView();
89
90        switch($this->lfGetMode()) {
91
92        // 登録ボタン押下時
93        case 'register':
94            // 画面遷移の正当性チェック
95            if (!SC_Utils::sfIsValidTransition($objSession)) {
96                sfDispError('');
97            }
98            // パラメータ検証
99            $objForm = $this->lfInitRegister();
100            if ($objForm->checkError()) {
101                SC_Utils_Ex::sfDispError('');
102            }
103
104            $template_code = $objForm->getValue('template_code');
105            $this->tpl_select = $template_code;
106
107            if($template_code == "") {
108                $template_code = "default";
109            }
110
111            // DBへ使用するテンプレートを登録
112            $this->lfRegisterTemplate($template_code);
113
114            // XXX コンパイルファイルのクリア処理を行う
115            $objView->_smarty->clear_compiled_tpl();
116
117            // common.cssの内容を更新
118            $this->lfChangeCommonCss($template_code);
119           
120            // テンプレートのコピー
121            $this->lfCopyTemplate($template_code);
122           
123            // ブロック位置を更新
124            $this->lfChangeBloc($template_code);
125
126            // 完了メッセージ
127            $this->tpl_onload="alert('登録が完了しました。');";
128            break;
129
130        // 削除ボタン押下時
131        case 'delete':
132            // 画面遷移の正当性チェック
133            if (!SC_Utils::sfIsValidTransition($objSession)) {
134                SC_Utils::sfDispError('');
135            }
136            // パラメータ検証
137            $objForm = $this->lfInitDelete();
138            if ($objForm->checkError()) {
139                SC_Utils::sfDispError('');
140            }
141           
142            //現在使用中のテンプレートとデフォルトのテンプレートは削除できないようにする
143            $template_code = $objForm->getValue('template_code_temp');
144            if ($template_code == TEMPLATE_NAME || $template_code == DEFAULT_TEMPLATE_NAME) {
145                $this->tpl_onload = "alert('選択中のテンプレートは削除出来ません');";
146                break;
147            }
148            $this->lfDeleteTemplate($template_code);
149            break;
150
151        // downloadボタン押下時
152        case 'download':
153            // 画面遷移の正当性チェック
154            if (!SC_Utils::sfIsValidTransition($objSession)) {
155                SC_Utils::sfDispError('');
156            }
157            // パラメータ検証
158            $objForm = $this->lfInitDownload();
159            $template_code = $objForm->getValue('template_code_temp');
160            // ユーザデータの下のファイルも保存する。
161            $from_dir = USER_TEMPLATE_PATH . $template_code . "/";
162            $to_dir = SMARTY_TEMPLATES_DIR . $template_code . "/_packages/";
163            SC_Utils::sfMakeDir($to_dir);
164            SC_Utils::sfCopyDir($from_dir, $to_dir);
165            SC_Helper_FileManager::downloadArchiveFiles(SMARTY_TEMPLATES_DIR . $template_code);
166            break;
167
168        // プレビューボタン押下時
169        case 'preview':
170            break;
171
172        default:
173            break;
174        }
175
176        // defaultパラメータのセット
177        $this->templates = $this->lfGetAllTemplates();
178        $this->now_template = TEMPLATE_NAME;
179        // 画面の表示
180        $objView->assignobj($this);
181        $objView->display(MAIN_FRAME);
182    }
183
184    /**
185     * デストラクタ.
186     *
187     * @return void
188     */
189    function destroy() {
190        parent::destroy();
191    }
192
193    function lfGetMode(){
194        if (isset($_POST['mode'])) return $_POST['mode'];
195    }
196
197    function lfInitRegister() {
198        $objForm = new SC_FormParam();
199        $objForm->addParam(
200            'template_code', 'template_code', STEXT_LEN, '',
201            array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK", "ALNUM_CHECK")
202        );
203        $objForm->setParam($_POST);
204
205        return $objForm;
206    }
207
208    function lfInitDelete() {
209        $objForm = new SC_FormParam();
210        $objForm->addParam(
211            'template_code_temp', 'template_code_temp', STEXT_LEN, '',
212            array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK", "ALNUM_CHECK")
213        );
214        $objForm->setParam($_POST);
215
216        return $objForm;
217    }
218
219    function lfInitDownload() {
220        $objForm = new SC_FormParam();
221        $objForm->addParam(
222            'template_code_temp', 'template_code_temp', STEXT_LEN, '',
223            array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK", "ALNUM_CHECK")
224        );
225        $objForm->setParam($_POST);
226
227        return $objForm;
228    }
229
230    /**
231     * 使用するテンプレートをDBへ登録する
232     */
233    function lfRegisterTemplate($template_code) {
234        $objQuery = new SC_Query();
235        $sqlval['name'] = "\"" . $template_code . "\"";
236        $objQuery->update("mtb_constants", $sqlval, "id = ?", array('TEMPLATE_NAME'));
237        // キャッシュを生成
238        $masterData = new SC_DB_MasterData_Ex();
239        // 更新したデータを取得
240        $mtb_constants = $masterData->getDBMasterData("mtb_constants");
241        $masterData->clearCache("mtb_constants");
242        $masterData->createCache("mtb_constants", $mtb_constants, true,
243                                 array("id", "remarks", "rank"));
244    }
245
246    /**
247     * common.cssの更新
248     */
249    function lfChangeCommonCss($template_code) {
250        $css_path = USER_PATH . "css/common.css";
251       
252        // ファイル内容取得
253        $css_data = file_get_contents($css_path);
254       
255        // ファイル内容更新
256        $pt = '/(@import url\("\.\.\/packages\/.+\/css\/import\.css"\);)/';
257        $rp = '@import url("../packages/'. $template_code. '/css/import.css");';
258        $css = preg_replace($pt, $rp, $css_data);
259       
260        // ファイル更新
261        $fp = fopen($css_path,"w");
262        fwrite($fp, $css);
263        fclose($fp);
264    }
265   
266    /**
267     * ブロック位置の更新
268     */
269    function lfChangeBloc($template_code) {
270        $objQuery = new SC_Query();
271        $filepath = USER_TEMPLATE_PATH. $template_code. "/sql/update_bloc.sql";
272       
273        // ブロック位置更新SQLファイル有
274        if(file_exists($filepath)) {
275            if($fp = fopen($filepath, "r")) {
276                $sql = fread($fp, filesize($filepath));
277                fclose($fp);
278            }
279            // 改行、タブを1スペースに変換
280            $sql = preg_replace("/[\r\n\t]/", " " ,$sql);
281            $sql_split = split(";", $sql);
282            foreach($sql_split as $key => $val){
283                if (trim($val) != "") {
284                    $objQuery->query($val);
285                }
286            }
287        }
288    }
289
290    /**
291     * テンプレートパッケージの削除
292     */
293    function lfDeleteTemplate($template_code) {
294        // DB更新
295        $objQuery = new SC_Query();
296        $objQuery->delete('dtb_templates', 'template_code = ?', array($template_code));
297        // テンプレート削除
298        $templates_dir = SMARTY_TEMPLATES_DIR. $template_code. "/";
299        SC_Utils_Ex::sfDelFile($templates_dir);
300        // コンパイル削除
301        $templates_c_dir = DATA_PATH. "Smarty/templates_c/". $template_code. "/";
302        SC_Utils_Ex::sfDelFile($templates_c_dir);
303        // ユーザーデータ削除
304        $user_dir = USER_TEMPLATE_PATH. $template_code. "/";
305        SC_Utils_Ex::sfDelFile($user_dir);
306    }
307
308    function lfGetAllTemplates() {
309        $objQuery = new SC_Query();
310        $arrRet = $objQuery->select('*', 'dtb_templates');
311        if (empty($arrRet)) return array();
312
313        return $arrRet;
314    }
315   
316    /**
317     * テンプレート変更時に既に存在するキャンペーンのテンプレートがない場合はテンプレートを生成する
318     *
319     */
320    function lfCopyTemplate($template_code){
321        //すべてのキャンペーンのテンプレートファイルを確認
322        $objQuery = new SC_Query();
323        $sql = "SELECT directory_name,cart_flg FROM dtb_campaign WHERE del_flg = 0";
324        $result = $objQuery->getall( $sql );
325        //デザインテンプレートディレクトリにファイルが存在するか確認
326        foreach( $result as $key => $val ){
327            //index.phpが存在すればキャンペーンのテンプレートがあると判定
328            //全部チェックした方がいいか?
329            $campaign_template_file_path = SMARTY_TEMPLATES_DIR.$template_code ."/". CAMPAIGN_TEMPLATE_DIR . $val['directory_name'] . "/" .CAMPAIGN_TEMPLATE_ACTIVE .  "site_frame.tpl";
330            var_dump("<font color='red'>$campaign_template_file_path</font><br>");
331            if(!file_exists($campaign_template_file_path)){
332                var_dump("<font color='red'>test</font>");
333                //ファイルがなければコピーして作成
334                $this->lfCreateTemplate(SMARTY_TEMPLATES_DIR.$template_code ."/" . CAMPAIGN_TEMPLATE_DIR , $val['directory_name'],$val['cart_flg'] );
335            }
336        }
337    }
338   
339   /*
340     * 関数名:lfCreateTemplate()
341     * 引数1 :ディレクトリパス
342     * 引数2 :作成ファイル名
343     * 説明 :キャンペーンの初期テンプレート作成
344     * 戻り値:無し
345     */
346    function lfCreateTemplate($dir, $file , $cart_flg) {
347        umask(0);
348        $objFileManager = new SC_Helper_FileManager_Ex();
349       
350        // 作成ファイルディレクトリ
351        $create_dir = $dir . $file;
352        $create_active_dir = $create_dir . "/" . CAMPAIGN_TEMPLATE_ACTIVE;
353        $create_end_dir = $create_dir . "/" . CAMPAIGN_TEMPLATE_END;
354        // デフォルトファイルディレクトリ
355        $default_dir = TEMPLATE_DIR . CAMPAIGN_TEMPLATE_DIR;
356        $default_active_dir = $default_dir . "/" . CAMPAIGN_TEMPLATE_ACTIVE;
357        $default_end_dir = $default_dir . "/" . CAMPAIGN_TEMPLATE_END;
358
359        $ret = $objFileManager->sfCreateFile($create_dir, 0755);
360        $ret = $objFileManager->sfCreateFile($create_active_dir, 0755);
361        $ret = $objFileManager->sfCreateFile($create_end_dir, 0755);
362
363        // キャンペーン実行PHPをコピー
364        $ret = $objFileManager->sfCreateFile(CAMPAIGN_PATH . $file);
365        copy(HTML_PATH . CAMPAIGN_TEMPLATE_DIR . "index.php", CAMPAIGN_PATH . $file . "/index.php");
366        copy(HTML_PATH . CAMPAIGN_TEMPLATE_DIR . "application.php", CAMPAIGN_PATH . $file . "/application.php");
367        copy(HTML_PATH . CAMPAIGN_TEMPLATE_DIR . "complete.php", CAMPAIGN_PATH . $file . "/complete.php");
368        copy(HTML_PATH . CAMPAIGN_TEMPLATE_DIR . "entry.php", CAMPAIGN_PATH . $file . "/entry.php");
369
370        // デフォルトテンプレート作成(キャンペーン中)
371        $header = $this->lfGetFileContents($default_active_dir."header.tpl");
372        SC_Utils_Ex::sfWriteFile($header, $create_active_dir."header.tpl", "w");
373        $contents = $this->lfGetFileContents($default_active_dir."contents.tpl");
374        if(!$cart_flg) {
375            $contents .= "\n" . '<!--{*ログインフォーム*}-->' . "\n";
376            $contents .= $this->lfGetFileContents(CAMPAIGN_BLOC_PATH . "login.tpl");
377            $contents .= '<!--{*会員登録フォーム*}-->'."\n";
378            $contents .= $this->lfGetFileContents(CAMPAIGN_BLOC_PATH . "entry.tpl");
379        }
380        SC_Utils_Ex::sfWriteFile($contents, $create_active_dir."contents.tpl", "w");
381        $footer = $this->lfGetFileContents($default_active_dir."footer.tpl");
382        SC_Utils_Ex::sfWriteFile($footer, $create_active_dir."footer.tpl", "w");
383
384        // サイトフレーム作成
385        $site_frame  = $header."\n";
386        $site_frame .= '<script type="text/javascript" src="<!--{$TPL_DIR}-->js/navi.js"></script>'."\n";
387        $site_frame .= '<script type="text/javascript" src="<!--{$TPL_DIR}-->js/site.js"></script>'."\n";
388        $site_frame .= '<!--{include file=$tpl_mainpage}-->'."\n";
389        $site_frame .= $footer."\n";
390        SC_Utils_Ex::sfWriteFile($site_frame, $create_active_dir."site_frame.tpl", "w");
391
392        /* デフォルトテンプレート作成(キャンペーン終了) */
393        $header = $this->lfGetFileContents($default_end_dir."header.tpl");
394        SC_Utils_Ex::sfWriteFile($header, $create_end_dir."header.tpl", "w");
395        $contents = $this->lfGetFileContents($default_end_dir."contents.tpl");
396        SC_Utils_Ex::sfWriteFile($contents, $create_end_dir."contents.tpl", "w");
397        $footer = $this->lfGetFileContents($default_end_dir."footer.tpl");
398        SC_Utils_Ex::sfWriteFile($footer, $create_end_dir."footer.tpl", "w");
399    }
400   
401    /*
402     * 関数名:lfGetFileContents()
403     * 引数1 :ファイルパス
404     * 説明 :ファイル読込
405     * 戻り値:無し
406     */
407    function lfGetFileContents($read_file) {
408
409        if(file_exists($read_file)) {
410            $contents = file_get_contents($read_file);
411        } else {
412            $contents = "";
413        }
414
415        return $contents;
416    }
417}
418?>
Note: See TracBrowser for help on using the repository browser.