PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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 -451 4.9.924.9.26 View file →
@@ -1,14 +1,9 @@
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 - HC3_Dic $dic,
11 6 HC3_Time $t,
12 7
13 8 HC3_IPermission $permission,
14 9 HC3_Translate $translate,
@@ -13,17 +8,11 @@
13 8 HC3_IPermission $permission,
14 9 HC3_Translate $translate,
15 10
16 11 SH4_Employees_Query $employeesQuery,
17 - SH4_Employees_Command $employeesCommand,
18 12 SH4_Calendars_Query $calendarsQuery,
19 - SH4_Calendars_Presenter $calendarPresenter,
20 13 SH4_Shifts_Presenter $shiftsPresenter,
21 14
22 - SH4_App_Query $appQuery,
23 - SH4_App_Command $appCommand,
24 - SH4_Employees_Presenter $employeePresenter,
25 -
26 15 SH4_Shifts_Conflicts $conflicts,
27 16 // SH4_Shifts_Acl $shiftsAcl,
28 17 SH4_Shifts_Query $shiftsQuery,
29 18 SH4_Shifts_Command $shiftsCommand
@@ -29,19 +18,13 @@
29 18 SH4_Shifts_Command $shiftsCommand
30 19 )
31 20 {
32 21 $this->t = $t;
33 - $this->dic = $dic;
34 22 $this->translate = $translate;
35 23 $this->permission = $hooks->wrap( $permission );
36 24
37 25 $this->employeesQuery = $hooks->wrap( $employeesQuery );
38 - $this->employeesCommand = $hooks->wrap( $employeesCommand );
39 26 $this->calendarsQuery = $hooks->wrap( $calendarsQuery );
40 - $this->calendarPresenter = $hooks->wrap( $calendarPresenter );
41 - $this->employeePresenter = $hooks->wrap( $employeePresenter );
42 - $this->appQuery = $hooks->wrap( $appQuery );
43 - $this->appCommand = $hooks->wrap( $appCommand );
44 27
45 28 $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter );
46 29 $this->conflicts = $hooks->wrap($conflicts);
47 30 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
@@ -53,61 +36,10 @@
53 36 add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 );
54 37 add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 );
55 38 add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 );
56 39 add_filter( 'shiftcontroller4/api/shifts/updatebyid', array($this->self, 'shiftsUpdateById'), 10, 2 );
57 -
58 - add_filter( 'shiftcontroller4/api/employees/get', array($this->self, 'employeesGet'), 10, 1 );
59 - add_filter( 'shiftcontroller4/api/employees/getbyid', array($this->self, 'employeesGetById'), 10, 1 );
60 - add_filter( 'shiftcontroller4/api/employees/create', array($this->self, 'employeesCreate'), 10, 1 );
61 - add_filter( 'shiftcontroller4/api/employees/getbyuserid', array($this->self, 'employeesGetByUserId'), 10, 1 );
62 - add_filter( 'shiftcontroller4/api/employees/createbyuserid', array($this->self, 'employeesCreateByUserId'), 10, 1 );
63 -
64 - add_filter( 'shiftcontroller4/api/calendars/get', array($this->self, 'calendarsGet'), 10, 1 );
65 - add_filter( 'shiftcontroller4/api/employees/getbycalendarid', array($this->self, 'employeesGetByCalendarId'), 10, 1 );
66 - add_filter( 'shiftcontroller4/api/employees/addtocalendar', array($this->self, 'employeesAddToCalendar'), 10, 2 );
67 - add_filter( 'shiftcontroller4/api/employees/removefromcalendar', array($this->self, 'employeesRemoveFromCalendar'), 10, 2 );
68 -
69 - add_filter( 'shiftcontroller4/api/calendars/getbyemployeeid', array($this->self, 'calendarsGetByEmployeeId'), 10, 1 );
70 40 }
71 41
72 - public function toArrayEmployee( array $employeeList )
73 - {
74 - $out = array();
75 -
76 - foreach( $employeeList as $employee ){
77 - $thisOut = $this->employeePresenter->export( $employee, true );
78 -
79 - $keys = array_keys( $thisOut );
80 - reset( $keys );
81 - foreach( $keys as $k ){
82 - $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
83 - }
84 -
85 - $out[] = $thisOut;
86 - }
87 -
88 - return $out;
89 - }
90 -
91 - public function toArrayCalendar( array $list )
92 - {
93 - $out = array();
94 -
95 - foreach( $list as $e ){
96 - $thisOut = $this->calendarPresenter->export( $e, true );
97 -
98 - $keys = array_keys( $thisOut );
99 - reset( $keys );
100 - foreach( $keys as $k ){
101 - $thisOut[$k] = $this->translate->translate( $thisOut[$k] );
102 - }
103 -
104 - $out[] = $thisOut;
105 - }
106 -
107 - return $out;
108 - }
109 -
110 42 public function toArray( array $shifts )
111 43 {
112 44 $out = array();
113 45
@@ -125,99 +57,8 @@
125 57
126 58 return $out;
127 59 }
128 60
129 - public function availableEmployeesGet( $queryParams = array() )
130 - {
131 - $calendarIdList = isset( $queryParams['calendar_id'] ) ? $queryParams['calendar_id'] : array();
132 - if( ! is_array($calendarIdList) ) $calendarIdList = array( $calendarIdList );
133 -
134 - if( isset($queryParams['from']) ){
135 - $fromParam = $queryParams['from'];
136 - // datetime
137 - if( strlen($fromParam) >= 12 ){
138 - $start = substr( $fromParam, 0, 12 );
139 - }
140 - // date
141 - else {
142 - $fromDate = substr( $fromParam, 0, 8 );
143 - $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
144 - }
145 - }
146 - else {
147 - $fromDate = $this->t->setNow()->formatDateDb();
148 - $start = $this->t->setDateDb( $fromDate )->formatDateTimeDb();
149 - }
150 -
151 - if( isset($queryParams['to']) ){
152 - $toParam = $queryParams['to'];
153 - // datetime
154 - if( strlen($toParam) >= 12 ){
155 - $end = substr( $toParam, 0, 12 );
156 - }
157 - // date
158 - else {
159 - $toDate = substr( $toParam, 0, 8 );
160 - $end = $this->t->setDateDb( $toDate )->modify('+1 day')->formatDateTimeDb();
161 - }
162 - }
163 - else {
164 - $end = $this->t->setDateTimeDb( $start )->modify('+1 day')->formatDateTimeDb();
165 - }
166 -
167 -// echo "$start - $end, $status<br>";
168 - $employee2calendar = array();
169 -
170 - $calendarList = $calendarIdList ? $this->calendarsQuery->findManyById( $calendarIdList ) : $this->calendarsQuery->findActive();
171 -
172 - foreach( $calendarList as $calendar ){
173 - // if no explicit calendars requested then leave only shifts
174 - if( ! $calendarIdList ){
175 - if( ! $calendar->isShift() ){
176 - continue;
177 - }
178 - }
179 -
180 - $calendarId = $calendar->getId();
181 - $thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar );
182 - foreach( array_keys($thisEmployees) as $employeeId ){
183 - $employee2calendar[ $employeeId ][ $calendarId ] = $calendar;
184 - }
185 - }
186 -
187 - $employees = array();
188 - if( $employee2calendar ){
189 - $employees = $this->employeesQuery->findManyActiveById( array_keys($employee2calendar) );
190 - }
191 - unset( $employees[0] );
192 -
193 - foreach( array_keys($employees) as $employeeId ){
194 - // no calendars at all
195 - if( ! isset($employee2calendar[$employeeId]) ){
196 - unset( $employees[$employeeId] );
197 - continue;
198 - }
199 -
200 - $employee = $employees[ $employeeId ];
201 - $thisCalendarList = $employee2calendar[ $employeeId ];
202 - foreach( $thisCalendarList as $calendar ){
203 - $testModel = new SH4_Shifts_Model( null, $calendar, $start, $end, $employee );
204 - $conflicts = $this->conflicts->get( $testModel );
205 - if( $conflicts ){
206 - unset( $employees[$employeeId] );
207 - break;
208 - }
209 - }
210 - }
211 -
212 - $ret = array();
213 - $ret[ 'from' ] = $start;
214 - $ret[ 'to' ] = $end;
215 - $ret[ 'employees' ] = $this->self->toArrayEmployee( $employees );
216 -
217 - return $ret;
218 - }
219 -
220 61 public function shiftsGet( $queryParams = array() )
221 62 {
222 63 $status = isset($queryParams['status_id']) ? $queryParams['status_id'] : null;
223 64
@@ -265,9 +106,9 @@
265 106 ;
266 107
267 108 $shifts = $this->shiftsQuery->find();
268 109
269 - $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' );
270 111 foreach( $default as $k ) unset( $queryParams[$k] );
271 112
272 113 // filter shifts
273 114 $ids = array_keys( $shifts );
@@ -328,26 +169,17 @@
328 169
329 170 reset( $queryParams );
330 171 foreach( $queryParams as $k => $v ){
331 172 if( ! array_key_exists($k, $a) ){
332 - continue;
333 - }
334 -
335 - if( $a[$k] != $v ){
336 173 unset( $shifts[$id] );
337 174 break;
338 175 }
339 -
340 - // if( ! array_key_exists($k, $a) ){
341 - // unset( $shifts[$id] );
342 - // break;
343 - // }
344 - // else {
345 - // if( $a[$k] != $v ){
346 - // unset( $shifts[$id] );
347 - // break;
348 - // }
349 - // }
176 + else {
177 + if( $a[$k] != $v ){
178 + unset( $shifts[$id] );
179 + break;
180 + }
181 + }
350 182 }
351 183 }
352 184 }
353 185
@@ -493,28 +325,8 @@
493 325
494 326 $this->shiftsCommand->draft( $shift );
495 327 }
496 328
497 - if (class_exists('SH4_CFields_Model') or class_exists('SH4_CFields2_Model')) {
498 - $cfieldsQuery = $this->dic->make('SH4_CFields_Query');
499 - $cfieldsCommand = $this->dic->make('SH4_CFields_Command');
500 - $cal = $calendar;
501 - $cfields = $cfieldsQuery->findByCalendar($cal);
502 -
503 - $cvals = array();
504 - foreach ($cfields as $cfield) {
505 - $cfieldName = $cfield->getName();
506 - $cfieldId = $cfield->getId();
507 - if (isset($values[$cfieldName])) {
508 - $cvals[$cfieldId] = $values[$cfieldName];
509 - }
510 - }
511 -
512 - if ($cvals) {
513 - $cfieldsCommand->updateShift($shift, $cvals);
514 - }
515 - }
516 -
517 329 return $shiftId;
518 330 }
519 331
520 332 public function shiftsDeleteById( $id )
@@ -697,263 +509,7 @@
697 509 if( $newStatus && (SH4_Shifts_Model::STATUS_DRAFT == $newStatus) && ( ! $shift->isDraft() ) ){
698 510 $this->shiftsCommand->unpublish( $shift );
699 511 }
700 512
701 - if (class_exists('SH4_CFields_Model') or class_exists('SH4_CFields2_Model')) {
702 - $cfieldsQuery = $this->dic->make('SH4_CFields_Query');
703 - $cfieldsCommand = $this->dic->make('SH4_CFields_Command');
704 - $cal = $newCalendar ? $newCalendar : $calendar;
705 - $cfields = $cfieldsQuery->findByCalendar($cal);
706 -
707 -
708 - $cvals = array();
709 - foreach ($cfields as $cfield) {
710 - $cfieldName = $cfield->getName();
711 - $cfieldId = $cfield->getId();
712 - if (isset($values[$cfieldName])) {
713 - $cvals[$cfieldId] = $values[$cfieldName];
714 - }
715 - }
716 -
717 - if ($cvals) {
718 - $cfieldsCommand->updateShift($shift, $cvals);
719 - }
720 - }
721 -
722 513 return;
723 - }
724 -
725 - public function employeesGet( $queryParams = array() )
726 - {
727 - $args = array();
728 - $ret = $this->employeesQuery->read();
729 - $ret = $this->self->toArrayEmployee( $ret );
730 - return $ret;
731 - }
732 -
733 - public function calendarsGet( $queryParams = array() )
734 - {
735 - $args = array();
736 - $ret = $this->calendarsQuery->read();
737 - $ret = $this->self->toArrayCalendar( $ret );
738 - return $ret;
739 - }
740 -
741 - public function employeesGetById( $id )
742 - {
743 - $args = array();
744 - $ret = $this->employeesQuery->findById( $id );
745 - if( ! $ret ){
746 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
747 - }
748 -
749 - $ret = [ $ret ];
750 - $ret = $this->self->toArrayEmployee( $ret );
751 - $ret = current( $ret );
752 - return $ret;
753 - }
754 -
755 - public function employeesGetByUserId( $userId )
756 - {
757 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
758 - if( ! $employee ){
759 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
760 - }
761 -
762 - $employees = array( $employee );
763 - $ret = $this->self->toArrayEmployee( $employees );
764 - $ret = array_shift( $ret );
765 -
766 - return $ret;
767 - }
768 -
769 - public function employeesGetByCalendarId( $calendarId )
770 - {
771 - $calendar = $this->calendarsQuery->findById( $calendarId );
772 - if( ! $calendar ){
773 - return new WP_Error( 'no_calendar', 'Not Found', array('status' => 404) );
774 - }
775 -
776 - $employees = $this->appQuery->findEmployeesForCalendar( $calendar );
777 - $ret = $this->self->toArrayEmployee( $employees );
778 -
779 - return $ret;
780 - }
781 -
782 - public function calendarsGetByEmployeeId( $employeeId )
783 - {
784 - $employee = $this->employeesQuery->findById( $employeeId );
785 - if( ! $employee ){
786 - return new WP_Error( 'no_employee', 'Not Found', array('status' => 404) );
787 - }
788 -
789 - $calendars = $this->appQuery->findCalendarsForEmployee( $employee );
790 - $ret = $this->self->toArrayCalendar( $calendars );
791 -
792 - return $ret;
793 - }
794 -
795 -/* returns employee id */
796 - public function employeesCreate( $values )
797 - {
798 - $errors = array();
799 -
800 - $userId = isset( $values['user_id'] ) ? $values['user_id'] : null;
801 - $title = isset( $values['title'] ) ? $values['title'] : null;
802 -
803 - if( $userId ){
804 - // check if there's an employee linked to that user
805 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
806 - if( $employee ){
807 - return new WP_Error( 'user_occupied', 'The user is already linked to another employee', array('status' => 500) );
808 - }
809 -
810 - $wpUser = get_user_by( 'id', $userId );
811 - if( ! $wpUser ){
812 - return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
813 - }
814 - }
815 -
816 - if( (! $userId) && (null === $title) ){
817 - $errors['title'] = 'Required Field';
818 - }
819 - $description = isset( $values['description'] ) ? $values['description'] : null;
820 -
821 - if( $errors ){
822 - $errorStr = array();
823 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
824 - $errorStr = join( ', ', $errorStr );
825 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
826 - }
827 -
828 - try {
829 - // create by user id
830 - if( null === $title ){
831 - $employeeId = $this->self->employeesCreateByUserId( $userId );
832 - }
833 - else {
834 - $employeeId = $this->employeesCommand->create( $title, $description );
835 - }
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 - if( ! $userId ){
849 - return $employeeId;
850 - }
851 -
852 - // link user to employee
853 - if( null !== $title ){
854 - $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
855 - }
856 -
857 - return $employeeId;
858 - }
859 -
860 -/* returns employee id */
861 - public function employeesCreateByUserId( $userId )
862 - {
863 - $employee = $this->appQuery->findEmployeeByUserId( $userId );
864 - if( $employee ){
865 - $ret = $employee->getId();
866 - return $ret;
867 - }
868 -
869 - $wpUser = get_user_by( 'id', $userId );
870 - if( ! $wpUser ){
871 - return new WP_Error( 'no_user', 'User not found', array('status' => 404) );
872 - }
873 -
874 - $title = $wpUser->display_name;
875 - $description = null;
876 -
877 - try {
878 - $employeeId = $this->employeesCommand->create( $title, $description );
879 - }
880 - catch( HC3_ExceptionArray $e ){
881 - $errors = $e->getErrors();
882 - }
883 -
884 - if( $errors ){
885 - $errorStr = array();
886 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
887 - $errorStr = join( ', ', $errorStr );
888 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
889 - }
890 -
891 - // link user to employee
892 - $this->appCommand->linkEmployeeToUserById( $employeeId, $userId );
893 -
894 - return $employeeId;
895 - }
896 -
897 -/* returns employee id */
898 - public function employeesAddToCalendar( $employeeId, $calendarId )
899 - {
900 - $errors = array();
901 -
902 - $employee = $this->employeesQuery->findById( $employeeId );
903 - if( ! $employee ){
904 - return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
905 - }
906 -
907 - $calendar = $this->calendarsQuery->findById( $calendarId );
908 - if( ! $calendar ){
909 - return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
910 - }
911 -
912 - try {
913 - $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
914 - }
915 - catch( HC3_ExceptionArray $e ){
916 - $errors = $e->getErrors();
917 - }
918 -
919 - if( $errors ){
920 - $errorStr = array();
921 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
922 - $errorStr = join( ', ', $errorStr );
923 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
924 - }
925 -
926 - return true;
927 - }
928 -
929 - public function employeesRemoveFromCalendar( $employeeId, $calendarId )
930 - {
931 - $errors = array();
932 -
933 - $employee = $this->employeesQuery->findById( $employeeId );
934 - if( ! $employee ){
935 - return new WP_Error( 'no_employee', 'Not found', array('status' => 404) );
936 - }
937 -
938 - $calendar = $this->calendarsQuery->findById( $calendarId );
939 - if( ! $calendar ){
940 - return new WP_Error( 'no_calendar', 'Not found', array('status' => 404) );
941 - }
942 -
943 - try {
944 - $this->appCommand->removeEmployeeFromCalendar( $employee, $calendar );
945 - }
946 - catch( HC3_ExceptionArray $e ){
947 - $errors = $e->getErrors();
948 - }
949 -
950 - if( $errors ){
951 - $errorStr = array();
952 - foreach( $errors as $k => $v ) $errorStr[] = $k . ': ' . $v;
953 - $errorStr = join( ', ', $errorStr );
954 - return new WP_Error( 'error', $errorStr, array('status' => 500) );
955 - }
956 -
957 - return true;
958 514 }
959 515 }