Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Add new finding
- */
- public function actionNewFinding($id)
- {
- $inspection = $this->findModel($id);
- if ($inspection->company->user_id != Yii::$app->user->id) {
- Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Invalid request. Please try again.'));
- return $this->redirect(['/inspections']);
- }
- $model = new InspectionFindings;
- $photos = [new FindingPhotos]; // for inserting multiple answer
- if ($model->load(Yii::$app->request->post())) {
- $model->inspection_id = $id;
- $model->created_at = date('Y-m-d H:i:s');
- $model->created_by = Yii::$app->user->id;
- $model->updated_at = date('Y-m-d H:i:s');
- $model->updated_by = Yii::$app->user->id;
- $photos = Model::createMultiple(FindingPhotos::className());
- Model::loadMultiple($photos, Yii::$app->request->post());
- foreach ($photos as $index => $photo) {
- $photo->remark = ($photo->remark=='') ? '-':$photo->remark;
- $imageFiles = \yii\web\UploadedFile::getInstance($photo, "[{$index}]img");
- $photo->img = $imageFiles;
- }
- $valid = $model->validate();
- $valid = Model::validateMultiple($photos) && $valid;
- if ($valid) {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- if ($flag = $model->save(false)) {
- foreach ($photos as $photo) {
- $photo->inspection_finding_id = $model->id;
- $upload_path = \Yii::getAlias('@app/web/upload');
- if (!is_dir($upload_path)) {
- mkdir($upload_path, 0777, true);
- }
- $finding_photo_path = \Yii::getAlias('@app/web/upload/photo_finding');
- if (!is_dir($finding_photo_path)) {
- mkdir($finding_photo_path, 0777, true);
- }
- $directory = $finding_photo_path . DIRECTORY_SEPARATOR . $model->id . DIRECTORY_SEPARATOR;
- if (!is_dir($directory)) {
- mkdir($directory,0777,true);
- }
- // if ada file
- if($photo->img) {
- $uid = uniqid(time(), true);
- $oriFileName = $photo->img->name;
- $fileName = $uid . '.' . $photo->img->extension;
- $filePath = $directory . $fileName;
- if ($photo->img->saveAs($filePath)) {
- $path = '/upload/photo_finding/' . $model->id . DIRECTORY_SEPARATOR . $fileName;
- // insert into table image
- $image = new Image;
- $image->path = $path;
- $image->size = (string)$photo->img->size;
- $image->type = $photo->img->type;
- $image->name = $fileName;
- $image->ori_name = $oriFileName;
- if (!$image->save()) {
- print_r($image->errors);exit;
- }
- $photo->image_id = $image->id; // set image id
- }
- if ($photo->remark) {
- if (($flag = $photo->save(false)) === false) {
- $transaction->rollBack();
- break;
- }
- }
- }
- } // end foreach $photos
- }
- if ($flag) {
- $transaction->commit();
- Yii::$app->getSession()->setFlash('success', Yii::t('app', 'New Finding Added'));
- return $this->redirect(['view', 'id' => $model->inspection_id]);
- }
- } catch (Exception $e) {
- $transaction->rollBack();
- }
- }
- }
- return $this->render('new_finding', [
- 'inspection' => $inspection,
- 'model' => $model,
- 'photos' => (empty($photos)) ? [new FindingPhotos] : $photos,
- ]);
- } // end actionNewFinding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement