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