| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Shifts_Controller_Time |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Post $post, |
| 8 |
HC3_Time $t, |
| 9 |
|
| 10 |
SH4_Shifts_Conflicts $conflicts, |
| 11 |
HC3_Auth $auth, |
| 12 |
HC3_IPermission $permission, |
| 13 |
SH4_App_Query $appQuery, |
| 14 |
SH4_Calendars_Permissions $calendarsPermissions, |
| 15 |
|
| 16 |
SH4_ShiftTypes_Query $shiftTypesQuery, |
| 17 |
|
| 18 |
SH4_Shifts_Query $query, |
| 19 |
SH4_Shifts_Command $command, |
| 20 |
SH4_Employees_Query $employees |
| 21 |
) |
| 22 |
{ |
| 23 |
$this->post = $post; |
| 24 |
$this->t = $t; |
| 25 |
|
| 26 |
$this->shiftTypesQuery = $hooks->wrap($shiftTypesQuery); |
| 27 |
|
| 28 |
$this->query = $hooks->wrap($query); |
| 29 |
$this->command = $hooks->wrap($command); |
| 30 |
$this->employees = $hooks->wrap($employees); |
| 31 |
|
| 32 |
$this->auth = $hooks->wrap( $auth ); |
| 33 |
$this->permission = $hooks->wrap( $permission ); |
| 34 |
$this->conflicts = $hooks->wrap($conflicts); |
| 35 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 36 |
$this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); |
| 37 |
} |
| 38 |
|
| 39 |
public function execute( $shiftId ) |
| 40 |
{ |
| 41 |
$shift = $this->query->findById( $shiftId ); |
| 42 |
|
| 43 |
$shiftTypeId = $this->post->get('shifttype'); |
| 44 |
if( NULL !== $shiftTypeId ){ |
| 45 |
$shiftType = $this->shiftTypesQuery->findById( $shiftTypeId ); |
| 46 |
|
| 47 |
$timeStart = $shiftType->getStart(); |
| 48 |
$timeEnd = $shiftType->getEnd(); |
| 49 |
$startBreak = (string) $shiftType->getBreakStart(); |
| 50 |
$endBreak = (string) $shiftType->getBreakEnd(); |
| 51 |
} |
| 52 |
else { |
| 53 |
$time = $this->post->get('time'); |
| 54 |
|
| 55 |
if( NULL === $time ){ |
| 56 |
$to = 'schedule'; |
| 57 |
$msg = '__Required Field__'; |
| 58 |
$return = array( $to, $msg, TRUE ); |
| 59 |
return $return; |
| 60 |
} |
| 61 |
|
| 62 |
list( $timeStart, $timeEnd ) = explode( '-', $time ); |
| 63 |
|
| 64 |
$startBreak = $endBreak = ''; |
| 65 |
$breakOn = $this->post->get('break_on'); |
| 66 |
if( $breakOn ){ |
| 67 |
$break = $this->post->get('break'); |
| 68 |
list( $startBreak, $endBreak ) = explode( '-', $break ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$date = $shift->getDateStart(); |
| 73 |
|
| 74 |
$start = $this->t->setDateDb( $date ) |
| 75 |
->modify( '+' . $timeStart . ' seconds' ) |
| 76 |
->formatDateTimeDb() |
| 77 |
; |
| 78 |
$end = $this->t->setDateDb( $date ) |
| 79 |
->modify( '+' . $timeEnd . ' seconds' ) |
| 80 |
->formatDateTimeDb() |
| 81 |
; |
| 82 |
|
| 83 |
if( strlen($startBreak) ){ |
| 84 |
$this->t->setDateDb( $date ); |
| 85 |
if( $startBreak ){ |
| 86 |
$this->t->modify( '+' . $startBreak . ' seconds' ); |
| 87 |
} |
| 88 |
$startBreak = $this->t->formatDateTimeDb(); |
| 89 |
} |
| 90 |
|
| 91 |
if( strlen($endBreak) ){ |
| 92 |
$this->t->setDateDb( $date ); |
| 93 |
if( $endBreak ){ |
| 94 |
$this->t->modify( '+' . $endBreak . ' seconds' ); |
| 95 |
} |
| 96 |
$endBreak = $this->t->formatDateTimeDb(); |
| 97 |
} |
| 98 |
|
| 99 |
if( $startBreak OR $endBreak ){ |
| 100 |
if( ($startBreak < $start) OR ($endBreak > $end) ){ |
| 101 |
$errors = array(); |
| 102 |
$msg = '__Incorrect luch break time__'; |
| 103 |
$errors['break'] = $msg; |
| 104 |
|
| 105 |
// $id = $shift->getId(); |
| 106 |
// $to = 'shifts/' . $id . '/time'; |
| 107 |
|
| 108 |
$to = $this->post->get( 'back' ); |
| 109 |
$to = $this->post->get( 'back' ); |
| 110 |
if( $to ){ |
| 111 |
$to = json_decode( $to, TRUE ); |
| 112 |
} |
| 113 |
else { |
| 114 |
$to = array( 'schedule', array() ); |
| 115 |
} |
| 116 |
|
| 117 |
// throw new HC3_ExceptionArray( $errors ); |
| 118 |
$return = array( $to, $msg, true ); |
| 119 |
return $return; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// check if it creates a conflict |
| 124 |
$calendar = $shift->getCalendar(); |
| 125 |
$calendarId = $calendar->getId(); |
| 126 |
|
| 127 |
// $testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $shift->getEmployee(), $startBreak, $endBreak ); |
| 128 |
$testModel = new SH4_Shifts_Model( $shift->getId(), $calendar, $start, $end, $shift->getEmployee(), $startBreak, $endBreak ); |
| 129 |
$conflicts = $this->conflicts->get( $testModel ); |
| 130 |
|
| 131 |
$allowed = TRUE; |
| 132 |
if( $conflicts ){ |
| 133 |
$isManager = FALSE; |
| 134 |
|
| 135 |
$currentUser = $this->auth->getCurrentUser(); |
| 136 |
$currentUserId = $currentUser->getId(); |
| 137 |
if( $currentUserId ){ |
| 138 |
if( $this->permission->isAdmin($currentUser) ){ |
| 139 |
$isManager = TRUE; |
| 140 |
} |
| 141 |
else { |
| 142 |
$calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 143 |
if( isset($calendarsAsManager[$calendarId]) ){ |
| 144 |
$isManager = TRUE; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if( ! $isManager ){ |
| 150 |
if( ! $this->calendarsPermissions->get($calendar, 'employee_create_own_conflicts') ){ |
| 151 |
$allowed = FALSE; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$to = $this->post->get( 'back' ); |
| 157 |
if( $to ){ |
| 158 |
$to = json_decode( $to, TRUE ); |
| 159 |
} |
| 160 |
else { |
| 161 |
$to = array( 'schedule', array() ); |
| 162 |
} |
| 163 |
|
| 164 |
if( $allowed ){ |
| 165 |
$this->command->reschedule( $shift, $start, $end, $startBreak, $endBreak ); |
| 166 |
$msg = '__Shift Rescheduled__'; |
| 167 |
$return = array( $to, $msg ); |
| 168 |
} |
| 169 |
else { |
| 170 |
$msg = '__You cannot create new shifts with conflicts.__'; |
| 171 |
$return = array( $to, $msg, TRUE ); |
| 172 |
} |
| 173 |
|
| 174 |
return $return; |
| 175 |
} |
| 176 |
} |