OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / ec_cart / include / container / ec_cartWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2012 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: ec_cartWidgetContainer.php 5970 2013-04-30 00:45:47Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getContainerPath()           . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() .   '/ec_cart_boxDb.php');
18
19 class ec_cartWidgetContainer extends BaseWidgetContainer
20 {
21         private $db;                    // DBオブジェクト
22         private $langId;                // 現在の言語
23         private $ecObj;                                 // EC共通ライブラリオブジェクト
24         private $titleLength;           // タイトル文字数
25 //      private $productClass;          // 商品クラス
26         protected $productTotal;                // 商品合計額
27         protected $productCount;                // 商品総数
28         protected $productImageWidth;           // 商品画像幅
29         protected $productImageHeight;          // 商品画像高さ
30         const PRICE_OBJ_ID = "eclib";           // 価格計算オブジェクトID
31         const DEFAULT_CART_WIDGET = 'photo_shop';               // カート内容表示用呼び出しウィジェットID
32         const DEFAULT_TITLE = 'カート';                      // デフォルトのウィジェットタイトル名
33         const PRODUCT_CLASS_PHOTO = 'photo';            // 商品クラス
34         const PRODUCT_CLASS_DEFAULT = '';               // 商品クラス
35         const PRODUCT_TYPE_DOWNLOAD = 'download';               // ダウンロード商品タイプ
36         const DEFAULT_TITLE_LENGTH = 10;                // タイトル名長さ
37         const ICON_SIZE = 64;
38         const CSS_FILE = '/style.css';          // CSSファイルのパス
39         const THUMBNAIL_DIR = '/widgets/photo/image';           // 画像格納ディレクトリ
40         const DEFAULT_IMAGE_EXT = 'jpg';                        // 画像ファイルのデフォルト拡張子
41         const DEFAULT_THUMBNAIL_SIZE = 128;             // サムネール画像サイズ
42         const PRODUCT_IMAGE_SMALL = 'small-product';            // 小サイズ商品画像ID
43         const PRODUCT_NAME_FORMAT       = '%s(%s)';             // 商品名表示フォーマット
44         const DEFAULT_PRODUCT_IMAGE_TYPE = 'c.jpg';                     // 商品画像ファイルのタイプ
45         const PRODUCT_IMAGE_DIR = '/widgets/product/image/';                            // 商品画像格納ディレクトリ
46         const CART_WIDGET_TYPE = 'product';                     // カート表示ウィジェットのウィジェットタイプ
47         
48         /**
49          * コンストラクタ
50          */
51         function __construct()
52         {
53                 // 親クラスを呼び出す
54                 parent::__construct();
55                 
56                 $this->db = new ec_cart_boxDb;                  // DBオブジェクト
57                 
58                 // 価格計算用オブジェクト取得
59                 $this->ecObj = $this->gInstance->getObject(self::PRICE_OBJ_ID);
60         }
61         /**
62          * テンプレートファイルを設定
63          *
64          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
65          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
66          *
67          * @param RequestManager $request               HTTPリクエスト処理クラス
68          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
69          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
70          */
71         function _setTemplate($request, &$param)
72         {       
73                 return 'main.tmpl.html';
74         }
75         /**
76          * テンプレートにデータ埋め込む
77          *
78          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
79          *
80          * @param RequestManager $request               HTTPリクエスト処理クラス
81          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
82          * @param                                                               なし
83          */
84         function _assign($request, &$param)
85         {
86                 $this->langId   = $this->gEnv->getCurrentLanguage();            // 表示言語を取得
87                 $this->productTotal = 0;                // 商品合計額
88                 $this->productCount = 0;                // 商品総数
89                 
90                 // 設定値を取得
91                 $this->titleLength = self::DEFAULT_TITLE_LENGTH;        // タイトル文字数
92                 $paramObj = $this->getWidgetParamObj();
93                 if (!empty($paramObj)){
94                         $this->titleLength      = $paramObj->titleLength;
95                 }
96                 // 画像情報を取得
97                 $this->productImageWidth = 0;           // 商品画像幅
98                 $this->productImageHeight = 0;          // 商品画像高さ
99                 $ret = $this->db->getProductImageInfo(self::PRODUCT_IMAGE_SMALL, $row);
100                 if ($ret){
101                         $this->productImageWidth = $row['is_width'];
102                         $this->productImageHeight = $row['is_height'];
103                 }
104                 
105                 // クッキー読み込み、カートIDを取得
106                 $cartId = $request->getCookieValue(M3_COOKIE_CART_ID);
107                 if (!empty($cartId)){   // カートIDが設定されているとき
108                         // カートの商品を取得
109 //                      $this->productClass = self::PRODUCT_CLASS_PHOTO;                // 商品クラス
110                         $this->ecObj->db->getCartItems($cartId, $this->langId, self::PRODUCT_CLASS_PHOTO, array($this, 'cartLoop'));    // フォトギャラリー画像
111 //                      $this->productClass = self::PRODUCT_CLASS_DEFAULT;              // 商品クラス
112                         $this->ecObj->db->getCartItems($cartId, $this->langId, self::PRODUCT_CLASS_DEFAULT, array($this, 'cartLoop'));  // 一般商品
113                 }
114                 if ($this->productCount <= 0) $this->tmpl->setAttribute('show_item', 'visibility', 'hidden');   // 商品一覧を非表示
115                 
116                 // カートリンク先
117                 $cartWidget = $this->gPage->getActiveMainWidgetIdByWidgetType(self::CART_WIDGET_TYPE);          // カート表示用ウィジェット取得
118                 if (empty($cartWidget)) $cartWidget = self::DEFAULT_CART_WIDGET;
119                 $url = $this->createCmdUrlToWidget($cartWidget, 'task=cart');
120                 $this->tmpl->addVar("_widget", "cart_detail_url", $this->getUrl($url, true));
121                 
122                 $this->tmpl->addVar("_widget", "item_count", $this->productCount);                      // 商品総数
123         }
124         /**
125          * CSSファイルをHTMLヘッダ部に設定
126          *
127          * CSSファイルをHTMLのheadタグ内に追加出力する。
128          * _assign()よりも後に実行される。
129          *
130          * @param RequestManager $request               HTTPリクエスト処理クラス
131          * @param object         $param                 任意使用パラメータ。
132          * @return string                                               CSS文字列。出力しない場合は空文字列を設定。
133          */
134         function _addCssFileToHead($request, &$param)
135         {
136                 return $this->getUrl($this->gEnv->getCurrentWidgetCssUrl() . self::CSS_FILE);
137         }
138         /**
139          * ウィジェットのタイトルを設定
140          *
141          * @param RequestManager $request               HTTPリクエスト処理クラス
142          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
143          * @return string                                               ウィジェットのタイトル名
144          */
145         function _setTitle($request, &$param)
146         {
147                 return self::DEFAULT_TITLE;
148         }
149         /**
150          * 取得したデータをテンプレートに設定する
151          *
152          * @param int $index                    行番号(0~)
153          * @param array $fetchedRow             フェッチ取得した行
154          * @param object $param                 未使用
155          * @return bool                                 true=処理続行の場合、false=処理終了の場合
156          */
157         function cartLoop($index, $fetchedRow, $param)
158         {
159                 $productClass   = $fetchedRow['si_product_class'];              // 商品クラス
160                 $productType    = $fetchedRow['si_product_type_id'];            // 商品タイプ
161                 $productId              = $fetchedRow['si_product_id'];                         // 商品ID
162                 $prePrice = $this->convertToDispString($fetchedRow['cu_symbol']);
163                 $postPrice = $this->convertToDispString($fetchedRow['cu_post_symbol']);
164
165                 // 価格作成
166                 $this->ecObj->setCurrencyType($fetchedRow['si_currency_id'], $this->langId);            // 通貨設定
167                 $this->ecObj->getPriceWithoutTax($fetchedRow['si_subtotal'], $dispPrice);                                       // 税込み価格取得
168                                         
169                 // 価格の有効判断
170                 $available = '';
171                 if (!$fetchedRow['si_available']) $available = '(無効)';
172                 
173                 //switch ($this->productClass){
174                 switch ($productClass){
175                         case self::PRODUCT_CLASS_PHOTO;         // フォトギャラリー画像
176                                 $photoId = $fetchedRow['ht_public_id'];         // 公開画像ID
177                                 $title = $fetchedRow['ht_name'];                // サムネール画像タイトル
178                                 if ($productType == self::PRODUCT_TYPE_DOWNLOAD){               // ダウンロード商品の場合
179                                         $productTypeName = $fetchedRow['py_name'];              // 商品タイプ名
180                                 } else {                                                        // フォト関連商品の場合
181                                         $productTypeName = $fetchedRow['hp_name'];              // 商品タイプ名
182                                 }
183
184                                 // 商品名
185                                 $productName = sprintf(self::PRODUCT_NAME_FORMAT, $productTypeName, $title);            // 商品名
186                                 $shortName = makeTruncStr($productName, $this->titleLength);// タイトル
187                                 
188                                 // 商品詳細へのリンク
189                                 $url = $this->gEnv->getDefaultUrl() . '?' . M3_REQUEST_PARAM_PHOTO_ID . '=' . $photoId;
190                                 $urlLink = $this->convertUrlToHtmlEntity($this->getUrl($url, true));
191                 
192                                 // 画像URL
193                                 $imageUrl = $this->gEnv->getResourceUrl() . self::THUMBNAIL_DIR . '/' . $photoId . '_' . self::DEFAULT_THUMBNAIL_SIZE . '.' . self::DEFAULT_IMAGE_EXT;
194                                 $imageWidth = self::ICON_SIZE;
195                                 $imageHeight = self::ICON_SIZE;
196                                 break;
197                         case self::PRODUCT_CLASS_DEFAULT;               // 一般商品
198                                 // 商品名
199                                 $title = $fetchedRow['pt_name'];                // 名前
200                                 $shortName = makeTruncStr($fetchedRow['pt_name'], $this->titleLength);// タイトル
201
202                                 // 商品詳細へのリンク
203                                 $url = $this->gEnv->getDefaultUrl() . '?' . M3_REQUEST_PARAM_PRODUCT_ID . '=' . $fetchedRow['si_product_id'];
204                                 $urlLink = $this->convertUrlToHtmlEntity($this->getUrl($url, true));
205                                 
206                                 // 画像URL
207                                 $ret = $this->db->getImageInfoByProductId($fetchedRow['si_product_id'], $this->langId, $row);
208                                 if ($ret){
209                                         $imageArray = $this->getImage($row, self::PRODUCT_IMAGE_SMALL);// 商品画像小
210                                         $imageUrl = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getRootUrl(), $imageArray['im_url']);
211                                         $imagePath = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->gEnv->getSystemRootPath(), $imageArray['im_url']);
212                                         if (!file_exists($imagePath)){
213                                                 $imageUrl = $this->getProductImageUrl('0_' . $this->productImageWidth . 'x' . $this->productImageHeight . self::DEFAULT_PRODUCT_IMAGE_TYPE);
214                                         }
215                                 }
216                                 $imageWidth = $this->productImageWidth;
217                                 $imageHeight = $this->productImageHeight;
218                                 break;
219                 }
220                 // 商品合計価格、総数
221                 $quantity = $fetchedRow['si_quantity'];
222                 $subtotal = $fetchedRow['si_subtotal'];
223                 $this->productTotal += $subtotal;                                       // 合計価格
224                 $this->productCount += $quantity;                                       // 商品総数
225                         
226                 // タグを作成
227                 $nameLink = '<a href="' . $urlLink . '">' . $this->convertToDispString($shortName) . '</a>';
228                 $itemStr = '<div class="photo_cart_image"><a href="' . $urlLink . '"><img src="' . $this->getUrl($imageUrl) . '" width="' . $imageWidth . '" height="' . $imageHeight . 
229                                                                 '" title="' . $this->convertToDispString($title) . '" alt="' . $this->convertToDispString($title) . '" /></a></div>';
230                 $itemStr .= '<div class="photo_cart_info"><div>' . $nameLink . '</div><div>数量' . $fetchedRow['si_quantity'] . '&nbsp;' . $prePrice . $dispPrice . $postPrice . $available . '</div></div>';
231                 $itemStr .= '<div style="clear:both;"></div>';
232                 
233                 $row = array(
234                         'cart_item' => $itemStr
235                 );
236                 $this->tmpl->addVars('itemlist', $row);
237                 $this->tmpl->parseTemplate('itemlist', 'a');
238                 return true;
239         }
240         /**
241          * 画像取得
242          *
243          * @param array         $srcRows                        画像リスト
244          * @param string        $imageType                      画像タイプ
245          * @return array                                                取得した行
246          */
247         function getImage($srcRows, $sizeType)
248         {
249                 for ($i = 0; $i < count($srcRows); $i++){
250                         if ($srcRows[$i]['im_size_id'] == $sizeType){
251                                 return $srcRows[$i];
252                         }
253                 }
254                 return array();
255         }
256         /**
257          * 商品画像URL取得
258          *
259          * @param string $filename              ファイル名
260          * @return string                               URL
261          */
262         function getProductImageUrl($filename)
263         {
264                 return $this->gEnv->getResourceUrl() . self::PRODUCT_IMAGE_DIR . $filename;
265         }
266 }
267 ?>