コード例 #1
0
 /**
  * PostClassified
  * Status : Oke
  * @return mixed
  */
 public function actionPostClassified()
 {
     $modelClassified = new Classified();
     $modelImage = [new ClassifiedImage()];
     $modelClassifiedGuest = new ClassifiedGuest();
     if (\Yii::$app->user->isGuest) {
         if ($modelClassifiedGuest->load(Yii::$app->request->post())) {
             $length = rand(1111111111, 99999999999);
             $modelClassifiedGuest->id = $length;
             if ($modelClassifiedGuest->save()) {
                 if ($modelClassified->load(\Yii::$app->request->post())) {
                     $modelImage = \common\models\Model::createMultiple(ClassifiedImage::className());
                     \common\models\Model::loadMultiple($modelImage, \Yii::$app->request->post());
                     //ajax validation
                     if (\Yii::$app->request->isAjax) {
                         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                         return \yii\helpers\ArrayHelper::merge(\yii\widgets\ActiveForm::validateMultiple($modelImage), \yii\widgets\ActiveForm::validate($modelClassified));
                     } else {
                         echo 'salah 3';
                     }
                     // validate all
                     $valid = $modelClassified->validate();
                     $valid = \common\models\Model::validateMultiple($modelImage) && $valid;
                     $modelClassified->create_at = new \yii\db\Expression('NOW()');
                     $modelClassified->update_at = new \yii\db\Expression('NOW()');
                     $modelClassified->user_id = $modelClassifiedGuest->id;
                     $modelClassified->is_status = 1;
                     $modelClassified->type = 0;
                     // 1 = login; 0 = guest
                     if ($valid) {
                         $transaction = \Yii::$app->db->beginTransaction();
                         try {
                             if ($flag = $modelClassified->save(false)) {
                                 foreach ($modelImage as $i => $modelImage) {
                                     $modelImage->classified_id = $modelClassified->id;
                                     $name = Yii::$app->security->generateRandomString();
                                     $modelImage->imageFile = UploadedFile::getInstance($modelImage, "[{$i}]imageFile");
                                     $modelImage->imageFile->saveAs('uploads/classified/' . $name . '.' . $modelImage->imageFile->extension);
                                     //Upload files to server
                                     ////save path in db column
                                     $modelImage->image = 'uploads/classified/' . $name . '.' . $modelImage->imageFile->extension;
                                     if (!($flag = $modelImage->save(false))) {
                                         $transaction->rollBack();
                                         break;
                                     }
                                 }
                             } else {
                                 echo 'salah 2';
                             }
                             if ($flag) {
                                 $transaction->commit();
                                 \Yii::$app->getSession()->setFlash('success', '<div class="alert d-alert-success" role="alert"><b>Thank you, your ads successfully saved!</b></div>');
                                 return $this->redirect(['success', 'id' => $modelClassified->id]);
                             }
                         } catch (Exception $ex) {
                             $transaction->rollBack();
                         }
                     }
                 } else {
                     echo 'ada salah 1';
                 }
             }
         } else {
             return $this->render('guest/post-classified', ['modelClassifiedGuest' => $modelClassifiedGuest, 'modelClassified' => $modelClassified, 'modelImage' => empty($modelImage) ? [new ClassifiedImage()] : $modelImage]);
         }
     } else {
         if ($modelClassified->load(\Yii::$app->request->post())) {
             $modelImage = \common\models\Model::createMultiple(ClassifiedImage::className());
             \common\models\Model::loadMultiple($modelImage, \Yii::$app->request->post());
             //ajax validation
             if (\Yii::$app->request->isAjax) {
                 \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                 return \yii\helpers\ArrayHelper::merge(\yii\widgets\ActiveForm::validateMultiple($modelImage), \yii\widgets\ActiveForm::validate($modelClassified));
             }
             // validate all
             $valid = $modelClassified->validate();
             $valid = \common\models\Model::validateMultiple($modelImage) && $valid;
             $modelClassified->create_at = new \yii\db\Expression('NOW()');
             $modelClassified->update_at = new \yii\db\Expression('NOW()');
             $modelClassified->user_id = Yii::$app->user->identity->id;
             $modelClassified->is_status = 1;
             $modelClassified->type = 1;
             // 1 = member ; 0 = guest
             if ($valid) {
                 $transaction = \Yii::$app->db->beginTransaction();
                 try {
                     if ($flag = $modelClassified->save(false)) {
                         foreach ($modelImage as $i => $modelImage) {
                             $modelImage->classified_id = $modelClassified->id;
                             $name = Yii::$app->security->generateRandomString();
                             $modelImage->imageFile = UploadedFile::getInstance($modelImage, "[{$i}]imageFile");
                             $modelImage->imageFile->saveAs('uploads/classified/' . $name . '.' . $modelImage->imageFile->extension);
                             //Upload files to server
                             ////save path in db column
                             $modelImage->image = 'uploads/classified/' . $name . '.' . $modelImage->imageFile->extension;
                             if (!($flag = $modelImage->save(false))) {
                                 $transaction->rollBack();
                                 break;
                             }
                         }
                     }
                     if ($flag) {
                         $transaction->commit();
                         \Yii::$app->getSession()->setFlash('success', '<div class="alert d-alert-success" role="alert"><b>Thank you, your ads successfully saved!</b></div>');
                         return $this->redirect(['success', 'id' => $modelClassified->id]);
                     }
                 } catch (Exception $ex) {
                     $transaction->rollBack();
                 }
             }
         } else {
             return $this->render('post-classified', ['modelClassifiedGuest' => $modelClassifiedGuest, 'modelClassified' => $modelClassified, 'modelImage' => empty($modelImage) ? [new ClassifiedImage()] : $modelImage]);
         }
     }
 }