PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
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 / feed / view.php

view.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/feed/view.php

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