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\Annotation\Inject; |
28
|
|
|
use Eccube\Annotation\Repository; |
29
|
|
|
use Eccube\Entity\Category; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* CategoryRepository |
33
|
|
|
* |
34
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
35
|
|
|
* repository methods below. |
36
|
|
|
* |
37
|
|
|
* @Repository |
38
|
|
|
*/ |
39
|
|
|
class CategoryRepository extends AbstractRepository |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @Inject("config") |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $appConfig; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 全カテゴリの合計を取得する. |
49
|
|
|
* |
50
|
|
|
* @return int 全カテゴリの合計数 |
51
|
|
|
*/ |
52
|
1 |
|
public function getTotalCount() |
53
|
|
|
{ |
54
|
|
|
$qb = $this |
55
|
1 |
|
->createQueryBuilder('c') |
56
|
1 |
|
->select('count(c.id)'); |
57
|
1 |
|
$count = $qb->getQuery() |
58
|
1 |
|
->getSingleScalarResult(); |
59
|
|
|
|
60
|
1 |
|
return $count; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* カテゴリ一覧を取得する. |
65
|
|
|
* |
66
|
|
|
* 引数 $Parent を指定した場合は, 指定したカテゴリの子以下を取得する. |
67
|
|
|
* |
68
|
|
|
* @param \Eccube\Entity\Category|null $Parent 指定の親カテゴリ |
69
|
|
|
* @param bool $flat trueの場合, 階層化されたカテゴリを一つの配列にまとめる |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return \Eccube\Entity\Category[] カテゴリの配列 |
72
|
|
|
*/ |
73
|
65 |
|
public function getList(Category $Parent = null, $flat = false) |
74
|
|
|
{ |
75
|
65 |
|
$options = $this->appConfig['doctrine_cache']; |
76
|
65 |
|
$lifetime = $options['result_cache']['lifetime']; |
77
|
|
|
|
78
|
65 |
|
$qb = $this->createQueryBuilder('c1') |
79
|
65 |
|
->select('c1, c2, c3, c4, c5') |
80
|
65 |
|
->leftJoin('c1.Children', 'c2') |
81
|
65 |
|
->leftJoin('c2.Children', 'c3') |
82
|
65 |
|
->leftJoin('c3.Children', 'c4') |
83
|
65 |
|
->leftJoin('c4.Children', 'c5') |
84
|
65 |
|
->orderBy('c1.rank', 'DESC') |
85
|
65 |
|
->addOrderBy('c2.rank', 'DESC') |
86
|
65 |
|
->addOrderBy('c3.rank', 'DESC') |
87
|
65 |
|
->addOrderBy('c4.rank', 'DESC') |
88
|
65 |
|
->addOrderBy('c5.rank', 'DESC'); |
89
|
|
|
|
90
|
65 |
|
if ($Parent) { |
91
|
2 |
|
$qb->where('c1.Parent = :Parent')->setParameter('Parent', $Parent); |
92
|
|
|
} else { |
93
|
64 |
|
$qb->where('c1.Parent IS NULL'); |
94
|
|
|
} |
95
|
65 |
|
$Categories = $qb->getQuery() |
96
|
65 |
|
->useResultCache(true, $lifetime) |
97
|
65 |
|
->getResult(); |
98
|
|
|
|
99
|
65 |
|
if ($flat) { |
100
|
60 |
|
$array = array(); |
101
|
60 |
|
foreach ($Categories as $Category) { |
102
|
60 |
|
$array = array_merge($array, $Category->getSelfAndDescendants()); |
103
|
|
|
} |
104
|
60 |
|
$Categories = $array; |
105
|
|
|
} |
106
|
|
|
|
107
|
65 |
|
return $Categories; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* カテゴリを保存する. |
112
|
|
|
* |
113
|
|
|
* @param \Eccube\Entity\Category $Category カテゴリ |
114
|
|
|
* @return boolean 成功した場合 true |
115
|
|
|
*/ |
116
|
7 |
|
public function save($Category) |
117
|
|
|
{ |
118
|
7 |
|
$em = $this->getEntityManager(); |
119
|
7 |
|
$em->getConnection()->beginTransaction(); |
120
|
|
|
try { |
121
|
7 |
|
if (!$Category->getId()) { |
122
|
5 |
|
$Parent = $Category->getParent(); |
123
|
5 |
|
if ($Parent) { |
124
|
1 |
|
$rank = $Parent->getRank() - 1; |
125
|
|
|
} else { |
126
|
4 |
|
$rank = $this->createQueryBuilder('c') |
127
|
4 |
|
->select('MAX(c.rank)') |
128
|
4 |
|
->getQuery() |
129
|
4 |
|
->getSingleScalarResult(); |
130
|
|
|
} |
131
|
5 |
|
if (!$rank) { |
132
|
|
|
$rank = 0; |
133
|
|
|
} |
134
|
5 |
|
$Category->setRank($rank + 1); |
135
|
|
|
|
136
|
5 |
|
$em->createQueryBuilder() |
137
|
5 |
|
->update('Eccube\Entity\Category', 'c') |
138
|
5 |
|
->set('c.rank', 'c.rank + 1') |
139
|
5 |
|
->where('c.rank > :rank') |
140
|
5 |
|
->setParameter('rank', $rank) |
141
|
5 |
|
->getQuery() |
142
|
5 |
|
->execute(); |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
$em->persist($Category); |
146
|
7 |
|
$em->flush(); |
147
|
|
|
|
148
|
6 |
|
$em->getConnection()->commit(); |
149
|
1 |
|
} catch (\Exception $e) { |
150
|
1 |
|
$em->getConnection()->rollback(); |
151
|
|
|
|
152
|
1 |
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
6 |
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* カテゴリを削除する. |
160
|
|
|
* |
161
|
|
|
* @param \Eccube\Entity\Category $Category 削除対象のカテゴリ |
162
|
|
|
* @return boolean 成功した場合 true, 子カテゴリが存在する場合, 商品カテゴリが紐づいている場合は false |
163
|
|
|
*/ |
164
|
3 |
|
public function delete($Category) |
165
|
|
|
{ |
166
|
3 |
|
$em = $this->getEntityManager(); |
167
|
3 |
|
$em->getConnection()->beginTransaction(); |
168
|
|
|
try { |
169
|
3 |
|
if ($Category->getChildren()->count() > 0 || $Category->getProductCategories()->count() > 0) { |
170
|
1 |
|
throw new \Exception(); |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
$rank = $Category->getRank(); |
174
|
|
|
|
175
|
2 |
|
$em->createQueryBuilder() |
176
|
2 |
|
->update('Eccube\Entity\Category', 'c') |
177
|
2 |
|
->set('c.rank', 'c.rank - 1') |
178
|
2 |
|
->where('c.rank > :rank') |
179
|
2 |
|
->setParameter('rank', $rank) |
180
|
2 |
|
->getQuery() |
181
|
2 |
|
->execute(); |
182
|
|
|
|
183
|
2 |
|
$em->remove($Category); |
184
|
2 |
|
$em->flush(); |
185
|
|
|
|
186
|
2 |
|
$em->getConnection()->commit(); |
187
|
1 |
|
} catch (\Exception $e) { |
188
|
1 |
|
$em->getConnection()->rollback(); |
189
|
|
|
|
190
|
1 |
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
2 |
|
return true; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|