PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Donors / Repositories / DonorNotesRepository.php

DonorNotesRepository.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Donors/Repositories/DonorNotesRepository.php

253 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Donors\Repositories;
4
5 use Exception;
6 use Give\Donors\Models\DonorNote;
7 use Give\Donors\ValueObjects\DonorNoteMetaKeys;
8 use Give\Donors\ValueObjects\DonorNoteType;
9 use Give\Framework\Database\DB;
10 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
11 use Give\Framework\Models\ModelQueryBuilder;
12 use Give\Framework\Support\Facades\DateTime\Temporal;
13 use Give\Helpers\Hooks;
14 use Give\Log\Log;
15
16 /**
17 * @since 4.4.0
18 */
19 class DonorNotesRepository
20 {
21 /**
22 * @since 4.4.0
23 *
24 * @var string[]
25 */
26 private $requiredDonorProperties = [
27 'donorId',
28 'content',
29 ];
30
31 /**
32 * @since 4.4.0
33 */
34 public function getById(int $noteId): ?DonorNote
35 {
36 return $this->prepareQuery()
37 ->where('comment_ID', $noteId)
38 ->get();
39 }
40
41 /**
42 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
43 * @since 4.4.0
44 *
45 * @throws Exception
46 */
47 public function insert(DonorNote $donorNote)
48 {
49 if ( ! $donorNote->type) {
50 $donorNote->type = DonorNoteType::ADMIN();
51 }
52
53 $this->validateDonorNote($donorNote);
54
55 Hooks::doAction('givewp_donor_note_creating', $donorNote);
56
57 $dateCreated = Temporal::withoutMicroseconds($donorNote->createdAt ?: Temporal::getCurrentDateTime());
58 $dateCreatedFormatted = Temporal::getFormattedDateTime($dateCreated);
59
60
61 DB::beginTransaction();
62
63 try {
64 DB::table('give_comments')
65 ->insert([
66 'comment_content' => $donorNote->content,
67 'comment_date' => $dateCreatedFormatted,
68 'comment_date_gmt' => get_gmt_from_date($dateCreatedFormatted),
69 'comment_parent' => $donorNote->donorId,
70 'comment_type' => 'donor',
71 ]);
72
73 $commentId = DB::last_insert_id();
74
75 if ($donorNote->type->isDonor()) {
76 DB::table('give_commentmeta')
77 ->insert([
78 'give_comment_id' => $commentId,
79 'meta_key' => DonorNoteMetaKeys::TYPE,
80 'meta_value' => DonorNoteType::DONOR,
81 ]);
82 }
83 } catch (Exception $exception) {
84 DB::rollback();
85
86 Log::error('Failed creating a donor note', compact('donorNote'));
87
88 throw new Exception('Failed creating a donor note', 0, $exception);
89 }
90
91 DB::commit();
92
93 $donorNote->id = $commentId;
94 $donorNote->createdAt = $dateCreated;
95
96 Hooks::doAction('givewp_donor_note_created', $donorNote);
97 }
98
99 /**
100 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
101 * @since 4.4.0
102 *
103 * @throws Exception
104 */
105 public function update(DonorNote $donorNote)
106 {
107 $this->validateDonorNote($donorNote);
108
109 Hooks::doAction('givewp_donor_note_updating', $donorNote);
110
111 DB::beginTransaction();
112
113 try {
114 DB::table('give_comments')
115 ->where('comment_ID', $donorNote->id)
116 ->update([
117 'comment_content' => $donorNote->content,
118 'comment_parent' => $donorNote->donorId,
119 'comment_type' => 'donor',
120 ]);
121 if ($donorNote->isDirty('type') && $donorNote->type->isDonor()) {
122 $this->upsertDonorNoteType($donorNote);
123 }
124 } catch (Exception $exception) {
125 DB::rollback();
126
127 Log::error('Failed updating a donor note', compact('donorNote'));
128
129 throw new Exception('Failed updating a donor note', 0, $exception);
130 }
131
132 DB::commit();
133
134 Hooks::doAction('givewp_donor_note_updated', $donorNote);
135 }
136
137 /**
138 * @since 4.18.0 Keep the original error as the previous exception when the write fails.
139 * @since 4.4.0
140 *
141 * @throws Exception
142 */
143 public function delete(DonorNote $donorNote): bool
144 {
145 DB::beginTransaction();
146
147 Hooks::doAction('givewp_donor_note_deleting', $donorNote);
148
149 try {
150 DB::table('give_comments')
151 ->where('comment_ID', $donorNote->id)
152 ->delete();
153
154 DB::table('give_commentmeta')
155 ->where('give_comment_id', $donorNote->id)
156 ->delete();
157 } catch (Exception $exception) {
158 DB::rollback();
159
160 Log::error('Failed deleting a donor note', compact('donorNote'));
161
162 throw new Exception('Failed deleting a donor note', 0, $exception);
163 }
164
165 DB::commit();
166
167 Hooks::doAction('givewp_donor_note_deleted', $donorNote);
168
169 return true;
170 }
171
172 /**
173 * @since 4.4.0
174 */
175 public function queryByDonorId(int $donorId): ModelQueryBuilder
176 {
177 return $this->prepareQuery()
178 ->where('comment_parent', $donorId)
179 ->orderBy('comment_ID', 'DESC');
180 }
181
182 /**
183 * @since 4.4.0
184 *
185 * @return void
186 */
187 private function validateDonorNote(DonorNote $donorNote)
188 {
189 foreach ($this->requiredDonorProperties as $key) {
190 if ( ! isset($donorNote->$key)) {
191 throw new InvalidArgumentException("'$key' is required.");
192 }
193 }
194
195 if ( ! $donorNote->donor) {
196 throw new InvalidArgumentException('Invalid donorId, Donor does not exist');
197 }
198 }
199
200 /**
201 * @since 4.4.0
202 *
203 * @return ModelQueryBuilder<DonorNote>
204 */
205 public function prepareQuery(): ModelQueryBuilder
206 {
207 $builder = new ModelQueryBuilder(DonorNote::class);
208
209 return $builder->from('give_comments')
210 ->select(
211 ['comment_ID', 'id'],
212 ['comment_parent', 'donorId'],
213 ['comment_content', 'content'],
214 ['comment_date', 'createdAt']
215 )
216 ->attachMeta(
217 'give_commentmeta',
218 'comment_ID',
219 'give_comment_id',
220 ...DonorNoteMetaKeys::getColumnsForAttachMetaQuery()
221 )
222 ->where('comment_type', 'donor');
223 }
224
225 /**
226 * @since 4.4.0
227 */
228 private function upsertDonorNoteType(DonorNote $donorNote)
229 {
230 $table = DB::table('give_commentmeta');
231
232 $query = $table
233 ->where('give_comment_id', $donorNote->id)
234 ->where('meta_key', DonorNoteMetaKeys::TYPE)
235 ->get();
236
237 if ( ! $query) {
238 $table->insert([
239 'give_comment_id' => $donorNote->id,
240 'meta_key' => DonorNoteMetaKeys::TYPE,
241 'meta_value' => $donorNote->type->getValue(),
242 ]);
243 } else {
244 $table
245 ->where('give_comment_id', $donorNote->id)
246 ->where('meta_key', DonorNoteMetaKeys::TYPE)
247 ->update([
248 'meta_value' => $donorNote->type->getValue(),
249 ]);
250 }
251 }
252 }
253