7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/ec_cart_boxDb.php');
19 class ec_cartWidgetContainer extends BaseWidgetContainer
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; // タイトル名長さ
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'; // カート表示ウィジェットのウィジェットタイプ
51 function __construct()
54 parent::__construct();
56 $this->db = new ec_cart_boxDb; // DBオブジェクト
59 $this->ecObj = $this->gInstance->getObject(self::PRICE_OBJ_ID);
64 * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
65 * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
67 * @param RequestManager $request HTTPリクエスト処理クラス
68 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
69 * @return string テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
71 function _setTemplate($request, &$param)
73 return 'main.tmpl.html';
78 * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
80 * @param RequestManager $request HTTPリクエスト処理クラス
81 * @param object $param 任意使用パラメータ。_setTemplate()と共有。
84 function _assign($request, &$param)
86 $this->langId = $this->gEnv->getCurrentLanguage(); // 表示言語を取得
87 $this->productTotal = 0; // 商品合計額
88 $this->productCount = 0; // 商品総数
91 $this->titleLength = self::DEFAULT_TITLE_LENGTH; // タイトル文字数
92 $paramObj = $this->getWidgetParamObj();
93 if (!empty($paramObj)){
94 $this->titleLength = $paramObj->titleLength;
97 $this->productImageWidth = 0; // 商品画像幅
98 $this->productImageHeight = 0; // 商品画像高さ
99 $ret = $this->db->getProductImageInfo(self::PRODUCT_IMAGE_SMALL, $row);
101 $this->productImageWidth = $row['is_width'];
102 $this->productImageHeight = $row['is_height'];
106 $cartId = $request->getCookieValue(M3_COOKIE_CART_ID);
107 if (!empty($cartId)){ // カートIDが設定されているとき
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')); // 一般商品
114 if ($this->productCount <= 0) $this->tmpl->setAttribute('show_item', 'visibility', 'hidden'); // 商品一覧を非表示
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));
122 $this->tmpl->addVar("_widget", "item_count", $this->productCount); // 商品総数
125 * CSSファイルをHTMLヘッダ部に設定
127 * CSSファイルをHTMLのheadタグ内に追加出力する。
128 * _assign()よりも後に実行される。
130 * @param RequestManager $request HTTPリクエスト処理クラス
131 * @param object $param 任意使用パラメータ。
132 * @return string CSS文字列。出力しない場合は空文字列を設定。
134 function _addCssFileToHead($request, &$param)
136 return $this->getUrl($this->gEnv->getCurrentWidgetCssUrl() . self::CSS_FILE);
141 * @param RequestManager $request HTTPリクエスト処理クラス
142 * @param object $param 任意使用パラメータ。そのまま_assign()に渡る
143 * @return string ウィジェットのタイトル名
145 function _setTitle($request, &$param)
147 return self::DEFAULT_TITLE;
150 * 取得したデータをテンプレートに設定する
152 * @param int $index 行番号(0~)
153 * @param array $fetchedRow フェッチ取得した行
154 * @param object $param 未使用
155 * @return bool true=処理続行の場合、false=処理終了の場合
157 function cartLoop($index, $fetchedRow, $param)
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']);
166 $this->ecObj->setCurrencyType($fetchedRow['si_currency_id'], $this->langId); // 通貨設定
167 $this->ecObj->getPriceWithoutTax($fetchedRow['si_subtotal'], $dispPrice); // 税込み価格取得
171 if (!$fetchedRow['si_available']) $available = '(無効)';
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']; // 商品タイプ名
185 $productName = sprintf(self::PRODUCT_NAME_FORMAT, $productTypeName, $title); // 商品名
186 $shortName = makeTruncStr($productName, $this->titleLength);// タイトル
189 $url = $this->gEnv->getDefaultUrl() . '?' . M3_REQUEST_PARAM_PHOTO_ID . '=' . $photoId;
190 $urlLink = $this->convertUrlToHtmlEntity($this->getUrl($url, true));
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;
197 case self::PRODUCT_CLASS_DEFAULT; // 一般商品
199 $title = $fetchedRow['pt_name']; // 名前
200 $shortName = makeTruncStr($fetchedRow['pt_name'], $this->titleLength);// タイトル
203 $url = $this->gEnv->getDefaultUrl() . '?' . M3_REQUEST_PARAM_PRODUCT_ID . '=' . $fetchedRow['si_product_id'];
204 $urlLink = $this->convertUrlToHtmlEntity($this->getUrl($url, true));
207 $ret = $this->db->getImageInfoByProductId($fetchedRow['si_product_id'], $this->langId, $row);
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);
216 $imageWidth = $this->productImageWidth;
217 $imageHeight = $this->productImageHeight;
221 $quantity = $fetchedRow['si_quantity'];
222 $subtotal = $fetchedRow['si_subtotal'];
223 $this->productTotal += $subtotal; // 合計価格
224 $this->productCount += $quantity; // 商品総数
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'] . ' ' . $prePrice . $dispPrice . $postPrice . $available . '</div></div>';
231 $itemStr .= '<div style="clear:both;"></div>';
234 'cart_item' => $itemStr
236 $this->tmpl->addVars('itemlist', $row);
237 $this->tmpl->parseTemplate('itemlist', 'a');
243 * @param array $srcRows 画像リスト
244 * @param string $imageType 画像タイプ
245 * @return array 取得した行
247 function getImage($srcRows, $sizeType)
249 for ($i = 0; $i < count($srcRows); $i++){
250 if ($srcRows[$i]['im_size_id'] == $sizeType){
259 * @param string $filename ファイル名
262 function getProductImageUrl($filename)
264 return $this->gEnv->getResourceUrl() . self::PRODUCT_IMAGE_DIR . $filename;