/** * Gets shifts for the supplied criteria * * This function handles a few possibilities: * As an employee, I want to know when I am working, by being able to see all of the shifts assigned to me. * As an employee, I want to be able to contact my managers, by seeing manager contact information for my shifts. * @example GET http://api.domain.com/shifts?user_id=3 * As a manager, I want to see the schedule, by listing shifts within a specific time period. * @example GET http://api.domain.com/shifts?user_id=11&start_time=2015-08-02 00:00:00&end_time=2015-08-03 23:59:59 * * @param integer $user_id ID of the WiwUser model * @param string $start_time Shift start time when searching for shifts within a time range * @param string $end_time Shift end time when searching for shifts within a time range * * @return array Returns an array of matching shifts * */ public function actionIndex($user_id, $start_time = null, $end_time = null) { $manager = WiwUser::findOne($user_id); $isManagerView = ($manager->isManager() && isset($start_time) && isset($end_time)) ? TRUE : FALSE; if($isManagerView){ $shifts = WiwShift::find() ->where(['between', 'start_time', $start_time, $end_time]) ->orderBy('start_time ASC') ->all(); } else { $shifts = WiwShift::find() ->where(['employee_id'=>$user_id]) ->orderBy('start_time ASC') ->all(); } foreach ($shifts as $key => $shift) { $data['id'] = $shift->id; $data['start_time'] = date(DATE_RFC2822, strtotime($shift->start_time)); $data['end_time'] = date(DATE_RFC2822, strtotime($shift->end_time)); if($isManagerView){ $data['employee'] = $shift->employee->name; } else { $data['manager'] = [ 'name' => $shift->manager->name, 'email' => $shift->manager->email, 'phone' => $shift->manager->phone, ]; } $this->results[] = $data; } return ["shifts" => $this->results]; }
public function getManager() { return $this->hasOne(WiwUser::className(), ['id' => 'manager_id']); }