1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\CanvasICSSync\SyncIntoCanvas; |
4
|
|
|
|
5
|
|
|
class Schedule extends Syncable |
6
|
|
|
{ |
7
|
|
|
const ONCE = 'once'; |
8
|
|
|
const HOURLY = 'hourly'; |
9
|
|
|
const DAILY = 'daily'; |
10
|
|
|
const WEEKLY = 'weekly'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Unique identifier for this schedule |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
protected $id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Calendar associated with this schedule |
20
|
|
|
* @var Calendar |
21
|
|
|
*/ |
22
|
|
|
protected $calendar; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Schedule identifier |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $schedule; |
29
|
|
|
|
30
|
|
|
public function __construct(Calendar $calendar, $schedule, $id = false) |
31
|
|
|
{ |
32
|
|
|
if (empty($calendar)) { |
33
|
|
|
throw new Exception("Valid Calendar required"); |
34
|
|
|
} |
35
|
|
|
$this->calendar = $calendar; |
36
|
|
|
$this->schedule = (string) $schedule; |
37
|
|
|
if ($id !== false) { |
38
|
|
|
$this->id = intval($id); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getId() |
43
|
|
|
{ |
44
|
|
|
return $this->id; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getCalendar() |
48
|
|
|
{ |
49
|
|
|
return $this->calendar; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSchedule() |
53
|
|
|
{ |
54
|
|
|
return $this->schedule; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setSchedule($schedule) |
58
|
|
|
{ |
59
|
|
|
$this->schedule = (string) $schedule; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function save() |
63
|
|
|
{ |
64
|
|
|
$db = Syncable::getDatabase(); |
65
|
|
|
$select = $db->prepare( |
66
|
|
|
"SELECT * FROM `schedules` WHERE `id` = :id" |
67
|
|
|
); |
68
|
|
|
$update = $db->prepare( |
69
|
|
|
"UPDATE `schedules` |
70
|
|
|
SET |
71
|
|
|
`synced` = :synced, |
72
|
|
|
`schedule` = :schedule |
73
|
|
|
WHERE |
74
|
|
|
`id` = :id" |
75
|
|
|
); |
76
|
|
|
$insert = $db->prepare( |
77
|
|
|
"INSERT INTO `schedules` |
78
|
|
|
( |
79
|
|
|
`calendar`, |
80
|
|
|
`schedule`, |
81
|
|
|
`synced` |
82
|
|
|
) VALUES ( |
83
|
|
|
:calendar, |
84
|
|
|
:schedule, |
85
|
|
|
:synced |
86
|
|
|
)" |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$params = [ |
90
|
|
|
'id' => $this->getId(), |
91
|
|
|
'calendar' => $this->getCalendar()->getId(), |
92
|
|
|
'schedule' => $this->getSchedule(), |
93
|
|
|
'synced' => Syncable::getTimestamp() |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$select->execute($params); |
97
|
|
|
if (($select->fetch()) === false) { |
98
|
|
|
$insert->execute($params); |
99
|
|
|
} else { |
100
|
|
|
$update->execute($params); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function delete() |
105
|
|
|
{ |
106
|
|
|
$delete = Syncable::getDatabase()->prepare( |
107
|
|
|
"DELETE FROM `schedules` |
108
|
|
|
WHERE `id` = :id" |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$delete->execute($this->getId()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function load($id) |
115
|
|
|
{ |
116
|
|
|
$select = Syncable::getDatabase()->prepare( |
117
|
|
|
"SELECT * FROM `schedules` WHERE `id` = :id" |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$select->execute($id); |
121
|
|
|
if (($_schedule = $select->fetch()) !== false) { |
122
|
|
|
return new Schedule(Calendar::load($_schedule['calendar']), $_schedule['schedule'], $_schedule['id']); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: