| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Campaigns\Actions; |
| 4 |
|
| 5 |
|
| 6 |
use Give\Campaigns\Models\Campaign; |
| 7 |
use Give\Framework\Database\DB; |
| 8 |
use Give\Framework\QueryBuilder\JoinQueryBuilder; |
| 9 |
|
| 10 |
/** |
| 11 |
* When a Campaign is archived, set all Forms to Draft Status |
| 12 |
* |
| 13 |
* @since 4.0.0 |
| 14 |
*/ |
| 15 |
class ArchiveCampaignFormsAsDraftStatus |
| 16 |
{ |
| 17 |
/** |
| 18 |
* TODO: update this to use single update query (QB was not working with whereIn and update) |
| 19 |
* @since 4.0.0 |
| 20 |
*/ |
| 21 |
public function __invoke(Campaign $campaign) |
| 22 |
{ |
| 23 |
if (!$campaign->status->isArchived()) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$query = DB::table('posts') |
| 28 |
->where('post_type', 'give_forms') |
| 29 |
->where('post_status', 'publish') |
| 30 |
->join(function (JoinQueryBuilder $builder) { |
| 31 |
$builder |
| 32 |
->leftJoin("give_campaign_forms", "campaign_forms") |
| 33 |
->on("campaign_forms.form_id", "id"); |
| 34 |
}) |
| 35 |
->where("campaign_forms.campaign_id", $campaign->id) |
| 36 |
->getAll(); |
| 37 |
|
| 38 |
$ids = array_values(array_column($query, 'ID')); |
| 39 |
|
| 40 |
foreach ($ids as $id) { |
| 41 |
DB::table('posts') |
| 42 |
->where('ID', $id) |
| 43 |
->update( |
| 44 |
['post_status' => 'draft'] |
| 45 |
); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|