Example #1
0
 public function actionForm($id = NULL)
 {
     $model = $id ? Visits::findOne($id) : new Visits();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         /* Check if it is a new record before we save it  */
         $newRecord = $model->isNewRecord;
         /* Attempt to save model */
         if ($model->save(false)) {
             /* Only when the record is new */
             if ($newRecord) {
                 /* Register null-observations for un-checked boxes if available */
                 if (!$model->checked_all) {
                     foreach ($model->project->boxes as $box) {
                         if (in_array($box->id, $model->checked_boxes)) {
                             Observations::createNullObservation($model->id, $box->id);
                         }
                     }
                 }
                 /* Register checked boxes */
                 if ($model->checked_boxes) {
                     $model->unlinkAll('visitBox', true);
                     foreach ($model->checked_boxes as $checkedBox) {
                         $visitBoxModel = new VisitBoxes();
                         $visitBoxModel->box_id = $checkedBox;
                         $visitBoxModel->link('visit', $model);
                         $visitBoxModel->save();
                     }
                 }
                 /* Register null-observations for all boxes in project when "checked-all" is selected */
                 if ($model->checked_all) {
                     foreach ($model->project->boxes as $box) {
                         Observations::createNullObservation($model->id, $box->id);
                     }
                 }
             }
             /* Register observers by this visit */
             if ($model->observers) {
                 $model->unlinkAll('observers', true);
                 foreach ($model->observers as $observer) {
                     $visitObservers = new VisitObservers();
                     $visitObservers->observer_id = $observer;
                     $visitObservers->link('visit', $model);
                     $visitObservers->save();
                 }
             }
             /* Redirect on success */
             return $this->redirect(Url::toRoute('visits/detail/' . $model->id));
         }
     }
     return $this->render('form', ['model' => $model, 'projects' => Projects::find()->hasRights()->hasBoxes()->all()]);
 }
Example #2
0
 public function getVisitBox()
 {
     return $this->hasMany(VisitBoxes::className(), ['visit_id' => 'id']);
 }