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
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Sync |
29
|
|
|
* @package plunner\Console\Commands |
30
|
|
|
* @author Claudio Cardinale <[email protected]> |
31
|
|
|
* @copyright 2015 Claudio Cardinale |
32
|
|
|
* @version 1.0.0 |
33
|
|
|
*/ |
34
|
|
|
class Sync |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Caldav |
38
|
|
|
*/ |
39
|
|
|
private $calendar; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sync constructor. |
43
|
|
|
* @param Caldav $calendar |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct(Caldav $calendar) |
46
|
|
|
{ |
47
|
4 |
|
$this->calendar = $calendar; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Caldav |
52
|
|
|
*/ |
53
|
2 |
|
public function getCalendar() |
54
|
|
|
{ |
55
|
2 |
|
return $this->calendar; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* perform the sync |
60
|
|
|
*/ |
61
|
4 |
|
public function sync() |
62
|
|
|
{ |
63
|
4 |
|
$this->syncToTimeSlots(); |
64
|
4 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array|\it\thecsea\caldav_client_adapter\EventInterface[] |
68
|
|
|
* @throws \it\thecsea\caldav_client_adapter\CaldavException |
69
|
|
|
* @thorws \Illuminate\Contracts\Encryption\DecryptException |
70
|
|
|
*/ |
71
|
4 |
|
private function getEvents() |
72
|
|
|
{ |
73
|
4 |
|
$caldavClient = new SimpleCaldavAdapter(); |
74
|
4 |
|
$caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password)); |
75
|
|
|
$calendars = $caldavClient->findCalendars(); |
76
|
|
|
$caldavClient->setCalendar($calendars[$this->calendar->calendar_name]); |
77
|
|
|
/** |
78
|
|
|
* 26 hours before to avoid tiemezone problems and dst problems |
79
|
|
|
* 30 days after |
80
|
|
|
*/ |
81
|
|
|
return $caldavClient->getEvents(date('Ymd\THis\Z', time()-93600), date('Ymd\THis\Z', time()+2592000)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
4 |
|
private function syncToTimeSlots() |
88
|
|
|
{ |
89
|
|
|
try |
90
|
|
|
{ |
91
|
4 |
|
$events = $this->getEvents(); |
92
|
4 |
|
}catch (\it\thecsea\caldav_client_adapter\CaldavException $e) |
93
|
|
|
{ |
94
|
4 |
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
95
|
4 |
|
return ; |
96
|
|
|
}catch(\Illuminate\Contracts\Encryption\DecryptException $e){ |
97
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
98
|
|
|
return ; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var $calendarMain \plunner\Calendar |
103
|
|
|
*/ |
104
|
|
|
$calendarMain = $this->calendar->Calendar; |
105
|
|
|
|
106
|
|
|
//remove old timeslots |
107
|
|
|
$calendarMain->timeslots()->delete(); |
108
|
|
|
foreach($events as $event){ |
109
|
|
|
if(!($event = $this->parseEvent($event))) |
110
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event')); |
111
|
|
|
else |
112
|
|
|
$calendarMain->timeslots()->create($event); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param EventInterface $event |
118
|
|
|
* @return \DateTime[]|null |
119
|
|
|
*/ |
120
|
|
|
private function parseEvent(EventInterface $event) |
121
|
|
|
{ |
122
|
|
|
$pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m"; |
123
|
|
|
if(preg_match_all($pattern, $event->getData(), $matches)){ |
124
|
|
|
if(!isset($matches[4]) || count($matches[4]) != 2) |
125
|
|
|
return null; |
126
|
|
|
$ret = []; |
127
|
|
|
if($tmp = $this->parseDate($matches[4][0])) |
128
|
|
|
$ret['time_start'] = $tmp; |
129
|
|
|
else |
130
|
|
|
return null; |
131
|
|
|
if($tmp = $this->parseDate($matches[4][1])) |
132
|
|
|
$ret['time_end'] = $tmp; |
133
|
|
|
else |
134
|
|
|
return null; |
135
|
|
|
return $ret; |
136
|
|
|
} |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param String $date |
142
|
|
|
* @return \DateTime|null|false |
143
|
|
|
*/ |
144
|
|
|
private function parseDate($date) |
145
|
|
|
{ |
146
|
|
|
$pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m"; |
147
|
|
|
if(preg_match_all($pattern, $date, $matches)){ |
148
|
|
|
if($matches[1][0] == 'TZID=') |
149
|
|
|
{ |
150
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0])); |
151
|
|
|
}else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE') |
152
|
|
|
{ |
153
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
} |