/** * @inheritdoc */ public function afterDelete() { parent::afterDelete(); foreach (FormSubmissionFile::find()->where(['submission_id' => $this->idCache])->all() as $fileModel) { $fileModel->delete(); } }
/** * @inheritdoc */ public function beforeDelete() { if (parent::beforeDelete()) { // Delete related Models $this->formData->delete(); $this->ui->delete(); $this->formConfirmation->delete(); $this->formEmail->delete(); // Delete all Charts, Submissions and Files related to this form // We use deleteAll for performance reason FormUser::deleteAll(["form_id" => $this->id]); FormRule::deleteAll(["form_id" => $this->id]); FormChart::deleteAll(["form_id" => $this->id]); FormSubmissionFile::deleteAll(["form_id" => $this->id]); FormSubmission::deleteAll(["form_id" => $this->id]); // Delete all Stats related to this form Event::deleteAll(["app_id" => $this->id]); StatsPerformance::deleteAll(["app_id" => $this->id]); StatsSubmissions::deleteAll(["app_id" => $this->id]); // Removes files directory (and all its content) // of this form (if exists) FileHelper::removeDirectory($this::FILES_DIRECTORY . DIRECTORY_SEPARATOR . $this->id); return true; } else { return false; } }
/** * Insert a Form Submission Model * * @param $id * @return array * @throws NotFoundHttpException * @throws \Exception * @throws \yii\db\Exception */ public function actionA($id) { if (Yii::$app->request->isAjax) { // The HTTP post request $post = Yii::$app->request->post(); if (isset($post)) { // If no model, throw NotFoundHttpException $formModel = $this->findFormModel($id); /**************+++++++++++++++++ /* Spam Filter /*******************************/ // Honeypot filter. If spam, throw NotFoundHttpException $formModel->checkHoneypot($post); // reCAPTCHA Validation. If error, send response to browser $formModel->validateRecaptcha($post); /**************+++++++++++++++++ /* Submission Limit /*******************************/ // If error, send response to browser $formModel->checkTotalLimit(); $formModel->checkIPLimit(); /**************+++++++++++++++++ /* Prepare response by default /*******************************/ // Response fornat Yii::$app->response->format = Response::FORMAT_JSON; // Default response $response = array('action' => 'submit', 'success' => true, 'id' => 0, 'message' => Yii::t('app', 'Your message has been sent. {startTag}Thank you!{endTag}', ['startTag' => '<strong>', 'endTag' => '</strong>'])); /**************+++++++++++++++++ /* Prepare data /*******************************/ // Set public scenario of the submission $formSubmissionModel = new FormSubmission(['scenario' => 'public']); /** @var \app\models\FormData $formDataModel */ $formDataModel = $formModel->formData; // Get all fields except buttons and files $fields = $formDataModel->getFieldsWithoutFilesAndButtons(); // Get file fields $fileFields = $formDataModel->getFileFields(); // Remove fields with null values and // Strip whitespace from the beginning and end of each post value $submissionData = $formSubmissionModel->cleanSubmission($fields, $post); // Get uploaded files $files = $formSubmissionModel->getUploadedFiles($fileFields); // File paths cache $filePaths = array(); // Prepare Submission for validation $postFormSubmission = ['FormSubmission' => ['form_id' => $formModel->id, 'data' => $submissionData]]; /**************+++++++++++++++++ /* FormSubmission Validation /*******************************/ if ($formSubmissionModel->load($postFormSubmission) && $formSubmissionModel->validate()) { Yii::$app->trigger($this::EVENT_SUBMISSION_RECEIVED, new SubmissionEvent(['sender' => $this, 'form' => $formModel, 'submission' => $formSubmissionModel, 'files' => $files])); if ($formModel->saveToDB()) { /**************+++++++++++++++++ /* Save to DB /*******************************/ // Save submission in single transaction $transaction = Form::getDb()->beginTransaction(); try { // Save submission without validation if ($formSubmissionModel->save(false)) { // Save files to DB and disk /* @var $file \yii\web\UploadedFile */ foreach ($files as $file) { if (isset($file)) { // Save file to DB $fileModel = new FormSubmissionFile(); $fileModel->submission_id = $formSubmissionModel->primaryKey; $fileModel->form_id = $formModel->id; // Replace special characters before the file is saved $fileModel->name = preg_replace("/[^a-zA-Z0-9]/", "", $file->baseName) . "-" . $formSubmissionModel->primaryKey; $fileModel->extension = $file->extension; $fileModel->size = $file->size; $fileModel->status = 1; $fileModel->save(); // Throw exception if validation fail if (isset($fileModel->errors) && count($fileModel->errors) > 0) { throw new \Exception(Yii::t("app", "Error saving files.")); } // Save file to disk $filePath = $fileModel->getFilePath(); $file->saveAs($filePath); array_push($filePaths, $filePath); } } // Change response id $response["id"] = $formSubmissionModel->primaryKey; } $transaction->commit(); } catch (\Exception $e) { // Rolls back the transaction $transaction->rollBack(); // Rethrow the exception throw $e; } } else { /**************+++++++++++++++++ /* Don't save to DB /*******************************/ // Save files to disk foreach ($files as $file) { /* @var $file \yii\web\UploadedFile */ if (isset($file)) { $filePath = $formModel::FILES_DIRECTORY . DIRECTORY_SEPARATOR . $formModel->id . DIRECTORY_SEPARATOR . $file->name; $file->saveAs($filePath); array_push($filePaths, $filePath); } } } Yii::$app->trigger($this::EVENT_SUBMISSION_ACCEPTED, new SubmissionEvent(['sender' => $this, 'form' => $formModel, 'submission' => $formSubmissionModel, 'files' => $files, 'filePaths' => $filePaths])); } else { Yii::$app->trigger($this::EVENT_SUBMISSION_REJECTED, new SubmissionEvent(['sender' => $this, 'form' => $formModel, 'submission' => $formSubmissionModel])); // Print validation errors $errors = array(); foreach ($formSubmissionModel->errors as $field => $messages) { array_push($errors, array("field" => $field, "messages" => $messages)); } // Change response $response["success"] = false; $response["message"] = Yii::t('app', 'There is {startTag}an error in your submission{endTag}.', ['startTag' => '<strong>', 'endTag' => '</strong>']); $response["errors"] = $errors; } return $response; } } return ''; }