1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * @copyright 2016 Mautic Contributors. 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\CoreBundle\Model; |
||||
13 | |||||
14 | use Doctrine\ORM\EntityManager; |
||||
15 | use Doctrine\ORM\Mapping\ClassMetadata; |
||||
16 | use Mautic\CoreBundle\Entity\CommonRepository; |
||||
17 | use Mautic\CoreBundle\Helper\ClickthroughHelper; |
||||
18 | use Mautic\CoreBundle\Helper\CoreParametersHelper; |
||||
19 | use Mautic\CoreBundle\Helper\UserHelper; |
||||
20 | use Mautic\CoreBundle\Security\Permissions\CorePermissions; |
||||
21 | use Monolog\Logger; |
||||
22 | use Psr\Log\LoggerInterface; |
||||
23 | use Symfony\Bundle\FrameworkBundle\Routing\Router; |
||||
24 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||||
25 | use Symfony\Component\Intl\Intl; |
||||
26 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||||
27 | use Symfony\Component\Translation\TranslatorInterface; |
||||
28 | |||||
29 | /** |
||||
30 | * Class AbstractCommonModel. |
||||
31 | */ |
||||
32 | abstract class AbstractCommonModel |
||||
33 | { |
||||
34 | /** |
||||
35 | * @var \Doctrine\ORM\EntityManager |
||||
36 | */ |
||||
37 | protected $em; |
||||
38 | |||||
39 | /** |
||||
40 | * @var \Mautic\CoreBundle\Security\Permissions\CorePermissions |
||||
41 | */ |
||||
42 | protected $security; |
||||
43 | |||||
44 | /** |
||||
45 | * @var EventDispatcherInterface |
||||
46 | */ |
||||
47 | protected $dispatcher; |
||||
48 | |||||
49 | /** |
||||
50 | * @var Router |
||||
51 | */ |
||||
52 | protected $router; |
||||
53 | |||||
54 | /** |
||||
55 | * @var TranslatorInterface |
||||
56 | */ |
||||
57 | protected $translator; |
||||
58 | |||||
59 | /** |
||||
60 | * @var UserHelper |
||||
61 | */ |
||||
62 | protected $userHelper; |
||||
63 | |||||
64 | /** |
||||
65 | * @var Logger |
||||
66 | */ |
||||
67 | protected $logger; |
||||
68 | |||||
69 | /** |
||||
70 | * @var CoreParametersHelper |
||||
71 | */ |
||||
72 | protected $coreParametersHelper; |
||||
73 | |||||
74 | public function setEntityManager(EntityManager $em) |
||||
75 | { |
||||
76 | $this->em = $em; |
||||
77 | } |
||||
78 | |||||
79 | public function setSecurity(CorePermissions $security) |
||||
80 | { |
||||
81 | $this->security = $security; |
||||
82 | } |
||||
83 | |||||
84 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||||
85 | { |
||||
86 | $this->dispatcher = $dispatcher; |
||||
87 | } |
||||
88 | |||||
89 | public function setRouter(Router $router) |
||||
90 | { |
||||
91 | $this->router = $router; |
||||
92 | } |
||||
93 | |||||
94 | public function setTranslator(TranslatorInterface $translator) |
||||
95 | { |
||||
96 | $this->translator = $translator; |
||||
97 | } |
||||
98 | |||||
99 | public function setLogger(LoggerInterface $logger) |
||||
100 | { |
||||
101 | $this->logger = $logger; |
||||
102 | } |
||||
103 | |||||
104 | /** |
||||
105 | * Initialize the user parameter for use in locking procedures. |
||||
106 | */ |
||||
107 | public function setUserHelper(UserHelper $userHelper) |
||||
108 | { |
||||
109 | $this->userHelper = $userHelper; |
||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * Initialize the CoreParameters parameter. |
||||
114 | */ |
||||
115 | public function setCoreParametersHelper(CoreParametersHelper $coreParametersHelper) |
||||
116 | { |
||||
117 | $this->coreParametersHelper = $coreParametersHelper; |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * Retrieve the supported search commands for a repository. |
||||
122 | * |
||||
123 | * @return array |
||||
124 | */ |
||||
125 | public function getSupportedSearchCommands() |
||||
126 | { |
||||
127 | return []; |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * Retrieve the search command list for a repository. |
||||
132 | * |
||||
133 | * @return array |
||||
134 | */ |
||||
135 | public function getCommandList() |
||||
136 | { |
||||
137 | $repo = $this->getRepository(); |
||||
138 | |||||
139 | return ($repo instanceof CommonRepository) ? $repo->getSearchCommands() : []; |
||||
140 | } |
||||
141 | |||||
142 | /** |
||||
143 | * Retrieve the repository for an entity. |
||||
144 | * |
||||
145 | * @return \Mautic\CoreBundle\Entity\CommonRepository|bool |
||||
146 | */ |
||||
147 | public function getRepository() |
||||
148 | { |
||||
149 | static $commonRepo; |
||||
150 | |||||
151 | if (null === $commonRepo) { |
||||
152 | $commonRepo = new CommonRepository($this->em, new ClassMetadata('MauticCoreBundle:FormEntity')); |
||||
153 | } |
||||
154 | |||||
155 | return $commonRepo; |
||||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * Retrieve the permissions base. |
||||
160 | * |
||||
161 | * @return string |
||||
162 | */ |
||||
163 | public function getPermissionBase() |
||||
164 | { |
||||
165 | return ''; |
||||
166 | } |
||||
167 | |||||
168 | /** |
||||
169 | * Return a list of entities. |
||||
170 | * |
||||
171 | * @param array $args [start, limit, filter, orderBy, orderByDir] |
||||
172 | * |
||||
173 | * @return \Doctrine\ORM\Tools\Pagination\Paginator|array |
||||
174 | */ |
||||
175 | public function getEntities(array $args = []) |
||||
176 | { |
||||
177 | //set the translator |
||||
178 | $repo = $this->getRepository(); |
||||
179 | |||||
180 | if ($repo instanceof CommonRepository) { |
||||
181 | $repo->setTranslator($this->translator); |
||||
182 | $repo->setCurrentUser($this->userHelper->getUser()); |
||||
183 | |||||
184 | return $repo->getEntities($args); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
185 | } |
||||
186 | |||||
187 | return []; |
||||
188 | } |
||||
189 | |||||
190 | /** |
||||
191 | * Get a specific entity. |
||||
192 | * |
||||
193 | * @param int|array id |
||||
194 | * |
||||
195 | * @return object|null |
||||
196 | */ |
||||
197 | public function getEntity($id = null) |
||||
198 | { |
||||
199 | if (null !== $id) { |
||||
200 | $repo = $this->getRepository(); |
||||
201 | if (method_exists($repo, 'getEntity')) { |
||||
202 | return $repo->getEntity($id); |
||||
203 | } |
||||
204 | |||||
205 | return $repo->find((int) $id); |
||||
206 | } |
||||
207 | |||||
208 | return null; |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * Encode an array to append to a URL. |
||||
213 | * |
||||
214 | * @param $array |
||||
215 | * |
||||
216 | * @return string |
||||
217 | */ |
||||
218 | public function encodeArrayForUrl($array) |
||||
219 | { |
||||
220 | return ClickthroughHelper::encodeArrayForUrl((array) $array); |
||||
221 | } |
||||
222 | |||||
223 | /** |
||||
224 | * Decode a string appended to URL into an array. |
||||
225 | * |
||||
226 | * @param $string |
||||
227 | * @param bool $urlDecode |
||||
228 | * |
||||
229 | * @return mixed |
||||
230 | */ |
||||
231 | public function decodeArrayFromUrl($string, $urlDecode = true) |
||||
232 | { |
||||
233 | return ClickthroughHelper::decodeArrayFromUrl($string, $urlDecode); |
||||
234 | } |
||||
235 | |||||
236 | /** |
||||
237 | * @param $route |
||||
238 | * @param array $routeParams |
||||
239 | * @param bool $absolute |
||||
240 | * @param array $clickthrough |
||||
241 | * @param array $utmTags |
||||
242 | * |
||||
243 | * @return string |
||||
244 | */ |
||||
245 | public function buildUrl($route, $routeParams = [], $absolute = true, $clickthrough = [], $utmTags = []) |
||||
0 ignored issues
–
show
The parameter
$utmTags is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||
246 | { |
||||
247 | $referenceType = ($absolute) ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH; |
||||
248 | $url = $this->router->generate($route, $routeParams, $referenceType); |
||||
249 | |||||
250 | return $url.((!empty($clickthrough)) ? '?ct='.$this->encodeArrayForUrl($clickthrough) : ''); |
||||
251 | } |
||||
252 | |||||
253 | /** |
||||
254 | * Retrieve entity based on id/alias slugs. |
||||
255 | * |
||||
256 | * @param string $slug |
||||
257 | * |
||||
258 | * @return object|bool |
||||
259 | */ |
||||
260 | public function getEntityBySlugs($slug) |
||||
261 | { |
||||
262 | $slugs = explode('/', $slug); |
||||
263 | $idSlug = ''; |
||||
264 | $category = null; |
||||
265 | $lang = null; |
||||
266 | |||||
267 | $slugCount = count($slugs); |
||||
268 | $locales = Intl::getLocaleBundle()->getLocaleNames(); |
||||
269 | |||||
270 | switch (true) { |
||||
271 | case 3 === $slugCount: |
||||
272 | list($lang, $category, $idSlug) = $slugs; |
||||
273 | |||||
274 | break; |
||||
275 | |||||
276 | case 2 === $slugCount: |
||||
277 | list($category, $idSlug) = $slugs; |
||||
278 | |||||
279 | // Check if the first slug is actually a locale |
||||
280 | if (isset($locales[$category])) { |
||||
281 | $lang = $category; |
||||
282 | $category = null; |
||||
283 | } |
||||
284 | |||||
285 | break; |
||||
286 | |||||
287 | case 1 === $slugCount: |
||||
288 | $idSlug = $slugs[0]; |
||||
289 | |||||
290 | break; |
||||
291 | } |
||||
292 | |||||
293 | // Check for uncategorized |
||||
294 | if ($this->translator->trans('mautic.core.url.uncategorized') == $category) { |
||||
295 | $category = null; |
||||
296 | } |
||||
297 | |||||
298 | if ($lang && !isset($locales[$lang])) { |
||||
299 | // Language doesn't exist so return false |
||||
300 | |||||
301 | return false; |
||||
302 | } |
||||
303 | |||||
304 | $entity = false; |
||||
305 | if (false !== strpos($idSlug, ':')) { |
||||
306 | $parts = explode(':', $idSlug); |
||||
307 | if (2 == count($parts)) { |
||||
308 | $entity = $this->getEntity($parts[0]); |
||||
309 | } |
||||
310 | } else { |
||||
311 | $entity = $this->getRepository()->findOneBySlugs($idSlug, $category, $lang); |
||||
312 | } |
||||
313 | |||||
314 | if ($entity && $lang) { |
||||
315 | // Set the slug used to fetch the entity |
||||
316 | $entity->languageSlug = $lang; |
||||
317 | } |
||||
318 | |||||
319 | return $entity; |
||||
320 | } |
||||
321 | |||||
322 | /** |
||||
323 | * @param $alias |
||||
324 | * |
||||
325 | * @return object|null |
||||
326 | */ |
||||
327 | public function getEntityByAlias($alias, $categoryAlias = null, $lang = null) |
||||
328 | { |
||||
329 | } |
||||
330 | } |
||||
331 |