DonorsExport.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Exports; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 7 | use Give_Batch_Export; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.21.2 |
| 11 | */ |
| 12 | class DonorsExport extends Give_Batch_Export |
| 13 | { |
| 14 | /** @inheritdoc */ |
| 15 | public $export_type = 'donors'; |
| 16 | |
| 17 | /** @var array */ |
| 18 | protected $postedData; |
| 19 | |
| 20 | /** @var String */ |
| 21 | protected $startDate; |
| 22 | |
| 23 | /** @var String */ |
| 24 | protected $endDate; |
| 25 | |
| 26 | /** @var String */ |
| 27 | protected $searchBy; |
| 28 | |
| 29 | /** |
| 30 | * @inheritdoc |
| 31 | */ |
| 32 | public function set_properties($request) |
| 33 | { |
| 34 | $this->postedData = $request; |
| 35 | |
| 36 | if ($this->postedData['giveDonorExport-startDate']) { |
| 37 | $this->startDate = date('Y-m-d', strtotime($this->postedData['giveDonorExport-startDate'])); |
| 38 | } |
| 39 | |
| 40 | if ($this->postedData['giveDonorExport-endDate']) { |
| 41 | $this->endDate = date('Y-m-d', strtotime($this->postedData['giveDonorExport-endDate'])); |
| 42 | } |
| 43 | |
| 44 | if ($this->postedData['searchBy']) { |
| 45 | $this->searchBy = $this->postedData['searchBy']; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @since 2.21.2 |
| 51 | */ |
| 52 | public function csv_cols(): array |
| 53 | { |
| 54 | return $this->flattenAddressColumn( |
| 55 | array_intersect_key([ |
| 56 | 'full_name' => __('Name', 'give'), |
| 57 | 'email' => __('Email', 'give'), |
| 58 | 'address' => [ |
| 59 | 'address_line1' => __('Address', 'give'), |
| 60 | 'address_line2' => __('Address 2', 'give'), |
| 61 | 'address_city' => __('City', 'give'), |
| 62 | 'address_state' => __('State', 'give'), |
| 63 | 'address_zip' => __('Zip', 'give'), |
| 64 | 'address_country' => __('Country', 'give'), |
| 65 | ], |
| 66 | 'userid' => __('User ID', 'give'), |
| 67 | 'donations' => __('Number of donations', 'give'), |
| 68 | 'donation_sum' => __('Total Donated', 'give'), |
| 69 | ], $this->postedData['give_export_columns']) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 2.21.2 |
| 75 | */ |
| 76 | public function get_data(): array |
| 77 | { |
| 78 | $donorQuery = DB::table('give_donors', 'donors') |
| 79 | ->distinct() |
| 80 | ->select( |
| 81 | ['donors.name', 'full_name'], |
| 82 | ['donors.email', 'email'], |
| 83 | ['donors.user_id', 'userid'], |
| 84 | ['donors.purchase_count', 'donations'], |
| 85 | ['donors.purchase_value', 'donation_sum'] |
| 86 | ); |
| 87 | |
| 88 | $donationQuery = DB::table('posts', 'donations') |
| 89 | ->select('donations.ID', ['meta.meta_value', 'donorId']) |
| 90 | ->join(function (JoinQueryBuilder $builder) { |
| 91 | $builder |
| 92 | ->leftJoin('give_donationmeta', 'meta') |
| 93 | ->on('donations.ID', 'meta.donation_id') |
| 94 | ->andOn('meta.meta_key', '_give_payment_donor_id', true); |
| 95 | }) |
| 96 | ->where('donations.post_type', 'give_payment'); |
| 97 | |
| 98 | if ($this->searchBy === 'donor') { |
| 99 | if ($this->startDate && $this->endDate) { |
| 100 | $donorQuery->whereBetween('DATE(donors.date_created)', $this->startDate, $this->endDate); |
| 101 | } elseif ($this->startDate) { |
| 102 | $donorQuery->where('DATE(donors.date_created)', $this->startDate, '>='); |
| 103 | } elseif ($this->endDate) { |
| 104 | $donorQuery->where('DATE(donors.date_created)', $this->endDate, '<='); |
| 105 | } |
| 106 | } else { |
| 107 | if ($this->startDate && $this->endDate) { |
| 108 | $donationQuery->whereBetween('DATE(donations.post_date)', $this->startDate, $this->endDate); |
| 109 | } elseif ($this->startDate) { |
| 110 | $donationQuery->where('DATE(donations.post_date)', $this->startDate, '>='); |
| 111 | } elseif ($this->endDate) { |
| 112 | $donationQuery->where('DATE(donations.post_date)', $this->endDate, '<='); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | $donorQuery->joinRaw("JOIN ({$donationQuery->getSQL()}) AS sub ON donors.id = sub.donorId"); |
| 117 | |
| 118 | if ($this->shouldIncludeAddress()) { |
| 119 | $donorQuery->attachMeta('give_donormeta', |
| 120 | 'donors.ID', |
| 121 | 'donor_id', |
| 122 | ['_give_donor_address_billing_line1_0', 'address_line1'], |
| 123 | ['_give_donor_address_billing_line2_0', 'address_line2'], |
| 124 | ['_give_donor_address_billing_city_0', 'address_city'], |
| 125 | ['_give_donor_address_billing_state_0', 'address_state'], |
| 126 | ['_give_donor_address_billing_zip_0', 'address_zip'], |
| 127 | ['_give_donor_address_billing_country_0', 'address_country'] |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $this->filterExportData( |
| 132 | array_map(function ($row) { |
| 133 | return array_intersect_key($row, $this->csv_cols()); |
| 134 | }, $donorQuery->getAll(ARRAY_A)) |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @since 2.21.2 |
| 140 | */ |
| 141 | protected function shouldIncludeAddress(): bool |
| 142 | { |
| 143 | return isset($this->postedData['give_export_columns']['address']); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @since 2.21.2 |
| 148 | */ |
| 149 | protected function flattenAddressColumn(array $columnarData): array |
| 150 | { |
| 151 | return $this->flattenColumn($columnarData, 'address'); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @since 2.21.2 |
| 156 | */ |
| 157 | protected function flattenColumn(array $columnarData, string $columnName): array |
| 158 | { |
| 159 | if (isset($columnarData[$columnName])) { |
| 160 | $columnarData = array_merge($columnarData, $columnarData[$columnName]); |
| 161 | unset($columnarData[$columnName]); |
| 162 | } |
| 163 | |
| 164 | return $columnarData; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @since 2.21.2 |
| 169 | */ |
| 170 | protected function filterExportData(array $exportData): array |
| 171 | { |
| 172 | /** |
| 173 | * @since 2.21.2 |
| 174 | * |
| 175 | * @param array $exportData |
| 176 | */ |
| 177 | return apply_filters("give_export_get_data_{$this->export_type}", $exportData); |
| 178 | } |
| 179 | } |
| 180 |