Initial commit
[TYPO3CMS/Extensions/roq_newsevent.git] / Classes / Domain / Repository / EventRepository.php
1 <?php
2
3 /**
4 * Copyright (c) 2012, ROQUIN B.V. (C), http://www.roquin.nl
5 *
6 * @author: J. de Groot
7 * @file: EventRepository.php
8 * @description: News event Repository, extending functionality from the News Repository
9 */
10
11 /**
12 * @package TYPO3
13 * @subpackage roq_newsevent
14 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
15 */
16 class Tx_RoqNewsevent_Domain_Repository_EventRepository extends Tx_News_Domain_Repository_NewsRepository {
17
18 /**
19 * Returns the constraint to determine if a news event is active or not (archived)
20 *
21 * @param Tx_Extbase_Persistence_QueryInterface $query
22 * @return Tx_Extbase_Persistence_QOM_Constraint $constraint
23 */
24 protected function createIsActiveConstraint(Tx_Extbase_Persistence_QueryInterface $query) {
25 /** @var $constraint Tx_Extbase_Persistence_QOM_Constraint */
26 $constraint = null;
27 $timestamp = time(); // + date('Z');
28
29 $constraint = $query->logicalOr(
30 // future events:
31 $query->greaterThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime',$timestamp),
32 // current multiple day events:
33 $query->logicalAnd(
34 $query->lessThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime',$timestamp),
35 $query->greaterThan('tx_roqnewsevent_enddate + tx_roqnewsevent_endtime',$timestamp)
36 ),
37 // current single day events:
38 $query->logicalAnd(
39 $query->lessThan('tx_roqnewsevent_startdate + tx_roqnewsevent_starttime',$timestamp),
40 $query->greaterThan('tx_roqnewsevent_startdate + tx_roqnewsevent_endtime',$timestamp),
41 $query->equals('tx_roqnewsevent_enddate',0)
42 )
43 );
44
45 return $constraint;
46 }
47
48 /**
49 * Returns an array of constraints created from a given demand object.
50 *
51 * @param Tx_Extbase_Persistence_QueryInterface $query
52 * @param Tx_News_Domain_Model_DemandInterface $demand
53 * @return array<Tx_Extbase_Persistence_QOM_Constraint>
54 */
55 protected function createConstraintsFromDemand(Tx_Extbase_Persistence_QueryInterface $query, Tx_News_Domain_Model_DemandInterface $demand) {
56 $constraints = array();
57
58 if ($demand->getCategories() && $demand->getCategories() !== '0') {
59 $constraints[] = $this->createCategoryConstraint(
60 $query,
61 $demand->getCategories(),
62 $demand->getCategoryConjunction(),
63 $demand->getIncludeSubCategories()
64 );
65 }
66
67 // archived
68 if ($demand->getArchiveRestriction() == 'archived') {
69 $constraints[] = $query->logicalNot($this->createIsActiveConstraint($query));
70 // non-archived (active)
71 } elseif ($demand->getArchiveRestriction() == 'active') {
72 $constraints[] = $this->createIsActiveConstraint($query);
73 }
74
75 // top news
76 if ($demand->getTopNewsRestriction() == 1) {
77 $constraints[] = $query->equals('istopnews', 1);
78 } elseif ($demand->getTopNewsRestriction() == 2) {
79 $constraints[] = $query->equals('istopnews', 0);
80 }
81
82 // storage page
83 if ($demand->getStoragePage() != 0) {
84 $pidList = t3lib_div::intExplode(',', $demand->getStoragePage(), TRUE);
85 $constraints[] = $query->in('pid', $pidList);
86 }
87
88 // month & year OR year only
89 if ($demand->getYear() > 0) {
90 if (is_null($demand->getDateField())) {
91 throw new InvalidArgumentException('No Datefield is set, therefore no Datemenu is possible!');
92 }
93 if ($demand->getMonth() > 0) {
94 if ($demand->getDay() > 0) {
95 $begin = mktime(0, 0, 0, $demand->getMonth(), $demand->getDay(), $demand->getYear());
96 $end = mktime(23, 59, 59, $demand->getMonth(), $demand->getDay(), $demand->getYear());
97 } else {
98 $begin = mktime(0, 0, 0, $demand->getMonth(), 1, $demand->getYear());
99 $end = mktime(23, 59, 59, ($demand->getMonth() + 1), 0, $demand->getYear());
100 }
101 } else {
102 $begin = mktime(0, 0, 0, 1, 1, $demand->getYear());
103 $end = mktime(23, 59, 59, 12, 31, $demand->getYear());
104 }
105 $constraints[] = $query->logicalAnd(
106 $query->greaterThanOrEqual($demand->getDateField(), $begin),
107 $query->lessThanOrEqual($demand->getDateField(), $end)
108 );
109 }
110
111 // Tags
112 if ($demand->getTags()) {
113 $constraints[] = $query->contains('tags', $demand->getTags());
114 }
115
116 // dummy records, used for UnitTests only!
117 if ($demand->getIsDummyRecord()) {
118 $constraints[] = $query->equals('isDummyRecord', 1);
119 }
120
121 // Search
122 if ($demand->getSearch() !== NULL) {
123 /* @var $searchObject Tx_News_Domain_Model_Dto_Search */
124 $searchObject = $demand->getSearch();
125
126 $searchFields = t3lib_div::trimExplode(',', $searchObject->getFields(), TRUE);
127 $searchConstraints = array();
128
129 if (count($searchFields) === 0) {
130 throw new UnexpectedValueException('No search fields defined', 1318497755);
131 }
132
133 $searchSubject = $searchObject->getSubject();
134 foreach($searchFields as $field) {
135 if (!empty($searchSubject)) {
136 $searchConstraints[] = $query->like($field, '%' . $searchSubject . '%');
137 }
138 }
139 $constraints[] = $query->logicalOr($searchConstraints);
140 }
141
142 // Clean not used constraints
143 foreach($constraints as $key => $value) {
144 if (is_null($value)) {
145 unset($constraints[$key]);
146 }
147 }
148
149 return $constraints;
150 }
151 }
152 ?>