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 / Campaigns / CampaignsDataQuery.php

CampaignsDataQuery.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Campaigns/CampaignsDataQuery.php

136 lines 4.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\Campaigns;
4
5 use Give\Donations\ValueObjects\DonationMetaKeys;
6 use Give\Framework\QueryBuilder\JoinQueryBuilder;
7 use Give\Framework\QueryBuilder\QueryBuilder;
8
9 /**
10 * Class used for loading the number of donors, donations and revenue amounts for multiple campaigns
11 *
12 * @since 4.0.0
13 */
14 class CampaignsDataQuery extends QueryBuilder
15 {
16 /**
17 * @since 4.0.0
18 *
19 * @param int[] $campaignIds
20 */
21 private function __construct(array $campaignIds)
22 {
23 $this->select('campaignId.meta_value as campaign_id');
24 $this->whereIn('donation.post_status', ['publish', 'give_subscription']);
25 $this->whereIn('campaignId.meta_value', $campaignIds);
26 $this->groupBy('campaign_id');
27 }
28
29
30 /**
31 * Donations query for campaigns
32 *
33 * @param int[] $campaignIds - campaign ids
34 *
35 * @return CampaignsDataQuery
36 */
37 public static function donations(array $campaignIds): CampaignsDataQuery
38 {
39 return (new self($campaignIds))
40 ->from('posts', 'donation')
41 ->where('post_type', 'give_payment')
42 ->joinDonationMeta(DonationMetaKeys::CAMPAIGN_ID, 'campaignId')
43 ->joinDonationMeta(DonationMetaKeys::MODE, 'paymentMode')
44 ->where('paymentMode.meta_value', give_is_test_mode() ? 'test' : 'live');
45 }
46
47 /**
48 * Subscriptions query for campaigns
49 *
50 * @param int[] $campaignIds - campaign ids
51 *
52 * @return CampaignsDataQuery
53 */
54 public static function subscriptions(array $campaignIds): CampaignsDataQuery
55 {
56 return (new self($campaignIds))
57 ->from('give_subscriptions', 'subscription')
58 ->join(function (JoinQueryBuilder $builder) {
59 $builder
60 ->leftJoin('posts', 'donation')
61 ->on('subscription.parent_payment_id', 'donation.ID');
62 })
63 ->joinDonationMeta(DonationMetaKeys::CAMPAIGN_ID, 'campaignId')
64 ->where('payment_mode', give_is_test_mode() ? 'test' : 'live')
65 ->where('donation.post_type', 'give_payment');
66 }
67
68 /**
69 * Returns a calculated sum of the intended amounts (without recovered fees) for the donations.
70 *
71 * @since 4.0.0
72 *
73 * @return array|object|null
74 */
75 public function collectIntendedAmounts()
76 {
77 return (clone $this)
78 ->select('SUM((IFNULL(amount.meta_value, 0) - IFNULL(feeAmountRecovered.meta_value, 0)) / IFNULL(exchangeRate.meta_value, 1)) as sum')
79 ->joinDonationMeta(DonationMetaKeys::AMOUNT, 'amount')
80 ->joinDonationMeta(DonationMetaKeys::FEE_AMOUNT_RECOVERED, 'feeAmountRecovered')
81 ->joinDonationMeta(DonationMetaKeys::EXCHANGE_RATE, 'exchangeRate')
82 ->getAll(ARRAY_A);
83 }
84
85 /**
86 * Returns a calculated sum of the initial amounts
87 *
88 * @since 4.0.0
89 *
90 * @return array|object|null
91 */
92 public function collectInitialAmounts()
93 {
94 return (clone $this)
95 ->select('SUM(initial_amount) as sum')
96 ->getAll(ARRAY_A);
97 }
98
99 /**
100 * @since 4.0.0
101 */
102 public function collectDonations()
103 {
104 return (clone $this)
105 ->select('COUNT(donation.ID) as count')
106 ->getAll(ARRAY_A);
107 }
108
109 /**
110 * @since 4.0.0
111 */
112 public function collectDonors()
113 {
114 return (clone $this)
115 ->select('COUNT(DISTINCT donorId.meta_value) as count')
116 ->joinDonationMeta(DonationMetaKeys::DONOR_ID, 'donorId')
117 ->getAll(ARRAY_A);
118 }
119
120 /**
121 * An opinionated join method for the donation meta table.
122 * @since 4.0.0
123 */
124 private function joinDonationMeta($key, $alias): self
125 {
126 $this->join(function (JoinQueryBuilder $builder) use ($key, $alias) {
127 $builder
128 ->leftJoin('give_donationmeta', $alias)
129 ->on('donation.ID', $alias . '.donation_id')
130 ->andOn($alias . '.meta_key', $key, true);
131 });
132
133 return $this;
134 }
135 }
136