| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Repository\EntryField; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 6 |
use IvyForms\Repository\AbstractRepository; |
| 7 |
use IvyForms\Services\InstallActions\DB\Field\FieldsTable; |
| 8 |
use IvyForms\Services\Translations\BackendStrings; |
| 9 |
|
| 10 |
class EntryFieldNameEmailRepository extends AbstractRepository implements EntryFieldNameEmailRepositoryInterface |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @param int[] $entryIds |
| 14 |
* @return array<int, array<string, string>> |
| 15 |
* @throws QueryExecutionException |
| 16 |
*/ |
| 17 |
public function findNameEmailValuesByEntryIds(array $entryIds): array |
| 18 |
{ |
| 19 |
if ($entryIds === []) { |
| 20 |
return []; |
| 21 |
} |
| 22 |
|
| 23 |
$fieldsTable = FieldsTable::getTableName(); |
| 24 |
$placeholders = implode(',', array_fill(0, count($entryIds), '%d')); |
| 25 |
$sql = "SELECT ef.entryId, ef.fieldValue, CONCAT(fld.type, '_', fld.fieldIndex) AS fieldKey |
| 26 |
FROM {$this->table} ef |
| 27 |
INNER JOIN {$fieldsTable} fld ON fld.id = ef.fieldId |
| 28 |
WHERE ef.entryId IN ({$placeholders}) |
| 29 |
AND (fld.parentId IS NULL OR fld.parentId = 0) |
| 30 |
AND fld.type IN ('name', 'email') |
| 31 |
AND ef.fieldValue <> ''"; |
| 32 |
$results = $this->wpdb->get_results($this->wpdb->prepare($sql, ...$entryIds), ARRAY_A); |
| 33 |
if ($results === false) { |
| 34 |
throw new QueryExecutionException( |
| 35 |
BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
$values = []; |
| 40 |
foreach ($results as $row) { |
| 41 |
$entryId = (int) ($row['entryId'] ?? 0); |
| 42 |
$fieldKey = (string) ($row['fieldKey'] ?? ''); |
| 43 |
if ($entryId > 0 && $fieldKey !== '') { |
| 44 |
$values[$entryId][$fieldKey] = (string) ($row['fieldValue'] ?? ''); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $values; |
| 49 |
} |
| 50 |
} |
| 51 |
|