PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Api / Form.php

Form.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.18, at app/Api/Form.php

169 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Api;
4
5 use FluentForm\App\Helpers\Helper;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7
8 class Form
9 {
10 public function forms($atts = [], $withFields = false)
11 {
12 $defaultAtts = [
13 'search' => '',
14 'status' => 'all',
15 'sort_column' => 'id',
16 'filter_by' => 'all',
17 'date_range' => [],
18 'sort_by' => 'DESC',
19 'per_page' => 10,
20 'page' => 1,
21 ];
22
23 $atts = wp_parse_args($atts, $defaultAtts);
24
25 $perPage = ArrayHelper::get($atts, 'per_page', 10);
26 $search = ArrayHelper::get($atts, 'search', '');
27 $status = ArrayHelper::get($atts, 'status', 'all');
28 $filter_by = ArrayHelper::get($atts, 'filter_by', 'all');
29 $dateRange = ArrayHelper::get($atts, 'date_range', []);
30 $is_filter_by_conv_or_step_form = $filter_by && ('conv_form' == $filter_by || 'step_form' == $filter_by);
31
32 $shortColumn = ArrayHelper::get($atts, 'sort_column', 'id');
33 $sortBy = ArrayHelper::get($atts, 'sort_by', 'DESC');
34
35 $query = wpFluent()->table('fluentform_forms')
36 ->orderBy($shortColumn, $sortBy);
37
38 if ($status && 'all' != $status) {
39 $query->where('status', $status);
40 }
41
42 if ($filter_by && !$is_filter_by_conv_or_step_form) {
43 switch ($filter_by) {
44 case 'published':
45 $query->where('status', 'published');
46 break;
47 case 'unpublished':
48 $query->where('status', 'unpublished');
49 break;
50 case 'post':
51 $query->where('type', 'post');
52 break;
53 case 'is_payment':
54 $query->where('has_payment', 1);
55 break;
56 default:
57 break;
58 }
59 }
60
61 if ($dateRange) {
62 $query->where('created_at', '>=', $dateRange[0] . ' 00:00:01');
63 $query->where('created_at', '<=', $dateRange[1] . ' 23:59:59');
64 }
65
66 if ($search) {
67 $query->where(function ($q) use ($search) {
68 $q->where('id', 'LIKE', '%' . $search . '%');
69 $q->orWhere('title', 'LIKE', '%' . $search . '%');
70 });
71 }
72
73 $currentPage = intval(ArrayHelper::get($atts, 'page', 1));
74 $total = $query->count();
75 $skip = $perPage * ($currentPage - 1);
76
77 if ($is_filter_by_conv_or_step_form) {
78 $data = (array) $query->select('*')->get();
79 } else {
80 $data = (array) $query->select('*')->limit($perPage)->offset($skip)->get();
81 }
82
83 $conversationOrStepForms = [];
84 foreach ($data as $form) {
85 $is_conv_form = Helper::isConversionForm($form->id);
86
87 // skip form if filter by conversation form but form is not conversational form
88 if ('conv_form' == $filter_by && !$is_conv_form) {
89 continue;
90 }
91 // skip form if filter by step form but form is not step form
92 if ('step_form' == $filter_by && !Helper::isMultiStepForm($form->id)) {
93 continue;
94 }
95 $formInstance = $this->form($form);
96 $form->preview_url = Helper::getPreviewUrl($form->id, 'classic');
97 $form->edit_url = Helper::getFormAdminPermalink('editor', $form);
98 $form->settings_url = Helper::getFormSettingsUrl($form);
99 $form->entries_url = Helper::getFormAdminPermalink('entries', $form);
100 $form->analytics_url = Helper::getFormAdminPermalink('analytics', $form);
101 $form->total_views = Helper::getFormMeta($form->id, '_total_views', 0);
102 $form->total_Submissions = $formInstance->submissionCount();
103 $form->unread_count = $formInstance->unreadCount();
104 $form->conversion = $formInstance->conversionRate();
105 if ($is_conv_form) {
106 $form->conversion_preview = Helper::getPreviewUrl($form->id, 'conversational');
107 }
108
109 if (!$withFields) {
110 unset($form->form_fields);
111 }
112 if ($is_filter_by_conv_or_step_form) {
113 $conversationOrStepForms[] = $form;
114 }
115 }
116 if ($is_filter_by_conv_or_step_form) {
117 $total = count($conversationOrStepForms);
118 $conversationOrStepForms = array_slice($conversationOrStepForms, $skip, $perPage);
119 $dataCount = count($conversationOrStepForms);
120 } else {
121 $dataCount = count($data);
122 }
123
124 $from = $dataCount > 0 ? ($currentPage - 1) * $perPage + 1 : null;
125
126 $to = $dataCount > 0 ? $from + $dataCount - 1 : null;
127 $lastPage = (int) ceil($total / $perPage);
128 return [
129 'current_page' => $currentPage,
130 'per_page' => $perPage,
131 'from' => $from,
132 'to' => $to,
133 'last_page' => $lastPage,
134 'total' => $total,
135 'data' => $is_filter_by_conv_or_step_form ? $conversationOrStepForms : $data,
136 ];
137 }
138
139 public function find($formId)
140 {
141 return wpFluent()->table('fluentform_forms')->where('id', $formId)->first();
142 }
143
144 /**
145 * Get Form Properties instance
146 *
147 * @param int|object $form
148 *
149 * @return \FluentForm\App\Api\FormProperties
150 */
151 public function form($form)
152 {
153 if (is_numeric($form)) {
154 $form = $this->find($form);
155 }
156
157 return (new FormProperties($form));
158 }
159
160 public function entryInstance($form)
161 {
162 if (is_numeric($form)) {
163 $form = $this->find($form);
164 }
165
166 return (new Entry($form));
167 }
168 }
169