forked from visol-forks/ext-roq_newsevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventRepository.php
217 lines (190 loc) · 8.43 KB
/
EventRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Copyright (c) 2012, ROQUIN B.V. (C), http://www.roquin.nl
*
* @author: J. de Groot
* @file: EventRepository.php
* @description: News event Repository, extending functionality from the News Repository
*/
/**
* @package TYPO3
* @subpackage roq_newsevent
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
*/
class Tx_RoqNewsevent_Domain_Repository_EventRepository extends GeorgRinger\News\Domain\Repository\NewsRepository {
/**
* Returns the constraint to determine if a news event is active or not (archived)
*
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
* @return Tx_Extbase_Persistence_QOM_Constraint $constraint
*/
protected function createIsActiveConstraint(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
/** @var $constraint Tx_Extbase_Persistence_QOM_Constraint */
$constraint = NULL;
$timestamp = time(); // + date('Z');
$constraint = $query->logicalOr(
// future events:
$query->greaterThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime', $timestamp),
// current multiple day events:
$query->logicalAnd(
$query->lessThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime', $timestamp),
$query->greaterThan('tx_roqnewsevent_enddate + tx_roqnewsevent_endtime', $timestamp)
),
// current single day events:
$query->logicalAnd(
$query->lessThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime', $timestamp),
$query->greaterThan('tx_roqnewsevent_startdate + tx_roqnewsevent_endtime', $timestamp),
$query->equals('tx_roqnewsevent_enddate', 0)
),
// current single day event without time:
$query->logicalAnd(
$query->greaterThan('tx_roqnewsevent_startdate + 86399', $timestamp),
$query->equals('tx_roqnewsevent_starttime', 0),
$query->equals('tx_roqnewsevent_enddate', 0),
$query->equals('tx_roqnewsevent_endtime', 0)
)
);
return $constraint;
}
/**
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
* @param \GeorgRinger\News\Domain\Model\DemandInterface $demand
* @return array
* @throws InvalidArgumentException
* @throws Exception
*/
protected function createConstraintsFromDemand(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query, \GeorgRinger\News\Domain\Model\DemandInterface $demand) {
$constraints = array();
if ($demand->getCategories() && $demand->getCategories() !== '0') {
$constraints[] = $this->createCategoryConstraint(
$query,
$demand->getCategories(),
$demand->getCategoryConjunction(),
$demand->getIncludeSubCategories()
);
}
if ($demand->getAuthor()) {
$constraints[] = $query->equals('author', $demand->getAuthor());
}
// archived
if ($demand->getArchiveRestriction() == 'archived') {
$constraints[] = $query->logicalNot($this->createIsActiveConstraint($query));
// non-archived (active)
} elseif ($demand->getArchiveRestriction() == 'active') {
$constraints[] = $this->createIsActiveConstraint($query);
}
// Time restriction greater than or equal
$timeRestrictionField = $demand->getDateField();
$timeRestrictionField = (empty($timeRestrictionField)) ? 'datetime' : $timeRestrictionField;
if ($demand->getTimeRestriction()) {
$timeLimit = 0;
// integer = timestamp
if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($demand->getTimeRestriction())) {
$timeLimit = $GLOBALS['EXEC_TIME'] - $demand->getTimeRestriction();
} else {
// try to check strtotime
$timeFromString = strtotime($demand->getTimeRestriction());
if ($timeFromString) {
$timeLimit = $timeFromString;
} else {
throw new Exception('Time limit Low could not be resolved to an integer. Given was: ' . htmlspecialchars($timeLimit));
}
}
$constraints[] = $query->greaterThanOrEqual(
$timeRestrictionField,
$timeLimit
);
}
// Time restriction less than or equal
if ($demand->getTimeRestrictionHigh()) {
$timeLimit = 0;
// integer = timestamp
if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($demand->getTimeRestrictionHigh())) {
$timeLimit = $GLOBALS['EXEC_TIME'] + $demand->getTimeRestrictionHigh();
} else {
// try to check strtotime
$timeFromString = strtotime($demand->getTimeRestrictionHigh());
if ($timeFromString) {
$timeLimit = $timeFromString;
} else {
throw new Exception('Time limit High could not be resolved to an integer. Given was: ' . htmlspecialchars($timeLimit));
}
}
$constraints[] = $query->lessThanOrEqual(
$timeRestrictionField,
$timeLimit
);
}
// top news
if ($demand->getTopNewsRestriction() == 1) {
$constraints[] = $query->equals('istopnews', 1);
} elseif ($demand->getTopNewsRestriction() == 2) {
$constraints[] = $query->equals('istopnews', 0);
}
// storage page
if ($demand->getStoragePage() != 0) {
$pidList = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $demand->getStoragePage(), TRUE);
$constraints[] = $query->in('pid', $pidList);
}
// month & year OR year only
if ($demand->getYear() > 0) {
if (is_null($demand->getDateField())) {
throw new InvalidArgumentException('No Datefield is set, therefore no Datemenu is possible!');
}
if ($demand->getMonth() > 0) {
if ($demand->getDay() > 0) {
$begin = mktime(0, 0, 0, $demand->getMonth(), $demand->getDay(), $demand->getYear());
$end = mktime(23, 59, 59, $demand->getMonth(), $demand->getDay(), $demand->getYear());
} else {
$begin = mktime(0, 0, 0, $demand->getMonth(), 1, $demand->getYear());
$end = mktime(23, 59, 59, ($demand->getMonth() + 1), 0, $demand->getYear());
}
} else {
$begin = mktime(0, 0, 0, 1, 1, $demand->getYear());
$end = mktime(23, 59, 59, 12, 31, $demand->getYear());
}
$constraints[] = $query->logicalAnd(
$query->greaterThanOrEqual($demand->getDateField(), $begin),
$query->lessThanOrEqual($demand->getDateField(), $end)
);
}
// Tags
$tags = $demand->getTags();
if ($tags) {
$tagList = explode(',', $tags);
foreach ($tagList as $singleTag) {
$constraints[] = $query->contains('tags', $singleTag);
}
}
// Search
$searchConstraints = $this->getSearchConstraints($query, $demand);
if (!empty($searchConstraints)) {
$constraints[] = $query->logicalAnd($searchConstraints);
}
// Exclude already displayed
if ($demand->getExcludeAlreadyDisplayedNews() && isset($GLOBALS['EXT']['news']['alreadyDisplayed']) && !empty($GLOBALS['EXT']['news']['alreadyDisplayed'])) {
$constraints[] = $query->logicalNot(
$query->in(
'uid',
$GLOBALS['EXT']['news']['alreadyDisplayed']
)
);
}
// events only
$constraints[] = $query->logicalAnd($query->equals('tx_roqnewsevent_is_event', 1));
// the event must have an event start date
$constraints[] = $query->logicalAnd(
$query->logicalNot(
$query->equals('tx_roqnewsevent_startdate', 0)
)
);
// Clean not used constraints
foreach($constraints as $key => $value) {
if (is_null($value)) {
unset($constraints[$key]);
}
}
return $constraints;
}
}
?>