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 / Donations / Repositories / DonationNotesRepository.php

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

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