request = $application->request; $this->model = wpFluent()->table('fluentform_forms'); } /** * Get all forms from database * * @return void * @throws \Exception */ public function index() { $search = $this->request->get('search'); $status = $this->request->get('status'); $query = $this->model->orderBy('created_at', 'DESC'); if($status && $status != 'all') { $query->where('status', $status); } if($search) { $query->where(function($q) use ($search) { $q->where('id', 'LIKE', '%'.$search.'%'); $q->where('title', 'LIKE', '%'.$search.'%'); }); } $forms = $query->paginate(); foreach ($forms['data'] as $form) { $form->preview_url = site_url('?fluentform_pages=1&preview_id='.$form->id).'#ff_preview';; $form->edit_url = $this->getAdminPermalink('editor', $form->id); $form->entries_url = $this->getAdminPermalink('entries', $form->id); $form->analytics_url = $this->getAdminPermalink('analytics', $form->id); $form->total_views = $this->getFormViewCount($form->id); $form->total_views = $this->getFormViewCount($form->id); $form->total_Submissions = $this->getSubmissionCount($form->id); $form->conversion = $this->getConversionRate($form); } wp_send_json($forms, 200); } private function getFormViewCount($formId) { $hasCount = wpFluent() ->table('fluentform_form_meta') ->where('meta_key', '_total_views') ->where('form_id', $formId) -> first(); if($hasCount) return intval($hasCount->value); return 0; } private function getSubmissionCount($formID) { return wpFluent()->table('fluentform_submissions')->where('form_id', $formID)->count(); } private function getConversionRate($form) { if(!$form->total_Submissions) return 0; if(!$form->total_views) return 0; return ceil(($form->total_Submissions / $form->total_views) * 100); } /** * Create a form from backend/editor * @return void */ public function store() { $type = $this->request->get('type', 'form'); $title = $this->request->get('title', 'My New Form'); $status = $this->request->get('status', 'published'); $createdBy = get_current_user_id(); //$this->validate(); $now = date("Y-m-d H:i:s"); $insertData = [ 'title' => $title, 'type' => $type, 'status' => $status, 'created_by' => $createdBy, 'created_at' => $now, 'updated_at' => $now ]; if ($this->formFields) { $insertData['form_fields'] = $this->formFields; } $formId = $this->model->insert($insertData); // add default form settings now $defaultSettings = $this->defaultSettings ?: $this->getFormsDefaultSettings($formId); wpFluent()->table('fluentform_form_meta') ->insert(array( 'form_id' => $formId, 'meta_key' => 'formSettings', 'value' => json_encode($defaultSettings) )); do_action('fluentform_inserted_new_form', $formId, $insertData); wp_send_json_success(array( 'formId' => $formId, 'redirect_url' => admin_url('admin.php?page=fluent_forms&form_id='.$formId.'&route=editor'), 'message' => __('Successfully created a form.', 'fluentform') ), 200); } private function getFormsDefaultSettings($formId = false) { $defaultSettings = array( 'confirmation' => array( 'redirectTo' => 'samePage', 'messageToShow' => 'Thank you for your message. We will get in touch with you shortly', 'customPage' => null, 'samePageFormBehavior' => 'hide_form', 'customUrl' => null ), 'restrictions' => array( 'limitNumberOfEntries' => array( 'enabled' => false, 'numberOfEntries' => null, 'period' => 'total', 'limitReachedMsg' => null ), 'scheduleForm' => array( 'enabled' => false, 'start' => null, 'end' => null, 'pendingMsg' => "Form submission is not started yet.", 'expiredMsg' => "Form submission is now closed." ), 'requireLogin' => array( 'enabled' => false, 'requireLoginMsg' => null, ), 'denyEmptySubmission' => [ 'enabled' => false, 'message' => 'Sorry, you cannot submit an empty form. Let\'s hear what you wanna say.' ] ), 'layout' => array( 'labelPlacement' => 'top', 'helpMessagePlacement' => 'with_label', 'errorMessagePlacement' => 'inline', 'cssClassName' => '' ) ); $globalSettings = get_option('_fluentform_global_form_settings'); if(isset($globalSettings['layout'])) { $defaultSettings['layout'] = $globalSettings['layout']; } return $defaultSettings; } /** * Find/Read a from from the database * @return void */ public function find() { $formId = $this->request->get('formId'); $form = $this->model->find($formId); wp_send_json(['form' => $form, 'metas' => []], 200); } /** * Save/update a form from backend/editor * @return void */ public function update() { $formId = $this->request->get('formId'); $title = $this->request->get('title'); $formFields = $this->request->get('formFields'); $status = $this->request->get('status', 'Draft'); $this->validate(); $this->model->where('id', $formId)->update([ 'title' => $title, 'status' => $status, 'form_fields' => $formFields, 'updated_at' => date('Y-m-d h:i:s') ]); wp_send_json([ 'message' => __('The form is successfully updated.', 'fluentform') ], 200); } /** * Delete a from from database * @return void */ public function delete() { $formId = $this->request->get('formId'); $this->model->where('id', $formId)->delete(); wp_send_json([ 'message' => __('Successfully deleted the form.', 'fluentform') ], 200); } /** * Validate a form only by form title * @return void */ private function validate() { if (! $this->request->get('title')) { wp_send_json([ 'title' => 'The title field is required.' ], 423); } } private function getAdminPermalink($route, $formId) { $baseUrl = admin_url('admin.php?page=fluent_forms'); return $baseUrl.'&route='.$route."&form_id=".$formId; } public function getAllForms() { $forms = $this->model->orderBy('created_at', 'DESC')->get(); wp_send_json($forms, 200); } }