Smarty

更新日 2008-11-14 (金) 11:58:34

Smartyのインストール

ロジックを作成する作業と、HTMLデザインを編集する作業を分担して行うことが可能になるPHPのテンプレートエンジンとしてSmartyがある。

http://www.smarty.net/download.phpからダウンロード。

設定

$ cd /usr/local/src
$ tar zxvf Smarty-2.6.19.tar.gz
$ cd /usr/local/src
$ su
# mv src/Smarty-2.6.19 .
# exit
$ cd

ホームディレクトリに作成するテンプレートを入れるフォルダsmatry_testを作成しディレクトリ構造を作成する(cache/ configs/ templates/ templates_c/を作成)

$ mkdir smatry_test
$ cd smatry_test
$ mkdir cache
$ mkdir configs
$ mkdir templates
$ mkdir templates_c

$ su
# chown -R apache.apache cache
# chown -R apache.apache templates_c

Smartyでwebアクセスする場所をsmarty_testとするため

# mkdir /var/www/html/smarty_test

設定ファイル

Smartyの環境を呼び出すのに必要

/var/www/html/smarty_test/init.php

<?php
// Linux スタイル (大文字の 'S' に注意)

define('SMARTY_DIR', '/usr/local/Smarty-2.6.19/libs/');  <==Smartyの展開ディレクトリ

require_once(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty(); 


// テンプレート、キャシュ等のディレクトリを指定。
$smarty->template_dir = '/home/okada/smatry_test/templates/';
$smarty->compile_dir  = '/home/okada/smatry_test/templates_c/';
$smarty->config_dir   = '/home/okada/smatry_test/configs/';
$smarty->cache_dir    = '/home/okada/smatry_test/cache/';


?>

/var/www/html/smarty_test/test.phpにアクセスページ(testページ)を指定

<?php

require_once("/var/www/html/smarty_test/init.php");

$smarty->assign('name','JE2ISM');
$smarty->assign('name2','JF2LYU');

//** 次の行のコメントをはずすと、デバッギングコンソールを表示します
//$smarty->debugging = true;

$smarty->display('index.tpl');
$smarty->display('index2.tpl');

?>

アクセス

http://(サーバ)/smarty_test/test.php

EC CUBEの複数の店のユーザを単一管理

DBからテーブルをエクスポートしてそれを別テーブルにインポート。新規の場合は同じユニークキーがあるレコードはスキップし、新規レコードをインポートする。修正時は修正レコードupdate_dateフィールのの一番新しいレコードを変更レコードと判断し(このあたりが臭いが)そのレコードをから変更前のpointを抽出したあと上書きし、その後、先の抽出したpointを書き戻す ある時は上書きする。

tbsync_new.php(新規登録)

<?php


        $cmd = "/usr/bin/mysql -B --skip-column-names -u ism 
\--password=ism -D eccube_ism -e 'select * from dtb_customer;' -h 192.168.30.16";
        exec($cmd, $output);
        $fp=fopen("/home/okada/temp/dtb_customer.txt","w");

//      fputs(implode("\n",$output) . "\n",$fp);
        foreach ($output as $a){
// NULL文字は\Nとしてインポートの必要あり
                $a = ereg_replace("NULL", "\N", $a);
                fputs($fp,$a."\n");
        }
        fclose($fp);

//      print_r($output);
//      echo "<BR>\n";
//      $a = implode("\n",$output) . "\n";
//      echo "$a <BR>\n";


        $cmd ="/usr/bin/mysqlimport --local  eccube_ism2 /home/okada/temp/dtb_customer.txt \
-u ism --password=ism --ignore -h 192.168.30.16";
        exec($cmd);
?>

html/entry/complete.phpの最後の行に

require_once("/home/okada/temp/tbsync.php");

を追加

tbsync_c.php(登録削除を含むレコードコピー)

<?php

// データ削除時に他のDBのレコードの書き換え(レコード全体)

// 元DBのデータ最終変更レコードのcustomer_idを抽出
        $cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism -e 'select customer_id from dtb_customer where \
update_date=(select max(update_date) from dtb_customer);' -h 192.168.30.16";
        exec($cmd, $id);


//      echo "ID $id[0] \n";

// 元DBのデータ先のcustomer_idのレコードを抽出してTextファイルに書き込み

        $cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism -e 'select * from dtb_customer where customer_id =" . $id[0] . ";' -h 192.168.30.16";
        exec($cmd, $output);

//      echo "$cmd <br>";
        $fp=fopen("/home/okada/temp/dtb_customer.txt","w");

        foreach ($output as $a){
// NULL文字は\Nとしてインポートの必要あり
                $a = ereg_replace("NULL", "\N", $a);
                fputs($fp,$a."\n");
        }
        fclose($fp);


// 元データの先のTextファイルを変更先DBにインポート

        $cmd ="/usr/bin/mysqlimport --local eccube_ism2 /home/okada/temp/dtb_customer.txt \
-u ism --password=ism -r -h 192.168.30.16";
        exec($cmd);


?>

html/mypage/refusal_complete.php最後の行に

require_once("/home/okada/temp/tbsync_d.php");

を追加

tbsync_r.php(登録変更時のコピーただしpointはコピーされない)

<?php

// データ修正時にpointを変更さいないようにする。

// 元DBのデータ最終変更レコードのcustomer_idを抽出
       $cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism -e 'select customer_id from dtb_customer where \
update_date=(select max(update_date) from dtb_customer);' -h 192.168.30.16";
       exec($cmd, $id);


//      echo "ID $id[0] \n";

// 変更先DBの先のcustomer_idのpointを抽出 (1)
        $cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism2 -e 'select point from dtb_customer where \
customer_id =" . $id[0] .";' -h 192.168.30.16";

        exec($cmd, $point);

//      echo "Henkousaki Point $point[0] <Br>\n";

// 元DBのデータ先のcustomer_idのレコードを抽出してTextファイルに書き込み

        $cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism -e 'select * from dtb_customer where \
customer_id =" . $id[0] . ";' -h 192.168.30.16";
        exec($cmd, $output);

//      echo "$cmd <br>";
        $fp=fopen("/home/okada/temp/dtb_customer.txt","w");

        foreach ($output as $a){
// NULL文字は\Nとしてインポートの必要あり
                $a = ereg_replace("NULL", "\N", $a);
                fputs($fp,$a."\n");
        }
        fclose($fp);


// 元データの先のTextファイルを変更先DBにインポート

        $cmd ="/usr/bin/mysqlimport --local eccube_ism2 /home/okada/temp/dtb_customer.txt \
-u ism --password=ism -r -h 192.168.30.16";
        exec($cmd);


// 変更先DBのポイントを(1)で抽出したデータに書き換えて変更されないようにする。
        $cmd ="/usr/bin/mysql -B --skip-column-names -u ism --password=ism \
-D eccube_ism2 -e 'update dtb_customer set point=" . $point[0] . " \
where customer_id=" . $id[0] . ";' -h 192.168.30.16";
        exec($cmd);

//      echo "$cmd \n";

?>

html/mypage/change_complete.php最後の行に

require_once("/home/okada/temp/tbsync_r.php");

を追加

Topの左上ヘッダーのロゴ画像をクリックしたとき指定URLへ移動

指定URL http://www2.data-map.net/~okada/shop_top.html

# cd shop/data/Smarty/templates/default

# cp header.tpl header2.tpl

header2.tpl

<!--▼HEADER-->
<div id="header">
  <h1>
    <a href="http://www2.data-map.net/~okada/shop_top.html">  <==ここを変更
      <em><!--{$arrSiteInfo.shop_name|escape}-->/<!--{$tpl_title|escape}--></em>
    </a>
  </h1>
  <div id="information">

data/class/SC_View.php

103行あたりに追加

       // ヘッダとフッタを割り当て
       $header_tpl = USER_PATH . USER_PACKAGE_DIR . TEMPLATE_NAME . "/" . "header.tpl";
       $footer_tpl = USER_PATH . USER_PACKAGE_DIR . TEMPLATE_NAME . "/" . "footer.tpl";

       // ユーザー作成のテンプレートが無ければ, 指定テンプレートを割り当て
       if (!$this->_smarty->template_exists($header_tpl)) {
// Edit By JE2ISM
//            if(ereg("/mypage/login.php", $_SERVER['REQUEST_URI'] ) || ereg("/entry/kiyaku.php", $_SERVER['REQUEST_URI'] ) || ereg("/entry/index.php",\
 $_SERVER['REQUEST_URI'] ) || ereg("/entry/complete.php", $_SERVER['REQUEST_URI']) || ereg("/mypage/index.php", $_SERVER['REQUEST_URI'])){

           if(ereg("/html/$", $_SERVER['REQUEST_URI'] || ereg("/html/\?
transactionid=", $_SERVER['REQUEST_URI'] ))){
               $header_tpl = TEMPLATE_DIR . "header2.tpl";
           }else{
               $header_tpl = TEMPLATE_DIR . "header.tpl";
           }
       }
       if (!$this->_smarty->template_exists($footer_tpl)) {
           $footer_tpl = TEMPLATE_DIR . "footer.tpl";
       }

管理画面の顧客管理

変更について

data/class/pages/admin/customer/LC_Page_Admin_Customer_Edit.php

197行ぐらいに追加

                    }
                    //-- 編集登録
                   $objDb->sfEditCustomerData($this->arrForm, $arrRegistColumn);
                    // 更新時にテーブルを同期させる(Edit By JE2ISM)
                    require_once("/home/okada/temp/tbsync_r.php");   <==追加
                }
            }
        }

        //---- ページ表示
        $objView->assignobj($this);
        $objView->display(MAIN_FRAME);

削除について

data/class/pages/admin/customer/LC_Page_Admin_Customer.php

211行あたりに追加

        // 顧客削除
        if ($_POST['mode'] == "delete") {
            $sql = "SELECT status,email FROM dtb_customer WHERE customer_id = ? AND del_flg = 0";
            $result_customer = $objQuery->conn->getAll($sql, array($_POST["edit_customer_id"]));

            if ($result_customer[0]["status"] == 2) {           //本会員削除
                $arrDel = array("del_flg" => 1, "update_date" => "NOW()");
                $objQuery->conn->autoExecute("dtb_customer", $arrDel, "customer_id = " . SC_Utils_Ex::sfQuoteSmart($_POST["edit_customer_id"]) );
            } elseif ($result_customer[0]["status"] == 1) {     //仮会員削除
                $sql = "DELETE FROM dtb_customer WHERE customer_id = ?";
                $objQuery->conn->query($sql, array($_POST["edit_customer_id"]));
            }

            // 削除時にテーブルを同期させる(Edit by JE2ISM)
            require_once("/home/okada/temp/tbsync_c.php");  <==追加


       }

以下のように管理者画面での変更、削除Smartyコンパイル後のファイルを変更してもよいが美しくないし、再コンパイルされると書き直す必要がある

Smartyのコンパイル後を変更

顧客管理で内容変更を行ったときtbsync.phpを実行させるにはSmartyのテンプレート上に記入するとよいのでテンプレートでphp実行できるように

data/module/Smarty/libs/Smarty.class.phpを

    * @var integer
    */
/*    var $php_handling    =  SMARTY_PHP_PASSTHRU;*/
   var $php_handling    =  SMARTY_PHP_ALLOW;  <==変更
    * @var array
    */
   var $security_settings  = array(
                                   /* 'PHP_HANDLING'    => false, */
                                   'PHP_HANDLING'    => true,  <==変更
                                   'IF_FUNCS'        => array('array', 'list',
                                                              'isset', 'empty',
                                                              'count', 'sizeof',
                                                              'in_array', 'is_array',
                                                              'true', 'false', 'null'),
                                   'INCLUDE_ANY'     => false,
                                   /* 'PHP_TAGS'        => false, */
                                   'PHP_TAGS'        => true,  <==変更
                                   'MODIFIER_FUNCS'  => array('count'),
                                   'ALLOW_CONSTANTS'  => false
                                  );

のように変更する

本来はdata/Smarty/templates_c/default/admin/edit_complete.tplに記載すればよいがデリミターが<!--{ }-->とdata/module/Smarty/libs/Smarty.class.phpに定義されいるため、<!--{php}-->がコメントになってしまうようなので、コンパイル済の
data/Smarty/templates_c/default/admin/%%38^380^3807D3B7%%edit_complete.tpl.phpに

</form>
</table>

<!-- 以下を追加 -->

<?php
require_once("/home/okada/temp/tbsync.php");
?>

<!--★★メインコンテンツ★★-->

これはコンパイル澄みなためdata/Smarty/templates_c/default/admin/edit_complete.tplを変更した場合は追加しなおす必要あり。

削除について

data/Smarty/templates_c/default/admin/%%18^185^1850FD4F%%index.tpl.php

   function fnDelete(customer_id) {
       if (confirm('この顧客情報を削除しても宜しいですか?')) {
           document.form1.mode.value = "delete"
           document.form1['edit_customer_id'].value = customer_id;
           document.form1.submit();
           <?php                                          -|
           require_once("/home/okada/temp/tbsync.php");    |<==追加
           ?>                                             -|
           return false;
       }
   }

%%にするとabcのように取り消しになるので、数値参照文字&#x(文字コード);にするとよい


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS