2010/01/24

oscommerce smartyにコントリビューションの移植

oscommerceをsmartyでデザインとロジックに分離した
alterというバージョンが存在する。これにログインボックスをつけてみる。

まずは簡単なコントリビューションからいってみよう。
本家にいってこれをDL (Login box 1.0)
http://www.oscommerce.com/community/contributions,645/page,6

smartyへ登録するために必要なのは基本的に以下の2行のみ。
$this->box_smarty_obj->assign("login_box_hedding_title",BOX_LOGINBOX_HEADING);
$this->box_smarty_obj->assign("login_box_contents",$loginboxcontent);

この2行でほとんどが対応可能。1行目でBOXのヘッダ部分を登録し、
2行目でBOXのコンテンツ部分の登録になります。

loginbox.phpでは、中で直接BOXを表示するためにHTMLが記載されています。
$loginboxcontentという変数に表示内容が設定されています。infoBoxHeadingやinfoBoxを生成していますが、これはBOXのヘッダ部分とコンテンツ部分の枠を作成する関数なので、smartyの場合は不要です。なぜなら、これらはtemplateに記載するためなので、
$loginboxcontentをsmartyに登録するだけでいいことになります。
どこを修正したかわかりやすいように不要な部分はコメントアウトにしました。

<?php
*osCommerce, Open Source E-Commerce Solutions
http:www.oscommerce.com Copyright (c) 2002 osCommerceReleased under the GNU General Public LicenseIMPORTANT NOTE:This script is not part of the official osC distribution
but an add-on contributed to the osC community. Please
read the README and INSTALL documents that are provided
with this file for further information and installation notes.loginbox.php - Version 1.0
This puts a login request in a box with a login button.
If already logged in, will not show anything.
*
?>
<!-- loginbox -->
<?php
if (!tep_session_is_registered('customer_id')) {
?>
<!--不要なので削除
<tr>
<td>
-->

<?php
以下も不要なので削除
$info_box_contents = array();
$info_box_contents[] = array('align' => 'left',
'text' => BOX_LOGINBOX_HEADING
);
new infoBoxHeading($info_box_contents, false, false);
$loginboxcontent = "
<table border="0" width="100%" cellspacing="0"cellpadding="0">
<form name="login" method="post" action="" . tep_href_link(FILENAME_LOGIN, 'action=process', 'SSL') . "">
<tr>
<td align="left" class="boxText">
" . BOX_LOGINBOX_EMAIL . "
<td>
<tr>
<tr>
<td align="left" class="boxText">
<input type="text" name="email_address" maxlength="96" size="20" value="">
<td>
<tr>
<tr>
<td align="left" class="boxText">
" . BOX_LOGINBOX_PASSWORD . "
<td>
<tr>
<tr>
<td align="left" class="boxText">
<input type="password" name="password" maxlength="40" size="20" value="">
<td>
<tr>
<tr>
<td align="center" class="boxText">
" . tep_image_submit('button_login.gif', IMAGE_BUTTON_LOGIN) . "
<td>
<tr>
<form>
<table>
"; 以下も不要なので削除
$info_box_contents = array();}
$info_box_contents[] = array('align' => 'center',
'text' => $loginboxcontent
);
new infoBox($info_box_contents); smartyに表示内容を登録する処理を追加
$this->box_smarty_obj->assign ("login_box_hedding_title",BOX_LOGINBOX_HEADING);
$this->box_smarty_obj->assign("login_box_contents",$loginboxcontent);?>
<!-- 以下は不要なので削除
<td>
<tr>
-->

<?php
}
else
{
If you want to display anything when the user IS logged in, put it
in here... Possibly a "You are logged in as :" box or something.
特に記載する必要なし。
}
?>
<!-- loginbox_eof -->

ほとんどコメントアウトし表示するためのデータをsmartyのオブジェクトにassignするだけです。

loginbox.phpを呼び出す
require(DIR_WS_BOXES . 'loginbox.php'); // 追加

templateに追加するには
{if $login_box_contents}
<!-- loginbox_info //-->
{$login_box_hedding_title}
{$login_box_contents}
<!-- loginbox_info_eof //-->
{/if}

これでよい(htmlで装飾するのもアリ)