1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic, Na. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\AssetBundle\Entity; |
||
13 | |||
14 | use Doctrine\ORM\NonUniqueResultException; |
||
15 | use Doctrine\ORM\NoResultException; |
||
16 | use Doctrine\ORM\Tools\Pagination\Paginator; |
||
17 | use Mautic\CoreBundle\Entity\CommonRepository; |
||
18 | |||
19 | /** |
||
20 | * Class AssetRepository. |
||
21 | */ |
||
22 | class AssetRepository extends CommonRepository |
||
23 | { |
||
24 | /** |
||
25 | * Get a list of entities. |
||
26 | * |
||
27 | * @return Paginator |
||
28 | */ |
||
29 | public function getEntities(array $args = []) |
||
30 | { |
||
31 | $q = $this |
||
32 | ->createQueryBuilder('a') |
||
33 | ->select('a') |
||
34 | ->leftJoin('a.category', 'c'); |
||
35 | |||
36 | $args['qb'] = $q; |
||
37 | |||
38 | return parent::getEntities($args); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $search |
||
43 | * @param int $limit |
||
44 | * @param int $start |
||
45 | * @param bool|false $viewOther |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | public function getAssetList($search = '', $limit = 10, $start = 0, $viewOther = false) |
||
50 | { |
||
51 | $q = $this->createQueryBuilder('a'); |
||
52 | $q->select('partial a.{id, title, path, alias, language}'); |
||
53 | |||
54 | if (!empty($search)) { |
||
55 | $q->andWhere($q->expr()->like('a.title', ':search')) |
||
56 | ->setParameter('search', "{$search}%"); |
||
57 | } |
||
58 | |||
59 | if (!$viewOther) { |
||
60 | $q->andWhere($q->expr()->eq('a.createdBy', ':id')) |
||
61 | ->setParameter('id', $this->currentUser->getId()); |
||
62 | } |
||
63 | |||
64 | $q->orderBy('a.title'); |
||
65 | |||
66 | if (!empty($limit)) { |
||
67 | $q->setFirstResult($start) |
||
68 | ->setMaxResults($limit); |
||
69 | } |
||
70 | |||
71 | return $q->getQuery()->getArrayResult(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q |
||
76 | * @param $filter |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | protected function addCatchAllWhereClause($q, $filter) |
||
81 | { |
||
82 | return $this->addStandardCatchAllWhereClause($q, $filter, [ |
||
83 | 'a.title', |
||
84 | 'a.alias', |
||
85 | ]); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q |
||
90 | * @param $filter |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | protected function addSearchCommandWhereClause($q, $filter) |
||
95 | { |
||
96 | list($expr, $parameters) = $this->addStandardSearchCommandWhereClause($q, $filter); |
||
97 | if ($expr) { |
||
98 | return [$expr, $parameters]; |
||
99 | } |
||
100 | |||
101 | $command = $field = $filter->command; |
||
102 | $unique = $this->generateRandomParameterName(); |
||
103 | $returnParameter = false; //returning a parameter that is not used will lead to a Doctrine error |
||
104 | switch ($command) { |
||
105 | case $this->translator->trans('mautic.asset.asset.searchcommand.lang'): |
||
106 | $langUnique = $this->generateRandomParameterName(); |
||
107 | $langValue = $filter->string.'_%'; |
||
108 | $forceParameters = [ |
||
109 | $langUnique => $langValue, |
||
110 | $unique => $filter->string, |
||
111 | ]; |
||
112 | $expr = $q->expr()->orX( |
||
113 | $q->expr()->eq('a.language', ":$unique"), |
||
114 | $q->expr()->like('a.language', ":$langUnique") |
||
115 | ); |
||
116 | $returnParameter = true; |
||
117 | break; |
||
118 | } |
||
119 | |||
120 | if ($expr && $filter->not) { |
||
121 | $expr = $q->expr()->not($expr); |
||
122 | } |
||
123 | |||
124 | if (!empty($forceParameters)) { |
||
125 | $parameters = $forceParameters; |
||
126 | } elseif (!$returnParameter) { |
||
127 | $parameters = []; |
||
128 | } else { |
||
129 | $string = ($filter->strict) ? $filter->string : "%{$filter->string}%"; |
||
130 | $parameters = ["$unique" => $string]; |
||
131 | } |
||
132 | |||
133 | return [$expr, $parameters]; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return array |
||
138 | */ |
||
139 | public function getSearchCommands() |
||
140 | { |
||
141 | $commands = [ |
||
142 | 'mautic.core.searchcommand.ispublished', |
||
143 | 'mautic.core.searchcommand.isunpublished', |
||
144 | 'mautic.core.searchcommand.isuncategorized', |
||
145 | 'mautic.core.searchcommand.ismine', |
||
146 | 'mautic.core.searchcommand.category', |
||
147 | 'mautic.asset.asset.searchcommand.lang', |
||
148 | ]; |
||
149 | |||
150 | return array_merge($commands, parent::getSearchCommands()); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | protected function getDefaultOrder() |
||
157 | { |
||
158 | return [ |
||
0 ignored issues
–
show
|
|||
159 | ['a.title', 'ASC'], |
||
160 | ]; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getTableAlias() |
||
169 | { |
||
170 | return 'a'; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Gets the sum size of assets. |
||
175 | * |
||
176 | * @return int |
||
177 | */ |
||
178 | public function getAssetSize(array $assets) |
||
179 | { |
||
180 | $q = $this->_em->getConnection()->createQueryBuilder(); |
||
181 | $q->select('sum(a.size) as total_size') |
||
182 | ->from(MAUTIC_TABLE_PREFIX.'assets', 'a') |
||
183 | ->where('a.id IN (:assetIds)') |
||
184 | ->setParameter('assetIds', $assets, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY); |
||
185 | |||
186 | $result = $q->execute()->fetchAll(); |
||
187 | |||
188 | return (int) $result[0]['total_size']; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param $id |
||
193 | * @param int $increaseBy |
||
194 | * @param bool|false $unique |
||
195 | */ |
||
196 | public function upDownloadCount($id, $increaseBy = 1, $unique = false) |
||
197 | { |
||
198 | $q = $this->_em->getConnection()->createQueryBuilder(); |
||
199 | |||
200 | $q->update(MAUTIC_TABLE_PREFIX.'assets') |
||
201 | ->set('download_count', 'download_count + '.(int) $increaseBy) |
||
202 | ->where('id = '.(int) $id); |
||
203 | |||
204 | if ($unique) { |
||
205 | $q->set('unique_download_count', 'unique_download_count + '.(int) $increaseBy); |
||
206 | } |
||
207 | |||
208 | $q->execute(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param int $categoryId |
||
213 | * |
||
214 | * @return Asset |
||
215 | * |
||
216 | * @throws NoResultException |
||
217 | * @throws NonUniqueResultException |
||
218 | */ |
||
219 | public function getLatestAssetForCategory($categoryId) |
||
220 | { |
||
221 | $q = $this->createQueryBuilder($this->getTableAlias()); |
||
222 | $q->where($this->getTableAlias().'.category = :categoryId'); |
||
223 | $q->andWhere($this->getTableAlias().'.isPublished = TRUE'); |
||
224 | $q->setParameter('categoryId', $categoryId); |
||
225 | $q->orderBy($this->getTableAlias().'.dateAdded', 'DESC'); |
||
226 | $q->setMaxResults(1); |
||
227 | |||
228 | return $q->getQuery()->getSingleResult(); |
||
229 | } |
||
230 | } |
||
231 |