/** * Updates an existing Forum model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id, $action = 'dashboard') { $model = $this->findModel($id); $newBoard = new Board(); if ($newBoard->load(Yii::$app->request->post())) { $newBoard->forum_id = $model->id; if ($newBoard->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Create successfully.')); } else { Yii::$app->getSession()->setFlash('error', 'Server error.'); } } Yii::setAlias('@upload', '@webroot/uploads/forum/icon/'); if (Yii::$app->request->isPost && !empty($_FILES)) { $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); $fileName = $model->id . '_' . time() . rand(1, 10000) . '.' . $extension; Image::thumbnail($_FILES['file']['tmp_name'], 160, 160)->save(Yii::getAlias('@upload') . $fileName, ['quality' => 80]); //delete old icon $file_exists = file_exists(Yii::getAlias('@upload') . $model->forum_icon); if ($file_exists && strpos($model->forum_icon, 'default') === false) { @unlink(Yii::getAlias('@upload') . $model->forum_icon); } $model->forum_icon = $fileName; $model->update(); } if ($model->load(Yii::$app->request->post())) { if ($model->validate() && $model->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Save successfully.')); } } return $this->render('update', ['model' => $model, 'newBoard' => $newBoard, 'action' => $action]); }