public function actionDelete($id) { $model = Visits::findOne($id); if ($model) { $model->deleted = true; $model->save(false); } foreach ($model->observations as $observation) { $observation->deleted = true; $observation->save(false); } return $this->redirect(Url::toRoute('visits/personal')); }
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()]); }
<?php use fedemotta\datatables\DataTables; use yii\helpers\Url; use yii\helpers\Html; use app\models\Visits; use app\models\Observations; use app\models\Boxtypes; use app\models\ProjectClusters; ?> <div class="table-responsive"> <?php echo DataTables::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['attribute' => 'code', 'format' => 'html', 'value' => function ($model, $key, $index, $column) { return Html::a($model['code'], Url::toRoute('boxes/detail/' . $model['id'])); }], ['attribute' => 'boxtype_id', 'format' => 'html', 'label' => 'Boxtype', 'value' => function ($model, $key, $index, $column) { $boxType = $model['boxtype_id'] ? Boxtypes::findOne($model['boxtype_id']) : NULL; return $boxType ? Html::a($boxType->model, Url::toRoute('boxtypes/detail/' . $boxType->id)) : "-"; }], ['attribute' => 'cluster_id', 'format' => 'html', 'label' => 'Cluster', 'value' => function ($model, $key, $index, $column) { $cluster = $model['cluster_id'] ? ProjectClusters::findOne($model['cluster_id']) : NULL; return $cluster ? $cluster->cluster : '-'; }], ['attribute' => 'placement_date', 'label' => Yii::t('app', 'Plaatsingsdatum')], ['label' => Yii::t('app', 'Laatste waarneming'), 'value' => function ($model, $key, $index, $column) { if (($observationModel = Observations::find()->byBox($model['id'])->one()) !== NULL) { return Visits::findOne($observationModel->visit_id)->date; } else { return "-"; } }]], 'clientOptions' => ['info' => false, 'responsive' => true, 'dom' => 'lfTrtip', 'tableTools' => ['aButtons' => [['sExtends' => 'copy', 'sButtonText' => Yii::t('app', 'Copy to clipboard')], ['sExtends' => 'csv', 'sButtonText' => Yii::t('app', 'Save to CSV')], ['sExtends' => 'pdf', 'sButtonText' => Yii::t('app', 'Save to PDF')], ['sExtends' => 'print', 'sButtonText' => Yii::t('app', 'Print')]]]]]); ?> </div>