PluginProbe
The Innovative Form Builder – IvyForms / 1.1.1
The Innovative Form Builder – IvyForms v1.1.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 / Repository / Confirmation / ConfirmationRepository.php

ConfirmationRepository.php in The Innovative Form Builder – IvyForms 1.1.1, at backend/src/Repository/Confirmation/ConfirmationRepository.php

119 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'type' => $data['type'],
34 'enabled' => (int) $data['enabled'],
35 'showForm' => (int) $data['showForm'],
36 'message' => $data['message'],
37 'url' => $data['url'],
38 'page' => $data['page'],
39 ],
40 ['%d', '%s', '%d', '%d', '%s', '%s', '%s']
41 );
42
43 if ($result === false) {
44 throw new QueryExecutionException(
45 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
46 );
47 }
48
49 return $this->wpdb->insert_id;
50 }
51
52 /**
53 * Update confirmation in the database
54 *
55 * @param int $id
56 * @param Confirmation $entity
57 *
58 * @return bool
59 *
60 * @throws QueryExecutionException
61 */
62 public function update(int $id, $entity): bool
63 {
64 $data = $entity->toArray();
65
66 $result = $this->wpdb->update(
67 $this->table,
68 [
69 'formId' => (int) $data['formId'],
70 'type' => $data['type'],
71 'enabled' => (int) $data['enabled'],
72 'showForm' => (int) $data['showForm'],
73 'message' => $data['message'],
74 'url' => $data['url'],
75 'page' => $data['page'],
76 ],
77 ['id' => $id],
78 ['%d', '%s', '%d', '%d', '%s', '%s', '%s'],
79 ['%d']
80 );
81
82 if ($result === false) {
83 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
84 }
85
86 return $result;
87 }
88
89 /**
90 * Get all confirmations by form ID.
91 *
92 * @param int $id
93 *
94 * @return array<Confirmation>
95 * @throws QueryExecutionException
96 */
97 public function getAllById(int $id): array
98 {
99 $rows = $this->wpdb->get_results(
100 $this->wpdb->prepare(
101 $this->selectQuery() . " WHERE $this->table.formId = %d",
102 $id
103 ),
104 ARRAY_A
105 );
106 if ($rows === false) {
107 throw new QueryExecutionException(
108 BackendStrings::getExceptionStrings()['unable_find_by_id'] . __CLASS__
109 );
110 }
111 if (empty($rows)) {
112 return [];
113 }
114 return array_map(function ($row) {
115 return call_user_func([static::FACTORY, 'create'], $row);
116 }, $rows);
117 }
118 }
119