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_Review |
---|
32 | { |
---|
33 | /** |
---|
34 | * レビュー情報のDB取得 |
---|
35 | * |
---|
36 | * @param int $review_id レビューID |
---|
37 | * @return array レビュー情報 |
---|
38 | */ |
---|
39 | public function get($review_id) |
---|
40 | { |
---|
41 | $query = array( |
---|
42 | 'review_id' => $review_id |
---|
43 | ); |
---|
44 | $arrReview = $this->find(array('query' => $query)); |
---|
45 | |
---|
46 | return $arrReview[0]; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * 商品に紐付いたレビューの一覧を取得 |
---|
51 | * |
---|
52 | * @param integer $product_id |
---|
53 | * @return array|null |
---|
54 | */ |
---|
55 | public function getListByProductId($product_id) |
---|
56 | { |
---|
57 | $query = array( |
---|
58 | 'product_id' => $product_id |
---|
59 | ); |
---|
60 | $arrReview = $this->find(array('query' => $query)); |
---|
61 | |
---|
62 | return $arrReview; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * レビュー情報を登録する. |
---|
67 | * |
---|
68 | * @param $data |
---|
69 | * @return bool |
---|
70 | */ |
---|
71 | public function save($data) |
---|
72 | { |
---|
73 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
74 | |
---|
75 | $review_id = $data['review_id']; |
---|
76 | $data['update_date'] = 'CURRENT_TIMESTAMP'; |
---|
77 | // 新規登録 |
---|
78 | if ($review_id == '') { |
---|
79 | // INSERTの実行 |
---|
80 | $data['creator_id'] = '0'; |
---|
81 | $data['create_date'] = 'CURRENT_TIMESTAMP'; |
---|
82 | $data['review_id'] = $objQuery->nextVal('dtb_review_review_id'); |
---|
83 | $ret = $objQuery->insert('dtb_review', $data); |
---|
84 | // 既存編集 |
---|
85 | } else { |
---|
86 | unset($data['creator_id']); |
---|
87 | unset($data['create_date']); |
---|
88 | $where = 'review_id = ?'; |
---|
89 | $ret = $objQuery->update('dtb_review', $data, $where, array($review_id)); |
---|
90 | } |
---|
91 | |
---|
92 | return ($ret) ? $data['review_id'] : FALSE; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * レビューを削除する(論理削除). |
---|
97 | * |
---|
98 | * @param $review_id |
---|
99 | */ |
---|
100 | public function delete($review_id) |
---|
101 | { |
---|
102 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
103 | $data['del_flg'] = 1; |
---|
104 | $objQuery->update('dtb_review', $data, 'review_id = ?', array($review_id)); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * 条件に合うレビュー情報の一覧を取得. |
---|
109 | * |
---|
110 | * @param array $params |
---|
111 | * @return array|null |
---|
112 | */ |
---|
113 | public function find($params = array()) |
---|
114 | { |
---|
115 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
116 | |
---|
117 | // 検索条件を作成 |
---|
118 | $query = (isset($params['query'])) ? $params['query'] : array(); |
---|
119 | list($where, $values) = $this->makeWhere($query); |
---|
120 | |
---|
121 | // 表示件数 |
---|
122 | if (isset($params['limit'])) { |
---|
123 | if (isset($params['offset'])) { |
---|
124 | $objQuery->setLimitOffset($params['limit'], $params['offset']); |
---|
125 | } else { |
---|
126 | $objQuery->setLimit($params['limit']); |
---|
127 | } |
---|
128 | } elseif (isset($params['offset'])) { |
---|
129 | $objQuery->setOffset($params['offset']); |
---|
130 | } |
---|
131 | |
---|
132 | // 表示順序 |
---|
133 | $order = (isset($params['order'])) ? $params['order'] : 'A.create_date DESC'; |
---|
134 | $objQuery->setOrder($order); |
---|
135 | |
---|
136 | //検索結果の取得 |
---|
137 | //レビュー情報のカラムの取得 |
---|
138 | $col = 'review_id, A.product_id, reviewer_name, sex, recommend_level, '; |
---|
139 | $col .= 'reviewer_url, title, comment, A.status, A.create_date, A.update_date, name'; |
---|
140 | $from = 'dtb_review AS A LEFT JOIN dtb_products AS B ON A.product_id = B.product_id '; |
---|
141 | $arrReview = $objQuery->select($col, $from, $where, $values); |
---|
142 | |
---|
143 | return $arrReview; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * 条件に合うレビュー情報の件数を取得. |
---|
148 | * |
---|
149 | * @param array $query |
---|
150 | * @return int |
---|
151 | */ |
---|
152 | public function count($query = array()) |
---|
153 | { |
---|
154 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
155 | // 検索条件を作成 |
---|
156 | list($where, $values) = $this->makeWhere($query); |
---|
157 | $from = 'dtb_review AS A LEFT JOIN dtb_products AS B ON A.product_id = B.product_id '; |
---|
158 | return $objQuery->count($from, $where, $values); |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * WHERE文の作成 |
---|
163 | * |
---|
164 | * @param array $query フォームデータ |
---|
165 | * @return array WHERE文、判定値 |
---|
166 | */ |
---|
167 | private function makeWhere($query = array()) |
---|
168 | { |
---|
169 | //削除されていない商品を検索 |
---|
170 | $where = 'A.del_flg = 0 AND B.del_flg = 0'; |
---|
171 | $values = array(); |
---|
172 | |
---|
173 | foreach ($query AS $key => $val) { |
---|
174 | if (empty($val)) continue; |
---|
175 | |
---|
176 | switch ($key) { |
---|
177 | case 'product_id': |
---|
178 | if (is_array($val)) { |
---|
179 | $where.= ' AND A.product_id IN (' . SC_Utils_Ex::sfGetCommaList($val) . ')'; |
---|
180 | } else { |
---|
181 | $where.= ' AND A.product_id = ?'; |
---|
182 | $values[] = $val; |
---|
183 | } |
---|
184 | break; |
---|
185 | |
---|
186 | case 'review_id': |
---|
187 | if (is_array($val)) { |
---|
188 | $where.= ' AND review_id IN (' . SC_Utils_Ex::sfGetCommaList($val) . ')'; |
---|
189 | } else { |
---|
190 | $where.= ' AND review_id = ?'; |
---|
191 | $values[] = $val; |
---|
192 | } |
---|
193 | break; |
---|
194 | |
---|
195 | case 'reviewer_name': |
---|
196 | $val = preg_replace('/ /', '%', $val); |
---|
197 | $where.= ' AND reviewer_name LIKE ? '; |
---|
198 | $values[] = "%$val%"; |
---|
199 | break; |
---|
200 | |
---|
201 | case 'reviewer_url': |
---|
202 | $val = preg_replace('/ /', '%', $val); |
---|
203 | $where.= ' AND reviewer_url LIKE ? '; |
---|
204 | $values[] = "%$val%"; |
---|
205 | break; |
---|
206 | |
---|
207 | case 'product_name': |
---|
208 | $val = preg_replace('/ /', '%', $val); |
---|
209 | $where.= ' AND name LIKE ? '; |
---|
210 | $values[] = "%$val%"; |
---|
211 | break; |
---|
212 | |
---|
213 | case 'product_code': |
---|
214 | $val = preg_replace('/ /', '%', $val); |
---|
215 | $where.= ' AND A.product_id IN (SELECT product_id FROM dtb_products_class WHERE product_code LIKE ?)'; |
---|
216 | $values[] = "%$val%"; |
---|
217 | break; |
---|
218 | |
---|
219 | case 'reviewer_sex': |
---|
220 | $tmp_where = ''; |
---|
221 | //$val=配列の中身,$element=各キーの値(1,2) |
---|
222 | if (is_array($val)) { |
---|
223 | foreach ($val as $element) { |
---|
224 | if ($element != '') { |
---|
225 | if ($tmp_where == '') { |
---|
226 | $tmp_where .= ' AND (sex = ?'; |
---|
227 | } else { |
---|
228 | $tmp_where .= ' OR sex = ?'; |
---|
229 | } |
---|
230 | $values[] = $element; |
---|
231 | } |
---|
232 | } |
---|
233 | if ($tmp_where != '') { |
---|
234 | $tmp_where .= ')'; |
---|
235 | $where .= " $tmp_where "; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | break; |
---|
240 | |
---|
241 | case 'recommend_level': |
---|
242 | $where.= ' AND recommend_level = ? '; |
---|
243 | $values[] = $val; |
---|
244 | break; |
---|
245 | |
---|
246 | case 'date_from': |
---|
247 | $where.= ' AND A.create_date >= ? '; |
---|
248 | $values[] = $val; |
---|
249 | break; |
---|
250 | |
---|
251 | case 'date_to': |
---|
252 | $where.= " AND A.create_date <= cast( ? as date) "; |
---|
253 | $values[] = $val; |
---|
254 | break; |
---|
255 | |
---|
256 | default: |
---|
257 | break; |
---|
258 | } |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | return array($where, $values); |
---|
263 | } |
---|
264 | } |
---|