source: branches/version-2_12-dev/data/class/SC_View.php @ 21597

Revision 21597, 8.4 KB checked in by h_yoshimoto, 12 years ago (diff)

#1603 テンプレートの変形機能による修正

  • 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-2011 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
24require_once realpath(dirname(__FILE__)) . '/../module/Smarty/libs/Smarty.class.php';
25
26class SC_View {
27
28    var $_smarty;
29
30    var $objPage;
31
32    // コンストラクタ
33    function SC_View($siteinfo = true) {
34
35        $this->_smarty = new Smarty;
36        $this->_smarty->left_delimiter = '<!--{';
37        $this->_smarty->right_delimiter = '}-->';
38        $this->_smarty->register_modifier('sfDispDBDate', array('SC_Utils_Ex', 'sfDispDBDate'));
39        $this->_smarty->register_modifier('sfConvSendDateToDisp', array('SC_Utils_Ex', 'sfConvSendDateToDisp'));
40        $this->_smarty->register_modifier('sfConvSendWdayToDisp', array('SC_Utils_Ex', 'sfConvSendWdayToDisp'));
41        $this->_smarty->register_modifier('sfGetVal', array('SC_Utils_Ex', 'sfGetVal'));
42        $this->_smarty->register_modifier('sfGetErrorColor', array('SC_Utils_Ex', 'sfGetErrorColor'));
43        $this->_smarty->register_modifier('sfTrim', array('SC_Utils_Ex', 'sfTrim'));
44        $this->_smarty->register_modifier('sfCalcIncTax', array('SC_Helper_DB_Ex', 'sfCalcIncTax'));
45        $this->_smarty->register_modifier('sfPrePoint', array('SC_Utils_Ex', 'sfPrePoint'));
46        $this->_smarty->register_modifier('sfGetChecked',array('SC_Utils_Ex', 'sfGetChecked'));
47        $this->_smarty->register_modifier('sfTrimURL', array('SC_Utils_Ex', 'sfTrimURL'));
48        $this->_smarty->register_modifier('sfMultiply', array('SC_Utils_Ex', 'sfMultiply'));
49        $this->_smarty->register_modifier('sfRmDupSlash', array('SC_Utils_Ex', 'sfRmDupSlash'));
50        $this->_smarty->register_modifier('sfCutString', array('SC_Utils_Ex', 'sfCutString'));
51        $this->_smarty->plugins_dir=array('plugins', realpath(dirname(__FILE__)) . '/../smarty_extends');
52        $this->_smarty->register_modifier('sfMbConvertEncoding', array('SC_Utils_Ex', 'sfMbConvertEncoding'));
53        $this->_smarty->register_modifier('sfGetEnabled', array('SC_Utils_Ex', 'sfGetEnabled'));
54        $this->_smarty->register_modifier('sfGetCategoryId', array('SC_Utils_Ex', 'sfGetCategoryId'));
55        $this->_smarty->register_modifier('sfNoImageMainList', array('SC_Utils_Ex', 'sfNoImageMainList'));
56        // XXX register_function で登録すると if で使用できないのではないか?
57        $this->_smarty->register_function('sfIsHTTPS', array('SC_Utils_Ex', 'sfIsHTTPS'));
58        $this->_smarty->register_function('sfSetErrorStyle', array('SC_Utils_Ex', 'sfSetErrorStyle'));
59        $this->_smarty->register_function('printXMLDeclaration', array('GC_Utils_Ex', 'printXMLDeclaration'));
60        $this->_smarty->default_modifiers = array('script_escape');
61
62        if (ADMIN_MODE == '1') {
63            $this->time_start = microtime(true);
64        }
65    }
66
67    // テンプレートに値を割り当てる
68    function assign($val1, $val2) {
69        $this->_smarty->assign($val1, $val2);
70    }
71
72    // テンプレートの処理結果を取得
73    function fetch($template, $no_error=false) {
74        return $this->_smarty->fetch($template);
75    }
76
77    /**
78     * SC_Display用にレスポンスを返す
79     * @global string $GLOBAL_ERR
80     * @param array $template
81     * @param boolean $no_error
82     * @return string
83     */
84    function getResponse($template, $no_error = false) {
85        if (!$no_error) {
86            global $GLOBAL_ERR;
87            if (!defined('OUTPUT_ERR')) {
88                // GLOBAL_ERR を割り当て
89                $this->assign('GLOBAL_ERR', $GLOBAL_ERR);
90                define('OUTPUT_ERR','ON');
91            }
92        }
93        // 各filterをセットします.
94        $this->registFilter();
95        $res =  $this->_smarty->fetch($template);
96        if (ADMIN_MODE == '1') {
97            $time_end = microtime(true);
98            $time = $time_end - $this->time_start;
99            $res .= '処理時間: ' . sprintf('%.3f', $time) . '秒';
100        }
101        return $res;
102    }
103
104    /**
105     * Pageオブジェクトをセットします.
106     * @param LC_Page_Ex $objPage
107     * @return void
108     */
109    function setPage(LC_Page_Ex $objPage) {
110       $this->objPage = $objPage;
111    }
112
113    /**
114     * Smartyのfilterをセットします.
115     * @return void
116     */
117    function registFilter() {
118        $this->_smarty->register_prefilter(array(&$this, 'prefilter_transform'));
119        $this->_smarty->register_outputfilter(array(&$this, 'outputfilter_transform'));
120    }
121
122    /**
123     * prefilter用のフィルタ関数。プラグイン用のフックポイント処理を実行
124     * @param string $source ソース
125     * @param Smarty_Compiler $smarty Smartyのコンパイラクラス
126     * @return string $source ソース
127     */
128    function prefilter_transform($source, &$smarty) {
129        // フックポイントを実行.
130        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->objPage->plugin_activate_flg);
131        $objPlugin->doAction('prefilterTransform', array(&$source, $this->objPage, $smarty->_current_file));
132        return $source;
133    }
134
135    /**
136     * outputfilter用のフィルタ関数。プラグイン用のフックポイント処理を実行
137     * @param string $source ソース
138     * @param Smarty_Compiler $smarty Smartyのコンパイラクラス
139     * @return string $source ソース
140     */
141    function outputfilter_transform($source, &$smarty) {
142        // フックポイントを実行.
143        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->objPage->plugin_activate_flg);
144        $objPlugin->doAction('outputfilterTransform', array(&$source, $this->objPage, $smarty->_current_file));
145        return $source;
146    }
147
148    // テンプレートの処理結果を表示
149    function display($template, $no_error = false) {
150        if (!$no_error) {
151            global $GLOBAL_ERR;
152            if (!defined('OUTPUT_ERR')) {
153                // GLOBAL_ERR を割り当て
154                $this->assign('GLOBAL_ERR', $GLOBAL_ERR);
155                define('OUTPUT_ERR','ON');
156            }
157        }
158
159        $this->_smarty->display($template);
160        if (ADMIN_MODE == '1') {
161            $time_end = microtime(true);
162            $time = $time_end - $this->time_start;
163            echo '処理時間: ' . sprintf('%.3f', $time) . '秒';
164        }
165    }
166
167    // オブジェクト内の変数をすべて割り当てる。
168    function assignobj($obj) {
169        $data = get_object_vars($obj);
170
171        foreach ($data as $key => $value) {
172            $this->_smarty->assign($key, $value);
173        }
174    }
175
176    // 連想配列内の変数をすべて割り当てる。
177    function assignarray($array) {
178        foreach ($array as $key => $val) {
179            $this->_smarty->assign($key, $val);
180        }
181    }
182
183    /* サイト初期設定 */
184    function initpath() {
185
186        $array['tpl_mainnavi'] = realpath(dirname(__FILE__)) . '/../Smarty/templates/frontparts/mainnavi.tpl';
187
188        $objDb = new SC_Helper_DB_Ex();
189        $array['tpl_root_id'] = $objDb->sfGetRootId();
190        $this->assignarray($array);
191    }
192
193    /**
194     * テンプレートパスをアサインする.
195     *
196     * @param integer $device_type_id 端末種別ID
197     */
198    function assignTemplatePath($device_type_id) {
199
200        // テンプレート変数を割り当て
201        $this->assign('TPL_URLPATH', SC_Helper_PageLayout_Ex::getUserDir($device_type_id, true));
202
203        // ヘッダとフッタを割り当て
204        $templatePath = SC_Helper_PageLayout_Ex::getTemplatePath($device_type_id);
205        $header_tpl = $templatePath . 'header.tpl';
206        $footer_tpl = $templatePath . 'footer.tpl';
207
208        $this->assign('header_tpl', $header_tpl);
209        $this->assign('footer_tpl', $footer_tpl);
210    }
211
212    // デバッグ
213    function debug($var = true) {
214        $this->_smarty->debugging = $var;
215    }
216}
Note: See TracBrowser for help on using the repository browser.