| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Repository\Confirmation; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 6 |
use IvyForms\Entity\Confirmation\Confirmation; |
| 7 |
use IvyForms\Factory\Confirmation\ConfirmationFactory; |
| 8 |
use IvyForms\Repository\AbstractRepository; |
| 9 |
use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface; |
| 10 |
use IvyForms\Services\Translations\BackendStrings; |
| 11 |
|
| 12 |
class ConfirmationRepository extends AbstractRepository implements ConfirmationRepositoryInterface |
| 13 |
{ |
| 14 |
public const FACTORY = ConfirmationFactory::class; |
| 15 |
|
| 16 |
/** |
| 17 |
* Add confirmation to the database |
| 18 |
* |
| 19 |
* @param Confirmation $entity |
| 20 |
* |
| 21 |
* @return int |
| 22 |
* |
| 23 |
* @throws QueryExecutionException |
| 24 |
*/ |
| 25 |
public function add($entity): int |
| 26 |
{ |
| 27 |
$data = $entity->toArray(); |
| 28 |
|
| 29 |
$result = $this->wpdb->insert( |
| 30 |
$this->table, |
| 31 |
[ |
| 32 |
'formId' => (int) $data['formId'], |
| 33 |
'name' => $data['name'], |
| 34 |
'type' => $data['type'], |
| 35 |
'enabled' => (int) $data['enabled'], |
| 36 |
'showForm' => (int) $data['showForm'], |
| 37 |
'message' => $data['message'], |
| 38 |
'url' => $data['url'], |
| 39 |
'page' => $data['page'], |
| 40 |
'position' => (int) $data['position'], |
| 41 |
'isDefault' => (int) $data['isDefault'], |
| 42 |
'smartLogic' => wp_json_encode($data['smartLogic']), |
| 43 |
], |
| 44 |
['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s'] |
| 45 |
); |
| 46 |
|
| 47 |
if ($result === false) { |
| 48 |
throw new QueryExecutionException( |
| 49 |
BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
return $this->wpdb->insert_id; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Update confirmation in the database |
| 58 |
* |
| 59 |
* @param int $id |
| 60 |
* @param Confirmation $entity |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
* |
| 64 |
* @throws QueryExecutionException |
| 65 |
*/ |
| 66 |
public function update(int $id, $entity): bool |
| 67 |
{ |
| 68 |
$data = $entity->toArray(); |
| 69 |
|
| 70 |
$result = $this->wpdb->update( |
| 71 |
$this->table, |
| 72 |
[ |
| 73 |
'formId' => (int) $data['formId'], |
| 74 |
'name' => $data['name'], |
| 75 |
'type' => $data['type'], |
| 76 |
'enabled' => (int) $data['enabled'], |
| 77 |
'showForm' => (int) $data['showForm'], |
| 78 |
'message' => $data['message'], |
| 79 |
'url' => $data['url'], |
| 80 |
'page' => $data['page'], |
| 81 |
'position' => (int) $data['position'], |
| 82 |
'isDefault' => (int) $data['isDefault'], |
| 83 |
'smartLogic' => wp_json_encode($data['smartLogic']), |
| 84 |
], |
| 85 |
['id' => $id], |
| 86 |
['%d', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%s'], |
| 87 |
['%d'] |
| 88 |
); |
| 89 |
|
| 90 |
if ($result === false) { |
| 91 |
throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); |
| 92 |
} |
| 93 |
|
| 94 |
return $result; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get all confirmations by form ID ordered by position. |
| 99 |
* |
| 100 |
* @param int $id |
| 101 |
* |
| 102 |
* @return array<Confirmation> |
| 103 |
* @throws QueryExecutionException |
| 104 |
*/ |
| 105 |
public function getAllById(int $id): array |
| 106 |
{ |
| 107 |
$sql = $this->selectQuery() |
| 108 |
. " WHERE $this->table.formId = %d" |
| 109 |
. " ORDER BY $this->table.position ASC, $this->table.id ASC"; |
| 110 |
|
| 111 |
$rows = $this->wpdb->get_results( |
| 112 |
$this->wpdb->prepare($sql, $id), |
| 113 |
ARRAY_A |
| 114 |
); |
| 115 |
if ($rows === false) { |
| 116 |
throw new QueryExecutionException( |
| 117 |
BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ |
| 118 |
); |
| 119 |
} |
| 120 |
if (empty($rows)) { |
| 121 |
return []; |
| 122 |
} |
| 123 |
return array_map(function ($row) { |
| 124 |
return call_user_func([static::FACTORY, 'create'], $row); |
| 125 |
}, $rows); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Count confirmations for a form. |
| 130 |
* |
| 131 |
* @param int $formId |
| 132 |
* @return int |
| 133 |
*/ |
| 134 |
public function countByFormId(int $formId): int |
| 135 |
{ |
| 136 |
return (int) $this->wpdb->get_var( |
| 137 |
$this->wpdb->prepare( |
| 138 |
"SELECT COUNT(*) FROM $this->table WHERE formId = %d", |
| 139 |
$formId |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the next position value for a new confirmation on a form. |
| 146 |
* |
| 147 |
* @param int $formId |
| 148 |
* @return int |
| 149 |
*/ |
| 150 |
public function getNextPosition(int $formId): int |
| 151 |
{ |
| 152 |
$max = $this->wpdb->get_var( |
| 153 |
$this->wpdb->prepare( |
| 154 |
"SELECT MAX(position) FROM $this->table WHERE formId = %d", |
| 155 |
$formId |
| 156 |
) |
| 157 |
); |
| 158 |
|
| 159 |
return $max !== null ? ((int) $max) + 1 : 0; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Update positions for confirmations on a form. |
| 164 |
* |
| 165 |
* @param int $formId |
| 166 |
* @param array<int, int> $orderedIds Confirmation IDs in desired order |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function updatePositions(int $formId, array $orderedIds): void |
| 170 |
{ |
| 171 |
foreach ($orderedIds as $position => $confirmationId) { |
| 172 |
$this->wpdb->update( |
| 173 |
$this->table, |
| 174 |
['position' => (int) $position], |
| 175 |
[ |
| 176 |
'id' => (int) $confirmationId, |
| 177 |
'formId' => $formId, |
| 178 |
], |
| 179 |
['%d'], |
| 180 |
['%d', '%d'] |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Set which confirmation is the default for a form. |
| 187 |
* |
| 188 |
* @param int $formId |
| 189 |
* @param int $defaultConfirmationId |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function syncDefaultFlag(int $formId, int $defaultConfirmationId): void |
| 193 |
{ |
| 194 |
$this->wpdb->query( |
| 195 |
$this->wpdb->prepare( |
| 196 |
"UPDATE {$this->table} SET isDefault = CASE WHEN id = %d THEN 1 ELSE 0 END WHERE formId = %d", |
| 197 |
$defaultConfirmationId, |
| 198 |
$formId |
| 199 |
) |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @return string[] |
| 205 |
*/ |
| 206 |
protected function getSearchableColumns(): array |
| 207 |
{ |
| 208 |
return ['name', 'type']; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @return string[] |
| 213 |
*/ |
| 214 |
protected function getFilterableColumns(): array |
| 215 |
{ |
| 216 |
return ['formId']; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @return string[] |
| 221 |
*/ |
| 222 |
protected function getSortableColumns(): array |
| 223 |
{ |
| 224 |
return [ |
| 225 |
'id', |
| 226 |
'name', |
| 227 |
'type', |
| 228 |
'enabled', |
| 229 |
'position', |
| 230 |
]; |
| 231 |
} |
| 232 |
} |
| 233 |
|