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 +48 -31 0.8.2 → 1.5.1 View file →
@@ -6,19 +6,16 @@
6 6 */
7 7
8 8 namespace IvyForms\Repository\Entry;
9 9
10 -// phpcs:disable PSR1.Files.SideEffects
11 -if (!defined('ABSPATH')) {
12 - exit; // Exit if accessed directly
13 -}
14 -
15 10 use IvyForms\Common\Exceptions\InvalidArgumentException;
16 11 use IvyForms\Common\Exceptions\QueryExecutionException;
12 +use IvyForms\Common\Helpers\EntryBatchUpdater;
17 13 use IvyForms\Common\Helpers\EntryQueryHelper;
18 14 use IvyForms\Entity\Entry\Entry;
19 15 use IvyForms\Factory\Entry\EntryFactory;
20 16 use IvyForms\Repository\AbstractRepository;
17 +use IvyForms\Repository\VisibleFormIdsQueryFilter;
21 18 use IvyForms\Services\InstallActions\DB\Entry\EntriesTable;
22 19 use IvyForms\Services\InstallActions\DB\EntryField\EntryFieldsTable as EntryFieldsTable;
23 20 use IvyForms\Services\InstallActions\DB\Form\FormsTable;
24 21 use IvyForms\Services\Translations\BackendStrings;
@@ -26,8 +23,12 @@
26 23 class EntryRepository extends AbstractRepository implements EntryRepositoryInterface
27 24 {
28 25 public const FACTORY = EntryFactory::class;
29 26
27 + // Column constants
28 + public const COLUMN_STATUS = 'status';
29 + public const COLUMN_STARRED = 'starred';
30 +
30 31 /**
31 32 * Add entry to the database
32 33 *
33 34 * @param Entry $entity
@@ -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,32 +452,28 @@
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);
465 + }
466 +
467 + /**
468 + * Get allowed columns for bulk update operations
469 + *
470 + * @return string[]
471 + */
472 + protected function getAllowedBulkUpdateColumns(): array
473 + {
474 + return [
475 + self::COLUMN_STARRED,
476 + self::COLUMN_STATUS,
477 + ];
461 478 }
462 479 }