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 / Field / FieldRepository.php

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

132 lines 3.4 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
20 /**
21 * Class FieldRepository
22 *
23 * @package IvyForms\Repository\Field
24 */
25 class FieldRepository extends AbstractRepository implements FieldRepositoryInterface
26 {
27 public const FACTORY = FieldFactory::class;
28
29 /**
30 * Add field to the database
31 *
32 * @param Field $entity
33 *
34 * @return int
35 *
36 * @throws QueryExecutionException
37 */
38 public function add($entity): int
39 {
40
41 $data = $entity->toArray();
42
43 $result = $this->wpdb->insert(
44 $this->table,
45 [
46 'fieldIndex' => $data['fieldIndex'],
47 'formId' => $data['formId'],
48 'type' => $data['type'],
49 'label' => $data['label'],
50 'required' => (int)$data['required'],
51 'defaultValue' => $data['defaultValue'],
52 'placeholder' => $data['placeholder'],
53 'position' => $data['position'],
54 'dateCreated' => current_time('mysql')
55 ],
56 ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s']
57 );
58
59 if ($result === false) {
60 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
61 }
62
63 return $this->wpdb->insert_id;
64 }
65
66 /**
67 * Update field in the database
68 *
69 * @param int $id
70 * @param Field $entity
71 *
72 * @return bool
73 *
74 * @throws QueryExecutionException
75 */
76 public function update(int $id, $entity): bool
77 {
78
79 $data = $entity->toArray();
80
81 $result = $this->wpdb->update(
82 $this->table,
83 [
84 'fieldIndex' => $data['fieldIndex'],
85 'formId' => $data['formId'],
86 'type' => $data['type'],
87 'label' => $data['label'],
88 'required' => (int)$data['required'],
89 'defaultValue' => $data['defaultValue'],
90 'placeholder' => $data['placeholder'],
91 'position' => $data['position'],
92 ],
93 [
94 'id' => $data['id']
95 ],
96 ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d'],
97 ['%d']
98 );
99
100 if ($result === false) {
101 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
102 }
103
104 return $result;
105 }
106
107 /**
108 * Get fields by form ID
109 *
110 * @param int $id
111 *
112 * @return mixed[]
113 */
114 public function getFieldsById(int $id): array
115 {
116 $rows = $this->wpdb->get_results(
117 $this->wpdb->prepare(
118 $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC",
119 $id
120 ),
121 ARRAY_A
122 );
123 $result = [];
124
125 foreach ($rows as $row) {
126 $result[] = call_user_func([static::FACTORY, 'create'], $row);
127 }
128
129 return $result;
130 }
131 }
132