Index: branches/beta/data/class/SC_View.php
===================================================================
--- branches/beta/data/class/SC_View.php	(revision 15690)
+++ branches/beta/data/class/SC_View.php	(revision 15825)
@@ -12,10 +12,41 @@
 class SC_View {
 
+    /** Smarty¥¤¥ó¥¹¥¿¥ó¥¹ */
     var $_smarty;
-    var $objSiteInfo; // ¥µ¥¤¥È¾ðÊó
-
-    // ¥³¥ó¥¹¥È¥é¥¯¥¿
-    function SC_View($siteinfo = true) {
-        global $SC_VIEW_PHP_DIR;
+    /** SC_SiteInfo¤Î¥¤¥ó¥¹¥¿¥ó¥¹ */
+    var $objSiteInfo;
+    /** ¥Ú¡¼¥¸½ÐÎÏ¥Ù¥ó¥Á¥Þ¡¼¥¯¤Î³«»Ï»þ´Ö */
+    var $time_start;
+    /** »ÈÍÑ¤·¤Æ¤¤¤ë¥Æ¥ó¥×¥ì¡¼¥È¥Ñ¥Ã¥±¡¼¥¸Ì¾ */
+    var $tplName;
+
+    /**
+     * ¥³¥ó¥¹¥È¥é¥¯¥¿
+     *
+     * @param boolean $assignSiteInfo ¥µ¥¤¥È¾ðÊó¤òassign¤¹¤ë¤«¤É¤¦¤«
+     */
+    function SC_View($assignSiteInfo = true) {
+        // ¥Ú¡¼¥¸½ÐÎÏ¤Î¥Ù¥ó¥Á¥Þ¡¼¥¯¥¹¥¿¡¼¥È
+        $this->setStartTime();
+
+        // Smarty½é´ü²½
+        $this->initSmarty();
+
+        $this->tplName = $this->getTemplateName();
+        $this->assignDefaultVars();
+
+        // ¥µ¥¤¥È¾ðÊó¤òassign¤¹¤ë
+        if($assignSiteInfo) $this->assignSiteInfo();
+    }
+
+    /**
+     * Smarty¤Î½é´ü²½.
+     * ½¤¾þ»ì¤ä¥Æ¥ó¥×¥ì¡¼¥È´Ø¿ô¤ÎÄêµÁ¤ò¹Ô¤¦.
+     *
+     * @param void
+     * @return void
+     */
+    function initSmarty() {
+        $SC_VIEW_PHP_DIR = realpath(dirname(__FILE__));
 
         $this->_smarty = new Smarty;
@@ -37,5 +68,5 @@
         $this->_smarty->register_function("sfRmDupSlash", "sfRmDupSlash");
         $this->_smarty->register_function("sfCutString", "sfCutString");
-        $this->_smarty->plugins_dir=array("plugins", $SC_VIEW_PHP_DIR . "/../smarty_extends");
+        $this->_smarty->plugins_dir = array("plugins", $SC_VIEW_PHP_DIR . "/../smarty_extends");
         $this->_smarty->register_function("sf_mb_convert_encoding","sf_mb_convert_encoding");
         $this->_smarty->register_function("sf_mktime","sf_mktime");
@@ -46,42 +77,107 @@
         $this->_smarty->register_function("sfIsHTTPS","sfIsHTTPS");
         $this->_smarty->default_modifiers = array('script_escape');
-
-        if(ADMIN_MODE == '1') {
-            $this->time_start = time();
-        }
-
-        // ¥µ¥¤¥È¾ðÊó¤ò¼èÆÀ¤¹¤ë
-        if($siteinfo) {
-            if(!defined('LOAD_SITEINFO')) {
-                $this->objSiteInfo = new SC_SiteInfo();
-                $arrInfo['arrSiteInfo'] = $this->objSiteInfo->data;
-
-                // ÅÔÆ»ÉÜ¸©Ì¾¤òÊÑ´¹
-                global $arrPref;
-                $arrInfo['arrSiteInfo']['pref'] = $arrPref[$arrInfo['arrSiteInfo']['pref']];
-
-                 // ¥µ¥¤¥È¾ðÊó¤ò³ä¤êÅö¤Æ¤ë
-                foreach ($arrInfo as $key => $value){
-                    $this->_smarty->assign($key, $value);
-                }
-
-                define('LOAD_SITEINFO', 1);
-            }
-        }
-    }
-
-    // ¥Æ¥ó¥×¥ì¡¼¥È¤ËÃÍ¤ò³ä¤êÅö¤Æ¤ë
-    function assign($val1, $val2) {
-        $this->_smarty->assign($val1, $val2);
-    }
-
-    // ¥Æ¥ó¥×¥ì¡¼¥È¤Î½èÍý·ë²Ì¤ò¼èÆÀ
+        $this->_smarty->force_compile = true;
+    }
+
+    /**
+     * ¥Ú¡¼¥¸½ÐÎÏ¥Ù¥ó¥Á¥Þ¡¼¥¯¤Î³«»Ï»þ´Ö¤ò¥»¥Ã¥È¤¹¤ë.
+     *
+     * @param void
+     * @return void
+     */
+    function setStartTime() {
+        // TODO PEAR::BenchMark»È¤¦¡©
+        $this->time_start = time();
+    }
+
+    /**
+     * ¥Ç¥Õ¥©¥ë¥È¤Î¥Æ¥ó¥×¥ì¡¼¥ÈÊÑ¿ô¤òassign¤¹¤ë.
+     */
+    function assignDefaultVars() {
+        $arrDefaultParams = array(
+            'URL_DIR' => URL_DIR,
+            // FIXME tplName¤¬null¤Î¾ì¹ç¤Î½èÍý
+            'TPL_PKG_URL' => URL_DIR . USER_DIR . TPL_PKG_DIR . $this->tplName . '/'
+        );
+        $this->assignArray($arrDefaultParams);
+    }
+
+    /**
+     * ¥µ¥¤¥È¾ðÊó¤òassign¤¹¤ë.
+     *
+     * @param void
+     * @return void
+     */
+    function assignSiteInfo() {
+        if (!defined('LOAD_SITEINFO')) {
+            $this->objSiteInfo = new SC_SiteInfo();
+            $arrInfo['arrSiteInfo'] = $this->objSiteInfo->data;
+
+            // ÅÔÆ»ÉÜ¸©Ì¾¤òÊÑ´¹
+            global $arrPref;
+            $arrInfo['arrSiteInfo']['pref'] = $arrPref[$arrInfo['arrSiteInfo']['pref']];
+
+            // ¥µ¥¤¥È¾ðÊó¤ò³ä¤êÅö¤Æ¤ë
+            $this->assignArray($arrInfo);
+
+            define('LOAD_SITEINFO', 1);
+        }
+    }
+
+    /**
+     * ¥Æ¥ó¥×¥ì¡¼¥È¤ËÃÍ¤ò³ä¤êÅö¤Æ¤ë
+     *
+     * @param string $key
+     * @param mixed $value
+     * @return void
+     */
+    function assign($key, $value) {
+        $this->_smarty->assign($key, $value);
+    }
+
+    /**
+     * ¥Æ¥ó¥×¥ì¡¼¥È¤Î½èÍý·ë²Ì¤ò¼èÆÀ
+     *
+     * @param string $template tpl¥Õ¥¡¥¤¥ë¤Î¥Ñ¥¹
+     * @return string ½ÐÎÏ·ë²Ì
+     */
     function fetch($template) {
         return $this->_smarty->fetch($template);
     }
 
-    // ¥Æ¥ó¥×¥ì¡¼¥È¤Î½èÍý·ë²Ì¤òÉ½¼¨
-    function display($template, $no_error = false) {
-        if(!$no_error) {
+    /**
+     * ¥Æ¥ó¥×¥ì¡¼¥È¤Î½èÍý·ë²Ì¤òÉ½¼¨.
+     *
+     * @param string $template tpl¥Õ¥¡¥¤¥ë¤Î¥Ñ¥¹
+     * @return void
+     */
+    function display($template, $display = false) {
+        // ¥Æ¥ó¥×¥ì¡¼¥ÈÀÚ¤êÂØ¤¨½èÍý
+        $this->initDisplay($template);
+
+        // ¥°¥í¡¼¥Ð¥ë¥¨¥é¡¼¤ÎÉ½¼¨
+        $this->displayGlobalError($display);
+
+        // ²èÌÌÉ½¼¨
+        $this->_smarty->display($template);
+
+        // ¥Ù¥ó¥Á¥Þ¡¼¥¯·ë²Ì¤ÎÉ½¼¨
+        $this->displayBenchMark();
+    }
+
+    /**
+     *  ¥Æ¥ó¥×¥ì¡¼¥ÈÀÚ¤êÂØ¤¨½èÍý¤ò¹Ô¤¦.
+     *  ¼ÂÁõ¤Ï»Ò¥¯¥é¥¹¤Ç¹Ô¤¦.
+     */
+    function initDisplay($template) {}
+
+    /**
+     * ¥°¥í¡¼¥Ð¥ë¥¨¥é¡¼¤òÉ½¼¨¤¹¤ë.
+     *
+     * @param boolean $display
+     * @return void
+     */
+    function displayGlobalError($display = false) {
+        if (!$display) {
             global $GLOBAL_ERR;
             if(!defined('OUTPUT_ERR')) {
@@ -90,7 +186,15 @@
             }
         }
-
-        $this->_smarty->display($template);
-        if(ADMIN_MODE == '1') {
+    }
+
+    /**
+     * ¥Ú¡¼¥¸½ÐÎÏ¥Ù¥ó¥Á¥Þ¡¼¥¯¤Î·ë²Ì¤òÉ½¼¨¤¹¤ë.
+     * ADMIN_MODE¤¬true¤Î¤È¤­¤Î¤ßÍ­¸ú.
+     *
+     * @param void
+     * @return void
+     */
+    function displayBenchMark() {
+        if (ADMIN_MODE) {
             $time_end = time();
             $time = $time_end - $this->time_start;
@@ -99,21 +203,44 @@
     }
 
-      // ¥ª¥Ö¥¸¥§¥¯¥ÈÆâ¤ÎÊÑ¿ô¤ò¤¹¤Ù¤Æ³ä¤êÅö¤Æ¤ë¡£
-      function assignobj($obj) {
-        $data = get_object_vars($obj);
-
-        foreach ($data as $key => $value){
-            $this->_smarty->assign($key, $value);
-        }
-      }
-
-      // Ï¢ÁÛÇÛÎóÆâ¤ÎÊÑ¿ô¤ò¤¹¤Ù¤Æ³ä¤êÅö¤Æ¤ë¡£
-      function assignarray($array) {
-          foreach ($array as $key => $val) {
-              $this->_smarty->assign($key, $val);
-          }
-      }
-
-    // ¥Ç¥Ð¥Ã¥°
+    /**
+     * ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥á¥ó¥ÐÊÑ¿ô¤òassign¤¹¤ë.
+     *
+     * @param object $obj LC_Page¤Î¥¤¥ó¥¹¥¿¥ó¥¹
+     * @return void
+     */
+    function assignObj($obj) {
+        $this->assignArray(get_object_vars($obj));
+    }
+
+    /**
+     * Ï¢ÁÛÇÛÎó¤òassign¤¹¤ë.
+     *
+     * @param array $arrAssignVars assign¤¹¤ëÏ¢ÁÛÇÛÎó
+     * @return void
+     */
+    function assignArray($arrAssignVars) {
+        foreach ($arrAssignVars as $key => $val) {
+            $this->assign($key, $val);
+        }
+    }
+
+    /**
+     * »ÈÍÑ¤·¤Æ¤¤¤ë¥Æ¥ó¥×¥ì¡¼¥È¥Ñ¥Ã¥±¡¼¥¸Ì¾¤ò¼èÆÀ¤¹¤ë
+     */
+    function getTemplateName() {
+        $objQuery = new SC_Query();
+        $arrRet = $objQuery->select('top_tpl', 'dtb_baseinfo');
+
+        if (isset($arrRet[0]['top_tpl'])) {
+            return $arrRet[0]['top_tpl'];
+        }
+        return null;
+    }
+    /**
+     * Smarty¤Î¥Ç¥Ð¥Ã¥°½ÐÎÏ¤òÍ­¸ú¤Ë¤¹¤ë.
+     *
+     * @param void
+     * @return void
+     */
     function debug($var = true){
         $this->_smarty->debugging = $var;
@@ -127,4 +254,5 @@
         $this->_smarty->compile_dir = COMPILE_ADMIN_DIR;
     }
+
 }
 
@@ -146,6 +274,28 @@
         }
     }
-}
-
+
+    function initDisplay($template) {
+        // ¥Æ¥ó¥×¥ì¡¼¥È¤ò»ÈÍÑ¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ïreturn
+        if (empty($this->tplName)) return;
+
+        /**
+         * tpl_mainpage¤¬ÁêÂÐ¥Ñ¥¹¤Ç¤¢¤ì¤Ð
+         * ¥æ¡¼¥¶¡¼¥Æ¥ó¥×¥ì¡¼¥È¤Ø¤ÎÀäÂÐ¥Ñ¥¹¤ËÊÑ¹¹¤·
+         * ¤½¤Î¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¤Ð¤¢¤¤¤Ïtpl_meinpage¤òºÆÅÙassign¤·¾å½ñ¤­¤¹¤ë
+         */
+        $tpl_mainpage  = $this->_smarty->get_template_vars('tpl_mainpage');
+        $template_dir = TPL_PKG_PATH . $this->tplName . '/templates/';
+
+        $win = "^[a-zA-Z]{1}:";
+        $other = "^\/";
+        $pattern = "/($win)|($other)/";
+        if (!preg_match($pattern, $tpl_mainpage)) {
+            $tpl_pkg_mainpage = $template_dir . $tpl_mainpage;
+            if (file_exists($tpl_pkg_mainpage)) {
+                $this->assign('tpl_mainpage', $tpl_pkg_mainpage);
+            }
+        }
+    }
+}
 class SC_UserView extends SC_SiteView{
     function SC_UserView($template_dir, $compile_dir = COMPILE_DIR) {
@@ -154,4 +304,8 @@
         $this->_smarty->compile_dir = $compile_dir;
     }
+
+    function display($template) {
+        $this->_smarty->display($template);
+    }
 }
 
@@ -162,4 +316,5 @@
         $this->_smarty->compile_dir = $compile_dir;
     }
+    function getTemplateName() {}
 }
 
@@ -171,3 +326,4 @@
     }
 }
+
 ?>
