/** * @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; } }
/** * Show & Save form report * * @param $id * @return array * @throws Exception */ public function actionReport($id) { Yii::$app->response->format = Response::FORMAT_JSON; // Extract report data from post request $post = Yii::$app->request->post(); // The raw data of the report $rawReport = isset($post) && isset($post['report']) ? $post['report'] : null; // Convert to charts array $charts = Json::decode($rawReport, true); if (isset($charts)) { // Save data in single transaction $transaction = FormChart::getDb()->beginTransaction(); $success = false; try { // Delete old charts if there are FormChart::deleteAll(['form_id' => $id]); if (count($charts) > 0) { // Populate each Form Chart Model and Save foreach ($charts as $chart) { $formChartModel = new FormChart(); // Add form_id to chart $chart['form_id'] = $id; // Prepare new model data $postFormChart = ['FormChart' => $chart]; // Load & Save the model if (!$formChartModel->load($postFormChart) || !$formChartModel->save()) { throw new Exception(Yii::t("app", "Error saving the chart")); } } } $transaction->commit(); // Change success flag and message $success = true; $message = Yii::t("app", "The report has been successfully updated."); } catch (Exception $e) { // Rolls back the transaction $transaction->rollBack(); // Rethrow the exception // throw $e; $message = $e->getMessage(); } $res = array('success' => $success, 'id' => $id, 'action' => 'update', 'message' => $message); return $res; } $submissions = array(); foreach (FormSubmission::find()->select(['data', 'created_at'])->where('form_id=:form_id', [':form_id' => $id])->each(10) as $submissionModel) { $submission = $submissionModel->data; $submission['created_at'] = $submissionModel->created_at; array_push($submissions, $submission); } return $submissions; }