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
← All changes | sh4/api/api.php +7 -408 4.9.784.9.24 View file →
@@ -1,11 +1,7 @@
1 1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 -#[AllowDynamicProperties]
3 2 class SH4_Api_Api
4 3 {
5 - public $employeePresenter;
6 - public $appQuery;
7 -
8 4 public function __construct(
9 5 HC3_Hooks $hooks,
10 6 HC3_Time $t,
11 7
@@ -12,17 +8,11 @@
12 8 HC3_IPermission $permission,
13 9 HC3_Translate $translate,
14 10
15 11 SH4_Employees_Query $employeesQuery,
16 - SH4_Employees_Command $employeesCommand,
17 12 SH4_Calendars_Query $calendarsQuery,
18 - SH4_Calendars_Presenter $calendarPresenter,
19 13 SH4_Shifts_Presenter $shiftsPresenter,
20 14
21 - SH4_App_Query $appQuery,
22 - SH4_App_Command $appCommand,
23 - SH4_Employees_Presenter $employeePresenter,
24 -
25 15 SH4_Shifts_Conflicts $conflicts,
26 16 // SH4_Shifts_Acl $shiftsAcl,
27 17 SH4_Shifts_Query $shiftsQuery,
28 18 SH4_Shifts_Command $shiftsCommand
@@ -32,14 +22,9 @@
32 22 $this->translate = $translate;
33 23 $this->permission = $hooks->wrap( $permission );
34 24
35 25 $this->employeesQuery = $hooks->wrap( $employeesQuery );
36 - $this->employeesCommand = $hooks->wrap( $employeesCommand );
37 26 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
38 - $this->calendarPresenter = $hooks->wrap( $calendarPresenter );
39 - $this->employeePresenter = $hooks->wrap( $employeePresenter );
40 - $this->appQuery = $hooks->wrap( $appQuery );
41 - $this->appCommand = $hooks->wrap( $appCommand );
42 27
43 28 $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
44 29 $this->conflicts = $hooks->wrap($conflicts);
45 30 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
@@ -51,61 +36,10 @@
51 36 add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 );
52 37 add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 );
53 38 add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 );
54 39 add_filter( 'shiftcontroller4/api/shifts/updatebyid', array($this->self, 'shiftsUpdateById'), 10, 2 );
55 -
56 - add_filter( 'shiftcontroller4/api/employees/get', array($this->self, 'employeesGet'), 10, 1 );
57 - add_filter( 'shiftcontroller4/api/employees/getbyid', array($this->self, 'employeesGetById'), 10, 1 );
58 - add_filter( 'shiftcontroller4/api/employees/create', array($this->self, 'employeesCreate'), 10, 1 );
59 - add_filter( 'shiftcontroller4/api/employees/getbyuserid', array($this->self, 'employeesGetByUserId'), 10, 1 );
60 - add_filter( 'shiftcontroller4/api/employees/createbyuserid', array($this->self, 'employeesCreateByUserId'), 10, 1 );
61 -
62 - add_filter( 'shiftcontroller4/api/calendars/get', array($this->self, 'calendarsGet'), 10, 1 );
63 - add_filter( 'shiftcontroller4/api/employees/getbycalendarid', array($this->self, 'employeesGetByCalendarId'), 10, 1 );
64 - add_filter( 'shiftcontroller4/api/employees/addtocalendar', array($this->self, 'employeesAddToCalendar'), 10, 2 );
65 - add_filter( 'shiftcontroller4/api/employees/removefromcalendar', array($this->self, 'employeesRemoveFromCalendar'), 10, 2 );
66 -
67 - add_filter( 'shiftcontroller4/api/calendars/getbyemployeeid', array($this->self, 'calendarsGetByEmployeeId'), 10, 1 );
68 40 }
69 41
70 - public function toArrayEmployee( array $employeeList )
71 - {
72 - $out = array();
73 -
74 - foreach( $employeeList as $employee ){
75 - $thisOut = $this->employeePresenter->export( $employee, true );
76 -
77 - $keys = array_keys( $thisOut );
78 - reset( $keys );
79 - foreach( $keys as $k ){
80 - $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
81 - }
82 -
83 - $out[] = $thisOut;
84 - }
85 -
86 - return $out;
87 - }
88 -
89 - public function toArrayCalendar( array $list )
90 - {
91 - $out = array();
92 -
93 - foreach( $list as $e ){
94 - $thisOut = $this->calendarPresenter->export( $e, true );
95 -
96 - $keys = array_keys( $thisOut );
97 - reset( $keys );
98 - foreach( $keys as $k ){
99 - $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
100 - }
101 -
102 - $out[] = $thisOut;
103 - }
104 -
105 - return $out;
106 - }
107 -
108 42 public function toArray( array $shifts )
109 43 {
110 44 $out = array();
111 45
@@ -123,99 +57,8 @@
123 57
124 58 return $out;
125 59 }
126 60
127 - public function availableEmployeesGet( $queryParams = array() )
128 - {
129 - $calendarIdList = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : array();
130 - if( ! is_array($calendarIdList) ) $calendarIdList = array( $calendarIdList );
131 -
132 - if( isset($queryParams['from']) ){
133 - $fromParam = $queryParams['from'];
134 - // datetime
135 - if( strlen($fromParam) >= 12 ){
136 - $start = substr( $fromParam, 0, 12 );
137 - }
138 - // date
139 - else {
140 - $fromDate = substr( $fromParam, 0, 8 );
141 - $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
142 - }
143 - }
144 - else {
145 - $fromDate = $this->t->setNow()->formatDateDb();
146 - $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
147 - }
148 -
149 - if( isset($queryParams['to']) ){
150 - $toParam = $queryParams['to'];
151 - // datetime
152 - if( strlen($toParam) >= 12 ){
153 - $end = substr( $toParam, 0, 12 );
154 - }
155 - // date
156 - else {
157 - $toDate = substr( $toParam, 0, 8 );
158 - $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
159 - }
160 - }
161 - else {
162 - $end = $this->t->setDateTimeDb( $start )->modify('+1 day')->formatDateTimeDb();
163 - }
164 -
165 -// echo "$start - $end, $status<br>";
166 - $employee2calendar = array();
167 -
168 - $calendarList = $calendarIdList ? $this->calendarsQuery->findManyById( $calendarIdList ) : $this->calendarsQuery->findActive();
169 -
170 - foreach( $calendarList as $calendar ){
171 - // if no explicit calendars requested then leave only shifts
172 - if( ! $calendarIdList ){
173 - if( ! $calendar->isShift() ){
174 - continue;
175 - }
176 - }
177 -
178 - $calendarId = $calendar->getId();
179 - $thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
180 - foreach( array_keys($thisEmployees) as $employeeId ){
181 - $employee2calendar[ $employeeId ][ $calendarId ] = $calendar;
182 - }
183 - }
184 -
185 - $employees = array();
186 - if( $employee2calendar ){
187 - $employees = $this->employeesQuery->findManyActiveById( array_keys($employee2calendar) );
188 - }
189 - unset( $employees[0] );
190 -
191 - foreach( array_keys($employees) as $employeeId ){
192 - // no calendars at all
193 - if( ! isset($employee2calendar[$employeeId]) ){
194 - unset( $employees[$employeeId] );
195 - continue;
196 - }
197 -
198 - $employee = $employees[ $employeeId ];
199 - $thisCalendarList = $employee2calendar[ $employeeId ];
200 - foreach( $thisCalendarList as $calendar ){
201 - $testModel = new SH4_Shifts_Model( null, $calendar, $start, $end, $employee );
202 - $conflicts = $this->conflicts->get( $testModel );
203 - if( $conflicts ){
204 - unset( $employees[$employeeId] );
205 - break;
206 - }
207 - }
208 - }
209 -
210 - $ret = array();
211 - $ret[ 'from' ] = $start;
212 - $ret[ 'to' ] = $end;
213 - $ret[ 'employees' ] = $this->self->toArrayEmployee( $employees );
214 -
215 - return $ret;
216 - }
217 -
218 61 public function shiftsGet( $queryParams = array() )
219 62 {
220 63 $status = isset($queryParams['status_id']) ? $queryParams['status_id'] : null;
221 64
@@ -263,9 +106,9 @@
263 106 ;
264 107
265 108 $shifts = $this->shiftsQuery->find();
266 109
267 - $default = array( 'status_id', 'calendar_id', 'employee_id', 'from', 'to', 'X-WP-ShiftController-AuthCode' );
110 + $default = array( 'status_id', 'calendar_id', 'employee_id', 'from', 'to' );
268 111 foreach( $default as $k ) unset( $queryParams[$k] );
269 112
270 113 // filter shifts
271 114 $ids = array_keys( $shifts );
@@ -326,26 +169,17 @@
326 169
327 170 reset( $queryParams );
328 171 foreach( $queryParams as $k => $v ){
329 172 if( ! array_key_exists($k, $a) ){
330 - continue;
331 - }
332 -
333 - if( $a[$k] != $v ){
334 173 unset( $shifts[$id] );
335 174 break;
336 175 }
337 -
338 - // if( ! array_key_exists($k, $a) ){
339 - // unset( $shifts[$id] );
340 - // break;
341 - // }
342 - // else {
343 - // if( $a[$k] != $v ){
344 - // unset( $shifts[$id] );
345 - // break;
346 - // }
347 - // }
176 + else {
177 + if( $a[$k] != $v ){
178 + unset( $shifts[$id] );
179 + break;
180 + }
181 + }
348 182 }
349 183 }
350 184 }
351 185
@@ -676,241 +510,6 @@
676 510 $this->shiftsCommand->unpublish( $shift );
677 511 }
678 512
679 513 return;
680 - }
681 -
682 - public function employeesGet( $queryParams = array() )
683 - {
684 - $args = array();
685 - $ret = $this->employeesQuery->read();
686 - $ret = $this->self->toArrayEmployee( $ret );
687 - return $ret;
688 - }
689 -
690 - public function calendarsGet( $queryParams = array() )
691 - {
692 - $args = array();
693 - $ret = $this->calendarsQuery->read();
694 - $ret = $this->self->toArrayCalendar( $ret );
695 - return $ret;
696 - }
697 -
698 - public function employeesGetById( $id )
699 - {
700 - $args = array();
701 - $ret = $this->employeesQuery->findById( $id );
702 - if( ! $ret ){
703 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
704 - }
705 -
706 - $ret = [ $ret ];
707 - $ret = $this->self->toArrayEmployee( $ret );
708 - $ret = current( $ret );
709 - return $ret;
710 - }
711 -
712 - public function employeesGetByUserId( $userId )
713 - {
714 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
715 - if( ! $employee ){
716 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
717 - }
718 -
719 - $employees = array( $employee );
720 - $ret = $this->self->toArrayEmployee( $employees );
721 - $ret = array_shift( $ret );
722 -
723 - return $ret;
724 - }
725 -
726 - public function employeesGetByCalendarId( $calendarId )
727 - {
728 - $calendar = $this->calendarsQuery->findById( $calendarId );
729 - if( ! $calendar ){
730 - return new WP_Error( 'no_calendar', 'Not Found', array('status' => 404) );
731 - }
732 -
733 - $employees = $this->appQuery->findEmployeesForCalendar( $calendar );
734 - $ret = $this->self->toArrayEmployee( $employees );
735 -
736 - return $ret;
737 - }
738 -
739 - public function calendarsGetByEmployeeId( $employeeId )
740 - {
741 - $employee = $this->employeesQuery->findById( $employeeId );
742 - if( ! $employee ){
743 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
744 - }
745 -
746 - $calendars = $this->appQuery->findCalendarsForEmployee( $employee );
747 - $ret = $this->self->toArrayCalendar( $calendars );
748 -
749 - return $ret;
750 - }
751 -
752 -/* returns employee id */
753 - public function employeesCreate( $values )
754 - {
755 - $errors = array();
756 -
757 - $userId = isset( $values['user_id'] ) ? $values['user_id'] : null;
758 - $title = isset( $values['title'] ) ? $values['title'] : null;
759 -
760 - if( $userId ){
761 - // check if there's an employee linked to that user
762 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
763 - if( $employee ){
764 - return new WP_Error( 'user_occupied', 'The user is already linked to another employee', array('status' => 500) );
765 - }
766 -
767 - $wpUser = get_user_by( 'id', $userId );
768 - if( ! $wpUser ){
769 - return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
770 - }
771 - }
772 -
773 - if( (! $userId) && (null === $title) ){
774 - $errors['title'] = 'Required Field';
775 - }
776 - $description = isset( $values['description'] ) ? $values['description'] : null;
777 -
778 - if( $errors ){
779 - $errorStr = array();
780 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
781 - $errorStr = join( ', ', $errorStr );
782 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
783 - }
784 -
785 - try {
786 - // create by user id
787 - if( null === $title ){
788 - $employeeId = $this->self->employeesCreateByUserId( $userId );
789 - }
790 - else {
791 - $employeeId = $this->employeesCommand->create( $title, $description );
792 - }
793 - }
794 - catch( HC3_ExceptionArray $e ){
795 - $errors = $e->getErrors();
796 - }
797 -
798 - if( $errors ){
799 - $errorStr = array();
800 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
801 - $errorStr = join( ', ', $errorStr );
802 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
803 - }
804 -
805 - if( ! $userId ){
806 - return $employeeId;
807 - }
808 -
809 - // link user to employee
810 - if( null !== $title ){
811 - $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
812 - }
813 -
814 - return $employeeId;
815 - }
816 -
817 -/* returns employee id */
818 - public function employeesCreateByUserId( $userId )
819 - {
820 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
821 - if( $employee ){
822 - $ret = $employee->getId();
823 - return $ret;
824 - }
825 -
826 - $wpUser = get_user_by( 'id', $userId );
827 - if( ! $wpUser ){
828 - return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
829 - }
830 -
831 - $title = $wpUser->display_name;
832 - $description = null;
833 -
834 - try {
835 - $employeeId = $this->employeesCommand->create( $title, $description );
836 - }
837 - catch( HC3_ExceptionArray $e ){
838 - $errors = $e->getErrors();
839 - }
840 -
841 - if( $errors ){
842 - $errorStr = array();
843 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
844 - $errorStr = join( ', ', $errorStr );
845 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
846 - }
847 -
848 - // link user to employee
849 - $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
850 -
851 - return $employeeId;
852 - }
853 -
854 -/* returns employee id */
855 - public function employeesAddToCalendar( $employeeId, $calendarId )
856 - {
857 - $errors = array();
858 -
859 - $employee = $this->employeesQuery->findById( $employeeId );
860 - if( ! $employee ){
861 - return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
862 - }
863 -
864 - $calendar = $this->calendarsQuery->findById( $calendarId );
865 - if( ! $calendar ){
866 - return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
867 - }
868 -
869 - try {
870 - $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
871 - }
872 - catch( HC3_ExceptionArray $e ){
873 - $errors = $e->getErrors();
874 - }
875 -
876 - if( $errors ){
877 - $errorStr = array();
878 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
879 - $errorStr = join( ', ', $errorStr );
880 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
881 - }
882 -
883 - return true;
884 - }
885 -
886 - public function employeesRemoveFromCalendar( $employeeId, $calendarId )
887 - {
888 - $errors = array();
889 -
890 - $employee = $this->employeesQuery->findById( $employeeId );
891 - if( ! $employee ){
892 - return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
893 - }
894 -
895 - $calendar = $this->calendarsQuery->findById( $calendarId );
896 - if( ! $calendar ){
897 - return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
898 - }
899 -
900 - try {
901 - $this->appCommand->removeEmployeeFromCalendar( $employee, $calendar );
902 - }
903 - catch( HC3_ExceptionArray $e ){
904 - $errors = $e->getErrors();
905 - }
906 -
907 - if( $errors ){
908 - $errorStr = array();
909 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
910 - $errorStr = join( ', ', $errorStr );
911 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
912 - }
913 -
914 - return true;
915 514 }
916 515 }