PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
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 2.3.2 2.30.0 2.31.0 All 254 releases
give / src / Donations / Repositories / DonationNotesRepository.php

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

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