PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.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.2.1, at backend/src/Repository/Field/FieldRepository.php

133 lines 3.5 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 'dateCreated' => current_time('mysql')
56 ],
57 ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s']
58 );
59
60 if ($result === false) {
61 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_add_data'] . __CLASS__);
62 }
63
64 return $this->wpdb->insert_id;
65 }
66
67 /**
68 * Update field in the database
69 *
70 * @param int $id
71 * @param Field $entity
72 *
73 * @return bool
74 *
75 * @throws QueryExecutionException
76 */
77 public function update(int $id, $entity): bool
78 {
79
80 $data = $entity->toArray();
81
82 $result = $this->wpdb->update(
83 $this->table,
84 [
85 'fieldIndex' => $data['fieldIndex'],
86 'formId' => $data['formId'],
87 'type' => $data['type'],
88 'label' => $data['label'],
89 'required' => (int)$data['required'],
90 'defaultValue' => $data['defaultValue'],
91 'placeholder' => $data['placeholder'],
92 'position' => $data['position'],
93 ],
94 [
95 'id' => $data['id']
96 ],
97 ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d'],
98 ['%d']
99 );
100
101 if ($result === false) {
102 throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
103 }
104
105 return $result;
106 }
107
108 /**
109 * Get fields by form ID
110 *
111 * @param int $id
112 *
113 * @return mixed[]
114 */
115 public function getFieldsById(int $id): array
116 {
117 $rows = $this->wpdb->get_results(
118 $this->wpdb->prepare(
119 $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC",
120 $id
121 ),
122 ARRAY_A
123 );
124 $result = [];
125
126 foreach ($rows as $row) {
127 $result[] = call_user_func([static::FACTORY, 'create'], $row);
128 }
129
130 return $result;
131 }
132 }
133