PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Repository/Entry/EntryRepository.php +30 -26 1.1.1 → 1.5.1 View file →
@@ -13,8 +13,9 @@
13 13 use IvyForms\Common\Helpers\EntryQueryHelper;
14 14 use IvyForms\Entity\Entry\Entry;
15 15 use IvyForms\Factory\Entry\EntryFactory;
16 16 use IvyForms\Repository\AbstractRepository;
17 +use IvyForms\Repository\VisibleFormIdsQueryFilter;
17 18 use IvyForms\Services\InstallActions\DB\Entry\EntriesTable;
18 19 use IvyForms\Services\InstallActions\DB\EntryField\EntryFieldsTable as EntryFieldsTable;
19 20 use IvyForms\Services\InstallActions\DB\Form\FormsTable;
20 21 use IvyForms\Services\Translations\BackendStrings;
@@ -39,15 +40,18 @@
39 40 public function add($entity): int
40 41 {
41 42 $data = $entity->toArray(true);
42 43
44 + $dateCreated = !empty($data['dateCreated']) ? (string)$data['dateCreated'] : current_time('mysql');
45 + $dateEdited = !empty($data['dateEdited']) ? (string)$data['dateEdited'] : $dateCreated;
46 +
43 47 $result = $this->wpdb->insert(
44 48 $this->table,
45 49 [
46 50 'formId' => $data['formId'],
47 51 'userId' => $data['userId'],
48 - 'dateCreated' => current_time('mysql'),
49 - 'dateEdited' => current_time('mysql'),
52 + 'dateCreated' => $dateCreated,
53 + 'dateEdited' => $dateEdited,
50 54 'status' => $data['status'],
51 55 'ipAddress' => $data['ipAddress'],
52 56 'userAgent' => $data['userAgent'],
53 57 'sourceURL' => $data['sourceURL'],
@@ -209,9 +213,24 @@
209 213 $countSql = "SELECT COUNT(DISTINCT e.id) FROM {$entriesTable} e $join WHERE {$whereClauses}";
210 214 $total = (int)$this->wpdb->get_var($this->wpdb->prepare($countSql, $queryParams));
211 215
212 216 // Fetch form names for all unique formIds in the results
213 - $formIdToName = $this->getFormIdToName($results, $formsTable);
217 + $formIdToName = [];
218 + $formNamesQuery = EntryQueryHelper::getFormNamesByIdsQuery($results, $formsTable);
219 + if (!empty($formNamesQuery['sql'])) {
220 + $formRows = $this->wpdb->get_results(
221 + $this->wpdb->prepare($formNamesQuery['sql'], $formNamesQuery['params']),
222 + ARRAY_A
223 + );
224 + if ($formRows === null) {
225 + throw new QueryExecutionException(
226 + BackendStrings::getExceptionStrings()['unable_get_form_names'] . __CLASS__
227 + );
228 + }
229 + foreach ($formRows as $row) {
230 + $formIdToName[$row['id']] = $row['name'];
231 + }
232 + }
214 233
215 234 return [
216 235 'data' => $results,
217 236 'meta' => [
@@ -247,8 +266,10 @@
247 266 $whereClauses = array_merge($whereClauses, $filterResult['where']);
248 267 // Ensure all values are strings
249 268 $queryParams = array_merge($queryParams, array_map('strval', $filterResult['params']));
250 269 }
270 +
271 + VisibleFormIdsQueryFilter::apply($params, $whereClauses, $queryParams, 'e.formId');
251 272 }
252 273
253 274 /**
254 275 * Get the columns that can be searched in the database (including virtual columns for search).
@@ -431,34 +452,17 @@
431 452 return $counts;
432 453 }
433 454
434 455 /**
435 - * Fetch form names for all unique formIds in the results.
436 - * @param array<int, array<string, mixed>> $results
437 - * @param string $formsTable
438 - * @return array<int, string>
456 + * Resolve owning form IDs for the given entry IDs in a single query.
457 + *
458 + * @param int[] $entryIds
459 + * @return array<int, int> entryId => formId
439 460 * @throws QueryExecutionException
440 461 */
441 - private function getFormIdToName(array $results, string $formsTable): array
462 + public function getFormIdsByEntryIds(array $entryIds): array
442 463 {
443 - $formIdToName = [];
444 - if (count($results) > 0) {
445 - $formIds = array_unique(array_column($results, 'formId'));
446 - if (count($formIds) > 0) {
447 - $placeholders = implode(',', array_fill(0, count($formIds), '%d'));
448 - $formSql = "SELECT id, name FROM {$formsTable} WHERE id IN ($placeholders)";
449 - $formRows = $this->wpdb->get_results($this->wpdb->prepare($formSql, $formIds), ARRAY_A);
450 - if ($formRows === false) {
451 - throw new QueryExecutionException(
452 - BackendStrings::getExceptionStrings()['unable_get_form_names'] . __CLASS__
453 - );
454 - }
455 - foreach ($formRows as $row) {
456 - $formIdToName[$row['id']] = $row['name'];
457 - }
458 - }
459 - }
460 - return $formIdToName;
464 + return EntryQueryHelper::fetchFormIdsByEntryIds($this->wpdb, $entryIds);
461 465 }
462 466
463 467 /**
464 468 * Get allowed columns for bulk update operations