PluginProbe
ShiftController Employee Shift Scheduling / 4.9.78
ShiftController Employee Shift Scheduling v4.9.78
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / ical / view.php

view.php in ShiftController Employee Shift Scheduling 4.9.78, at sh4/ical/view.php

258 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 class SH4_Ical_View
3 {
4 public $self, $t, $ical, $auth, $appQuery, $shiftsQuery, $myUnique, $setting;
5
6 public function __construct(
7 HC3_Hooks $hooks,
8 HC3_Time $t,
9 HC3_Auth $auth,
10 HC3_Settings $setting,
11
12 SH4_App_Query $appQuery,
13 SH4_Shifts_Query $shiftsQuery,
14 SH4_Ical_Lib_iCalCreator $ical
15 )
16 {
17 $this->t = $t;
18 $this->ical = $ical;
19 $this->auth = $auth;
20
21 $this->appQuery = $hooks->wrap( $appQuery );
22 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
23 $this->self = $hooks->wrap( $this );
24 $this->setting = $hooks->wrap( $setting );
25
26 $this->myUnique = 'shiftcontroller';
27 }
28
29 public function renderDescription( SH4_Shifts_Model $shift )
30 {
31 $calendar = $shift->getCalendar();
32 $employee = $shift->getEmployee();
33
34 $calendarView = $calendar->getTitle();
35 $calendarDescription = $calendar->getDescription();
36 if( strlen($calendarDescription) ){
37 // $calendarView .= ' (' . $calendarDescription . ')';
38 $calendarView .= '\\n' . $calendarDescription;
39 }
40 $employeeView = $employee->getTitle();
41
42 $return = $employeeView . ' @ ' . $calendarView;
43 return $return;
44 }
45
46 public function makeIcalEvent( $shift, $user )
47 {
48 static $t2 = null;
49
50 if( null === $t2 ){
51 $t2 = clone $this->t;
52 $t2->setTimezone('UTC');
53 }
54
55 $event = new hc_vevent(); // initiate a new EVENT
56
57 $shiftId = $shift->getId();
58 $calendar = $shift->getCalendar();
59 $employee = $shift->getEmployee();
60 $start = $shift->getStart();
61 $end = $shift->getEnd();
62
63 $calendarView = $calendar->getTitle();
64 $employeeView = $employee->getTitle();
65
66 $event->setProperty( 'uid', 'obj-' . $shiftId . '-' . $this->myUnique );
67
68 $summary = $employeeView . ' @ ' . $calendarView;
69 $description = $this->self->renderDescription( $shift, $user );
70 $description = preg_replace( '/\R+/', '\\n', $description );
71 // $description = str_replace( "\n", " ", $description );
72 // $description = nl2br( $description );
73 $description = strip_tags( $description );
74
75 $isMultiDay = $shift->isMultiDay();
76
77 $this->t->setDateTimeDb( $start );
78 $t2->setTimestamp( $this->t->getTimestamp() );
79
80 if( $isMultiDay ){
81 // for multiday timezone may shift date so use utc
82 // $date = $t2->formatDateDb();
83 $date = $this->t->formatDateDb();
84 $event->setProperty( 'dtstart', $date, array('VALUE' => 'DATE'));
85 }
86 else {
87 list( $year, $month, $day, $hour, $min ) = $t2->getParts();
88 $event->setProperty( 'dtstart', $year, $month, $day, $hour, $min, 00, 'Z' ); // 24 dec 2006 19.30
89 }
90
91 $this->t->setDateTimeDb( $end );
92 $t2->setTimestamp( $this->t->getTimestamp() );
93
94 if( $isMultiDay ){
95 // for multiday timezone may shift date so use utc
96 // $date = $t2->formatDateDb();
97 $date = $this->t->formatDateDb();
98 $event->setProperty( 'dtend', $date, array('VALUE' => 'DATE'));
99 }
100 else {
101 list( $year, $month, $day, $hour, $min ) = $t2->getParts();
102 $event->setProperty( 'dtend', $year, $month, $day, $hour, $min, 00, 'Z' ); // 24 dec 2006 19.30
103 }
104
105 // $event->setProperty( 'location', $calendarView );
106 $event->setProperty( 'description', $description );
107 $event->setProperty( 'summary', $summary );
108
109 if( $shift->isPublished() ){
110 $event->setProperty( 'status', 'TENTATIVE' );
111 }
112 else {
113 $event->setProperty( 'status', 'CONFIRMED' );
114 }
115
116 return $event;
117 }
118
119 public function render( $token, $calendarId = NULL, $employeeId = NULL )
120 {
121 $user = $this->auth->getUserByToken( $token );
122
123 if( ! $user ){
124 echo "wrong link";
125 exit;
126 return;
127 }
128
129 if( null !== $calendarId ){
130 if( false !== strpos($calendarId, ',') ){
131 $calendarId = explode( ',', $calendarId );
132 }
133 }
134
135 if( null !== $employeeId ){
136 if( false !== strpos($employeeId, ',') ){
137 $employeeId = explode( ',', $employeeId );
138 }
139 }
140
141 $userId = $user->getId();
142 $pname = 'ical_max_advance' . '_' . $userId;
143 $maxAdvance = $this->setting->get( $pname );
144
145 $ok = true;
146
147 if( ! $maxAdvance ){
148 $ok = false;
149 }
150 else {
151 if( false === strpos($maxAdvance, ' ') ){
152 $ok = false;
153 }
154 }
155
156 if( ! $ok ){
157 $maxAdvance = '3 month';
158 }
159
160 /* 1 month before and 3 months after */
161 $start = $this->t->setNow()->modify('-1 month')->formatDateTimeDb();
162 $end = $this->t->setNow()->modify('+' . $maxAdvance)->formatDateTimeDb();
163
164 $this->shiftsQuery
165 ->setStart( $start )
166 ->setEnd( $end )
167 ;
168
169 $shifts = $this->shiftsQuery->find();
170
171 // filter shifts
172 $ids = array_keys( $shifts );
173 foreach( $ids as $id ){
174 $shift = $shifts[$id];
175
176 $shiftCalendar = $shift->getCalendar();
177 $shiftCalendarId = $shiftCalendar->getId();
178
179 $shiftEmployee = $shift->getEmployee();
180 $shiftEmployeeId = $shiftEmployee->getId();
181
182 if( NULL !== $calendarId ){
183 if( 't' == $calendarId ){
184 if( ! $shiftCalendar->isTimeoff() ){
185 unset( $shifts[$id] );
186 }
187 }
188 elseif( 's' == $calendarId ){
189 if( ! $shiftCalendar->isShift() ){
190 unset( $shifts[$id] );
191 }
192 }
193 elseif( 'x' != $calendarId ){
194 if( is_array($calendarId) ){
195 if( ! in_array($shiftCalendarId, $calendarId) ){
196 unset( $shifts[$id] );
197 }
198 }
199 else {
200 if( $shiftCalendarId != $calendarId ){
201 unset( $shifts[$id] );
202 }
203 }
204 }
205 }
206
207 // if( (NULL !== $calendarId) && ('x' != $calendarId) ){
208 // if( (NULL !== $calendarId) && ('x' != $calendarId) ){
209 // if( $shiftCalendarId != $calendarId ){
210 // unset( $shifts[$id] );
211 // }
212 // }
213 // }
214 // else {
215 if( (NULL !== $employeeId) && ('x' != $employeeId) ){
216 if( is_array($employeeId) ){
217 if( ! in_array($shiftEmployeeId, $employeeId) ){
218 unset( $shifts[$id] );
219 }
220 }
221 else {
222 if( $shiftEmployeeId != $employeeId ){
223 unset( $shifts[$id] );
224 }
225 }
226 }
227 // }
228 }
229
230 $shifts = $this->appQuery->filterShiftsForUser( $user, $shifts );
231
232 $timezoneObj = $this->t->getTimezone();
233 $timezone = $timezoneObj->getName();
234
235 $cal = $this->ical;
236
237 $cal->setConfig( 'unique_id', $this->myUnique );
238 // $cal->setProperty( 'method', 'publish' );
239 $cal->setProperty( 'method', 'request' );
240 $cal->setProperty( 'x-wr-timezone', $timezone );
241
242 $vtz = new hc_vtimezone();
243 $vtz->setProperty( 'tzid', $timezone );
244 $cal->addComponent( $vtz );
245
246 reset( $shifts );
247 foreach( $shifts as $shift ){
248 $event = $this->self->makeIcalEvent( $shift, $user );
249 $cal->addComponent( $event );
250 }
251
252 $ret = $cal->createCalendar();
253
254 // header('Content-Type: text/calendar');
255 echo $ret;
256 exit;
257 }
258 }