| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © TMS-Plugins. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Repository\EntryField; |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Files.SideEffects |
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
use Exception; |
| 16 |
use IvyForms\Factory\EntryField\EntryFieldFactory; |
| 17 |
use IvyForms\Repository\AbstractRepository; |
| 18 |
use IvyForms\Services\Translations\BackendStrings; |
| 19 |
|
| 20 |
class EntryFieldRepository extends AbstractRepository implements EntryFieldRepositoryInterface |
| 21 |
{ |
| 22 |
public const FACTORY = EntryFieldFactory::class; |
| 23 |
|
| 24 |
/** |
| 25 |
* Check if an entry field exists in the database. |
| 26 |
* |
| 27 |
* @param int $entryFieldId |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public function exists(int $entryFieldId): bool |
| 31 |
{ |
| 32 |
$query = $this->wpdb->prepare( |
| 33 |
"SELECT COUNT(*) FROM {$this->table} WHERE id = %d", |
| 34 |
$entryFieldId |
| 35 |
); |
| 36 |
|
| 37 |
return (bool) $this->wpdb->get_var($query); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @throws \Exception |
| 42 |
*/ |
| 43 |
public function add(object $entity): int |
| 44 |
{ |
| 45 |
$data = $entity->toArray(); |
| 46 |
|
| 47 |
$result = $this->wpdb->insert( |
| 48 |
$this->table, |
| 49 |
[ |
| 50 |
'id' => $data['id'] ?? null, |
| 51 |
'entryId' => $data['entryId'], |
| 52 |
'fieldId' => $data['fieldId'], |
| 53 |
'fieldValue' => $data['fieldValue'], |
| 54 |
], |
| 55 |
[ |
| 56 |
'%d', |
| 57 |
'%d', |
| 58 |
'%d', |
| 59 |
'%s', |
| 60 |
], |
| 61 |
); |
| 62 |
|
| 63 |
if ($result === false) { |
| 64 |
throw new Exception( |
| 65 |
BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
return $this->wpdb->insert_id; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @throws Exception |
| 74 |
*/ |
| 75 |
public function update(int $id, object $entity): bool |
| 76 |
{ |
| 77 |
$data = $entity->toArray(); |
| 78 |
|
| 79 |
$result = $this->wpdb->update( |
| 80 |
$this->table, |
| 81 |
[ |
| 82 |
'fieldId' => $data['fieldId'], |
| 83 |
'fieldValue' => $data['fieldValue'], |
| 84 |
], |
| 85 |
['id' => $id], |
| 86 |
[ |
| 87 |
'%d', |
| 88 |
'%s', |
| 89 |
], |
| 90 |
['%d'] |
| 91 |
); |
| 92 |
|
| 93 |
if ($result === false) { |
| 94 |
throw new Exception( |
| 95 |
BackendStrings::getExceptionStrings()['unable_update_data'] . __CLASS__ |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
return (bool) $result; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Find an entry field by its ID. |
| 104 |
* |
| 105 |
* @param array<mixed> $array |
| 106 |
* @return array<mixed> |
| 107 |
*/ |
| 108 |
public function findBy(array $array): array |
| 109 |
{ |
| 110 |
|
| 111 |
$sql = $this->selectQuery() . " {$this->table}"; |
| 112 |
$where = []; |
| 113 |
$values = []; |
| 114 |
|
| 115 |
foreach ($array as $field => $value) { |
| 116 |
$where[] = "{$field} = %s"; |
| 117 |
$values[] = $value; |
| 118 |
} |
| 119 |
|
| 120 |
if ($where) { |
| 121 |
$sql .= ' WHERE ' . implode(' AND ', $where); |
| 122 |
} |
| 123 |
|
| 124 |
$query = $this->wpdb->prepare($sql, ...$values); |
| 125 |
$rows = $this->wpdb->get_results($query, ARRAY_A); |
| 126 |
|
| 127 |
$results = []; |
| 128 |
foreach ($rows as $row) { |
| 129 |
$results[] = call_user_func([static::FACTORY, 'create'], $row); |
| 130 |
} |
| 131 |
|
| 132 |
return $results; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get all entry field IDs for the given entry IDs in a single query. |
| 137 |
* |
| 138 |
* @param int[] $entryIds |
| 139 |
* @return int[] |
| 140 |
*/ |
| 141 |
public function findIdsByEntryIds(array $entryIds): array |
| 142 |
{ |
| 143 |
if (empty($entryIds)) { |
| 144 |
return []; |
| 145 |
} |
| 146 |
$placeholders = implode(',', array_fill(0, count($entryIds), '%d')); |
| 147 |
$query = "SELECT id FROM {$this->table} WHERE entryId IN ($placeholders)"; |
| 148 |
$results = $this->wpdb->get_results($this->wpdb->prepare($query, ...$entryIds), ARRAY_A); |
| 149 |
return array_map(fn($row) => (int)$row['id'], $results); |
| 150 |
} |
| 151 |
} |
| 152 |
|