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