|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\ThemeModule\Entity\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
|
15
|
|
|
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface; |
|
16
|
|
|
use Zikula\ThemeModule\Entity\ThemeEntity; |
|
17
|
|
|
|
|
18
|
|
|
class ThemeEntityRepository extends EntityRepository |
|
19
|
|
|
{ |
|
20
|
|
|
const STATE_ALL = 0; |
|
21
|
|
|
const STATE_ACTIVE = 1; |
|
22
|
|
|
const STATE_INACTIVE = 2; |
|
23
|
|
|
|
|
24
|
|
|
const TYPE_ALL = 0; |
|
25
|
|
|
const TYPE_XANTHIA3 = 3; |
|
26
|
|
|
|
|
27
|
|
|
const FILTER_ALL = 0; |
|
28
|
|
|
const FILTER_USER = 1; |
|
29
|
|
|
const FILTER_SYSTEM = 2; |
|
30
|
|
|
const FILTER_ADMIN = 3; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ZikulaHttpKernelInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $kernel; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ZikulaHttpKernelInterface $kernel |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setKernel(ZikulaHttpKernelInterface $kernel) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->kernel = $kernel; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return ZikulaHttpKernelInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private function getKernel() |
|
49
|
|
|
{ |
|
50
|
|
|
if (!$this->kernel instanceof ZikulaHttpKernelInterface) { |
|
51
|
|
|
// no need to translate this message as it will only be seen by developers. |
|
52
|
|
|
$message = 'The "kernel" attribute is NULL. ' |
|
53
|
|
|
. 'Did you retrieved this repository using `$doctrine->getRepository()`? ' |
|
54
|
|
|
. 'If so, retrieve it instead directly from the container'; |
|
55
|
|
|
throw new \LogicException($message); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->kernel; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function get($filter = self::FILTER_ALL, $state = self::STATE_ACTIVE, $type = self::TYPE_ALL) |
|
62
|
|
|
{ |
|
63
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder() |
|
64
|
|
|
->select('t') |
|
65
|
|
|
->from('ZikulaThemeModule:ThemeEntity', 't'); |
|
66
|
|
|
|
|
67
|
|
|
if ($state != self::STATE_ALL) { |
|
68
|
|
|
$qb->andWhere('t.state = :state') |
|
69
|
|
|
->setParameter('state', $state); |
|
70
|
|
|
} |
|
71
|
|
|
if ($type != self::TYPE_ALL) { |
|
72
|
|
|
$qb->andWhere('t.type = :type') |
|
73
|
|
|
->setParameter('type', $type); |
|
74
|
|
|
} |
|
75
|
|
|
switch ($filter) { |
|
76
|
|
|
case self::FILTER_USER: |
|
77
|
|
|
$qb->andWhere('t.user = 1'); |
|
78
|
|
|
break; |
|
79
|
|
|
case self::FILTER_SYSTEM: |
|
80
|
|
|
$qb->andWhere('t.system = 1'); |
|
81
|
|
|
break; |
|
82
|
|
|
case self::FILTER_ADMIN: |
|
83
|
|
|
$qb->andWhere('t.admin = 1'); |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$qb->orderBy('t.displayname', 'ASC'); |
|
88
|
|
|
$query = $qb->getQuery(); |
|
89
|
|
|
|
|
90
|
|
|
/** @var $result ThemeEntity[] */ |
|
91
|
|
|
$result = $query->getResult(); |
|
92
|
|
|
$themesArray = []; |
|
93
|
|
|
foreach ($result as $theme) { |
|
94
|
|
|
$themesArray[$theme->getName()]= $theme->toArray(); |
|
95
|
|
|
$kernel = $this->getKernel(); // allow to throw exception outside the try/catch block |
|
96
|
|
|
try { |
|
97
|
|
|
$themeBundle = $kernel->getTheme($theme['name']); |
|
98
|
|
|
} catch (\Exception $e) { |
|
99
|
|
|
$themeBundle = null; |
|
100
|
|
|
} |
|
101
|
|
|
$themesArray[$theme['name']]['vars'] = isset($themeBundle) ? $themeBundle->getThemeVars() : false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if (!$themesArray) { |
|
|
|
|
|
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $themesArray; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function removeAndFlush($entity) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->_em->remove($entity); |
|
114
|
|
|
$this->_em->flush(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function persistAndFlush($entity) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->_em->persist($entity); |
|
120
|
|
|
$this->_em->flush(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.