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

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