1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Cms\Service\Loader; |
4
|
|
|
|
5
|
|
|
use Charcoal\Loader\CollectionLoader; |
6
|
|
|
use DateTime; |
7
|
|
|
use DateTimeInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Event Loader |
11
|
|
|
*/ |
12
|
|
|
class EventLoader extends AbstractLoader |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Event prototype. |
16
|
|
|
* @var ModelInterface $proto |
17
|
|
|
*/ |
18
|
|
|
private $proto; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string $lifespan The lifespan of events. |
22
|
|
|
*/ |
23
|
|
|
private $lifespan; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* [proto description] |
27
|
|
|
* @return [type] [description] |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public function proto() |
30
|
|
|
{ |
31
|
|
|
if (!$this->proto) { |
32
|
|
|
$this->proto = $this->modelFactory()->get($this->objType()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->proto; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return CollectionLoader |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function all() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$proto = $this->proto(); |
44
|
|
|
$loader = $this->collectionLoader()->setModel($proto); |
45
|
|
|
$loader->addFilter('active', true); |
46
|
|
|
|
47
|
|
|
return $loader; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return CollectionLoader |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function published() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$now = new DateTime(); |
56
|
|
|
$loader = $this->all(); |
57
|
|
|
$loader->addFilter('publish_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ]) |
58
|
|
|
->addOrder('start_date', 'asc'); |
59
|
|
|
|
60
|
|
|
return $loader; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Fetch upcoming entries based on the lifespan or now. |
65
|
|
|
* @return CollectionLoader |
66
|
|
|
*/ |
67
|
|
|
public function upcoming() |
68
|
|
|
{ |
69
|
|
|
$lifespan = $this->lifespan(); |
70
|
|
|
$date = is_string($lifespan) ? new DateTime($lifespan) : new DateTime(); |
71
|
|
|
|
72
|
|
|
return $this->since($date); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Fetch upcoming entries based on the lifespan or now. |
77
|
|
|
* @return CollectionLoader |
78
|
|
|
*/ |
79
|
|
|
public function archive() |
80
|
|
|
{ |
81
|
|
|
$lifespan = $this->lifespan(); |
82
|
|
|
$date = is_string($lifespan) ? new DateTime($lifespan) : new DateTime(); |
83
|
|
|
|
84
|
|
|
return $this->to($date); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $date The news date to filter |
89
|
|
|
* [startDate, endDate] |
90
|
|
|
* DateTimeInterface |
91
|
|
|
* string. |
92
|
|
|
* @return CollectionLoader |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function since($date) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$date = $this->parseAsDate($date); |
97
|
|
|
$loader = $this->published(); |
98
|
|
|
$loader->addFilter('end_date', $date->format('Y-m-d H:i:s'), [ 'operator' => '>=' ]); |
99
|
|
|
|
100
|
|
|
return $loader; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed $date The news date to filter |
105
|
|
|
* [startDate, endDate] |
106
|
|
|
* DateTimeInterface |
107
|
|
|
* string. |
108
|
|
|
* @return CollectionLoader |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function to($date) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$date = $this->parseAsDate($date); |
113
|
|
|
$loader = $this->published(); |
114
|
|
|
$loader->addFilter('end_date', $date->format('Y-m-d H:i:s'), [ 'operator' => '<' ]); |
115
|
|
|
|
116
|
|
|
return $loader; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// ========================================================================== |
120
|
|
|
// GETTERS |
121
|
|
|
// ========================================================================== |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function lifespan() |
127
|
|
|
{ |
128
|
|
|
return $this->lifespan; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return object |
133
|
|
|
*/ |
134
|
|
|
public function objType() |
135
|
|
|
{ |
136
|
|
|
return $this->objType; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// ========================================================================== |
140
|
|
|
// SETTERS |
141
|
|
|
// ========================================================================== |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $lifespan The lifespan of events. |
145
|
|
|
* @return self Chainable |
146
|
|
|
*/ |
147
|
|
|
public function setLifespan($lifespan) |
148
|
|
|
{ |
149
|
|
|
$this->lifespan = $lifespan; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param object $objType The object type. |
156
|
|
|
* @return self Chainable |
157
|
|
|
*/ |
158
|
|
|
public function setObjType($objType) |
159
|
|
|
{ |
160
|
|
|
$this->objType = $objType; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// ========================================================================== |
166
|
|
|
// UTILS |
167
|
|
|
// ========================================================================== |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param mixed $date The date to convert. |
171
|
|
|
* @return DateTime |
172
|
|
|
*/ |
173
|
|
|
private function parseAsDate($date) |
174
|
|
|
{ |
175
|
|
|
if ($date instanceof DateTimeInterface) { |
176
|
|
|
return $date; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return new DateTime($date); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.