Source for file LC_Page_Admin_Design_Template.php

Documentation is available at LC_Page_Admin_Design_Template.php

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

Documentation generated on Tue, 28 Apr 2009 18:11:10 +0900 by phpDocumentor 1.4.2