PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.4
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.4, at includes/Core/Database/FormEntryMetaModel.php

385 lines 15.7 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 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
106 $selectedMeta = "`bitforms_form_entry_id` as entry_id,";
107 $selectedMeta .= "e.user_id as '__user_id',";
108 $selectedMeta .= "e.user_ip as '__user_ip',";
109 $selectedMeta .= "e.user_location as '__user_location',";
110 $selectedMeta .= "e.user_device as '__user_device',";
111 $selectedMeta .= "e.referer as '__referer',";
112 $selectedMeta .= "e.created_at as '__created_at',";
113 $selectedMeta .= "e.updated_at as '__updated_at',";
114
115 $all_values = array();
116 $metaChecker = 0;
117 $fieldCount = count($formFields);
118 $globalFilterString = '';
119 $globalFilterValues = array();
120 $formFieldsNames = array();
121 foreach ($formFields as $fieldKey => $fieldDetails) {
122 $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
123 $selectedMeta .= "GROUP_CONCAT(
124 CASE
125 `meta_key`
126 WHEN '$fieldFormat' THEN `meta_value`
127 END
128 ) AS '$fieldFormat'";
129 $metaChecker += 1;
130 $all_values[] = $fieldDetails['key'];
131 $all_values[] = $fieldDetails['key'];
132 $formFieldsNames[] = $fieldDetails['key'];
133 if ($metaChecker < $fieldCount) {
134 $selectedMeta .= ",";
135 }
136
137 if (!empty($filter['global'])) {
138 $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat($filter['global']) . "%%' ";
139 if ($metaChecker < $fieldCount) {
140 $globalFilterString .= " OR ";
141 }
142 $globalFilterValues[] = $filter['global'];
143 }
144 }
145 $entryIDs = array();
146 $entryCount = count($entries);
147 $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']);
148 $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries;
149 foreach ($entries as $entryKey => $entryDetail) {
150 $entryIDs[] = $entryDetail->id;
151 }
152 if (empty($entryIDs)) {
153 return array(
154 'count' => 0,
155 'entries' => [],
156 );
157 }
158 $condition['bitforms_form_entry_id'] = $entryIDs;
159 $formattedCondition = $this->getFormatedCondition($condition);
160 if ($formattedCondition) {
161 $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
162 `bitforms_form_entry_id` ";
163 $all_values = array_merge($all_values, $formattedCondition['values']);
164 } else {
165 $groupedCondition = null;
166 }
167 $isRecount = false;
168 if (!empty($filter['field'])) {
169 $isRecount = true;
170 $filterFieldCount = count($filter['field']);
171 $filterFieldChecker = 0;
172 if ($filterFieldCount > 0) {
173 $groupedCondition .= " HAVING ";
174 }
175 foreach ($filter['field'] as $filterFieldKey => $filterFieldDetails) {
176 $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat($filterFieldDetails->value) . "%%'";
177 $all_values[] = $filterFieldDetails->value;
178 if ($filterFieldChecker < $filterFieldCount) {
179 $groupedCondition .= " AND ";
180 }
181 }
182 }
183 if (!empty($filter['global']) && !empty($globalFilterString) && !empty($globalFilterValues)) {
184 $isRecount = true;
185 if (!empty($filter['field'])) {
186 $groupedCondition .= " AND (" . $globalFilterString . ") ";
187 } else {
188 $groupedCondition .= " HAVING $globalFilterString ";
189 }
190 $offset = 0;
191 $all_values = array_merge($all_values, $globalFilterValues);
192 }
193 $orderCondition = null;
194 if (!empty($sortBy)) {
195 $sortableFieldCount = count($sortBy);
196 $sortableFieldChecker = 0;
197 if ($sortableFieldCount > 0) {
198 $orderCondition .= " ORDER BY ";
199 }
200 $orderList = '';
201 foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) {
202 // $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id);
203 $sortableFieldChecker += 1;
204 if (in_array($sortableFieldDetails->id, $formFieldsNames)) {
205 $orderList .= " `$sortableFieldDetails->id` ";
206 $orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC ';
207 $orderList .= " " . $orderFollow;
208 if ($sortableFieldChecker < $sortableFieldCount) {
209 $orderList .= ", ";
210 }
211 }
212 }
213 if (empty($orderList)) {
214 $orderCondition = null;
215 } else {
216 $orderCondition .= $orderList;
217 }
218 }
219 $orderCondition .= empty($orderCondition) ? " ORDER BY `bitforms_form_entry_id` DESC " : ",`bitforms_form_entry_id` DESC ";
220 $paginate = null;
221 if (!\is_null($limit) && !$paginateEntry) {
222 $limit = \intval($limit);
223 $paginate .= " LIMIT $limit ";
224 }
225 if (!\is_null($offset) && !$paginateEntry) {
226 $offset = \intval($offset);
227 $paginate .= " OFFSET $offset ";
228 }
229 $sql = "SELECT $selectedMeta FROM `$this->table_name` em";
230 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
231 $sql .= $groupedCondition . $orderCondition . $paginate;
232 // echo $sql;
233 $result = $this->execute($sql, $all_values)->getResult();
234 if (is_wp_error($result)) {
235 return array(
236 'count' => $paginateEntry ? $entryCount : 0,
237 'entries' => [],
238 'error' => $result->get_error_message()
239 );
240 }
241 if ($isRecount) {
242 $sql = "SELECT count(*) as count FROM (";
243 $sql .= "SELECT $selectedMeta FROM `$this->table_name`";
244 $sql .= $groupedCondition . $orderCondition;
245 $sql .= ") as retrievedData";
246 $countResult = $this->execute($sql, $all_values)->getResult();
247 if (!(is_wp_error($result) && $result->get_error_code() === 'result_empty')) {
248 $entryCount = $countResult[0]->count;
249 }
250 }
251 $resultedEntries = array(
252 'count' => $entryCount,
253 'entries' => $result,
254 );
255 return $resultedEntries;
256 }
257 public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit = null, $sortBy = null, $sortByField = null)
258 {
259 $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
260 $selectedEntryMeta = "`bitforms_form_entry_id` as entry_id,";
261 $selectedEntryMeta .= "e.user_id as '__user_id',";
262 $selectedEntryMeta .= "e.user_ip as '__user_ip',";
263 $selectedEntryMeta .= "e.user_location as '__user_location',";
264 $selectedEntryMeta .= "e.user_device as '__user_device',";
265 $selectedEntryMeta .= "e.referer as '__referer',";
266 $selectedEntryMeta .= "e.created_at as '__created_at',";
267 $selectedEntryMeta .= "e.updated_at as '__updated_at',";
268 $metaChecker = 0;
269
270 $entryInfo = ['__user_id','__user_ip',/* '__user_location', */'__user_device',
271 '__referer','__created_at','__updated_at'];
272 $all_values = array();
273 if ($formFields == []) {
274 $data = array(
275 'count' => 0,
276 'entries' => [],
277 );
278 wp_send_json_success($data, 200);
279 }
280 $fieldCount = count($formFields) - count(array_intersect($formFields, $entryInfo));
281 $formFieldsNames = array();
282 foreach ($formFields as $fldKey) {
283 $formFieldsNames[] = $fldKey;
284 if (in_array($fldKey, $entryInfo)) {
285 continue;
286 }
287 $fieldFormat = $this->getFieldFormat($fldKey);
288 $selectedEntryMeta .= "GROUP_CONCAT(
289 CASE
290 `meta_key`
291 WHEN '$fieldFormat' THEN `meta_value`
292 END
293 ) AS '$fieldFormat'";
294 $metaChecker += 1;
295 $all_values[] = $fldKey;
296 $all_values[] = $fldKey;
297 if ($metaChecker < $fieldCount) {
298 $selectedEntryMeta .= ",";
299 }
300 }
301 $entryIDs = array();
302 foreach ($entries as $entryKey => $entryDetail) {
303 $entryIDs[] = $entryDetail->id;
304 }
305 if (empty($entryIDs)) {
306 return array(
307 'count' => 0,
308 'entries' => [],
309 );
310 }
311 $condition['bitforms_form_entry_id'] = $entryIDs;
312 $formattedCondition = $this->getFormatedCondition($condition);
313 $groupedCondition = null;
314 if ($formattedCondition) {
315 $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
316 `bitforms_form_entry_id` ";
317 $all_values = array_merge($all_values, $formattedCondition['values']);
318 }
319 $order = \is_null($sortBy) ? "DESC " : "$sortBy";
320 $orderField = \is_null($sortByField) ? "bitforms_form_entry_id" : "`$sortByField`";
321
322 $orderCondition = "ORDER BY $orderField $order ";
323 if (!\is_null($limit)) {
324 $limitInt = \intval($limit);
325 $limit = " LIMIT $limitInt ";
326 }
327 $sql = "SELECT $selectedEntryMeta FROM `$this->table_name` em";
328 $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
329 $sql .= $groupedCondition . $orderCondition . $limit;
330
331 $result = $this->execute($sql, $all_values)->getResult();
332 $allData = [];
333 $entry_id = "entry_id";
334 $users = get_users(['fields' => ['ID', 'display_name']]);
335 $userNames = [];
336 foreach ($users as $key => $value) {
337 $userNames[$value->ID] = $value->display_name;
338 }
339 foreach ($result as $key => $value) {
340 foreach ($formFieldsNames as $formFieldName) {
341 $allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->$entry_id);
342 if ($formFieldName === '__user_id' && intval($value->$formFieldName) > 0) {
343 $allData[$key][$formFieldName] = $userNames[$value->$formFieldName];
344 } elseif ($formFieldName === '__user_ip') {
345 $allData[$key][$formFieldName] = long2ip($value->$formFieldName);
346 } else {
347 $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
348 }
349 }
350 }
351
352 if (is_wp_error($result)) {
353 wp_send_json_error('Internal server error', 500);
354 } else {
355 foreach ($fieldLabels as $field) {
356 foreach ($allData as $index => $entry) {
357 if (array_key_exists($field['key'], $entry) && $field['type'] == 'file-up') {
358 $key = $field['key'];
359 if (empty($entry[$key])) {
360 continue;
361 }
362 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entry['entry_id'];
363 if (is_array(explode(",", $entry[$key]))) {
364 $fileData = array();
365 foreach (explode(",", $entry[$key]) as $file) {
366 $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=$file";
367 if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file)) {
368 $fileData[] = site_url($path, null);
369 }
370 }
371 $allData[$index][$key] = implode(',', $fileData);
372 } else {
373 $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=" . $entry[$key];
374 if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $entry[$key])) {
375 $allData[$index][$key] = site_url($path, null);
376 }
377 }
378 }
379 }
380 }
381 wp_send_json_success($allData, 200);
382 }
383 }
384 }
385