| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | public $appQuery; |
| 7 | 7 | |
| 8 | 8 | public function __construct( |
| 9 | 9 | HC3_Hooks $hooks, |
| 10 | - HC3_Dic $dic, | |
| 11 | 10 | HC3_Time $t, |
| 12 | 11 | |
| 13 | 12 | HC3_IPermission $permission, |
| 14 | 13 | HC3_Translate $translate, |
| @@ -13,15 +12,12 @@ | ||
| 13 | 12 | HC3_IPermission $permission, |
| 14 | 13 | HC3_Translate $translate, |
| 15 | 14 | |
| 16 | 15 | SH4_Employees_Query $employeesQuery, |
| 17 | - SH4_Employees_Command $employeesCommand, | |
| 18 | 16 | SH4_Calendars_Query $calendarsQuery, |
| 19 | - SH4_Calendars_Presenter $calendarPresenter, | |
| 20 | 17 | SH4_Shifts_Presenter $shiftsPresenter, |
| 21 | 18 | |
| 22 | 19 | SH4_App_Query $appQuery, |
| 23 | - SH4_App_Command $appCommand, | |
| 24 | 20 | SH4_Employees_Presenter $employeePresenter, |
| 25 | 21 | |
| 26 | 22 | SH4_Shifts_Conflicts $conflicts, |
| 27 | 23 | // SH4_Shifts_Acl $shiftsAcl, |
| @@ -29,19 +25,15 @@ | ||
| 29 | 25 | SH4_Shifts_Command $shiftsCommand |
| 30 | 26 | ) |
| 31 | 27 | { |
| 32 | 28 | $this->t = $t; |
| 33 | - $this->dic = $dic; | |
| 34 | 29 | $this->translate = $translate; |
| 35 | 30 | $this->permission = $hooks->wrap( $permission ); |
| 36 | 31 | |
| 37 | 32 | $this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 38 | - $this->employeesCommand = $hooks->wrap( $employeesCommand ); | |
| 39 | 33 | $this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 40 | - $this->calendarPresenter = $hooks->wrap( $calendarPresenter ); | |
| 41 | 34 | $this->employeePresenter = $hooks->wrap( $employeePresenter ); |
| 42 | 35 | $this->appQuery = $hooks->wrap( $appQuery ); |
| 43 | - $this->appCommand = $hooks->wrap( $appCommand ); | |
| 44 | 36 | |
| 45 | 37 | $this->shiftsPresenter = $hooks->wrap( $shiftsPresenter ); |
| 46 | 38 | $this->conflicts = $hooks->wrap($conflicts); |
| 47 | 39 | $this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| @@ -53,21 +45,8 @@ | ||
| 53 | 45 | add_filter( 'shiftcontroller4/api/shifts/getbyid', array($this->self, 'shiftsGetById'), 10, 1 ); |
| 54 | 46 | add_filter( 'shiftcontroller4/api/shifts/create', array($this->self, 'shiftsCreate'), 10, 1 ); |
| 55 | 47 | add_filter( 'shiftcontroller4/api/shifts/deletebyid', array($this->self, 'shiftsDeleteById'), 10, 1 ); |
| 56 | 48 | 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 | 49 | } |
| 71 | 50 | |
| 72 | 51 | public function toArrayEmployee( array $employeeList ) |
| 73 | 52 | { |
| @@ -87,27 +66,8 @@ | ||
| 87 | 66 | |
| 88 | 67 | return $out; |
| 89 | 68 | } |
| 90 | 69 | |
| 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 | 70 | public function toArray( array $shifts ) |
| 111 | 71 | { |
| 112 | 72 | $out = array(); |
| 113 | 73 | |
| @@ -493,28 +453,8 @@ | ||
| 493 | 453 | |
| 494 | 454 | $this->shiftsCommand->draft( $shift ); |
| 495 | 455 | } |
| 496 | 456 | |
| 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 | 457 | return $shiftId; |
| 518 | 458 | } |
| 519 | 459 | |
| 520 | 460 | public function shiftsDeleteById( $id ) |
| @@ -697,263 +637,7 @@ | ||
| 697 | 637 | if( $newStatus && (SH4_Shifts_Model::STATUS_DRAFT == $newStatus) && ( ! $shift->isDraft() ) ){ |
| 698 | 638 | $this->shiftsCommand->unpublish( $shift ); |
| 699 | 639 | } |
| 700 | 640 | |
| 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 | 641 | 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 | 642 | } |
| 959 | 643 | } |