Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. /*
  2.      * Add new finding
  3.      */
  4.     public function actionNewFinding($id)
  5.     {
  6.         $inspection = $this->findModel($id);
  7.  
  8.         if ($inspection->company->user_id != Yii::$app->user->id) {
  9.             Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Invalid request. Please try again.'));
  10.             return $this->redirect(['/inspections']);
  11.         }
  12.  
  13.         $model = new InspectionFindings;
  14.         $photos = [new FindingPhotos]; // for inserting multiple answer
  15.  
  16.         if ($model->load(Yii::$app->request->post())) {
  17.  
  18.             $model->inspection_id = $id;
  19.             $model->created_at = date('Y-m-d H:i:s');
  20.             $model->created_by = Yii::$app->user->id;
  21.             $model->updated_at = date('Y-m-d H:i:s');
  22.             $model->updated_by = Yii::$app->user->id;
  23.  
  24.             $photos = Model::createMultiple(FindingPhotos::className());
  25.             Model::loadMultiple($photos, Yii::$app->request->post());
  26.             foreach ($photos as $index => $photo) {
  27.                 $photo->remark = ($photo->remark=='') ? '-':$photo->remark;
  28.                 $imageFiles = \yii\web\UploadedFile::getInstance($photo, "[{$index}]img");
  29.                 $photo->img = $imageFiles;
  30.  
  31.             }
  32.  
  33.             $valid = $model->validate();
  34.             $valid = Model::validateMultiple($photos) && $valid;
  35.  
  36.             if ($valid) {
  37.                 $transaction = Yii::$app->db->beginTransaction();
  38.  
  39.                 try {
  40.  
  41.                     if ($flag = $model->save(false)) {
  42.                         foreach ($photos as $photo) {
  43.  
  44.                             $photo->inspection_finding_id = $model->id;
  45.  
  46.                             $upload_path = \Yii::getAlias('@app/web/upload');
  47.                             if (!is_dir($upload_path)) {
  48.                                 mkdir($upload_path, 0777, true);
  49.                             }
  50.  
  51.                             $finding_photo_path = \Yii::getAlias('@app/web/upload/photo_finding');
  52.                             if (!is_dir($finding_photo_path)) {
  53.                                 mkdir($finding_photo_path, 0777, true);
  54.                             }
  55.  
  56.                             $directory = $finding_photo_path . DIRECTORY_SEPARATOR . $model->id . DIRECTORY_SEPARATOR;
  57.  
  58.                             if (!is_dir($directory)) {
  59.                                 mkdir($directory,0777,true);
  60.                             }
  61.  
  62.                             // if ada file
  63.                             if($photo->img) {
  64.  
  65.                                 $uid = uniqid(time(), true);
  66.                                 $oriFileName = $photo->img->name;
  67.                                 $fileName = $uid . '.' . $photo->img->extension;
  68.                                 $filePath = $directory . $fileName;
  69.                                 if ($photo->img->saveAs($filePath)) {
  70.                                     $path = '/upload/photo_finding/' . $model->id . DIRECTORY_SEPARATOR . $fileName;
  71.  
  72.                                     // insert into table image
  73.                                     $image = new Image;
  74.                                     $image->path = $path;
  75.                                     $image->size = (string)$photo->img->size;
  76.                                     $image->type = $photo->img->type;
  77.                                     $image->name = $fileName;
  78.                                     $image->ori_name = $oriFileName;
  79.  
  80.                                     if (!$image->save()) {
  81.                                         print_r($image->errors);exit;
  82.                                     }
  83.                                     $photo->image_id = $image->id; // set image id
  84.                                 }
  85.                                 if ($photo->remark) {
  86.                                     if (($flag = $photo->save(false)) === false) {
  87.                                         $transaction->rollBack();
  88.                                         break;
  89.                                     }
  90.                                 }
  91.                             }
  92.  
  93.                         } // end foreach $photos
  94.                     }
  95.  
  96.                     if ($flag) {
  97.                         $transaction->commit();
  98.  
  99.                         Yii::$app->getSession()->setFlash('success', Yii::t('app', 'New Finding Added'));
  100.                         return $this->redirect(['view', 'id' => $model->inspection_id]);
  101.  
  102.                     }
  103.  
  104.                 } catch (Exception $e) {
  105.  
  106.                     $transaction->rollBack();
  107.  
  108.                 }
  109.             }
  110.         }
  111.  
  112.         return $this->render('new_finding', [
  113.             'inspection' => $inspection,
  114.             'model' => $model,
  115.             'photos' => (empty($photos)) ? [new FindingPhotos] : $photos,
  116.         ]);
  117.     } // end actionNewFinding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement