| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Shifts_Controller_Date |
| 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 |
$date = $this->post->get('date'); |
| 44 |
|
| 45 |
if( NULL === $date ){ |
| 46 |
$to = 'schedule'; |
| 47 |
$msg = '__Required Field__'; |
| 48 |
$ret = array( $to, $msg, TRUE ); |
| 49 |
return $ret; |
| 50 |
} |
| 51 |
|
| 52 |
$timeStart = $shift->getStartInDay(); |
| 53 |
$timeEnd = $shift->getEndInDay(); |
| 54 |
|
| 55 |
$startBreak = $shift->getBreakStartInDay(); |
| 56 |
$endBreak = $shift->getBreakEndInDay(); |
| 57 |
|
| 58 |
$start = $this->t->setDateDb( $date ) |
| 59 |
->modify( '+' . $timeStart . ' seconds' ) |
| 60 |
->formatDateTimeDb() |
| 61 |
; |
| 62 |
|
| 63 |
$tsStart = $this->t->setDateTimeDb( $shift->getStart() )->getTimestamp(); |
| 64 |
$tsEnd = $this->t->setDateTimeDb( $shift->getEnd() )->getTimestamp();; |
| 65 |
$duration = $tsEnd - $tsStart; |
| 66 |
$durationInDays = ceil( $duration/(24*60*60) ); |
| 67 |
|
| 68 |
$this->t->setDateDb( $date ); |
| 69 |
if( $durationInDays > 1 ){ |
| 70 |
$this->t->modify( '+' . ($durationInDays - 1) . ' days' ); |
| 71 |
} |
| 72 |
$end = $this->t |
| 73 |
->modify( '+' . $timeEnd . ' seconds' ) |
| 74 |
->formatDateTimeDb() |
| 75 |
; |
| 76 |
|
| 77 |
if( NULL !== $startBreak ){ |
| 78 |
$startBreak = $this->t->setDateDb( $date ) |
| 79 |
->modify( '+' . $startBreak . ' seconds' ) |
| 80 |
->formatDateTimeDb() |
| 81 |
; |
| 82 |
} |
| 83 |
if( NULL !== $endBreak ){ |
| 84 |
$endBreak = $this->t->setDateDb( $date ) |
| 85 |
->modify( '+' . $endBreak . ' seconds' ) |
| 86 |
->formatDateTimeDb() |
| 87 |
; |
| 88 |
} |
| 89 |
|
| 90 |
// check if it creates a conflict |
| 91 |
$calendar = $shift->getCalendar(); |
| 92 |
$calendarId = $calendar->getId(); |
| 93 |
|
| 94 |
// $testModel = new SH4_Shifts_Model( NULL, $calendar, $start, $end, $shift->getEmployee(), $startBreak, $endBreak ); |
| 95 |
$testModel = new SH4_Shifts_Model( $shift->getId(), $calendar, $start, $end, $shift->getEmployee(), $startBreak, $endBreak ); |
| 96 |
$conflicts = $this->conflicts->get( $testModel ); |
| 97 |
|
| 98 |
$allowed = TRUE; |
| 99 |
if( $conflicts ){ |
| 100 |
$isManager = FALSE; |
| 101 |
|
| 102 |
$currentUser = $this->auth->getCurrentUser(); |
| 103 |
$currentUserId = $currentUser->getId(); |
| 104 |
if( $currentUserId ){ |
| 105 |
if( $this->permission->isAdmin($currentUser) ){ |
| 106 |
$isManager = TRUE; |
| 107 |
} |
| 108 |
else { |
| 109 |
$calendarsAsManager = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 110 |
if( isset($calendarsAsManager[$calendarId]) ){ |
| 111 |
$isManager = TRUE; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if( ! $isManager ){ |
| 117 |
if( ! $this->calendarsPermissions->get($calendar, 'employee_create_own_conflicts') ){ |
| 118 |
$allowed = FALSE; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$to = $this->post->get( 'back' ); |
| 124 |
if( $to ){ |
| 125 |
$to = json_decode( $to, TRUE ); |
| 126 |
} |
| 127 |
else { |
| 128 |
$to = array( 'schedule', array() ); |
| 129 |
} |
| 130 |
|
| 131 |
if( $allowed ){ |
| 132 |
$this->command->reschedule( $shift, $start, $end, $startBreak, $endBreak ); |
| 133 |
$msg = '__Shift Rescheduled__'; |
| 134 |
$return = array( $to, $msg ); |
| 135 |
} |
| 136 |
else { |
| 137 |
$msg = '__You cannot create new shifts with conflicts.__'; |
| 138 |
$return = array( $to, $msg, TRUE ); |
| 139 |
} |
| 140 |
|
| 141 |
return $return; |
| 142 |
} |
| 143 |
} |