public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->user_id != Yii::$app->user->id) {
throw new ErrorException(Yii::t('app', 'Forbidden to change entries of other users'));
}
$oldFile = $model->getImageFile();
$oldattachment = $model->attachment;
$oldFileName = $model->filename;
$model->edit_datetime = date("Y-m-d H:i:s");
if ($model->load(Yii::$app->request->post())) {
// process uploaded image file instance
$file = $model->uploadImage();
// revert back if no valid file instance uploaded
if ($file === false) {
$model->attachment = $oldattachment;
$model->filename = $oldFileName;
}
if ($model->save()) {
// upload only if valid uploaded file instance found
if ($file !== false && unlink($oldFile)) {
// delete old and overwrite
$path = $model->getImageFile();
$file->saveAs($path);
}
Yii::$app->session->setFlash("Entry-success", Yii::t("app", "Entry updated"));
return $this->redirect(['index']);
} else {
// error in saving model
}
}
return $this->render('update', ['model' => $model]);
}