| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Feed_View |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Time $t, |
| 8 |
HC3_Auth $auth, |
| 9 |
|
| 10 |
HC3_Translate $translate, |
| 11 |
SH4_Shifts_Presenter $shiftsPresenter, |
| 12 |
|
| 13 |
SH4_App_Query $appQuery, |
| 14 |
SH4_Shifts_Query $shiftsQuery |
| 15 |
) |
| 16 |
{ |
| 17 |
$this->t = $t; |
| 18 |
$this->auth = $auth; |
| 19 |
$this->translate = $translate; |
| 20 |
|
| 21 |
$this->shiftsPresenter = $hooks->wrap( $shiftsPresenter ); |
| 22 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 23 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 24 |
} |
| 25 |
|
| 26 |
protected function _shifts( $token, $calendarId = NULL, $employeeId = NULL, $fromDate = NULL, $toDate = NULL ) |
| 27 |
{ |
| 28 |
$user = $this->auth->getUserByToken( $token ); |
| 29 |
|
| 30 |
if( ! $user ){ |
| 31 |
echo "wrong link"; |
| 32 |
exit; |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
if( NULL === $fromDate ){ |
| 37 |
$fromDate = $this->t->setNow()->formatDateDb(); |
| 38 |
$toDate = $this->t->modify('+1 year')->formatDateDb(); |
| 39 |
} |
| 40 |
|
| 41 |
$start = $this->t->setDateDb( $fromDate )->formatDateTimeDb(); |
| 42 |
$end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb(); |
| 43 |
|
| 44 |
$this->shiftsQuery |
| 45 |
->setStart( $start ) |
| 46 |
->setEnd( $end ) |
| 47 |
; |
| 48 |
|
| 49 |
$shifts = $this->shiftsQuery->find(); |
| 50 |
|
| 51 |
$moreKeys = array(); |
| 52 |
foreach( array_keys($_GET) as $k ){ |
| 53 |
if( 'misc' == substr($k, 0, strlen('misc')) ) $moreKeys[] = $k; |
| 54 |
} |
| 55 |
|
| 56 |
// filter shifts |
| 57 |
$ids = array_keys( $shifts ); |
| 58 |
foreach( $ids as $id ){ |
| 59 |
$shift = $shifts[$id]; |
| 60 |
|
| 61 |
if( $shift->getStart() >= $end ){ |
| 62 |
unset( $shifts[$id] ); |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
if( $shift->getEnd() <= $start ){ |
| 67 |
unset( $shifts[$id] ); |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$shiftCalendar = $shift->getCalendar(); |
| 72 |
$shiftCalendarId = $shiftCalendar->getId(); |
| 73 |
|
| 74 |
$shiftEmployee = $shift->getEmployee(); |
| 75 |
$shiftEmployeeId = $shiftEmployee->getId(); |
| 76 |
|
| 77 |
if( NULL !== $calendarId ){ |
| 78 |
if( 't' == $calendarId ){ |
| 79 |
if( ! $shiftCalendar->isTimeoff() ){ |
| 80 |
unset( $shifts[$id] ); |
| 81 |
continue; |
| 82 |
} |
| 83 |
} |
| 84 |
elseif( 's' == $calendarId ){ |
| 85 |
if( ! $shiftCalendar->isShift() ){ |
| 86 |
unset( $shifts[$id] ); |
| 87 |
continue; |
| 88 |
} |
| 89 |
} |
| 90 |
elseif( 'x' != $calendarId ){ |
| 91 |
if( $shiftCalendarId != $calendarId ){ |
| 92 |
unset( $shifts[$id] ); |
| 93 |
continue; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// if( (NULL !== $calendarId) && ('x' != $calendarId) ){ |
| 99 |
// if( (NULL !== $calendarId) && ('x' != $calendarId) ){ |
| 100 |
// if( $shiftCalendarId != $calendarId ){ |
| 101 |
// unset( $shifts[$id] ); |
| 102 |
// } |
| 103 |
// } |
| 104 |
// } |
| 105 |
// else { |
| 106 |
if( (NULL !== $employeeId) && ('x' != $employeeId) ){ |
| 107 |
if( $shiftEmployeeId != $employeeId ){ |
| 108 |
unset( $shifts[$id] ); |
| 109 |
continue; |
| 110 |
} |
| 111 |
} |
| 112 |
// } |
| 113 |
|
| 114 |
// anything more in get params to filter out? |
| 115 |
if( $moreKeys ){ |
| 116 |
$thisOut = $this->shiftsPresenter->export( $shifts[$id], true, $user ); |
| 117 |
$skipThis = false; |
| 118 |
foreach( $moreKeys as $k ){ |
| 119 |
if( ! array_key_exists($k, $thisOut) ){ |
| 120 |
$skipThis = true; |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
$v = sanitize_text_field( $_GET[$k] ); |
| 125 |
if( $thisOut[$k] != $v ){ |
| 126 |
$skipThis = true; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if( $skipThis ){ |
| 132 |
unset( $shifts[$id] ); |
| 133 |
continue; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$shifts = $this->appQuery->filterShiftsForUser( $user, $shifts ); |
| 139 |
return $shifts; |
| 140 |
} |
| 141 |
|
| 142 |
public function renderJson( $token, $calendarId = NULL, $employeeId = NULL, $fromDate = NULL, $toDate = NULL ) |
| 143 |
{ |
| 144 |
$shifts = $this->_shifts( $token, $calendarId, $employeeId, $fromDate, $toDate ); |
| 145 |
$user = $this->auth->getUserByToken( $token ); |
| 146 |
|
| 147 |
$separator = ','; |
| 148 |
$out = array(); |
| 149 |
foreach( $shifts as $shift ){ |
| 150 |
$thisOut = $this->shiftsPresenter->export( $shift, TRUE, $user ); |
| 151 |
$keys = array_keys( $thisOut ); |
| 152 |
|
| 153 |
reset( $keys ); |
| 154 |
foreach( $keys as $k ){ |
| 155 |
$thisOut[$k] = $this->translate->translate( $thisOut[$k] ); |
| 156 |
} |
| 157 |
|
| 158 |
// $thisOut = HC3_Functions::buildCsv( $thisOut, $separator ); |
| 159 |
$out[] = $thisOut; |
| 160 |
} |
| 161 |
|
| 162 |
$out = json_encode( $out, JSON_UNESCAPED_UNICODE ); |
| 163 |
echo $out; |
| 164 |
exit; |
| 165 |
|
| 166 |
// echo $out; |
| 167 |
// exit; |
| 168 |
|
| 169 |
$fileName = 'feed'; |
| 170 |
$fileName .= '-' . date('Y-m-d_H-i') . '.csv'; |
| 171 |
HC3_Functions::pushDownload( $fileName, $out ); |
| 172 |
exit; |
| 173 |
} |
| 174 |
|
| 175 |
public function render( $token, $calendarId = NULL, $employeeId = NULL, $fromDate = NULL, $toDate = NULL ) |
| 176 |
{ |
| 177 |
$shifts = $this->_shifts( $token, $calendarId, $employeeId, $fromDate, $toDate ); |
| 178 |
$user = $this->auth->getUserByToken( $token ); |
| 179 |
|
| 180 |
$separator = ','; |
| 181 |
|
| 182 |
$shiftsOut = array(); |
| 183 |
$header = array(); |
| 184 |
|
| 185 |
foreach( $shifts as $shift ){ |
| 186 |
$thisOut = $this->shiftsPresenter->export( $shift ); |
| 187 |
|
| 188 |
$thisHeader = array_keys( $thisOut ); |
| 189 |
foreach( $thisHeader as $th ){ |
| 190 |
if( ! isset($header[$th]) ){ |
| 191 |
$header[$th] = $th; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
$shiftsOut[] = $thisOut; |
| 196 |
} |
| 197 |
|
| 198 |
$keys = array_keys( $header ); |
| 199 |
$out = array(); |
| 200 |
reset( $shiftsOut ); |
| 201 |
foreach( $shiftsOut as $shiftOut ){ |
| 202 |
$thisOut = array(); |
| 203 |
|
| 204 |
reset( $keys ); |
| 205 |
foreach( $keys as $k ){ |
| 206 |
if( isset($shiftOut[$k]) ){ |
| 207 |
$thisOut[$k] = $this->translate->translate( $shiftOut[$k] ); |
| 208 |
} |
| 209 |
else { |
| 210 |
$thisOut[$k] = null; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
$thisOut = HC3_Functions::buildCsv( $thisOut, $separator ); |
| 215 |
$out[] = $thisOut; |
| 216 |
} |
| 217 |
|
| 218 |
if( $out ){ |
| 219 |
$header = HC3_Functions::buildCsv( $header, $separator ); |
| 220 |
$header = array_unshift( $out, $header ); |
| 221 |
} |
| 222 |
|
| 223 |
$out = join("\n", $out); |
| 224 |
|
| 225 |
// echo $out; |
| 226 |
// exit; |
| 227 |
|
| 228 |
$fileName = 'feed'; |
| 229 |
$fileName .= '-' . date('Y-m-d_H-i') . '.csv'; |
| 230 |
HC3_Functions::pushDownload( $fileName, $out ); |
| 231 |
exit; |
| 232 |
} |
| 233 |
} |