Ejemplo n.º 1
0
 /**
  * Updates an existing news model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $params = Yii::$app->request->post();
     if (empty($params['updated_images'])) {
         $params['updated_images'] = array();
     }
     if (empty($params['updated_files'])) {
         $params['updated_files'] = array();
     }
     if ($model->load(Yii::$app->request->post())) {
         $image = Upload::uploadImage($model);
         $images = Upload::uploadImages($model);
         $attachments = Upload::uploadFiles($model);
         if (!$image) {
             $model->image = $model->oldAttributes['image'];
         }
         if (!$images) {
             //if(!empty($params['updated_images'])) {
             $model->images = json_encode($params['updated_images']);
             // } else {
             //     $model->images = '';
             // }
         } else {
             foreach ($images as $img) {
                 $path = Upload::getImageFile($img);
                 $image_array[] = $img->name;
                 $img->saveAs($path);
             }
             $model->images = json_encode(array_merge($params['updated_images'], $image_array));
         }
         if (!$attachments) {
             //if(!empty($params['updated_files'])) {
             $model->attachment = json_encode($params['updated_files']);
             // } else {
             //     $model->attachment = '';
             // }
         } else {
             foreach ($attachments as $attachment) {
                 $path = Upload::getImageFile($attachment);
                 $attachment_array[] = $attachment->name;
                 $attachment->saveAs($path);
             }
             $model->attachment = json_encode(array_merge($params['updated_files'], $attachment_array));
         }
         if ($model->save()) {
             // upload only if valid uploaded file instance found
             if ($image !== false) {
                 $path = Upload::getImageFile($image);
                 $image->saveAs($path);
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }