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

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