1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 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
|
|
|
namespace Eccube\Repository; |
26
|
|
|
|
27
|
|
|
use Eccube\Util\Str; |
28
|
|
|
use Doctrine\ORM\EntityRepository; |
29
|
|
|
use Doctrine\ORM\NoResultException; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ProductRepository |
34
|
|
|
* |
35
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
36
|
|
|
* repository methods below. |
37
|
|
|
*/ |
38
|
|
|
class ProductRepository extends EntityRepository |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* get Product. |
43
|
|
|
* |
44
|
|
|
* @param integer $productId |
45
|
|
|
* @return \Eccube\Entity\Product |
46
|
|
|
* |
47
|
|
|
* @throws NotFoundHttpException |
48
|
|
|
*/ |
49
|
|
|
public function get($productId) |
50
|
|
|
{ |
51
|
|
|
// Product |
52
|
|
|
try { |
53
|
|
|
$qb = $this->createQueryBuilder('p') |
54
|
|
|
->andWhere('p.id = :id'); |
55
|
|
|
|
56
|
|
|
$product = $qb |
57
|
|
|
->getQuery() |
58
|
|
|
->setParameters(array( |
59
|
|
|
'id' => $productId, |
60
|
|
|
)) |
61
|
|
|
->getSingleResult(); |
62
|
|
|
} catch (NoResultException $e) { |
63
|
|
|
throw new NotFoundHttpException(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $product; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* get query builder. |
71
|
|
|
* |
72
|
|
|
* @param array $searchData |
73
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
74
|
|
|
*/ |
75
|
|
|
public function getQueryBuilderBySearchData($searchData) |
76
|
|
|
{ |
77
|
|
|
$qb = $this->createQueryBuilder('p') |
78
|
|
|
->andWhere('p.Status = 1'); |
79
|
|
|
|
80
|
|
|
// category |
81
|
|
|
$categoryJoin = false; |
82
|
|
View Code Duplication |
if (!empty($searchData['category_id']) && $searchData['category_id']) { |
|
|
|
|
83
|
|
|
$Categories = $searchData['category_id']->getSelfAndDescendants(); |
84
|
|
|
if ($Categories) { |
85
|
|
|
$qb |
86
|
|
|
->innerJoin('p.ProductCategories', 'pct') |
87
|
|
|
->innerJoin('pct.Category', 'c') |
88
|
|
|
->andWhere($qb->expr()->in('pct.Category', ':Categories')) |
89
|
|
|
->setParameter('Categories', $Categories); |
90
|
|
|
$categoryJoin = true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// name |
95
|
|
|
if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) { |
96
|
|
|
$keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY); |
97
|
|
|
|
98
|
|
|
foreach ($keywords as $index => $keyword) { |
99
|
|
|
$key = sprintf('keyword%s', $index); |
100
|
|
|
$qb |
101
|
|
|
->andWhere(sprintf('p.name LIKE :%s OR p.search_word LIKE :%s', $key, $key)) |
102
|
|
|
->setParameter($key, '%' . $keyword . '%'); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Order By |
107
|
|
|
// 価格順 |
108
|
|
|
if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '1') { |
109
|
|
|
//@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html |
110
|
|
|
$qb->addSelect('MIN(pc.price02) as HIDDEN price02_min'); |
111
|
|
|
$qb->innerJoin('p.ProductClasses', 'pc'); |
112
|
|
|
$qb->groupBy('p'); |
113
|
|
|
$qb->orderBy('price02_min', 'ASC'); |
114
|
|
|
// 新着順 |
115
|
|
|
} else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == '2') { |
116
|
|
|
$qb->orderBy('p.create_date', 'DESC'); |
117
|
|
|
} else { |
118
|
|
|
if ($categoryJoin === false) { |
119
|
|
|
$qb |
120
|
|
|
->leftJoin('p.ProductCategories', 'pct') |
121
|
|
|
->leftJoin('pct.Category', 'c'); |
122
|
|
|
} |
123
|
|
|
$qb |
124
|
|
|
->addOrderBy('p.id', 'DESC'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $qb; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* get query builder. |
132
|
|
|
* |
133
|
|
|
* @param array $searchData |
134
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
135
|
|
|
*/ |
136
|
|
|
public function getQueryBuilderBySearchDataForAdmin($searchData) |
137
|
|
|
{ |
138
|
|
|
$qb = $this->createQueryBuilder('p') |
139
|
|
|
->innerJoin('p.ProductClasses', 'pc'); |
140
|
|
|
|
141
|
|
|
// id |
142
|
|
View Code Duplication |
if (isset($searchData['id']) && Str::isNotBlank($searchData['id'])) { |
|
|
|
|
143
|
|
|
$id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null; |
144
|
|
|
$qb |
145
|
|
|
->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid') |
146
|
|
|
->setParameter('id', $id) |
147
|
|
|
->setParameter('likeid', '%' . $searchData['id'] . '%'); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// code |
151
|
|
|
/* |
|
|
|
|
152
|
|
|
if (!empty($searchData['code']) && $searchData['code']) { |
153
|
|
|
$qb |
154
|
|
|
->innerJoin('p.ProductClasses', 'pc') |
155
|
|
|
->andWhere('pc.code LIKE :code') |
156
|
|
|
->setParameter('code', '%' . $searchData['code'] . '%'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// name |
160
|
|
|
if (!empty($searchData['name']) && $searchData['name']) { |
161
|
|
|
$keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY); |
162
|
|
|
foreach ($keywords as $keyword) { |
163
|
|
|
$qb |
164
|
|
|
->andWhere('p.name LIKE :name') |
165
|
|
|
->setParameter('name', '%' . $keyword . '%'); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
*/ |
169
|
|
|
|
170
|
|
|
// category |
171
|
|
View Code Duplication |
if (!empty($searchData['category_id']) && $searchData['category_id']) { |
|
|
|
|
172
|
|
|
$Categories = $searchData['category_id']->getSelfAndDescendants(); |
173
|
|
|
if ($Categories) { |
174
|
|
|
$qb |
175
|
|
|
->innerJoin('p.ProductCategories', 'pct') |
176
|
|
|
->innerJoin('pct.Category', 'c') |
177
|
|
|
->andWhere($qb->expr()->in('pct.Category', ':Categories')) |
178
|
|
|
->setParameter('Categories', $Categories); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// status |
183
|
|
|
if (!empty($searchData['status']) && $searchData['status']->toArray()) { |
184
|
|
|
$qb |
185
|
|
|
->andWhere($qb->expr()->in('p.Status', ':Status')) |
186
|
|
|
->setParameter('Status', $searchData['status']->toArray()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// link_status |
190
|
|
|
if (isset($searchData['link_status'])) { |
191
|
|
|
$qb |
192
|
|
|
->andWhere($qb->expr()->in('p.Status', ':Status')) |
193
|
|
|
->setParameter('Status', $searchData['link_status']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// stock status |
197
|
|
|
if (isset($searchData['stock_status'])) { |
198
|
|
|
$qb |
199
|
|
|
->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0') |
200
|
|
|
->setParameter('StockUnlimited', $searchData['stock_status']); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// crate_date |
204
|
|
View Code Duplication |
if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) { |
|
|
|
|
205
|
|
|
$date = $searchData['create_date_start'] |
206
|
|
|
->format('Y-m-d H:i:s'); |
207
|
|
|
$qb |
208
|
|
|
->andWhere('p.create_date >= :create_date_start') |
209
|
|
|
->setParameter('create_date_start', $date); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
View Code Duplication |
if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) { |
|
|
|
|
213
|
|
|
$date = clone $searchData['create_date_end']; |
214
|
|
|
$date = $date |
215
|
|
|
->modify('+1 days') |
216
|
|
|
->format('Y-m-d H:i:s'); |
217
|
|
|
$qb |
218
|
|
|
->andWhere('p.create_date < :create_date_end') |
219
|
|
|
->setParameter('create_date_end', $date); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// update_date |
223
|
|
View Code Duplication |
if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
|
|
|
224
|
|
|
$date = $searchData['update_date_start'] |
225
|
|
|
->format('Y-m-d H:i:s'); |
226
|
|
|
$qb |
227
|
|
|
->andWhere('p.update_date >= :update_date_start') |
228
|
|
|
->setParameter('update_date_start', $date); |
229
|
|
|
} |
230
|
|
View Code Duplication |
if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
|
|
|
231
|
|
|
$date = clone $searchData['update_date_end']; |
232
|
|
|
$date = $date |
233
|
|
|
->modify('+1 days') |
234
|
|
|
->format('Y-m-d H:i:s'); |
235
|
|
|
$qb |
236
|
|
|
->andWhere('p.update_date < :update_date_end') |
237
|
|
|
->setParameter('update_date_end', $date); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
// Order By |
242
|
|
|
$qb |
243
|
|
|
->orderBy('p.update_date', 'DESC'); |
244
|
|
|
|
245
|
|
|
return $qb; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
|
|
|
|
249
|
|
|
* get query builder. |
250
|
|
|
* |
251
|
|
|
* @param $Customer |
|
|
|
|
252
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
253
|
|
|
*/ |
254
|
|
|
public function getFavoriteProductQueryBuilderByCustomer($Customer) |
255
|
|
|
{ |
256
|
|
|
$qb = $this->createQueryBuilder('p') |
257
|
|
|
->innerJoin('p.CustomerFavoriteProducts', 'cfp') |
258
|
|
|
->where('cfp.Customer = :Customer AND p.Status = 1') |
259
|
|
|
->setParameter('Customer', $Customer); |
260
|
|
|
|
261
|
|
|
// Order By |
262
|
|
|
$qb->addOrderBy('cfp.create_date', 'DESC'); |
263
|
|
|
|
264
|
|
|
return $qb; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.