1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2014 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 | /** |
---|
25 | * おすすめ商品を管理するヘルパークラス. |
---|
26 | * |
---|
27 | * @package Helper |
---|
28 | * @author pineray |
---|
29 | * @version $Id:$ |
---|
30 | */ |
---|
31 | class SC_Helper_BestProducts |
---|
32 | { |
---|
33 | /** |
---|
34 | * おすすめ商品の情報を取得. |
---|
35 | * |
---|
36 | * @param integer $best_id おすすめ商品ID |
---|
37 | * @param boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false |
---|
38 | * @return array |
---|
39 | */ |
---|
40 | public function getBestProducts($best_id, $has_deleted = false) |
---|
41 | { |
---|
42 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
43 | $col = '*'; |
---|
44 | $where = 'best_id = ?'; |
---|
45 | if (!$has_deleted) { |
---|
46 | $where .= ' AND del_flg = 0'; |
---|
47 | } |
---|
48 | $arrRet = $objQuery->select($col, 'dtb_best_products', $where, array($best_id)); |
---|
49 | |
---|
50 | return $arrRet[0]; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * おすすめ商品の情報をランクから取得. |
---|
55 | * |
---|
56 | * @param integer $rank ランク |
---|
57 | * @param boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false |
---|
58 | * @return array |
---|
59 | */ |
---|
60 | public function getByRank($rank, $has_deleted = false) |
---|
61 | { |
---|
62 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
63 | $col = '*'; |
---|
64 | $where = 'rank = ?'; |
---|
65 | if (!$has_deleted) { |
---|
66 | $where .= ' AND del_flg = 0'; |
---|
67 | } |
---|
68 | $arrRet = $objQuery->select($col, 'dtb_best_products', $where, array($rank)); |
---|
69 | |
---|
70 | return $arrRet[0]; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * おすすめ商品一覧の取得. |
---|
75 | * |
---|
76 | * @param integer $dispNumber 表示件数 |
---|
77 | * @param integer $pageNumber ページ番号 |
---|
78 | * @param boolean $has_deleted 削除されたおすすめ商品も含む場合 true; 初期値 false |
---|
79 | * @return array |
---|
80 | */ |
---|
81 | public function getList($dispNumber = 0, $pageNumber = 0, $has_deleted = false) |
---|
82 | { |
---|
83 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
84 | $col = '*'; |
---|
85 | $where = ''; |
---|
86 | if (!$has_deleted) { |
---|
87 | $where .= 'del_flg = 0'; |
---|
88 | } |
---|
89 | $table = 'dtb_best_products'; |
---|
90 | $objQuery->setOrder('rank'); |
---|
91 | if ($dispNumber > 0) { |
---|
92 | if ($pageNumber > 0) { |
---|
93 | $objQuery->setLimitOffset($dispNumber, (($pageNumber - 1) * $dispNumber)); |
---|
94 | } else { |
---|
95 | $objQuery->setLimit($dispNumber); |
---|
96 | } |
---|
97 | } |
---|
98 | $arrRet = $objQuery->select($col, $table, $where); |
---|
99 | |
---|
100 | return $arrRet; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * おすすめ商品の登録. |
---|
105 | * |
---|
106 | * @param array $sqlval |
---|
107 | * @return multiple 登録成功:おすすめ商品ID, 失敗:FALSE |
---|
108 | */ |
---|
109 | public function saveBestProducts($sqlval) |
---|
110 | { |
---|
111 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
112 | |
---|
113 | $best_id = $sqlval['best_id']; |
---|
114 | $sqlval['update_date'] = 'CURRENT_TIMESTAMP'; |
---|
115 | // 新規登録 |
---|
116 | if ($best_id == '') { |
---|
117 | // INSERTの実行 |
---|
118 | if (!$sqlval['rank']) { |
---|
119 | $sqlval['rank'] = $objQuery->max('rank', 'dtb_best_products') + 1; |
---|
120 | } |
---|
121 | $sqlval['create_date'] = 'CURRENT_TIMESTAMP'; |
---|
122 | $sqlval['best_id'] = $objQuery->nextVal('dtb_best_products_best_id'); |
---|
123 | $ret = $objQuery->insert('dtb_best_products', $sqlval); |
---|
124 | // 既存編集 |
---|
125 | } else { |
---|
126 | unset($sqlval['creator_id']); |
---|
127 | unset($sqlval['create_date']); |
---|
128 | $where = 'best_id = ?'; |
---|
129 | $ret = $objQuery->update('dtb_best_products', $sqlval, $where, array($best_id)); |
---|
130 | } |
---|
131 | |
---|
132 | return ($ret) ? $sqlval['best_id'] : FALSE; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * おすすめ商品の削除. |
---|
137 | * |
---|
138 | * @param integer $best_id おすすめ商品ID |
---|
139 | * @return void |
---|
140 | */ |
---|
141 | public function deleteBestProducts($best_id) |
---|
142 | { |
---|
143 | $objDb = new SC_Helper_DB_Ex(); |
---|
144 | // ランク付きレコードの削除 |
---|
145 | $objDb->sfDeleteRankRecord('dtb_best_products', 'best_id', $best_id, '', TRUE); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * 商品IDの配列からおすすめ商品を削除. |
---|
150 | * |
---|
151 | * @param array $productIDs 商品ID |
---|
152 | * @return void |
---|
153 | */ |
---|
154 | public function deleteByProductIDs($productIDs) |
---|
155 | { |
---|
156 | $objDb = new SC_Helper_DB_Ex(); |
---|
157 | $arrList = $this->getList(); |
---|
158 | foreach ($arrList as $recommend) { |
---|
159 | if (in_array($recommend['product_id'], $productIDs)) { |
---|
160 | $objDb->sfDeleteRankRecord('dtb_best_products', 'best_id', $recommend['best_id'], '', TRUE); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * おすすめ商品の表示順をひとつ上げる. |
---|
167 | * |
---|
168 | * @param integer $best_id おすすめ商品ID |
---|
169 | * @return void |
---|
170 | */ |
---|
171 | public function rankUp($best_id) |
---|
172 | { |
---|
173 | $arrBestProducts = $this->getBestProducts($best_id); |
---|
174 | $rank = $arrBestProducts['rank']; |
---|
175 | |
---|
176 | if ($rank > 1) { |
---|
177 | // 表示順が一つ上のIDを取得する |
---|
178 | $arrAboveBestProducts = $this->getByRank($rank - 1); |
---|
179 | $above_best_id = $arrAboveBestProducts['best_id']; |
---|
180 | |
---|
181 | if ($above_best_id) { |
---|
182 | // 一つ上のものを一つ下に下げる |
---|
183 | $this->changeRank($above_best_id, $rank); |
---|
184 | } else { |
---|
185 | // 無ければ何もしない。(歯抜けの場合) |
---|
186 | } |
---|
187 | |
---|
188 | // 一つ上に上げる |
---|
189 | $this->changeRank($best_id, $rank - 1); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * おすすめ商品の表示順をひとつ下げる. |
---|
195 | * |
---|
196 | * @param integer $best_id おすすめ商品ID |
---|
197 | * @return void |
---|
198 | */ |
---|
199 | public function rankDown($best_id) |
---|
200 | { |
---|
201 | $arrBestProducts = $this->getBestProducts($best_id); |
---|
202 | $rank = $arrBestProducts['rank']; |
---|
203 | |
---|
204 | if ($rank < RECOMMEND_NUM) { |
---|
205 | // 表示順が一つ下のIDを取得する |
---|
206 | $arrBelowBestProducts = $this->getByRank($rank + 1); |
---|
207 | $below_best_id = $arrBelowBestProducts['best_id']; |
---|
208 | |
---|
209 | if ($below_best_id) { |
---|
210 | // 一つ下のものを一つ上に上げる |
---|
211 | $this->changeRank($below_best_id, $rank); |
---|
212 | } else { |
---|
213 | // 無ければ何もしない。(歯抜けの場合) |
---|
214 | } |
---|
215 | |
---|
216 | // 一つ下に下げる |
---|
217 | $this->changeRank($best_id, $rank + 1); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * 対象IDのrankを指定値に変更する |
---|
223 | * |
---|
224 | * @param integer $best_id 対象ID |
---|
225 | * @param integer $rank 変更したいrank値 |
---|
226 | * @return void |
---|
227 | */ |
---|
228 | public function changeRank($best_id, $rank) |
---|
229 | { |
---|
230 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
231 | |
---|
232 | $table = 'dtb_best_products'; |
---|
233 | $sqlval = array('rank' => $rank); |
---|
234 | $where = 'best_id = ?'; |
---|
235 | $arrWhereVal = array($best_id); |
---|
236 | $objQuery->update($table, $sqlval, $where, $arrWhereVal); |
---|
237 | } |
---|
238 | } |
---|