Actions
8 months ago
CustomFields
1 year ago
DataTransferObjects
1 year ago
Endpoints
8 months ago
Exceptions
2 years ago
Factories
1 year ago
ListTable
8 months ago
Migrations
1 year ago
Models
1 year ago
Repositories
8 months ago
ValueObjects
9 months ago
ViewModels
9 months ago
resources
8 months ago
DonorStatisticsQuery.php
9 months ago
DonorsAdminPage.php
11 months ago
DonorsQuery.php
9 months ago
ServiceProvider.php
11 months ago
DonorsQuery.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Donors; |
| 4 | |
| 5 | use Give\Donations\ValueObjects\DonationMetaKeys; |
| 6 | use Give\Donors\Models\Donor; |
| 7 | use Give\Framework\Database\DB; |
| 8 | use Give\Framework\Models\ModelQueryBuilder; |
| 9 | use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 10 | use Give\Framework\QueryBuilder\QueryBuilder; |
| 11 | |
| 12 | /** |
| 13 | * @since 4.4.0 |
| 14 | */ |
| 15 | class DonorsQuery |
| 16 | { |
| 17 | /** |
| 18 | * @var ModelQueryBuilder<Donor> |
| 19 | */ |
| 20 | protected $query; |
| 21 | |
| 22 | /** |
| 23 | * @since 4.4.0 |
| 24 | */ |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->query = Donor::query(); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Delegates methods not defined locally to the underlying query. |
| 32 | * |
| 33 | * @since 4.4.0 |
| 34 | * |
| 35 | * @return mixed |
| 36 | */ |
| 37 | public function __call(string $method, array $args) |
| 38 | { |
| 39 | if (method_exists($this, $method)) { |
| 40 | return $this->$method(...$args); |
| 41 | } |
| 42 | |
| 43 | return $this->query->$method(...$args); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @since 4.8.0 Fix subqueries to not return duplicate donors |
| 48 | * @since 4.4.0 |
| 49 | */ |
| 50 | public function whereDonorsHaveDonations( |
| 51 | string $mode = '', |
| 52 | int $campaignId = 0, |
| 53 | bool $excludeAnonymousDonors = true |
| 54 | ): self { |
| 55 | if (empty($mode)) { |
| 56 | $mode = give_is_test_mode() ? 'test' : 'live'; |
| 57 | } |
| 58 | |
| 59 | $this->query->whereExists(function (QueryBuilder $builder) use ($mode, $campaignId, $excludeAnonymousDonors) { |
| 60 | $builder |
| 61 | ->select('1') |
| 62 | ->from('give_donationmeta', 'dm1') |
| 63 | ->join(function (JoinQueryBuilder $joinBuilder) use ($mode) { |
| 64 | $joinBuilder |
| 65 | ->innerJoin('give_donationmeta', 'dm2') |
| 66 | ->on('dm2.donation_id', 'dm1.donation_id') |
| 67 | ->andOn('dm2.meta_key', DonationMetaKeys::MODE, true) |
| 68 | ->andOn('dm2.meta_value', $mode, true); |
| 69 | }) |
| 70 | ->join(function (JoinQueryBuilder $joinBuilder) { |
| 71 | $joinBuilder |
| 72 | ->innerJoin('posts', 'p') |
| 73 | ->on('p.ID', 'dm1.donation_id') |
| 74 | ->andOn('p.post_type', 'give_payment', true); |
| 75 | }) |
| 76 | ->whereIn('p.post_status', ['publish', 'give_subscription']) |
| 77 | ->where('dm1.meta_key', DonationMetaKeys::DONOR_ID) |
| 78 | ->whereRaw(sprintf('AND dm1.meta_value = %s', $this->query->prefixTable('give_donors.id'))); |
| 79 | |
| 80 | if ($campaignId) { |
| 81 | $builder->join(function (JoinQueryBuilder $joinBuilder) use ($campaignId) { |
| 82 | $joinBuilder |
| 83 | ->innerJoin('give_donationmeta', 'dm3') |
| 84 | ->on('dm3.donation_id', 'dm1.donation_id') |
| 85 | ->andOn('dm3.meta_key', DonationMetaKeys::CAMPAIGN_ID, true) |
| 86 | ->andOn('dm3.meta_value', $campaignId, true); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if ($excludeAnonymousDonors) { |
| 91 | $builder->join(function (JoinQueryBuilder $joinBuilder) { |
| 92 | $joinBuilder |
| 93 | ->innerJoin('give_donationmeta', 'dm4') |
| 94 | ->on('dm4.donation_id', 'dm1.donation_id') |
| 95 | ->andOn('dm4.meta_key', DonationMetaKeys::ANONYMOUS, true) |
| 96 | ->andOn('dm4.meta_value', '0', true); |
| 97 | }); |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 4.4.0 |
| 106 | */ |
| 107 | public function limit(int $limit): self |
| 108 | { |
| 109 | $this->query->limit($limit); |
| 110 | |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @since 4.4.0 |
| 116 | */ |
| 117 | public function offset(int $offset): self |
| 118 | { |
| 119 | $this->query->offset($offset); |
| 120 | |
| 121 | return $this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @since 4.4.0 |
| 126 | */ |
| 127 | public function orderBy(string $column, string $direction = 'ASC'): self |
| 128 | { |
| 129 | $this->query->orderBy($column, $direction); |
| 130 | |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @since 4.4.0 |
| 136 | */ |
| 137 | public function getAll(): array |
| 138 | { |
| 139 | return $this->query->getAll() ?? []; |
| 140 | } |
| 141 | } |
| 142 |