/**
  * Creates a new Shift model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Shift();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Displays a single Team model.
  * @param integer $id
  * @return mixed
  */
 public function actionView($id)
 {
     $model = $this->findModel($id);
     $event = $model->event;
     $start = new MDateTime($event->start, new \DateTimeZone('EST5EDT'));
     $start->subToStart('D');
     $days = [];
     while ($start->timestamp < $event->end) {
         $days[$start->timestamp] = $model->getDayDataProvider($start->timestamp);
         $start->add(new \DateInterval('P1D'));
     }
     $dp = new ActiveDataProvider(['query' => Shift::find()->where(['team_id' => $id]), 'pagination' => false]);
     $shift = new Shift();
     $shift->team_id = $model->id;
     $shift->active = true;
     if ($shift->load(Yii::$app->request->post())) {
         $shift->save();
     }
     $requirements = Requirement::find()->orderBy('name ASC')->all();
     $event = $model->event;
     return $this->render('view', ['model' => $model, 'shift' => $shift, 'event' => $event, 'dataProvider' => $dp, 'days' => $days, 'requirements' => $requirements]);
 }
 public function importShifts($data)
 {
     //Data should be a 2D array of the following
     //[title, start_timestamp, length, min_participants, max_participants, requirement_name]
     $count = 0;
     foreach ($data as $row) {
         $timestamp = strtotime($row[1]);
         if (isset($row[5])) {
             $requirement = Requirement::findOne(['name' => $row[5]]);
         } else {
             $requirement = null;
         }
         $shift = new Shift();
         $shift->title = $row[0];
         $shift->start_time = $timestamp;
         $shift->team_id = $this->id;
         $shift->length = $row[2];
         $shift->min_needed = !empty($row[3]) ? $row[3] : null;
         $shift->max_needed = !empty($row[4]) ? $row[4] : null;
         $shift->requirement_id = isset($requirement) ? $requirement->id : null;
         $shift->active = true;
         if ($shift->save()) {
             $count++;
         }
     }
     Yii::$app->session->addFlash('success', sprintf("Imported %d of %d shifts successfully", $count, count($data)));
 }
 public function makeCopy($new_start)
 {
     $new_start += $this->start % (60 * 60 * 24);
     $new_end = $new_start + ($this->end - $this->start);
     $time_diff = $new_start - $this->start - floor(($new_start - $this->start) / (60 * 60 * 24));
     var_dump($time_diff);
     //Clone event
     $new_event = new Event();
     $new_event->attributes = $this->attributes;
     $new_event->formStart = date(self::DATE_FORMAT, $new_start);
     $new_event->formEnd = date(self::DATE_FORMAT, $new_end);
     $new_event->name = "Copy of " . $this->name;
     $new_event->save();
     //Clone teams
     $old_teams = $this->teams;
     foreach ($old_teams as $old_team) {
         $new_team = new Team();
         $new_team->attributes = $old_team->attributes;
         $new_team->event_id = $new_event->id;
         $new_team->save();
         //Clone shifts
         $old_shifts = $old_team->shifts;
         foreach ($old_shifts as $old_shift) {
             $new_shift = new Shift();
             $new_shift->attributes = $old_shift->attributes;
             $new_shift->team_id = $new_team->id;
             $new_shift->start_time += $time_diff;
             $new_shift->save();
         }
     }
     return $new_event->id;
 }