Ejemplo n.º 1
0
 public function actionForm($id = NULL)
 {
     $model = $id ? Observations::findOne($id) : new Observations();
     if (Yii::$app->getRequest()->getQueryParam('visit_id') && is_null($id)) {
         $model->visit_id = Yii::$app->getRequest()->getQueryParam('visit_id');
     }
     if ($model->load(Yii::$app->request->post())) {
         /* Load picture file */
         $model->pictureFile = UploadedFile::getInstance($model, 'pictureFile');
         /* Validate model */
         if ($model->validate()) {
             /* Upload picture */
             if ($model->pictureFile) {
                 $model->upload();
             }
             /* Delete picture if selected */
             if ($model->deleteImage && !$model->pictureFile) {
                 $model->picture = NULL;
             }
             /* Check for auto-validation on observqation */
             if (Yii::$app->user->getIdentity()->hasRole(['validator', 'administrator'])) {
                 $model->markAsValidated();
             }
             /* When the observation type equals a null-observation, clear all other fields */
             if ($model->observation_type == Observations::OBSERVATION_TYPE_NULL) {
                 foreach ($model->attributes as $attribute => $value) {
                     if (!in_array($attribute, ['id', 'visit_id', 'observation_type', 'validated_by_id', 'validated_date', 'box_id', 'date_created', 'date_updated', 'deleted'])) {
                         $model->{$attribute} = NULL;
                     }
                 }
             }
             $model->save(false);
             if ($model->observation_type != Observations::OBSERVATION_TYPE_NULL && Observations::find()->where(['and', ['deleted' => false], ['box_id' => $model->box_id], ['observation_type' => Observations::OBSERVATION_TYPE_NULL]])->exists()) {
                 foreach (Observations::find()->where(['and', ['deleted' => false], ['observation_type' => Observations::OBSERVATION_TYPE_NULL], ['box_id' => $model->box_id]])->all() as $nullObservation) {
                     $nullObservation->delete();
                 }
             }
             return $this->redirect(Url::toRoute(['visits/detail/' . $model->visit_id]));
         }
     }
     return $this->render('form', ['model' => $model, 'boxes' => Boxes::find()->where(['project_id' => Visits::findOne($model->visit_id)->project_id])->all(), 'species' => Species::find()->asArray()->all(), 'parasites' => Species::find()->where(['taxon' => Species::TAXONOMY_ARTHROPOD])->all()]);
 }
Ejemplo n.º 2
0
 public function rules()
 {
     return [[['code', 'cord_lat', 'cord_lng', 'province'], 'required'], [['project_id', 'boxtype_id'], 'integer'], [['placement_date', 'removal_date', 'date_created', 'date_updated', 'cluster_id', 'deleteImage'], 'safe'], [['remarks', 'placement_height'], 'string'], [['deleted'], 'boolean'], [['code', 'location'], 'string', 'max' => 45], [['cord_lat', 'cord_lng'], 'string', 'max' => 50], ['province', 'string', 'max' => 20], [['direction'], 'string', 'max' => 2], [['picture'], 'string', 'max' => 100], ['placement_height', 'trim'], ['project_id', 'required', 'when' => function () {
         return $this->isNewRecord ? true : false;
     }], ['code', function ($attribute, $params) {
         if (isset($this->project)) {
             $uniqueQuery = Boxes::find()->where(['and', ['code' => $this->{$attribute}], ['project_id' => $this->project->id]]);
             if (!$this->isNewRecord) {
                 $uniqueQuery->andWhere(['<>', 'id', $this->id]);
             }
             if ($uniqueQuery->exists()) {
                 $this->addError($attribute, Yii::t('app', 'Kastcode bestaat al binnen dit project.'));
             }
         }
     }], ['placement_date', function ($attribute, $params) {
         if ($this->placement_date >= $this->removal_date && !empty($this->removal_date)) {
             $this->addError($attribute, Yii::t('app', 'Plaatsingsdatum mag niet na de verwijderdatum liggen.'));
         }
     }], ['removal_date', function ($attribute, $params) {
         if ($this->removal_date <= $this->placement_date && !empty($this->removal_date)) {
             $this->addError($attribute, Yii::t('app', 'Verwijderdatum mag niet voor de plaatsingsdatum liggen.'));
         }
     }], ['cord_lat', function ($attribute, $params) {
         if (!WGS84::inrange(-90, $this->cord_lat, 90)) {
             $this->addError($attribute, Yii::t('app', 'Latitude coördinaat is niet valide'));
         }
     }], ['cord_lng', function ($attribute, $params) {
         if (!WGS84::inrange(-180, $this->cord_lng, 180)) {
             $this->addError($attribute, Yii::t('app', 'Longitude coördinaat is niet valide'));
         }
     }], ['project_id', function ($attribute, $params) {
         if (!Projects::find()->andWhere(['projects.id' => $this->{$attribute}])->hasRights()->exists() && $this->isNewRecord) {
             return $this->addError($attribute, Yii::t('app', 'U heeft geen toegang tot het geselecteerde project'));
         }
     }]];
 }
Ejemplo n.º 3
0
 public function actionAjaxGetBoxes($q = NULL, $pid = 0, $vid = NULL)
 {
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $boxes = Boxes::find()->select(['id', 'code'])->byProject($pid)->all();
     return $boxes;
 }