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