PluginProbe
The Innovative Form Builder – IvyForms / 0.1.2
The Innovative Form Builder – IvyForms v0.1.2
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 0.1.2, at backend/src/Repository/Confirmation/ConfirmationRepository.php

117 lines 3.1 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 // 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
16 class ConfirmationRepository extends AbstractRepository implements ConfirmationRepositoryInterface
17 {
18 public const FACTORY = ConfirmationFactory::class;
19
20 /**
21 * Add confirmation to the database
22 *
23 * @param Confirmation $entity
24 *
25 * @return int
26 *
27 * @throws QueryExecutionException
28 */
29 public function add($entity): int
30 {
31 $data = $entity->toArray();
32
33 $result = $this->wpdb->insert(
34 $this->table,
35 [
36 'formId' => (int) $data['formId'],
37 'type' => $data['type'],
38 'status' => (int) $data['status'],
39 'showForm' => (int) $data['showForm'],
40 'message' => $data['message'],
41 'url' => $data['url'],
42 'page' => $data['page'],
43 ],
44 ['%d', '%s', '%d', '%d', '%s', '%s', '%s']
45 );
46
47 if ($result === false) {
48 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
49 }
50
51 return $this->wpdb->insert_id;
52 }
53
54 /**
55 * Update confirmation in the database
56 *
57 * @param int $id
58 * @param Confirmation $entity
59 *
60 * @return bool
61 *
62 * @throws QueryExecutionException
63 */
64 public function update(int $id, $entity): bool
65 {
66 $data = $entity->toArray();
67
68 $result = $this->wpdb->update(
69 $this->table,
70 [
71 'formId' => (int) $data['formId'],
72 'type' => $data['type'],
73 'status' => (int) $data['status'],
74 'showForm' => (int) $data['showForm'],
75 'message' => $data['message'],
76 'url' => $data['url'],
77 'page' => $data['page'],
78 ],
79 ['id' => $id],
80 ['%d', '%s', '%d', '%d', '%s', '%s', '%s'],
81 ['%d']
82 );
83
84 if ($result === false) {
85 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
86 }
87
88 return $result;
89 }
90
91 /**
92 * Get all confirmations by form ID.
93 *
94 * @param int $id
95 *
96 * @return array<Confirmation>
97 */
98 public function getAllById(int $id): array
99 {
100 $rows = $this->wpdb->get_results(
101 $this->wpdb->prepare(
102 $this->selectQuery() . " WHERE $this->table.formId = %d",
103 $id
104 ),
105 ARRAY_A
106 );
107
108 if (empty($rows)) {
109 return [];
110 }
111
112 return array_map(function ($row) {
113 return call_user_func([static::FACTORY, 'create'], $row);
114 }, $rows);
115 }
116 }
117