| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Repository\Confirmation; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 11 |
use IvyForms\Entity\Confirmation\Confirmation; |
| 12 |
use IvyForms\Factory\Confirmation\ConfirmationFactory; |
| 13 |
use IvyForms\Repository\AbstractRepository; |
| 14 |
use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface; |
| 15 |
use IvyForms\Services\Translations\BackendStrings; |
| 16 |
|
| 17 |
class ConfirmationRepository extends AbstractRepository implements ConfirmationRepositoryInterface |
| 18 |
{ |
| 19 |
public const FACTORY = ConfirmationFactory::class; |
| 20 |
|
| 21 |
/** |
| 22 |
* Add confirmation to the database |
| 23 |
* |
| 24 |
* @param Confirmation $entity |
| 25 |
* |
| 26 |
* @return int |
| 27 |
* |
| 28 |
* @throws QueryExecutionException |
| 29 |
*/ |
| 30 |
public function add($entity): int |
| 31 |
{ |
| 32 |
$data = $entity->toArray(); |
| 33 |
|
| 34 |
$result = $this->wpdb->insert( |
| 35 |
$this->table, |
| 36 |
[ |
| 37 |
'formId' => (int) $data['formId'], |
| 38 |
'type' => $data['type'], |
| 39 |
'enabled' => (int) $data['enabled'], |
| 40 |
'showForm' => (int) $data['showForm'], |
| 41 |
'message' => $data['message'], |
| 42 |
'url' => $data['url'], |
| 43 |
'page' => $data['page'], |
| 44 |
], |
| 45 |
['%d', '%s', '%d', '%d', '%s', '%s', '%s'] |
| 46 |
); |
| 47 |
|
| 48 |
if ($result === false) { |
| 49 |
throw new QueryExecutionException( |
| 50 |
BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__ |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return $this->wpdb->insert_id; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Update confirmation in the database |
| 59 |
* |
| 60 |
* @param int $id |
| 61 |
* @param Confirmation $entity |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
* |
| 65 |
* @throws QueryExecutionException |
| 66 |
*/ |
| 67 |
public function update(int $id, $entity): bool |
| 68 |
{ |
| 69 |
$data = $entity->toArray(); |
| 70 |
|
| 71 |
$result = $this->wpdb->update( |
| 72 |
$this->table, |
| 73 |
[ |
| 74 |
'formId' => (int) $data['formId'], |
| 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 |
], |
| 82 |
['id' => $id], |
| 83 |
['%d', '%s', '%d', '%d', '%s', '%s', '%s'], |
| 84 |
['%d'] |
| 85 |
); |
| 86 |
|
| 87 |
if ($result === false) { |
| 88 |
throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__); |
| 89 |
} |
| 90 |
|
| 91 |
return $result; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get all confirmations by form ID. |
| 96 |
* |
| 97 |
* @param int $id |
| 98 |
* |
| 99 |
* @return array<Confirmation> |
| 100 |
* @throws QueryExecutionException |
| 101 |
*/ |
| 102 |
public function getAllById(int $id): array |
| 103 |
{ |
| 104 |
$rows = $this->wpdb->get_results( |
| 105 |
$this->wpdb->prepare( |
| 106 |
$this->selectQuery() . " WHERE $this->table.formId = %d", |
| 107 |
$id |
| 108 |
), |
| 109 |
ARRAY_A |
| 110 |
); |
| 111 |
if ($rows === false) { |
| 112 |
throw new QueryExecutionException( |
| 113 |
BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__ |
| 114 |
); |
| 115 |
} |
| 116 |
if (empty($rows)) { |
| 117 |
return []; |
| 118 |
} |
| 119 |
return array_map(function ($row) { |
| 120 |
return call_user_func([static::FACTORY, 'create'], $row); |
| 121 |
}, $rows); |
| 122 |
} |
| 123 |
} |
| 124 |
|