| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormMigration\Actions; |
| 4 |
|
| 5 |
use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 6 |
use Give\Framework\Database\DB; |
| 7 |
|
| 8 |
class TransferDonations |
| 9 |
{ |
| 10 |
protected $sourceId; |
| 11 |
|
| 12 |
public function __construct($sourceId) |
| 13 |
{ |
| 14 |
$this->sourceId = $sourceId; |
| 15 |
} |
| 16 |
|
| 17 |
public static function from($sourceId): self |
| 18 |
{ |
| 19 |
return new TransferDonations($sourceId); |
| 20 |
} |
| 21 |
|
| 22 |
public function to($destinationId): void |
| 23 |
{ |
| 24 |
$this->__invoke($destinationId); |
| 25 |
} |
| 26 |
|
| 27 |
public function __invoke($destinationId) |
| 28 |
{ |
| 29 |
DB::transaction(function() use ($destinationId) { |
| 30 |
|
| 31 |
// Mark the v2 form as "upgraded". |
| 32 |
DB::table('posts') |
| 33 |
->where('ID', $this->sourceId) |
| 34 |
->update(['post_status' => DonationFormStatus::UPGRADED]); |
| 35 |
|
| 36 |
DB::table('give_donationmeta') |
| 37 |
->where('meta_key', '_give_payment_form_id') |
| 38 |
->where('meta_value', $this->sourceId) |
| 39 |
->update(['meta_value' => $destinationId]); |
| 40 |
|
| 41 |
DB::table('give_revenue') |
| 42 |
->where('form_id', $this->sourceId) |
| 43 |
->update(['form_id' => $destinationId]); |
| 44 |
|
| 45 |
// Update subscriptions to use v3 form ID |
| 46 |
DB::table('give_subscriptions') |
| 47 |
->where('product_id', $this->sourceId) |
| 48 |
->update(['product_id' => $destinationId]); |
| 49 |
|
| 50 |
give_update_meta( |
| 51 |
$destinationId, |
| 52 |
'_give_form_sales', |
| 53 |
(int)give_get_meta($this->sourceId, '_give_form_sales', true) |
| 54 |
); |
| 55 |
give_update_meta($this->sourceId, '_give_form_sales', 0); |
| 56 |
|
| 57 |
give_update_meta( |
| 58 |
$destinationId, |
| 59 |
'_give_form_earnings', |
| 60 |
(float)give_get_meta($this->sourceId, '_give_form_earnings', true) |
| 61 |
); |
| 62 |
give_update_meta($this->sourceId, '_give_form_earnings', 0); |
| 63 |
|
| 64 |
}); |
| 65 |
} |
| 66 |
} |
| 67 |
|