1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - Calendar App |
4
|
|
|
* |
5
|
|
|
* @author Georg Ehrke |
6
|
|
|
* @copyright 2014 Georg Ehrke <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 3 of the License, or any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public |
19
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
namespace OCA\Calendar\Db; |
23
|
|
|
|
24
|
|
|
use Sabre\VObject\Component\VCalendar; |
25
|
|
|
use Sabre\VObject\Component\VEvent; |
26
|
|
|
use Sabre\VObject\Component\VJournal; |
27
|
|
|
use Sabre\VObject\Component\VTodo; |
28
|
|
|
use OCA\Calendar\IObjectCollection; |
29
|
|
|
use OCA\Calendar\IObject; |
30
|
|
|
use OCA\Calendar\ITimezone; |
31
|
|
|
|
32
|
|
|
use DateTime; |
33
|
|
|
|
34
|
|
|
class ObjectCollection extends Collection implements IObjectCollection { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function inPeriod(DateTime $start, DateTime $end) { |
|
|
|
|
40
|
|
|
$objectsInPeriod = new ObjectCollection(); |
41
|
|
|
|
42
|
|
|
foreach ($this->objects as $object) { |
43
|
|
|
if ($object->getRepeating() === true) { |
44
|
|
|
$objectsInPeriod->add($object); |
45
|
|
|
} else { |
46
|
|
|
if ($object->isInTimeRange($start, $end)) { |
47
|
|
|
$objectsInPeriod->add($object); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $objectsInPeriod; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function expand(DateTime $start, DateTime $end) { |
|
|
|
|
60
|
|
|
$expandedObjects = new ObjectCollection(); |
61
|
|
|
|
62
|
|
|
foreach ($this->objects as $object) { |
63
|
|
|
if ($object->getRepeating() === true) { |
64
|
|
|
$vobject = $object->getVObject(); |
65
|
|
|
$vobject->expand($start, $end); |
66
|
|
|
$object->setVObject($vobject); |
67
|
|
|
$expandedObjects->add($object); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $expandedObjects; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function ownedBy($userId) { |
79
|
|
|
$objectsOwnedBy = new ObjectCollection(); |
80
|
|
|
|
81
|
|
|
foreach ($this->objects as $object) { |
82
|
|
|
if ($object->getCalendar() instanceof Calendar && |
83
|
|
|
$object->getCalendar()->getOwnerId() === $userId) { |
84
|
|
|
$objectsOwnedBy->add($object); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $objectsOwnedBy; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function ofType($type) { |
96
|
|
|
$objectsOfType = new ObjectCollection(); |
97
|
|
|
|
98
|
|
|
foreach ($this->objects as $object) { |
99
|
|
|
if ($object->getType() & $type) { |
100
|
|
|
$objectsOfType->add($object); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $objectsOfType; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function addGlobalIds(array $idTable) { |
112
|
|
|
foreach ($this->objects as $object) { |
113
|
|
|
$uri = $object->getUri(); |
114
|
|
|
if (isset($idTable[$uri])) { |
115
|
|
|
$object->setId($idTable[$uri]); |
116
|
|
|
} else { |
117
|
|
|
//TODO how to handle this case |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function getVObject() { |
127
|
|
|
$vCalendar = new VCalendar(); |
128
|
|
|
|
129
|
|
|
foreach($this->objects as &$object) { |
130
|
|
|
if (!($object instanceof IObject) && !($object instanceof ITimezone)) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$vObject = $object->getVObject(); |
135
|
|
|
foreach($vObject->children() as $child) { |
136
|
|
|
if ($child instanceof VEvent || |
|
|
|
|
137
|
|
|
$child instanceof VJournal || |
|
|
|
|
138
|
|
|
$child instanceof VTodo || |
|
|
|
|
139
|
|
|
$child->name === 'VTIMEZONE') { |
140
|
|
|
$vCalendar->add($child); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $vCalendar; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function getVObjects() { |
153
|
|
|
$vObjects = array(); |
154
|
|
|
|
155
|
|
|
foreach($this->objects as &$object) { |
156
|
|
|
if (!($object instanceof IObject) && !($object instanceof ITimezone)) { |
157
|
|
|
continue; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$vObjects[] = $object->getVObject(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $vObjects; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* {@inheritDoc} |
169
|
|
|
*/ |
170
|
|
|
public function listAll($type=ObjectType::ALL) { |
171
|
|
|
$objects = $this->ofType($type); |
172
|
|
|
|
173
|
|
|
$list = []; |
174
|
|
|
foreach($objects as $object) { |
175
|
|
|
/** @var IObject $object */ |
176
|
|
|
$list[] = $object->getUri(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $list; |
180
|
|
|
} |
181
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.