getFavoriteProductQueryBuilderByCustomer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.ec-cube.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 Doctrine\ORM\EntityRepository;
28
use Doctrine\ORM\NoResultException;
29
use Eccube\Application;
30
use Eccube\Util\Str;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32
33
/**
34
 * ProductRepository
35
 *
36
 * This class was generated by the Doctrine ORM. Add your own custom
37
 * repository methods below.
38
 */
39
class ProductRepository extends EntityRepository
40
{
41
42
    /**
43
     * @var \Eccube\Application
44
     */
45
    protected $app;
46
47
    /**
48
     * @param Application $app
49
     */
50
    public function setApplication(Application $app)
51
    {
52
        $this->app = $app;
53
    }
54
55
    /**
56
     * get Product.
57
     *
58
     * @param  integer $productId
59
     * @return \Eccube\Entity\Product
60
     *
61
     * @throws NotFoundHttpException
62
     */
63
    public function get($productId)
64
    {
65
        // Product
66
        try {
67
            $qb = $this->createQueryBuilder('p');
68
            $qb->addSelect(array('pc', 'cc1', 'cc2', 'pi', 'ps'))
69
                ->innerJoin('p.ProductClasses', 'pc')
70
                ->leftJoin('pc.ClassCategory1', 'cc1')
71
                ->leftJoin('pc.ClassCategory2', 'cc2')
72
                ->leftJoin('p.ProductImage', 'pi')
73
                ->innerJoin('pc.ProductStock', 'ps')
74
                ->where('p.id = :id')
75
                ->orderBy('cc1.rank', 'DESC')
76
                ->addOrderBy('cc2.rank', 'DESC');
77
78
            $product = $qb
79
                ->getQuery()
80
                ->setParameters(array(
81
                    'id' => $productId,
82
                ))
83
                ->getSingleResult();
84
        } catch (NoResultException $e) {
85
            throw new NotFoundHttpException();
86
        }
87
88
        return $product;
89
    }
90
91
    /**
92
     * get query builder.
93
     *
94
     * @param  array $searchData
95
     * @return \Doctrine\ORM\QueryBuilder
96
     */
97
    public function getQueryBuilderBySearchData($searchData)
98
    {
99
        $qb = $this->createQueryBuilder('p')
100
            ->andWhere('p.Status = 1');
101
102
        // category
103
        $categoryJoin = false;
104 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
105
            $Categories = $searchData['category_id']->getSelfAndDescendants();
106
            if ($Categories) {
107
                $qb
108
                    ->innerJoin('p.ProductCategories', 'pct')
109
                    ->innerJoin('pct.Category', 'c')
110
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
111
                    ->setParameter('Categories', $Categories);
112
                $categoryJoin = true;
113
            }
114
        }
115
116
        // name
117
        if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) {
118
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
119
120
            foreach ($keywords as $index => $keyword) {
121
                $key = sprintf('keyword%s', $index);
122
                $qb
123
                    ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key))
124
                    ->setParameter($key, '%' . $keyword . '%');
125
            }
126
        }
127
128
        // Order By
129
        // 価格低い順
130
        $config = $this->app['config'];
131
        if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_lower']) {
132
            //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html
133
            $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min');
134
            $qb->innerJoin('p.ProductClasses', 'pc');
135
            $qb->groupBy('p');
136
            // postgres9.0以下は, groupBy('p.id')が利用できない
137
            // mysqlおよびpostgresql9.1以上であればgroupBy('p.id')にすることで性能向上が期待できる.
138
            // @see https://github.com/EC-CUBE/ec-cube/issues/1904
139
            // $qb->groupBy('p.id');
140
            $qb->orderBy('price02_min', 'ASC');
141
            $qb->addOrderBy('p.id', 'DESC');
142
            // 価格高い順
143
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) {
144
            $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max');
145
            $qb->innerJoin('p.ProductClasses', 'pc');
146
            $qb->groupBy('p');
147
            $qb->orderBy('price02_max', 'DESC');
148
            $qb->addOrderBy('p.id', 'DESC');
149
            // 新着順
150
        } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_newer']) {
151
            // 在庫切れ商品非表示の設定が有効時対応
152
            // @see https://github.com/EC-CUBE/ec-cube/issues/1998
153
            if ($this->app['orm.em']->getFilters()->isEnabled('nostock_hidden') == true) {
154
                $qb->innerJoin('p.ProductClasses', 'pc');
155
            }
156
            $qb->orderBy('p.create_date', 'DESC');
157
            $qb->addOrderBy('p.id', 'DESC');
158
        } else {
159
            if ($categoryJoin === false) {
160
                $qb
161
                    ->leftJoin('p.ProductCategories', 'pct')
162
                    ->leftJoin('pct.Category', 'c');
163
            }
164
            $qb
165
                ->addOrderBy('p.id', 'DESC');
166
        }
167
168
        return $qb;
169
    }
170
171
    /**
172
     * get query builder.
173
     *
174
     * @param  array $searchData
175
     * @return \Doctrine\ORM\QueryBuilder
176
     */
177
    public function getQueryBuilderBySearchDataForAdmin($searchData)
178
    {
179
        $qb = $this->createQueryBuilder('p')
180
            ->innerJoin('p.ProductClasses', 'pc');
181
182
        // id
183 View Code Duplication
        if (isset($searchData['id']) && Str::isNotBlank($searchData['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
184
            $id = preg_match('/^\d+$/', $searchData['id']) ? $searchData['id'] : null;
185
            $qb
186
                ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid')
187
                ->setParameter('id', $id)
188
                ->setParameter('likeid', '%' . $searchData['id'] . '%');
189
        }
190
191
        // code
192
        /*
193
        if (!empty($searchData['code']) && $searchData['code']) {
194
            $qb
195
                ->innerJoin('p.ProductClasses', 'pc')
196
                ->andWhere('pc.code LIKE :code')
197
                ->setParameter('code', '%' . $searchData['code'] . '%');
198
        }
199
200
        // name
201
        if (!empty($searchData['name']) && $searchData['name']) {
202
            $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY);
203
            foreach ($keywords as $keyword) {
204
                $qb
205
                    ->andWhere('p.name LIKE :name')
206
                    ->setParameter('name', '%' . $keyword . '%');
207
            }
208
        }
209
       */
210
211
        // category
212 View Code Duplication
        if (!empty($searchData['category_id']) && $searchData['category_id']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
213
            $Categories = $searchData['category_id']->getSelfAndDescendants();
214
            if ($Categories) {
215
                $qb
216
                    ->innerJoin('p.ProductCategories', 'pct')
217
                    ->innerJoin('pct.Category', 'c')
218
                    ->andWhere($qb->expr()->in('pct.Category', ':Categories'))
219
                    ->setParameter('Categories', $Categories);
220
            }
221
        }
222
223
        // status
224
        if (!empty($searchData['status']) && $searchData['status']->toArray()) {
225
            $qb
226
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
227
                ->setParameter('Status', $searchData['status']->toArray());
228
        }
229
230
        // link_status
231
        if (isset($searchData['link_status'])) {
232
            $qb
233
                ->andWhere($qb->expr()->in('p.Status', ':Status'))
234
                ->setParameter('Status', $searchData['link_status']);
235
        }
236
237
        // stock status
238
        if (isset($searchData['stock_status'])) {
239
            $qb
240
                ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0')
241
                ->setParameter('StockUnlimited', $searchData['stock_status']);
242
        }
243
244
        // crate_date
245 View Code Duplication
        if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
246
            $date = $searchData['create_date_start']
247
                ->format('Y-m-d H:i:s');
248
            $qb
249
                ->andWhere('p.create_date >= :create_date_start')
250
                ->setParameter('create_date_start', $date);
251
        }
252
253
        if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) {
254
            $date = clone $searchData['create_date_end'];
255
            $date = $date
256
                ->modify('+1 days')
257
                ->format('Y-m-d H:i:s');
258
            $qb
259
                ->andWhere('p.create_date < :create_date_end')
260
                ->setParameter('create_date_end', $date);
261
        }
262
263
        // update_date
264 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
265
            $date = $searchData['update_date_start']
266
                ->format('Y-m-d H:i:s');
267
            $qb
268
                ->andWhere('p.update_date >= :update_date_start')
269
                ->setParameter('update_date_start', $date);
270
        }
271
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
272
            $date = clone $searchData['update_date_end'];
273
            $date = $date
274
                ->modify('+1 days')
275
                ->format('Y-m-d H:i:s');
276
            $qb
277
                ->andWhere('p.update_date < :update_date_end')
278
                ->setParameter('update_date_end', $date);
279
        }
280
281
282
        // Order By
283
        $qb
284
            ->orderBy('p.update_date', 'DESC');
285
286
        return $qb;
287
    }
288
289
    /**
290
     * get query builder.
291
     *
292
     * @param $Customer
293
     * @return \Doctrine\ORM\QueryBuilder
294
     * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer()
295
     * @deprecated since 3.0.0, to be removed in 3.1
296
     */
297
    public function getFavoriteProductQueryBuilderByCustomer($Customer)
298
    {
299
        $qb = $this->createQueryBuilder('p')
300
            ->innerJoin('p.CustomerFavoriteProducts', 'cfp')
301
            ->where('cfp.Customer = :Customer AND p.Status = 1')
302
            ->setParameter('Customer', $Customer);
303
304
        // Order By
305
        // XXX Paginater を使用した場合に PostgreSQL で正しくソートできない
306
        $qb->addOrderBy('cfp.create_date', 'DESC');
307
308
        return $qb;
309
    }
310
}
311