PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.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 / GoogleSheets / GoogleSheetsRepository.php

GoogleSheetsRepository.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Repository/GoogleSheets/GoogleSheetsRepository.php

188 lines 4.8 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\GoogleSheets;
4
5 use IvyForms\Entity\GoogleSheets\GoogleSheetIntegration;
6 use IvyForms\Factory\GoogleSheets\GoogleSheetIntegrationFactory;
7 use IvyForms\Repository\AbstractRepository;
8
9 /**
10 * Google Sheets repository.
11 */
12 class GoogleSheetsRepository extends AbstractRepository implements GoogleSheetsRepositoryInterface
13 {
14 public const FACTORY = GoogleSheetIntegrationFactory::class;
15
16 public function __construct(string $tableName)
17 {
18 parent::__construct($tableName);
19 }
20
21 public function findById(int $id): ?GoogleSheetIntegration
22 {
23 $entity = $this->getById($id);
24
25 return $entity instanceof GoogleSheetIntegration ? $entity : null;
26 }
27
28 /**
29 * @return array<GoogleSheetIntegration>
30 */
31 public function findByFormId(int $formId): array
32 {
33 $query = $this->wpdb->prepare(
34 "SELECT * FROM {$this->table} WHERE form_id = %d ORDER BY id ASC",
35 $formId
36 );
37
38 $rows = $this->wpdb->get_results($query, ARRAY_A);
39 $result = [];
40
41 foreach ($rows as $row) {
42 $result[] = call_user_func([static::FACTORY, 'create'], $row);
43 }
44
45 return $result;
46 }
47
48 /**
49 * @return array<GoogleSheetIntegration>
50 */
51 public function findEnabledByFormId(int $formId): array
52 {
53 $query = $this->wpdb->prepare(
54 "SELECT * FROM {$this->table} WHERE form_id = %d AND enabled = 1 ORDER BY id ASC",
55 $formId
56 );
57
58 $rows = $this->wpdb->get_results($query, ARRAY_A);
59 $result = [];
60
61 foreach ($rows as $row) {
62 $result[] = call_user_func([static::FACTORY, 'create'], $row);
63 }
64
65 return $result;
66 }
67
68 public function countByFormId(int $formId): int
69 {
70 $query = $this->wpdb->prepare(
71 "SELECT COUNT(*) FROM {$this->table} WHERE form_id = %d",
72 $formId
73 );
74
75 return (int) $this->wpdb->get_var($query);
76 }
77
78 public function deleteById(int $id): bool
79 {
80 $result = $this->wpdb->delete(
81 $this->table,
82 ['id' => $id],
83 ['%d']
84 );
85
86 return $result !== false;
87 }
88
89 public function deleteByFormId(int $formId): bool
90 {
91 $result = $this->wpdb->delete(
92 $this->table,
93 ['form_id' => $formId],
94 ['%d']
95 );
96
97 return $result !== false;
98 }
99
100 public function updateSyncStatus(int $id, string $status, ?string $lastRunAt = null): bool
101 {
102 $result = $this->wpdb->update(
103 $this->table,
104 [
105 'last_status' => $status,
106 'last_run_at' => $lastRunAt ?? current_time('mysql'),
107 ],
108 ['id' => $id],
109 ['%s', '%s'],
110 ['%d']
111 );
112
113 return $result !== false;
114 }
115
116 public function add(object $entity): int
117 {
118 if (!$entity instanceof GoogleSheetIntegration) {
119 return 0;
120 }
121
122 if ($entity->getId()) {
123 error_log('GoogleSheetsRepository: Cannot add integration that already has an ID.');
124 return 0;
125 }
126
127 $saved = $this->save($entity);
128
129 return $saved->getId() ?? 0;
130 }
131
132 public function update(int $id, object $entity): bool
133 {
134 if (!$entity instanceof GoogleSheetIntegration) {
135 return false;
136 }
137
138 $entity->setId($id);
139 $this->save($entity);
140
141 return true;
142 }
143
144 private function save(GoogleSheetIntegration $integration): GoogleSheetIntegration
145 {
146 $data = $integration->toArray();
147
148 $dbData = [
149 'form_id' => $data['form_id'],
150 'spreadsheet_id' => $data['spreadsheet_id'],
151 'spreadsheet_name' => $data['spreadsheet_name'],
152 'worksheet_name' => $data['worksheet_name'],
153 'field_mapping' => wp_json_encode($data['field_mapping']),
154 'smart_logic' => wp_json_encode($data['smart_logic'] ?? []),
155 'enabled' => $data['enabled'] ? 1 : 0,
156 'last_status' => $data['last_status'],
157 'last_run_at' => $data['last_run_at'],
158 ];
159
160 $format = ['%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s'];
161
162 if ($integration->getId()) {
163 $this->wpdb->update(
164 $this->table,
165 $dbData,
166 ['id' => $integration->getId()],
167 $format,
168 ['%d']
169 );
170
171 return $integration;
172 }
173
174 $this->wpdb->insert($this->table, $dbData, $format);
175 $integration->setId((int) $this->wpdb->insert_id);
176
177 return $integration;
178 }
179
180 /**
181 * @return string[]
182 */
183 protected function getAllowedForeignColumns(): array
184 {
185 return ['form_id'];
186 }
187 }
188