/** * Check if user has reached his submission limit */ public function checkIPLimit() { if ($this->ip_limit === $this::ON) { $startTime = TimeHelper::startTime($this->ip_limit_period); $ip = Yii::$app->getRequest()->getUserIP(); if ($ip === "::1") { // Usefull when app run in localhost $ip = "81.2.69.160"; } $submissions = FormSubmission::find()->select('id')->asArray()->where(['form_id' => $this->id])->andWhere(['between', 'created_at', $startTime, time()])->andWhere(['ip' => $ip])->count(); if ($this->ip_limit_number <= $submissions) { /** @var \yii\web\Response $response */ $response = Yii::$app->getResponse(); $response->format = Response::FORMAT_JSON; $response->data = array('action' => 'submit', 'success' => false, 'id' => 0, 'message' => Yii::t("app", "You have reached your Submission Limit per {period}.", ['period' => TimeHelper::getPeriodByCode($this->ip_limit_period)])); $response->send(); exit; } } }
/** * 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; }
public function uniqueFieldsValidation() { $message = Yii::t("app", "{label} '{value}' has already been taken."); foreach ($this->uniqueFields as $field) { // Only when the input value is not empty if (isset($field["name"]) && trim($this->data[$field["name"]]) !== "") { // Strip whitespace from the beginning and end of a string $value = trim($this->data[$field["name"]]); // Search "fieldName":"fieldValue" $query = FormSubmission::find()->where('form_id=:form_id', [':form_id' => $this->dataModel->form_id])->andWhere(['like', 'data', '"' . $field["name"] . '":"' . $value . '"']); if ($query->count() > 0) { $this->addError($field["name"], $field["label"], $value, $message); } } } }
/** * Export form submissions. * * @param integer $id */ public function actionExportSubmissions($id) { $formModel = $this->findFormModel($id); $formDataModel = $formModel->formData; $query = FormSubmission::find()->select(['data', 'created_at'])->where('form_id=:form_id', [':form_id' => $id])->asArray(); // Create the CSV into memory $csv = Writer::createFromFileObject(new SplTempFileObject()); // Insert fields names as the CSV header $header = array_values($formDataModel->getLabels()); array_push($header, 'Submitted at'); $csv->insertOne($header); // To iterate the row one by one foreach ($query->each() as $submission) { // $submission represents one row of data from the form_submission table $data = Json::decode($submission['data'], true); foreach ($data as &$field) { if (is_array($field)) { $field = implode(', ', $field); } } $data["created_at"] = Yii::$app->formatter->asDatetime($submission['created_at']); $csv->insertOne($data); } // Print to the output stream $csv->output($formModel->name . '.csv'); }
public function actionDeleteall() { // Get ids param $request = Yii::$app->getRequest(); $id = $request->post('id'); $ids = $request->post('ids'); // Default $success = false; $message = "No items matched the query"; $itemsDeleted = 0; try { // The number of rows deleted $itemsDeleted = 0; // Delete one to one for trigger events foreach (FormSubmission::find()->where(['id' => $ids, 'form_id' => $id])->all() as $submissionModel) { $deleted = $submissionModel->delete(); if ($deleted) { $itemsDeleted++; } } // Set response if ($itemsDeleted > 0) { $success = true; $message = Yii::t("app", "Items deleted successfully"); } } catch (\Exception $e) { // Rethrow the exception // throw $e; $message = $e->getMessage(); } // Response fornat Yii::$app->response->format = Response::FORMAT_JSON; // Response to Client $res = array('success' => $success, 'action' => 'deleteall', 'itemsDeleted' => $itemsDeleted, 'ids' => $ids, 'message' => $message); return $res; }