PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.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
ivyforms / backend / src / Common / Helpers / EntryQueryHelper.php

EntryQueryHelper.php in The Innovative Form Builder – IvyForms 1.1, at backend/src/Common/Helpers/EntryQueryHelper.php

199 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Common\Helpers;
4
5 use IvyForms\Common\Exceptions\InvalidArgumentException;
6 use IvyForms\Services\InstallActions\DB\Entry\EntriesTable;
7 use IvyForms\Services\InstallActions\DB\EntryField\EntryFieldsTable;
8 use IvyForms\Services\InstallActions\DB\Form\FormsTable;
9
10 class EntryQueryHelper
11 {
12 /**
13 * Helper to return search conditions for entries (for complexity reduction)
14 *
15 * @param array<string, mixed>|null $params
16 * @return array<string>
17 */
18 public static function getEntrySearchConditions(?array $params = null): array
19 {
20 $conditions = [
21 'e.id LIKE %s',
22 'e.formId LIKE %s',
23 'f.name LIKE %s',
24 ];
25 // Only include fieldValue if searchFieldValue is true (default true for BC)
26 if (!isset($params['searchFieldValue']) || $params['searchFieldValue']) {
27 $conditions[] = 'eft.fieldValue LIKE %s';
28 }
29 return $conditions;
30 }
31
32 /**
33 * Get entry fields SQL and params for the given entries.
34 *
35 * @param array<int, array<string, mixed>> $entries
36 * @return array{sql: string, params: array<int, int>}
37 * @throws InvalidArgumentException
38 */
39 public static function getEntryFieldsQuery(array $entries): array
40 {
41 if (empty($entries)) {
42 return ['sql' => '', 'params' => []];
43 }
44 $entryIds = array_column($entries, 'id');
45 $placeholders = implode(',', array_fill(0, count($entryIds), '%d'));
46 $entryFieldsTable = EntryFieldsTable::getTableName();
47 $sql = "SELECT * FROM {$entryFieldsTable} WHERE entryId IN ($placeholders)";
48 return ['sql' => $sql, 'params' => $entryIds];
49 }
50
51 /**
52 * Get the count of entries for a specific form (SQL and params).
53 *
54 * @param string $table
55 * @param int $formId
56 * @return array{sql: string, params: array<int, int>}
57 */
58 public static function getCountByFormIdQuery(string $table, int $formId): array
59 {
60 $sql = "SELECT COUNT(*) FROM {$table} WHERE formId = %d";
61 return ['sql' => $sql, 'params' => [$formId]];
62 }
63
64 /**
65 * Get the count of entries for the filter dropdown (SQL and params).
66 *
67 * @param string $table
68 * @param array<string, mixed>|null $params
69 * @return array{sql: string, params: array<int, int>}
70 */
71 public static function getFilterCountQuery(string $table, ?array $params = null): array
72 {
73 $where = '';
74 $queryParams = [];
75 if (!empty($params['filters']['formId'])) {
76 $where = 'WHERE formId = %d';
77 $queryParams[] = (int)$params['filters']['formId'];
78 }
79 $sql = "SELECT
80 SUM(status = 'read') as readTrueCount,
81 SUM(status = 'unread') as readFalseCount,
82 SUM(starred = 1) as starredTrueCount,
83 SUM(starred = 0) as starredFalseCount
84 FROM {$table} $where";
85 return ['sql' => $sql, 'params' => $queryParams];
86 }
87
88 /**
89 * Get the count of entries for multiple form IDs (SQL and params).
90 *
91 * @param int[] $formIds
92 * @return array{sql: string, params: array<int, int>, formIds: array<int, int>}
93 * @throws InvalidArgumentException
94 */
95 public static function getEntryCountByFormIdsQuery(array $formIds): array
96 {
97 if (empty($formIds)) {
98 return ['sql' => '', 'params' => [], 'formIds' => []];
99 }
100 $formIds = array_map('intval', $formIds);
101 $placeholders = implode(',', array_fill(0, count($formIds), '%d'));
102 $tableName = EntriesTable::getTableName();
103 $sql = "SELECT formId, COUNT(*) as count FROM {$tableName} WHERE formId IN ($placeholders) GROUP BY formId";
104 return ['sql' => $sql, 'params' => $formIds, 'formIds' => $formIds];
105 }
106
107 /**
108 * Build the SELECT query and parameters for entries, with or without pagination.
109 * @param string $entriesTable
110 * @param string $join
111 * @param string $whereClauses
112 * @param string $sortColumn
113 * @param string $order
114 * @param array<int|string, mixed> $queryParams
115 * @param int|string $perPage
116 * @param int $page
117 * @return array{sql: string, params: array<int|string, mixed>}
118 */
119 public static function buildEntrySelectQuery(
120 string $entriesTable,
121 string $join,
122 string $whereClauses,
123 string $sortColumn,
124 string $order,
125 array $queryParams,
126 $perPage,
127 int $page
128 ): array {
129 $isAll = ($perPage === 'all' || $perPage === 0);
130 if ($isAll) {
131 $sql = "SELECT DISTINCT e.* FROM {$entriesTable} e $join
132 WHERE {$whereClauses} ORDER BY {$sortColumn} {$order}";
133 return ['sql' => $sql, 'params' => $queryParams];
134 }
135 $perPage = max((int)$perPage, 1);
136 $offset = ($page - 1) * $perPage;
137 $params = $queryParams;
138 $params[] = $perPage;
139 $params[] = $offset;
140 $sql = "SELECT DISTINCT e.* FROM {$entriesTable} e $join
141 WHERE {$whereClauses} ORDER BY {$sortColumn} {$order} LIMIT %d OFFSET %d";
142 return ['sql' => $sql, 'params' => $params];
143 }
144
145 /**
146 * Get the JOIN clause for entries, entry fields, and forms.
147 *
148 * @return string
149 * @throws InvalidArgumentException
150 */
151 public static function getEntryJoinClause(): string
152 {
153 $entryFieldsTable = EntryFieldsTable::getTableName();
154 $formsTable = FormsTable::getTableName();
155 $join = " LEFT JOIN {$entryFieldsTable} eft ON e.id = eft.entryId ";
156 $join .= " LEFT JOIN {$formsTable} f ON e.formId = f.id ";
157 return $join;
158 }
159
160 /**
161 * Build search WHERE clause and query parameters for entry search.
162 * @param array<string, mixed>|null $params
163 * @return array{where: string|null, params: array<int, string>}
164 */
165 public static function buildSearchWhereClause(?array $params): array
166 {
167 if (!empty($params['search'])) {
168 global $wpdb;
169 $searchEscaped = '%' . $wpdb->esc_like($params['search']) . '%';
170 $searchConditions = self::getEntrySearchConditions($params);
171 $where = '(' . implode(' OR ', $searchConditions) . ')';
172 $queryParams = array_fill(0, count($searchConditions), $searchEscaped);
173 return ['where' => $where, 'params' => $queryParams];
174 }
175 return ['where' => null, 'params' => []];
176 }
177
178 /**
179 * Build filter WHERE clause and query parameters for entry filter.
180 * @param array<string, mixed>|null $params
181 * @param array<int, string> $filterableColumns
182 * @return array{where: array<int, string>, params: array<int, string>}
183 */
184 public static function buildFilterWhereClause(?array $params, array $filterableColumns): array
185 {
186 $whereClauses = [];
187 $queryParams = [];
188 if (!empty($params['filters'])) {
189 foreach ($params['filters'] as $key => $value) {
190 if (in_array($key, $filterableColumns, true) && $value !== null && $value !== '') {
191 $whereClauses[] = "e.{$key} = %s";
192 $queryParams[] = sanitize_text_field($value);
193 }
194 }
195 }
196 return ['where' => $whereClauses, 'params' => $queryParams];
197 }
198 }
199