PluginProbe
The Innovative Form Builder – IvyForms / 0.3.1
The Innovative Form Builder – IvyForms v0.3.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 / Field / FieldRepository.php

FieldRepository.php in The Innovative Form Builder – IvyForms 0.3.1, at backend/src/Repository/Field/FieldRepository.php

149 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Field;
9
10 // phpcs:disable PSR1.Files.SideEffects
11 if (!defined('ABSPATH')) {
12 exit; // Exit if accessed directly
13 }
14
15 use IvyForms\Common\Exceptions\QueryExecutionException;
16 use IvyForms\Entity\Field\Field;
17 use IvyForms\Factory\Field\FieldFactory;
18 use IvyForms\Repository\AbstractRepository;
19 use IvyForms\Services\Translations\BackendStrings;
20
21 /**
22 * Class FieldRepository
23 *
24 * @package IvyForms\Repository\Field
25 */
26 class FieldRepository extends AbstractRepository implements FieldRepositoryInterface
27 {
28 public const FACTORY = FieldFactory::class;
29
30 /**
31 * Add field to the database
32 *
33 * @param Field $entity
34 *
35 * @return int
36 *
37 * @throws QueryExecutionException
38 */
39 public function add($entity): int
40 {
41
42 $data = $entity->toArray();
43
44 $result = $this->wpdb->insert(
45 $this->table,
46 [
47 'fieldIndex' => $data['fieldIndex'],
48 'formId' => $data['formId'],
49 'type' => $data['type'],
50 'label' => $data['label'],
51 'required' => (int)$data['required'],
52 'defaultValue' => $data['defaultValue'],
53 'placeholder' => $data['placeholder'],
54 'position' => $data['position'],
55 'settings' => json_encode([
56 'hideLabel' => (int)$data['hideLabel'],
57 'readOnly' => (int)$data['readOnly'],
58 'description' => $data['description'],
59 'requiredMessage' => $data['requiredMessage'],
60 'cssClasses' => $data['cssClasses'],
61 ]),
62 'dateCreated' => current_time('mysql')
63 ],
64 ['%s', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%s', '%s']
65 );
66
67 if ($result === false) {
68 throw new QueryExecutionException(
69 BackendStrings::getExceptionStrings()['unable_to_add_data'] . __CLASS__
70 );
71 }
72
73 return $this->wpdb->insert_id;
74 }
75
76 /**
77 * Update field in the database
78 *
79 * @param int $id
80 * @param Field $entity
81 *
82 * @return bool
83 *
84 * @throws QueryExecutionException
85 */
86 public function update(int $id, $entity): bool
87 {
88
89 $data = $entity->toArray();
90
91 $result = $this->wpdb->update(
92 $this->table,
93 [
94 'fieldIndex' => $data['fieldIndex'],
95 'formId' => $data['formId'],
96 'type' => $data['type'],
97 'label' => $data['label'],
98 'required' => (int)$data['required'],
99 'defaultValue' => $data['defaultValue'],
100 'placeholder' => $data['placeholder'],
101 'position' => $data['position'],
102 'settings' => json_encode([
103 'hideLabel' => (int)$data['hideLabel'],
104 'readOnly' => (int)$data['readOnly'],
105 'description' => $data['description'],
106 'requiredMessage' => $data['requiredMessage'],
107 'cssClasses' => $data['cssClasses'],
108 ]),
109 ],
110 [
111 'id' => $data['id']
112 ],
113 ['%s', '%d', '%s', '%s', '%d', '%s', '%s', '%d', '%s'],
114 ['%d']
115 );
116
117 if ($result === false) {
118 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
119 }
120
121 return $result;
122 }
123
124 /**
125 * Get fields by form ID
126 *
127 * @param int $id
128 *
129 * @return mixed[]
130 */
131 public function getFieldsById(int $id): array
132 {
133 $rows = $this->wpdb->get_results(
134 $this->wpdb->prepare(
135 $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC",
136 $id
137 ),
138 ARRAY_A
139 );
140 $result = [];
141
142 foreach ($rows as $row) {
143 $result[] = call_user_func([static::FACTORY, 'create'], $row);
144 }
145
146 return $result;
147 }
148 }
149