PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
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 2.3.2 2.30.0 All 255 releases
give / src / Donors / CustomFields / Controllers / DonorDetailsController.php

DonorDetailsController.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Donors/CustomFields/Controllers/DonorDetailsController.php

75 lines 2.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\CustomFields\Controllers;
4
5 use Give\DonationForms\Models\DonationForm;
6 use Give\DonationForms\Repositories\DonationFormRepository;
7 use Give\Donations\Models\Donation;
8 use Give\Donors\CustomFields\Views\DonorDetailsView;
9 use Give\Donors\Models\Donor;
10
11 use function array_reduce;
12
13 /**
14 * TODO: move into donors domain
15 * @since 3.0.0
16 */
17 class DonorDetailsController
18 {
19 /**
20 * @since 3.0.0
21 */
22 public function show(Donor $donor): string
23 {
24 $forms = $this->getUniqueDonationFormsForDonor($donor);
25
26 if (!$forms) {
27 return '';
28 }
29
30 $fields = array_reduce($forms, function ($fields, DonationForm $form) {
31 return $fields + $this->getDisplayedDonorMetaFieldsForForm($form);
32 }, []);
33
34 return (new DonorDetailsView($donor, $fields))->render();
35 }
36
37 /**
38 * @since 4.0.0 return array_filter to reduce null values
39 * @since 3.2.2 added array fallback when no donations are found
40 * @since 3.0.0
41 *
42 * @param Donor $donor
43 *
44 * @return DonationForm[]
45 */
46 protected function getUniqueDonationFormsForDonor(Donor $donor): array
47 {
48 $formIds = array_map(static function (Donation $donation) {
49 return $donation->formId;
50 }, $donor->donations()->getAll() ?? []);
51
52 $formIds = array_filter($formIds, static function ($formId) {
53 return !give(DonationFormRepository::class)->isLegacyForm($formId);
54 });
55
56 return array_filter(array_map(static function ($formId) {
57 return DonationForm::find($formId);
58 }, array_unique($formIds)));
59 }
60
61 /**
62 * @since 3.0.0
63 *
64 * @param DonationForm $form
65 *
66 * @return array
67 */
68 protected function getDisplayedDonorMetaFieldsForForm(DonationForm $form): array
69 {
70 return array_filter($form->schema()->getFields(), static function ($field) {
71 return $field->shouldShowInAdmin() && $field->shouldStoreAsDonorMeta();
72 });
73 }
74 }
75