PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.1.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.1.1
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Database / FormEntryMetaModel.php

FormEntryMetaModel.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 1.1.1, at includes/Core/Database/FormEntryMetaModel.php

313 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Provides Base Model Class
5 */
6
7 namespace BitCode\BitForm\Core\Database;
8
9 /**
10 * Undocumented class
11 */
12
13 use BitCode\BitForm\Core\Database\Model;
14
15 class FormEntryMetaModel extends Model
16 {
17 protected static $table = 'bitforms_form_entrymeta';
18
19
20 public function duplicateEntryMeta($data)
21 {
22 $values[] = $data['duplicateID'];
23 $values[] = $data['entryID'];
24 $sql = "INSERT INTO $this->table_name (bitforms_form_entry_id,meta_key,meta_value)"
25 . " SELECT %d as bitforms_form_entry_id,meta_key,meta_value"
26 . " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d";
27 return $this->execute($sql, $values)->getResult();
28 }
29
30 public function update(array $data, array $condition)
31 {
32 $entryID = $condition['bitforms_form_entry_id'];
33 if (empty($entryID)) {
34 return false;
35 }
36 $formEntryMeta = $this->get(
37 array(
38 'meta_key',
39 'meta_value'
40 ),
41 array(
42 'bitforms_form_entry_id' => $entryID
43 )
44 );
45 $oldEntries = array();
46 foreach ($formEntryMeta as $oldKey => $oldValue) {
47 $oldEntries[$oldValue->meta_key] = $oldValue->meta_value;
48 }
49 $updatedData = array();
50 $oldEntriesKey = array_keys($oldEntries);
51 foreach ($data as $upKey => $upValue) {
52 $updatedData[$upKey] = is_string($upValue) ?
53 $upValue :
54 wp_json_encode($upValue);
55 if (!\in_array($upKey, $oldEntriesKey)) {
56 $this->insert(
57 array(
58 'bitforms_form_entry_id' => $entryID,
59 'meta_key' => $upKey,
60 'meta_value' => $updatedData[$upKey]
61 )
62 );
63 unset($data[$upKey]);
64 }
65 }
66 if (empty($data)) {
67 return $updatedData;
68 }
69 $checkCondition = $this->checkCondition($condition);
70 if (is_wp_error($checkCondition)) {
71 return $checkCondition;
72 }
73 $case_part = ' ';
74 $all_values = array();
75 $condition['meta_key'] = array_keys($data);
76 foreach ($data as $key => $value) {
77 $value = is_string($value) ? $value : wp_json_encode($value);
78 $case_part .= "
79 WHEN '$key' THEN " . $this->getFieldFormat($value);
80 $all_values[] = $value;
81 }
82 $formattedCondition = $this->getFormatedCondition($condition);
83 if ($formattedCondition) {
84 $condition_to_check = $formattedCondition['conditions'];
85 $all_values = array_merge($all_values, $formattedCondition['values']);
86 } else {
87 $condition_to_check = null;
88 }
89
90 $sql = "UPDATE $this->table_name
91 SET meta_value = ( CASE meta_key
92 $case_part
93 END
94 )";
95 $sql .= " " . $condition_to_check;
96 $status = $this->execute($sql, $all_values)->getResult();
97 if (is_wp_error($status) && $status->get_error_code() !== 'result_empty') {
98 return $status;
99 }
100 return $updatedData;
101 }
102
103 public function getEntryMeta($formFields, $entries, $limit = null, $offset = null, $filter = null, $sortBy = null)
104 {
105 $selectedMeta = "`bitforms_form_entry_id` as entry_id,";
106 $all_values = array();
107 $metaChecker = 0;
108 $fieldCount = count($formFields);
109 $globalFilterString = '';
110 $globalFilterValues = array();
111 $formFieldsNames = array();
112 foreach ($formFields as $fieldKey => $fieldDetails) {
113 $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
114 $selectedMeta .= "GROUP_CONCAT(
115 CASE
116 `meta_key`
117 WHEN '$fieldFormat' THEN `meta_value`
118 END
119 ) AS '$fieldFormat'";
120 $metaChecker += 1;
121 $all_values[] = $fieldDetails['key'];
122 $all_values[] = $fieldDetails['key'];
123 $formFieldsNames[] = $fieldDetails['key'];
124 if ($metaChecker < $fieldCount) {
125 $selectedMeta .= ",";
126 }
127
128 if (!empty($filter['global'])) {
129 $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat($filter['global']) . "%%' ";
130 if ($metaChecker < $fieldCount) {
131 $globalFilterString .= " OR ";
132 }
133 $globalFilterValues[] = $filter['global'];
134 }
135 }
136 $entryIDs = array();
137 $entryCount = count($entries);
138 $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']);
139 $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries;
140 foreach ($entries as $entryKey => $entryDetail) {
141 $entryIDs[] = $entryDetail->id;
142 }
143 if (empty($entryIDs)) {
144 return array(
145 'count' => 0,
146 'entries' => [],
147 );
148 }
149 $condition['bitforms_form_entry_id'] = $entryIDs;
150 $formattedCondition = $this->getFormatedCondition($condition);
151 if ($formattedCondition) {
152 $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
153 `bitforms_form_entry_id` ";
154 $all_values = array_merge($all_values, $formattedCondition['values']);
155 } else {
156 $groupedCondition = null;
157 }
158 $isRecount = false;
159 if (!empty($filter['field'])) {
160 $isRecount = true;
161 $filterFieldCount = count($filter['field']);
162 $filterFieldChecker = 0;
163 if ($filterFieldCount > 0) {
164 $groupedCondition .= " HAVING ";
165 }
166 foreach ($filter['field'] as $filterFieldKey => $filterFieldDetails) {
167 $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat($filterFieldDetails->value) . "%%'";
168 $all_values[] = $filterFieldDetails->value;
169 if ($filterFieldChecker < $filterFieldCount) {
170 $groupedCondition .= " AND ";
171 }
172 }
173 }
174 if (!empty($filter['global']) && !empty($globalFilterString) && !empty($globalFilterValues)) {
175 $isRecount = true;
176 if (!empty($filter['field'])) {
177 $groupedCondition .= " AND (" . $globalFilterString . ") ";
178 } else {
179 $groupedCondition .= " HAVING $globalFilterString ";
180 }
181 $offset = 0;
182 $all_values = array_merge($all_values, $globalFilterValues);
183 }
184 $orderCondition = null;
185 if (!empty($sortBy)) {
186 $sortableFieldCount = count($sortBy);
187 $sortableFieldChecker = 0;
188 if ($sortableFieldCount > 0) {
189 $orderCondition .= " ORDER BY ";
190 }
191 $orderList = '';
192 foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) {
193 // $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id);
194 $sortableFieldChecker += 1;
195 if (in_array($sortableFieldDetails->id, $formFieldsNames)) {
196 $orderList .= " `$sortableFieldDetails->id` ";
197 $orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC ';
198 $orderList .= " " . $orderFollow;
199 if ($sortableFieldChecker < $sortableFieldCount) {
200 $orderList .= ", ";
201 }
202 }
203 }
204 if (empty($orderList)) {
205 $orderCondition = null;
206 } else {
207 $orderCondition .= $orderList;
208 }
209 }
210 $orderCondition .= empty($orderCondition) ? " ORDER BY `bitforms_form_entry_id` DESC " : ",`bitforms_form_entry_id` DESC ";
211 $paginate = null;
212 if (!\is_null($limit) && !$paginateEntry) {
213 $limit = \intval($limit);
214 $paginate .= " LIMIT $limit ";
215 }
216 if (!\is_null($offset) && !$paginateEntry) {
217 $offset = \intval($offset);
218 $paginate .= " OFFSET $offset ";
219 }
220 $sql = "SELECT $selectedMeta FROM `$this->table_name`"
221 . $groupedCondition . $orderCondition . $paginate;
222 // echo $sql;
223 $result = $this->execute($sql, $all_values)->getResult();
224 if (is_wp_error($result) && $result->get_error_code() === 'result_empty') {
225 return array(
226 'count' => $paginateEntry ? $entryCount : 0,
227 'entries' => [],
228 );
229 }
230 // var_dump('test');
231 if ($isRecount) {
232 $sql = "SELECT count(*) as count FROM (";
233 $sql .= "SELECT $selectedMeta FROM `$this->table_name`";
234 $sql .= $groupedCondition . $orderCondition;
235 $sql .= ") as retrievedData";
236 $countResult = $this->execute($sql, $all_values)->getResult();
237 if (!(is_wp_error($result) && $result->get_error_code() === 'result_empty')) {
238 $entryCount = $countResult[0]->count;
239 }
240 }
241 $resultedEntries = array(
242 'count' => $entryCount,
243 'entries' => $result,
244 );
245 return $resultedEntries;
246 }
247
248 public function getExportEntry($formFields, $entries, $limit = null, $sortBy = null, $sortByField = null, $filter = null)
249 {
250 $selectedEntryMeta = null;
251 $fieldCount = count($formFields);
252 $metaChecker = 0;
253 $all_values = array();
254 // $selected_fields = ['bf29-1-','bf29-2-','bf29-6'];
255 $formFieldsNames = array();
256 foreach ($formFields as $fieldKey => $fieldDetails) {
257 $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
258 $selectedEntryMeta .= "GROUP_CONCAT(
259 CASE
260 `meta_key`
261 WHEN '$fieldFormat' THEN `meta_value`
262 END
263 ) AS '$fieldFormat'";
264 $metaChecker += 1;
265 $all_values[] = $fieldDetails['key'];
266 $all_values[] = $fieldDetails['key'];
267 $formFieldsNames[] = $fieldDetails['key'];
268 if ($metaChecker < $fieldCount) {
269 $selectedEntryMeta .= ",";
270 }
271 }
272 $entryIDs = array();
273 foreach ($entries as $entryKey => $entryDetail) {
274 $entryIDs[] = $entryDetail->id;
275 }
276 if (empty($entryIDs)) {
277 return array(
278 'count' => 0,
279 'entries' => [],
280 );
281 }
282 $condition['bitforms_form_entry_id'] = $entryIDs;
283 $formattedCondition = $this->getFormatedCondition($condition);
284 $groupedCondition = null;
285 if ($formattedCondition) {
286 $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
287 `bitforms_form_entry_id` ";
288 $all_values = array_merge($all_values, $formattedCondition['values']);
289 }
290 $order = \is_null($sortBy) ? "DESC " : "$sortBy";
291 $orderField = \is_null($sortByField) ? "bitforms_form_entry_id " : "`$sortByField`";
292 $orderCondition = "ORDER BY $orderField $order ";
293 if (!\is_null($limit)) {
294 $limitInt = \intval($limit);
295 $limit = " LIMIT $limitInt ";
296 }
297 $sql = "SELECT $selectedEntryMeta FROM `$this->table_name`"
298 . $groupedCondition . $orderCondition . $limit;
299 $result = $this->execute($sql, $all_values)->getResult();
300 $allData = [];
301 foreach ($result as $key => $value) {
302 foreach ($formFieldsNames as $formFieldName) {
303 $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
304 }
305 }
306 if (is_wp_error($result)) {
307 wp_send_json_error('Internal server error', 500);
308 } else {
309 wp_send_json_success($allData, 200);
310 }
311 }
312 }
313