|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Repository; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\ORM\EntityRepository; |
|
12
|
|
|
use AnimeDb\Bundle\AppBundle\Entity\Notice as NoticeEntity; |
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
14
|
|
|
|
|
15
|
|
|
class Notice extends EntityRepository |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
const SEE_LATER_INTERVAL = 3600; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return NoticeEntity|null |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getFirstShow() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->getEntityManager()->createQuery(' |
|
28
|
|
|
SELECT |
|
29
|
|
|
n |
|
30
|
|
|
FROM |
|
31
|
|
|
AnimeDbAppBundle:Notice n |
|
32
|
|
|
WHERE |
|
33
|
|
|
n.status != :closed AND |
|
34
|
|
|
n.date_start <= :time AND |
|
35
|
|
|
(n.date_closed IS NULL OR n.date_closed >= :time) |
|
36
|
|
|
ORDER BY |
|
37
|
|
|
n.date_created, n.id ASC |
|
38
|
|
|
') |
|
39
|
|
|
->setMaxResults(1) |
|
40
|
|
|
->setParameter('closed', NoticeEntity::STATUS_CLOSED) |
|
41
|
|
|
->setParameter('time', date('Y-m-d H:i:s')) |
|
42
|
|
|
->getOneOrNullResult(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $limit |
|
47
|
|
|
* @param int $offset |
|
48
|
|
|
* |
|
49
|
|
|
* @return NoticeEntity[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getList($limit, $offset = 0) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getEntityManager()->createQuery(' |
|
54
|
|
|
SELECT |
|
55
|
|
|
n |
|
56
|
|
|
FROM |
|
57
|
|
|
AnimeDbAppBundle:Notice n |
|
58
|
|
|
ORDER BY |
|
59
|
|
|
n.date_created DESC |
|
60
|
|
|
') |
|
61
|
|
|
->setFirstResult($offset) |
|
62
|
|
|
->setMaxResults($limit) |
|
63
|
|
|
->getResult(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
|
|
public function count() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getEntityManager()->createQuery(' |
|
72
|
|
|
SELECT |
|
73
|
|
|
COUNT(n) |
|
74
|
|
|
FROM |
|
75
|
|
|
AnimeDbAppBundle:Notice n |
|
76
|
|
|
')->getSingleScalarResult(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function seeLater() |
|
80
|
|
|
{ |
|
81
|
|
|
$time = time(); |
|
82
|
|
|
$start = date('Y-m-d H:i:s', $time + self::SEE_LATER_INTERVAL); |
|
83
|
|
|
$time = date('Y-m-d H:i:s', $time); |
|
84
|
|
|
|
|
85
|
|
|
// not shown notice |
|
86
|
|
|
$this->getEntityManager() |
|
87
|
|
|
->createQuery(' |
|
88
|
|
|
UPDATE |
|
89
|
|
|
AnimeDbAppBundle:Notice n |
|
90
|
|
|
SET |
|
91
|
|
|
n.date_start = :start |
|
92
|
|
|
WHERE |
|
93
|
|
|
n.status != :closed AND |
|
94
|
|
|
n.date_closed IS NULL |
|
95
|
|
|
') |
|
96
|
|
|
->setParameter('start', $start) |
|
97
|
|
|
->setParameter('closed', NoticeEntity::STATUS_CLOSED) |
|
98
|
|
|
->execute(); |
|
99
|
|
|
|
|
100
|
|
|
// rigidly set closing date |
|
101
|
|
|
$this->getEntityManager() |
|
102
|
|
|
->createQuery(' |
|
103
|
|
|
UPDATE |
|
104
|
|
|
AnimeDbAppBundle:Notice n |
|
105
|
|
|
SET |
|
106
|
|
|
n.date_start = :start, |
|
107
|
|
|
n.date_closed = DATETIME(n.date_closed, :interval) |
|
108
|
|
|
WHERE |
|
109
|
|
|
n.status != :closed AND |
|
110
|
|
|
n.date_closed IS NOT NULL AND |
|
111
|
|
|
n.date_closed > :time |
|
112
|
|
|
') |
|
113
|
|
|
->setParameter('start', $start) |
|
114
|
|
|
->setParameter('interval', '+'.self::SEE_LATER_INTERVAL.' seconds') |
|
115
|
|
|
->setParameter('closed', NoticeEntity::STATUS_CLOSED) |
|
116
|
|
|
->setParameter('time', $time) |
|
117
|
|
|
->execute(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $notices |
|
122
|
|
|
*/ |
|
123
|
|
View Code Duplication |
public function remove(array $notices) |
|
124
|
|
|
{ |
|
125
|
|
|
$ids = []; |
|
126
|
|
|
/* @var $notice NoticeEntity */ |
|
127
|
|
|
foreach ($notices as $notice) { |
|
128
|
|
|
$ids[] = $notice->getId(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->_em |
|
132
|
|
|
->createQuery(' |
|
133
|
|
|
DELETE FROM |
|
134
|
|
|
AnimeDbAppBundle:Notice n |
|
135
|
|
|
WHERE |
|
136
|
|
|
n.id IN (:ids)' |
|
137
|
|
|
) |
|
138
|
|
|
->setParameter('ids', $ids) |
|
139
|
|
|
->execute(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param array $notices |
|
144
|
|
|
* @param string $status |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function setStatus(array $notices, $status) |
|
147
|
|
|
{ |
|
148
|
|
|
$ids = []; |
|
149
|
|
|
/* @var $notice NoticeEntity */ |
|
150
|
|
|
foreach ($notices as $notice) { |
|
151
|
|
|
$ids[] = $notice->getId(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->getEntityManager() |
|
155
|
|
|
->createQuery(' |
|
156
|
|
|
UPDATE |
|
157
|
|
|
AnimeDbAppBundle:Notice n |
|
158
|
|
|
SET |
|
159
|
|
|
n.status = :status |
|
160
|
|
|
WHERE |
|
161
|
|
|
n.id IN (:ids) |
|
162
|
|
|
') |
|
163
|
|
|
->setParameter('ids', $ids) |
|
164
|
|
|
->setParameter('status', $status) |
|
165
|
|
|
->execute(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param int $status |
|
170
|
|
|
* @param string $type |
|
171
|
|
|
* |
|
172
|
|
|
* @return QueryBuilder |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getFilteredQuery($status, $type) |
|
175
|
|
|
{ |
|
176
|
|
|
$query = $this->createQueryBuilder('n'); |
|
177
|
|
|
|
|
178
|
|
|
if (is_int($status) && in_array($status, NoticeEntity::getStatuses())) { |
|
179
|
|
|
$query |
|
180
|
|
|
->where('n.status = :status') |
|
181
|
|
|
->setParameter('status', $status); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if ($type) { |
|
185
|
|
|
$query |
|
186
|
|
|
->andWhere('n.type = :type') |
|
187
|
|
|
->setParameter('type', $type); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $query; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|