PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.17.6
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.17.6
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
← All changes | includes/Core/Database/FormEntryMetaModel.php +518 -272 1.32.17.6 View file →
@@ -5,315 +5,561 @@
5 5 */
6 6
7 7 namespace BitCode\BitForm\Core\Database;
8 8
9 +use BitCode\BitForm\Core\Util\FileHandler;
10 +
9 11 /**
10 12 * Undocumented class
11 13 */
12 14
13 -use BitCode\BitForm\Core\Database\Model;
14 -
15 15 class FormEntryMetaModel extends Model
16 16 {
17 - protected static $table = 'bitforms_form_entrymeta';
17 + protected static $table = 'bitforms_form_entrymeta';
18 18
19 + public function duplicateEntryMeta($data)
20 + {
21 + $values[] = $data['duplicateID'];
22 + $values[] = $data['entryID'];
23 + $sql = "INSERT INTO $this->table_name (bitforms_form_entry_id,meta_key,meta_value)"
24 + . ' SELECT %d as bitforms_form_entry_id,meta_key,meta_value'
25 + . " FROM `$this->table_name` WHERE bitforms_form_entry_id = %d";
26 + return $this->execute($sql, $values)->getResult();
27 + }
19 28
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();
29 + public function update(array $data, array $condition)
30 + {
31 + $entryID = $condition['bitforms_form_entry_id'];
32 + if (empty($entryID)) {
33 + return false;
28 34 }
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 - )
35 + $formEntryMeta = $this->get(
36 + [
37 + 'meta_key',
38 + 'meta_value',
39 + ],
40 + [
41 + 'bitforms_form_entry_id' => $entryID,
42 + ]
43 + );
44 + $oldEntries = [];
45 + foreach ($formEntryMeta as $oldKey => $oldValue) {
46 + $oldEntries[$oldValue->meta_key] = $oldValue->meta_value;
47 + }
48 + $updatedData = [];
49 + $oldEntriesKey = array_keys($oldEntries);
50 + foreach ($data as $upKey => $upValue) {
51 + $updatedData[$upKey] = is_string($upValue) ?
52 + $upValue :
53 + wp_json_encode($upValue);
54 + if (!\in_array($upKey, $oldEntriesKey)) {
55 + $this->insert(
56 + [
57 + 'bitforms_form_entry_id' => $entryID,
58 + 'meta_key' => $upKey,
59 + 'meta_value' => $updatedData[$upKey],
60 + ]
44 61 );
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 .= "
62 + unset($data[$upKey]);
63 + }
64 + }
65 + if (empty($data)) {
66 + return $updatedData;
67 + }
68 + $checkCondition = $this->checkCondition($condition);
69 + if (is_wp_error($checkCondition)) {
70 + return $checkCondition;
71 + }
72 + $case_part = ' ';
73 + $all_values = [];
74 + $condition['meta_key'] = array_keys($data);
75 + foreach ($data as $key => $value) {
76 + $value = is_string($value) ? $value : wp_json_encode($value);
77 + $case_part .= "
79 78 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 - }
79 + $all_values[] = $value;
80 + }
81 + $formattedCondition = $this->getFormatedCondition($condition);
82 + if ($formattedCondition) {
83 + $condition_to_check = $formattedCondition['conditions'];
84 + $all_values = array_merge($all_values, $formattedCondition['values']);
85 + } else {
86 + $condition_to_check = null;
87 + }
89 88
90 - $sql = "UPDATE $this->table_name
89 + $sql = "UPDATE $this->table_name
91 90 SET meta_value = ( CASE meta_key
92 91 $case_part
93 92 END
94 93 )";
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;
94 + $sql .= ' ' . $condition_to_check;
95 + $status = $this->execute($sql, $all_values)->getResult();
96 + if (is_wp_error($status) && 'result_empty' !== $status->get_error_code()) {
97 + return $status;
98 + }
99 + return $updatedData;
100 + }
101 +
102 + public function validQueryCondition($conditions)
103 + {
104 + foreach ($conditions as $index => $condition) {
105 + if (is_object($condition)) {
106 + if (empty($condition->field) || empty($condition->logic) || empty($condition->val) && !in_array($condition->val, ['0', 0, 0.0], true)) {
107 + unset($conditions[$index], $conditions[$index + 1]);
99 108 }
100 - return $updatedData;
109 + }
101 110 }
111 + return $conditions;
112 + }
102 113
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(
114 + public function sqlQryGenerateByFldCondition($conditions)
115 + {
116 + $dbHelper = new Helper();
117 + $sql = '';
118 +
119 + $dateQuery = $dbHelper->dateQueryList();
120 +
121 + $validCondtions = $this->validQueryCondition($conditions);
122 +
123 + foreach ($validCondtions as $condition) {
124 + if (is_object($condition)) {
125 + if (is_array($condition->val)) {
126 + $value = $dbHelper->arrValueModifyByLogic($condition->logic, $condition->val);
127 + } else {
128 + $value = $dbHelper->strValueModifyByLogic($condition->logic, $condition->val);
129 + }
130 +
131 + $operator = $dbHelper->convertToSqlOperator($condition->logic);
132 + if (!is_array($condition->val) && isset($dateQuery[$condition->val])) {
133 + $sql .= $dbHelper->fieldQueryByDate($condition->field, $operator, $value, $condition->logic);
134 + } else {
135 + if (!is_int($value)) {
136 + $value = "'" . $value . "'";
137 + }
138 +
139 + $sql .= "`$condition->field` $operator $value";
140 + }
141 + } else {
142 + $sql .= ' ' . $condition;
143 + }
144 + }
145 +
146 + return trim($sql);
147 + }
148 +
149 + public function queryRecount($selectedMeta, $groupedCondition, $orderCondition, $all_values)
150 + {
151 + $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
152 + $sql = 'SELECT count(*) as count FROM (';
153 + $sql .= "SELECT $selectedMeta FROM `$this->table_name` em";
154 + $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
155 + $sql .= $groupedCondition . $orderCondition;
156 + $sql .= ') as retrievedData';
157 + $countResult = $this->execute($sql, $all_values)->getResult();
158 + return $countResult[0]->count;
159 + }
160 +
161 + public function selectedEntryMeta($formFields, $fieldCount)
162 + {
163 + $all_values = [];
164 + $formFieldsNames = [];
165 + $metaChecker = 0;
166 + $selectedMeta = '`bitforms_form_entry_id` as entry_id,';
167 + $selectedMeta .= "e.user_id as '__user_id',";
168 + $selectedMeta .= "e.user_ip as '__user_ip',";
169 + $selectedMeta .= "e.status as '__entry_status',";
170 + $selectedMeta .= "e.user_location as '__user_location',";
171 + $selectedMeta .= "e.user_device as '__user_device',";
172 + $selectedMeta .= "e.referer as '__referer',";
173 + $selectedMeta .= "e.created_at as '__created_at',";
174 + $selectedMeta .= "e.updated_at as '__updated_at'";
175 +
176 + if ($fieldCount > 0) {
177 + $selectedMeta .= ',';
178 + }
179 +
180 + foreach ($formFields as $fieldDetails) {
181 + $fieldFormat = $this->getFieldFormat($fieldDetails['key']);
182 + $selectedMeta .= "GROUP_CONCAT(
115 183 CASE
116 184 `meta_key`
117 185 WHEN '$fieldFormat' THEN `meta_value`
118 186 END
119 187 ) 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 - }
188 + $metaChecker += 1;
189 + $all_values[] = $fieldDetails['key'];
190 + $all_values[] = $fieldDetails['key'];
191 + $formFieldsNames[] = $fieldDetails['key'];
192 + if ($metaChecker < $fieldCount) {
193 + $selectedMeta .= ',';
194 + }
195 + //#unused code commented by me##
196 + // if ( !empty( $filter['global'] ) ) {
197 + // $globalFilterString .= " `" . $fieldDetails['key'] . "` LIKE '%%" . $this->getFieldFormat( $filter['global'] ) . "%%' ";
198 + // if ( $metaChecker < $fieldCount ) {
199 + // $globalFilterString .= " OR ";
200 + // }
201 + // $globalFilterValues[] = $filter['global'];
202 + // }
203 + }
127 204
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 - }
205 + return [
206 + 'selected_meta' => $selectedMeta,
207 + 'form_fields_names' => $formFieldsNames,
208 + 'all_values' => $all_values,
209 + ];
210 + }
211 +
212 + public function groupedCondition($condition, $all_values, $fieldConditions)
213 + {
214 + $isFldCondition = false;
215 + $formattedCondition = $this->getFormatedCondition($condition);
216 + if ($formattedCondition) {
217 + $groupedCondition = $formattedCondition['conditions'] . 'GROUP BY
218 + `bitforms_form_entry_id` ';
219 + $all_values = array_merge($all_values, $formattedCondition['values']);
220 + } else {
221 + $groupedCondition = null;
222 + }
223 + //#unused code commented by me##
224 + //$isRecount = false;
225 +
226 + // if ( !empty( $filter['field'] ) ) {
227 + // $isRecount = true;
228 + // $filterFieldCount = count( $filter['field'] );
229 + // $filterFieldChecker = 0;
230 + // if ( $filterFieldCount > 0 ) {
231 + // $groupedCondition .= " HAVING ";
232 + // }
233 + // foreach ( $filter['field'] as $filterFieldKey => $filterFieldDetails ) {
234 + // $groupedCondition .= " `$filterFieldDetails->id` ='%%" . $this->getFieldFormat( $filterFieldDetails->value ) . "%%'";
235 + // $all_values[] = $filterFieldDetails->value;
236 + // if ( $filterFieldChecker < $filterFieldCount ) {
237 + // $groupedCondition .= " AND ";
238 + // }
239 + // }
240 + // }
241 +
242 + // if ( !empty( $filter['global'] ) && !empty( $globalFilterString ) && !empty( $globalFilterValues ) ) {
243 + // $isRecount = true;
244 + // if ( !empty( $filter['field'] ) ) {
245 + // $groupedCondition .= " AND (" . $globalFilterString . ") ";
246 + // } else {
247 + // $groupedCondition .= " HAVING $globalFilterString ";
248 + // }
249 + // $offset = 0;
250 + // $all_values = array_merge( $all_values, $globalFilterValues );
251 + // }
252 +
253 + // if ( !empty( $dateBetweenFilter ) && !empty( $dateBetweenFilter->start_date ) && !empty( $dateBetweenFilter->end_date ) ) {
254 + // if ( strpos( $groupedCondition, 'HAVING' ) !== false ) {
255 + // $groupedCondition .= " AND `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
256 + // } else {
257 + // $groupedCondition .= " HAVING `__created_at` BETWEEN '" . $dateBetweenFilter->start_date . "' AND '" . $dateBetweenFilter->end_date . "' ";
258 + // }
259 + // }
260 +
261 + $sqlQryByFldCondtion = $this->sqlQryGenerateByFldCondition($fieldConditions);
262 +
263 + if (!empty($sqlQryByFldCondtion)) {
264 + $isFldCondition = true;
265 + if (false !== strpos($groupedCondition, 'HAVING')) {
266 + $groupedCondition .= ' AND (' . $sqlQryByFldCondtion . ') ';
267 + } else {
268 + $groupedCondition .= ' HAVING (' . $sqlQryByFldCondtion . ') ';
269 + }
270 + }
271 + return [
272 + 'groupedCondition' => $groupedCondition,
273 + 'all_values' => $all_values,
274 + 'isFldCondition' => $isFldCondition,
275 + ];
276 + }
277 +
278 + public function orderCondition($formFieldsNames, $sortBy)
279 + {
280 + $orderCondition = null;
281 + if (!empty($sortBy)) {
282 + $sortableFieldCount = count($sortBy);
283 + $sortableFieldChecker = 0;
284 + if ($sortableFieldCount > 0) {
285 + $orderCondition .= ' ORDER BY ';
286 + }
287 + $orderList = '';
288 + foreach ($sortBy as $sortableFieldKey => $sortableFieldDetails) {
289 + // $orderCondition .=" ".$this->getFieldFormat($sortableFieldDetails->id);
290 + $sortableFieldChecker += 1;
291 + if (in_array($sortableFieldDetails->id, $formFieldsNames)) {
292 + $orderList .= " `$sortableFieldDetails->id` ";
293 + $orderFollow = $sortableFieldDetails->desc ? ' DESC ' : ' ASC ';
294 + $orderList .= ' ' . $orderFollow;
295 + if ($sortableFieldChecker < $sortableFieldCount) {
296 + $orderList .= ', ';
297 + }
135 298 }
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 - }
299 + }
300 + if (empty($orderList)) {
184 301 $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)) {
225 - return array(
226 - 'count' => $paginateEntry ? $entryCount : 0,
227 - 'entries' => [],
228 - 'error' => $result->get_error_message()
229 - );
230 - }
231 - // var_dump('test');
232 - if ($isRecount) {
233 - $sql = "SELECT count(*) as count FROM (";
234 - $sql .= "SELECT $selectedMeta FROM `$this->table_name`";
235 - $sql .= $groupedCondition . $orderCondition;
236 - $sql .= ") as retrievedData";
237 - $countResult = $this->execute($sql, $all_values)->getResult();
238 - if (!(is_wp_error($result) && $result->get_error_code() === 'result_empty')) {
239 - $entryCount = $countResult[0]->count;
240 - }
241 - }
242 - $resultedEntries = array(
243 - 'count' => $entryCount,
244 - 'entries' => $result,
245 - );
246 - return $resultedEntries;
302 + } else {
303 + $orderCondition .= $orderList;
304 + }
247 305 }
306 + $orderCondition .= empty($orderCondition) ? ' ORDER BY `bitforms_form_entry_id` DESC ' : ',`bitforms_form_entry_id` DESC ';
307 + return $orderCondition;
308 + }
248 309
249 - public function getExportEntry($formFields, $entries, $limit = null, $sortBy = null, $sortByField = null, $filter = null)
250 - {
251 - $selectedEntryMeta = null;
252 - $metaChecker = 0;
253 - $all_values = array();
254 - if ($formFields == []) {
255 - $data = array(
256 - 'count' => 0,
257 - 'entries' => [],
258 - );
259 - wp_send_json_success($data, 200);
260 - }
261 - $fieldCount = count($formFields);
262 - $formFieldsNames = array();
263 - foreach ($formFields as $fieldKey => $fieldDetails) {
264 - $fieldFormat = $this->getFieldFormat($fieldDetails);
265 - $selectedEntryMeta .= "GROUP_CONCAT(
310 + public function isEntryMetaExist($conditions)
311 + {
312 + $existMetaData = $this->get(
313 + [
314 + 'meta_key',
315 + 'meta_value',
316 + ],
317 + $conditions
318 + );
319 + if (!is_wp_error($existMetaData) && count($existMetaData) > 0) {
320 + return true;
321 + }
322 + return false;
323 + }
324 +
325 + public function getEntryMeta($formFields, $entries, $limit = null, $offset = null, $filter = null, $sortBy = null, $fieldConditions = null, $dateBetweenFilter = null)
326 + {
327 + $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
328 + $fieldCount = count($formFields);
329 + $getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount);
330 +
331 + $selectedMeta = $getSelectedMetaFldValue['selected_meta'];
332 + $formFieldsNames = $getSelectedMetaFldValue['form_fields_names'];
333 + $all_values = $getSelectedMetaFldValue['all_values'];
334 + $entryIDs = [];
335 + $entryCount = count($entries);
336 + // $paginateEntry = empty($sortBy) && empty($filter['field']) && empty($filter['global']);
337 + // $entries = $paginateEntry ? array_slice($entries, $offset, $limit) : $entries;
338 + $paginateEntry = false;
339 + foreach ($entries as $entryDetail) {
340 + $entryIDs[] = $entryDetail->id;
341 + }
342 + if (empty($entryIDs)) {
343 + return [
344 + 'count' => 0,
345 + 'entries' => [],
346 + ];
347 + }
348 + $condition['bitforms_form_entry_id'] = $entryIDs;
349 + $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
350 + $groupedCondition = $group['groupedCondition'];
351 + $all_values = $group['all_values'];
352 + $isFldCondition = $group['isFldCondition'];
353 + $orderCondition = $this->orderCondition($formFieldsNames, $sortBy);
354 +
355 + $paginate = null;
356 + if (!\is_null($limit)) {
357 + $limit = \intval($limit);
358 + $paginate .= " LIMIT $limit ";
359 + }
360 + if (!\is_null($offset)) {
361 + $offset = \intval($offset);
362 + $paginate .= " OFFSET $offset ";
363 + }
364 + $sql = "SELECT $selectedMeta FROM `$this->table_name` em";
365 + $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
366 + if ($dateBetweenFilter) {
367 + $startDate = $dateBetweenFilter->start_date;
368 + $endDate = $dateBetweenFilter->end_date;
369 + if ($startDate && $endDate) {
370 + $sql .= " AND DATE(e.created_at) BETWEEN '$startDate' AND '$endDate' ";
371 + } elseif ($startDate) {
372 + $sql .= " AND DATE(e.created_at) >= '$startDate' ";
373 + } elseif ($endDate) {
374 + $sql .= " AND DATE(e.created_at) <= '$endDate' ";
375 + }
376 + }
377 + $sql .= $groupedCondition . $orderCondition . $paginate;
378 + $result = $this->execute($sql, $all_values)->getResult();
379 + if (is_wp_error($result)) {
380 + return [
381 + 'count' => $paginateEntry ? $entryCount : 0,
382 + 'entries' => [],
383 + 'error' => $result->get_error_message()
384 + ];
385 + }
386 + if ($isFldCondition) {
387 + $condition['bitforms_form_entry_id'] = $entryIDs;
388 + $group = $this->groupedCondition($condition, $all_values, $fieldConditions);
389 + $all_values = $group['all_values'];
390 + $entryCount = $this->queryRecount($selectedMeta, $group['groupedCondition'], $orderCondition, $all_values);
391 + }
392 + $resultedEntries = [
393 + 'count' => $entryCount,
394 + 'entries' => $result,
395 + ];
396 + return $resultedEntries;
397 + }
398 +
399 + public function getSingleEntryMeta($formFields, $entryId)
400 + {
401 + $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
402 + $fieldCount = count($formFields);
403 + $getSelectedMetaFldValue = $this->selectedEntryMeta($formFields, $fieldCount);
404 + $selectedMeta = $getSelectedMetaFldValue['selected_meta'];
405 + $formFieldsNames = $getSelectedMetaFldValue['form_fields_names'];
406 + $all_values = $getSelectedMetaFldValue['all_values'];
407 + $condition['bitforms_form_entry_id'] = [$entryId];
408 + $group = $this->groupedCondition($condition, $all_values, []);
409 + $groupedCondition = $group['groupedCondition'];
410 + $all_values = $group['all_values'];
411 + $orderCondition = $this->orderCondition($formFieldsNames, null);
412 + $sql = "SELECT $selectedMeta FROM `$this->table_name` em";
413 + $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
414 + $sql .= $groupedCondition . $orderCondition;
415 + $result = $this->execute($sql, $all_values)->getResult();
416 +
417 + if (is_wp_error($result)) {
418 + return [];
419 + }
420 + return $result;
421 + }
422 +
423 + private function csvInjectionPrevent($value)
424 + {
425 + $formula = ['=', '-', '+', '@', "\t", "\r"];
426 + $valueFilter = preg_replace('/[\]["]/i', '', $value);
427 + if (\in_array(substr($value, 0, 1), $formula, true)) {
428 + $valueFilter = "'" . trim($valueFilter);
429 + }
430 +
431 + return $valueFilter;
432 + }
433 +
434 + public function getExportEntry($formFields, $entries, $formId, $fieldLabels, $limit = null, $sortBy = null, $sortByField = null)
435 + {
436 + $entry_table = $this->app_db->prefix . 'bitforms_form_entries';
437 + $selectedEntryMeta = '`bitforms_form_entry_id` as entry_id,';
438 + $selectedEntryMeta .= "e.user_id as '__user_id',";
439 + $selectedEntryMeta .= "e.status as '__entry_status',";
440 + $selectedEntryMeta .= "e.user_ip as '__user_ip',";
441 + $selectedEntryMeta .= "e.user_location as '__user_location',";
442 + $selectedEntryMeta .= "e.user_device as '__user_device',";
443 + $selectedEntryMeta .= "e.referer as '__referer',";
444 + $selectedEntryMeta .= "e.created_at as '__created_at',";
445 + $selectedEntryMeta .= "e.updated_at as '__updated_at',";
446 + $metaChecker = 0;
447 +
448 + $entryInfo = ['__user_id', '__user_ip', /* '__user_location', */'__user_device',
449 + '__referer', '__created_at', '__updated_at'];
450 + $all_values = [];
451 + if ([] === $formFields) {
452 + $data = [
453 + 'count' => 0,
454 + 'entries' => [],
455 + ];
456 + wp_send_json_success($data, 200);
457 + }
458 + $fieldCount = count($formFields) - count(array_intersect($formFields, $entryInfo));
459 + $formFieldsNames = [];
460 + foreach ($formFields as $fldKey) {
461 + $formFieldsNames[] = $fldKey;
462 + if (in_array($fldKey, $entryInfo)) {
463 + continue;
464 + }
465 + $fieldFormat = $this->getFieldFormat($fldKey);
466 + $selectedEntryMeta .= "GROUP_CONCAT(
266 467 CASE
267 468 `meta_key`
268 469 WHEN '$fieldFormat' THEN `meta_value`
269 470 END
270 471 ) AS '$fieldFormat'";
271 - $metaChecker += 1;
272 - $all_values[] = $fieldDetails;
273 - $all_values[] = $fieldDetails;
274 - $formFieldsNames[] = $fieldDetails;
275 - if ($metaChecker < $fieldCount) {
276 - $selectedEntryMeta .= ",";
472 + $metaChecker += 1;
473 + $all_values[] = $fldKey;
474 + $all_values[] = $fldKey;
475 + if ($metaChecker < $fieldCount) {
476 + $selectedEntryMeta .= ',';
477 + }
478 + }
479 + $entryIDs = [];
480 + foreach ($entries as $entryDetail) {
481 + $entryIDs[] = $entryDetail->id;
482 + }
483 + if (empty($entryIDs)) {
484 + return [
485 + 'count' => 0,
486 + 'entries' => [],
487 + ];
488 + }
489 + $condition['bitforms_form_entry_id'] = $entryIDs;
490 + $formattedCondition = $this->getFormatedCondition($condition);
491 + $groupedCondition = null;
492 + if ($formattedCondition) {
493 + $groupedCondition = $formattedCondition['conditions'] . ' GROUP BY
494 + `bitforms_form_entry_id` ';
495 + $all_values = array_merge($all_values, $formattedCondition['values']);
496 + }
497 + $order = 'DESC' === $sortBy ? 'DESC ' : 'ASC ';
498 + $orderField = \is_null($sortByField) ? 'bitforms_form_entry_id' : "`$sortByField`";
499 +
500 + $orderCondition = "ORDER BY $orderField $order ";
501 + if (!\is_null($limit)) {
502 + $limitInt = \intval($limit);
503 + $limit = " LIMIT $limitInt ";
504 + }
505 + $sql = "SELECT $selectedEntryMeta FROM `$this->table_name` em";
506 + $sql .= " INNER JOIN $entry_table e on e.id = em.bitforms_form_entry_id ";
507 + $sql .= $groupedCondition . $orderCondition . $limit;
508 +
509 + $result = $this->execute($sql, $all_values)->getResult();
510 + $allData = [];
511 + $entry_id = 'entry_id';
512 + $users = get_users(['fields' => ['ID', 'display_name']]);
513 + $userNames = [];
514 + foreach ($users as $key => $value) {
515 + $userNames[$value->ID] = $value->display_name;
516 + }
517 + foreach ($result as $key => $value) {
518 + foreach ($formFieldsNames as $formFieldName) {
519 + $allData[$key]['entry_id'] = preg_replace('/[\]["]/i', '', $value->$entry_id);
520 + if ('__user_id' === $formFieldName && intval($value->$formFieldName) > 0) {
521 + $allData[$key][$formFieldName] = $userNames[$value->$formFieldName];
522 + } elseif ('__user_ip' === $formFieldName) {
523 + $allData[$key][$formFieldName] = long2ip((int)$value->$formFieldName);
524 + } else {
525 + $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
526 + }
527 + }
528 + }
529 +
530 + if (is_wp_error($result)) {
531 + wp_send_json_error('Internal server error', 500);
532 + } else {
533 + $downloadableFieldType = ['file-up', 'signature', 'advanced-file-up'];
534 + foreach ($fieldLabels as $field) {
535 + foreach ($allData as $index => $entry) {
536 + if (array_key_exists($field['key'], $entry) && in_array($field['type'], $downloadableFieldType, true)) {
537 + $key = $field['key'];
538 + if (empty($entry[$key])) {
539 + continue;
277 540 }
278 - }
279 - $entryIDs = array();
280 - foreach ($entries as $entryKey => $entryDetail) {
281 - $entryIDs[] = $entryDetail->id;
282 - }
283 - if (empty($entryIDs)) {
284 - return array(
285 - 'count' => 0,
286 - 'entries' => [],
287 - );
288 - }
289 - $condition['bitforms_form_entry_id'] = $entryIDs;
290 - $formattedCondition = $this->getFormatedCondition($condition);
291 - $groupedCondition = null;
292 - if ($formattedCondition) {
293 - $groupedCondition = $formattedCondition['conditions'] . " GROUP BY
294 - `bitforms_form_entry_id` ";
295 - $all_values = array_merge($all_values, $formattedCondition['values']);
296 - }
297 - $order = \is_null($sortBy) ? "DESC " : "$sortBy";
298 - $orderField = \is_null($sortByField) ? "bitforms_form_entry_id " : "`$sortByField`";
299 - $orderCondition = "ORDER BY $orderField $order ";
300 - if (!\is_null($limit)) {
301 - $limitInt = \intval($limit);
302 - $limit = " LIMIT $limitInt ";
303 - }
304 - $sql = "SELECT $selectedEntryMeta FROM `$this->table_name`"
305 - . $groupedCondition . $orderCondition . $limit;
306 - $result = $this->execute($sql, $all_values)->getResult();
307 - $allData = [];
308 - foreach ($result as $key => $value) {
309 - foreach ($formFieldsNames as $formFieldName) {
310 - $allData[$key][$formFieldName] = preg_replace('/[\]["]/i', '', $value->$formFieldName);
541 + $_upload_dir = FileHandler::getEntriesFileUploadDir($formId, $entry['entry_id']);
542 + $imageArray = explode(',', $entry[$key]);
543 + if (is_array($imageArray)) {
544 + $fileData = [];
545 + foreach ($imageArray as $file) {
546 + $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . "&fileID=$file";
547 + if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file)) {
548 + $fileData[] = site_url($path, null);
549 + }
550 + }
551 + $allData[$index][$key] = implode(',', $fileData);
552 + } else {
553 + $uploadedFile = explode('_', $entry[$key]);
554 + $path = "bitforms/bitforms-file/?formID=$formId&entryID=" . $entry['entry_id'] . '&fileID=' . $uploadedFile[0];
555 + if (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $uploadedFile[0])) {
556 + $allData[$index][$key] = site_url($path, null);
557 + }
311 558 }
559 + }
312 560 }
313 - if (is_wp_error($result)) {
314 - wp_send_json_error('Internal server error', 500);
315 - } else {
316 - wp_send_json_success($allData, 200);
317 - }
561 + }
562 + wp_send_json_success($allData, 200);
318 563 }
564 + }
319 565 }