1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Claudio Cardinale <[email protected]> |
5
|
|
|
* Date: 03/12/15 |
6
|
|
|
* Time: 2.33 |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program; if not, write to the Free Software |
17
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace plunner\Console\Commands\SyncCaldav; |
21
|
|
|
|
22
|
|
|
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter; |
23
|
|
|
use \it\thecsea\caldav_client_adapter\EventInterface; |
24
|
|
|
use plunner\Caldav; |
25
|
|
|
use plunner\Events\CaldavErrorEvent; |
26
|
|
|
use plunner\Events\CaldavSyncOkEvent; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Sync |
30
|
|
|
* @package plunner\Console\Commands |
31
|
|
|
* @author Claudio Cardinale <[email protected]> |
32
|
|
|
* @copyright 2015 Claudio Cardinale |
33
|
|
|
* @version 1.0.0 |
34
|
|
|
*/ |
35
|
|
|
class Sync |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var Caldav |
39
|
|
|
*/ |
40
|
|
|
private $calendar; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sync constructor. |
44
|
|
|
* @param Caldav $calendar |
45
|
|
|
*/ |
46
|
4 |
|
public function __construct(Caldav $calendar) |
47
|
2 |
|
{ |
48
|
4 |
|
$this->calendar = $calendar; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Caldav |
53
|
|
|
*/ |
54
|
2 |
|
public function getCalendar() |
55
|
2 |
|
{ |
56
|
|
|
return $this->calendar; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* perform the sync |
61
|
|
|
*/ |
62
|
4 |
|
public function sync() |
63
|
|
|
{ |
64
|
4 |
|
$this->syncToTimeSlots(); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array|\it\thecsea\caldav_client_adapter\EventInterface[] |
69
|
|
|
* @throws \it\thecsea\caldav_client_adapter\CaldavException |
70
|
|
|
* @thorws \Illuminate\Contracts\Encryption\DecryptException |
71
|
|
|
*/ |
72
|
4 |
|
private function getEvents() |
73
|
|
|
{ |
74
|
|
|
//TODO catch php errors like array key |
75
|
4 |
|
$caldavClient = new SimpleCaldavAdapter(); |
76
|
4 |
|
$caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password)); |
77
|
|
|
$calendars = $caldavClient->findCalendars(); |
78
|
|
|
$caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);//TODO error if the calendar name is wrong |
79
|
|
|
/** |
80
|
|
|
* 26 hours before to avoid tiemezone problems and dst problems |
81
|
|
|
* 30 days after |
82
|
|
|
*/ |
83
|
|
|
return $caldavClient->getEvents(date('Ymd\THis\Z', time()-93600), date('Ymd\THis\Z', time()+2592000)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
*/ |
89
|
4 |
|
private function syncToTimeSlots() |
90
|
|
|
{ |
91
|
|
|
try |
92
|
|
|
{ |
93
|
4 |
|
$events = $this->getEvents(); |
94
|
4 |
|
}catch (\it\thecsea\caldav_client_adapter\CaldavException $e) |
95
|
|
|
{ |
96
|
4 |
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
97
|
4 |
|
return ; |
98
|
|
|
}catch(\Illuminate\Contracts\Encryption\DecryptException $e){ |
99
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
100
|
|
|
return ; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var $calendarMain \plunner\Calendar |
105
|
|
|
*/ |
106
|
|
|
$calendarMain = $this->calendar->Calendar; |
107
|
|
|
|
108
|
|
|
//remove old timeslots |
109
|
|
|
$calendarMain->timeslots()->delete(); |
110
|
|
|
|
111
|
|
|
//insert new timeslots |
112
|
|
|
foreach($events as $event){ |
113
|
|
|
if(!($event = $this->parseEvent($event))) |
114
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event')); |
115
|
|
|
else |
116
|
|
|
$calendarMain->timeslots()->create($event); |
117
|
|
|
} |
118
|
|
|
\Event::fire(new CaldavSyncOkEvent($this->calendar)); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param EventInterface $event |
123
|
|
|
* @return \DateTime[]|null |
124
|
|
|
*/ |
125
|
|
|
private function parseEvent(EventInterface $event) |
126
|
|
|
{ |
127
|
|
|
$pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m"; |
128
|
|
|
if(preg_match_all($pattern, $event->getData(), $matches)){ |
129
|
|
|
if(!isset($matches[4]) || count($matches[4]) != 2) |
130
|
|
|
return null; |
131
|
|
|
$ret = []; |
132
|
|
|
if($tmp = $this->parseDate($matches[4][0])) |
133
|
|
|
$ret['time_start'] = $tmp; |
134
|
|
|
else |
135
|
|
|
return null; |
136
|
|
|
if($tmp = $this->parseDate($matches[4][1])) |
137
|
|
|
$ret['time_end'] = $tmp; |
138
|
|
|
else |
139
|
|
|
return null; |
140
|
|
|
return $ret; |
141
|
|
|
} |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param String $date |
147
|
|
|
* @return \DateTime|null|false |
148
|
|
|
*/ |
149
|
|
|
private function parseDate($date) |
150
|
|
|
{ |
151
|
|
|
$pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m"; |
152
|
|
|
if(preg_match_all($pattern, $date, $matches)){ |
153
|
|
|
if($matches[1][0] == 'TZID=') |
154
|
|
|
{ |
155
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0])); |
156
|
|
|
}else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE') |
157
|
|
|
{ |
158
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: