Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Default form #7616

Open
wants to merge 9 commits into
base: epic/campaigns
Choose a base branch
from
1 change: 1 addition & 0 deletions src/Campaigns/Actions/ConvertQueryDataToCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __invoke(object $queryObject): Campaign
{
return new Campaign([
'id' => (int)$queryObject->id,
'defaultFormId' => (int)$queryObject->defaultFormId,
'type' => new CampaignType($queryObject->type),
'title' => $queryObject->title,
'shortDescription' => $queryObject->shortDescription,
Expand Down
4 changes: 1 addition & 3 deletions src/Campaigns/Actions/LoadCampaignDetailsAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Give\Campaigns\Models\Campaign;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use Give\Helpers\Form\Utils;

/**
* @unreleased
Expand Down Expand Up @@ -34,13 +33,12 @@ public function __invoke()
);

$defaultForm = $campaign->defaultForm();
$defaultFormTitle = Utils::isV3Form($defaultForm->id) ? $defaultForm->settings->formTitle : $defaultForm->title;
wp_localize_script($handleName, 'GiveCampaignDetails',
[
'adminUrl' => admin_url(),
'currency' => give_get_currency(),
'isRecurringEnabled' => defined('GIVE_RECURRING_VERSION') ? GIVE_RECURRING_VERSION : null,
'defaultForm' => $defaultFormTitle,
'defaultForm' => $defaultForm->title,
]
);

Expand Down
18 changes: 16 additions & 2 deletions src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public function updateCampaign(WP_REST_Request $request)
$campaign->goalType = new CampaignGoalType($value);
break;
case 'defaultFormId':
give(CampaignRepository::class)->updateDefaultCampaignForm($campaign,
$request->get_param('defaultFormId'));
give(CampaignRepository::class)->updateDefaultCampaignForm($campaign, $request->get_param('defaultFormId'));
break;
default:
if ($campaign->hasProperty($key)) {
Expand All @@ -142,6 +141,21 @@ public function updateCampaign(WP_REST_Request $request)
return new WP_REST_Response($campaign->toArray());
}

/**
* @unreleased
*
* @throws Exception
*/
public function mergeCampaigns(WP_REST_Request $request): WP_REST_Response
{
$destinationCampaign = Campaign::find($request->get_param('id'));
$campaignsToMerge = Campaign::query()->whereIn('id', $request->get_param('campaignsToMergeIds'))->getAll();

$campaignsMerged = $destinationCampaign->merge(...$campaignsToMerge);

return new WP_REST_Response($campaignsMerged);
}


/**
* @unreleased
Expand Down
10 changes: 5 additions & 5 deletions src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function createCampaignForForm($formData): void

DB::table('give_campaigns')
->insert([
'form_id' => $formId,
'campaign_type' => 'core',
'campaign_title' => $formTitle,
'status' => $this->mapFormToCampaignStatus($formStatus),
Expand All @@ -134,27 +135,26 @@ public function createCampaignForForm($formData): void

$campaignId = DB::last_insert_id();

$this->addCampaignFormRelationship($formId, $campaignId, true);
$this->addCampaignFormRelationship($formId, $campaignId);
}

/**
* @param $data
*/
protected function addUpgradedFormToCampaign($data): void
{
$this->addCampaignFormRelationship($data->migratedFormId, $data->campaignId, false);
$this->addCampaignFormRelationship($data->migratedFormId, $data->campaignId);
}

/**
* @unreleased
*/
protected function addCampaignFormRelationship($formId, $campaignId, $isDefault)
protected function addCampaignFormRelationship($formId, $campaignId)
{
DB::table('give_campaign_forms')
->insert([
'form_id' => $formId,
'campaign_id' => $campaignId,
'is_default' => $isDefault,
'campaign_id' => $campaignId
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function run(): void
$sql = "CREATE TABLE $table (
campaign_id INT UNSIGNED NOT NULL,
form_id INT UNSIGNED NOT NULL,
is_default BOOLEAN NOT NULL DEFAULT 0,
KEY form_id (form_id),
KEY campaign_id (campaign_id),
PRIMARY KEY (campaign_id, form_id)
Expand Down
40 changes: 10 additions & 30 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Give\Campaigns\ValueObjects\CampaignGoalType;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Campaigns\ValueObjects\CampaignType;
use Give\DonationForms\Models\DonationForm;
use Give\DonationForms\V2\Models\DonationForm as LegacyDonationForm;
use Give\DonationForms\V2\Models\DonationForm;
use Give\DonationForms\V2\Repositories\DonationFormsRepository;
use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Framework\Models\Contracts\ModelCrud;
use Give\Framework\Models\Contracts\ModelHasFactory;
Expand All @@ -24,6 +24,7 @@
* @unreleased
*
* @property int $id
* @property int $defaultFormId
* @property CampaignType $type
* @property string $title
* @property string $url
Expand All @@ -47,6 +48,7 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory
*/
protected $properties = [
'id' => 'int',
'defaultFormId' => 'int',
'type' => CampaignType::class,
'title' => 'string',
'shortDescription' => 'string',
Expand All @@ -65,22 +67,10 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory

/**
* @unreleased
*
* @return DonationForm | LegacyDonationForm
*/
public function defaultForm()
public function defaultForm(): ?DonationForm
{
$defaultForm = $this->forms()
->where('campaign_forms.is_default', true)
->get();

if (is_null($defaultForm)) {
$defaultForm = $this->legacyForms()
->where('campaign_forms.is_default', true)
->get();
}

return $defaultForm;
return give(DonationFormsRepository::class)->getById($this->defaultFormId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we're talkin! 🙌

}

/**
Expand All @@ -90,21 +80,11 @@ public function forms(): ModelQueryBuilder
{
return DonationForm::query()
->join(function (JoinQueryBuilder $builder) {
$builder->leftJoin('give_campaign_forms', 'campaign_forms')
$builder
->leftJoin('give_campaign_forms', 'campaign_forms')
->on('campaign_forms.form_id', 'forms.id');
})->where('campaign_forms.campaign_id', $this->id);
}

/**
* @unreleased
*/
public function legacyForms(): ModelQueryBuilder
{
return LegacyDonationForm::query()
->join(function (JoinQueryBuilder $builder) {
$builder->leftJoin('give_campaign_forms', 'campaign_forms')
->on('campaign_forms.form_id', 'id');
})->where('campaign_forms.campaign_id', $this->id);
})
->where('campaign_forms.campaign_id', $this->id);
}

/**
Expand Down
36 changes: 15 additions & 21 deletions src/Campaigns/Repositories/CampaignRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,19 @@ public function addCampaignForm(Campaign $campaign, int $donationFormId, bool $i
DB::query('START TRANSACTION');

try {
// Make sure we'll have only one default form
if ($isDefault) {
DB::table('give_campaign_forms')
->where('campaign_id', $campaign->id)
DB::table('give_campaigns')
->where('id', $campaign->id)
->update([
'is_default' => false,
'form_id' => $donationFormId,
]);
}

$table = DB::prefix('give_campaign_forms');
DB::query(
DB::prepare("INSERT INTO {$table} (form_id, campaign_id, is_default ) VALUES (%d, %d, %d)",
[
$donationFormId,
$campaign->id,
$isDefault,
])
);
DB::table('give_campaign_forms')
->insert([
'form_id' => $donationFormId,
'campaign_id' => $campaign->id,
]);
} catch (Exception $exception) {
DB::query('ROLLBACK');

Expand All @@ -211,13 +206,11 @@ public function updateDefaultCampaignForm(Campaign $campaign, int $donationFormI
DB::query('START TRANSACTION');

try {
DB::query(
DB::prepare('UPDATE ' . DB::prefix('give_campaign_forms') . ' SET is_default = IF(form_id = %d, 1, 0) WHERE campaign_id = %d',
[
$donationFormId,
$campaign->id,
])
);
DB::table('give_campaigns')
->where('id', $campaign->id)
->update([
'form_id' => $donationFormId
]);
} catch (Exception $exception) {
DB::query('ROLLBACK');

Expand Down Expand Up @@ -293,7 +286,7 @@ public function mergeCampaigns(Campaign $destinationCampaign, Campaign ...$campa

// Migrate forms from campaigns to merge to the destination campaign
DB::query(
DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " SET is_default = 0, campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)",
DB::prepare("UPDATE " . DB::prefix('give_campaign_forms') . " campaign_id = %d WHERE campaign_id IN ($campaignsToMergeIdsString)",
[
$destinationCampaign->id,
])
Expand Down Expand Up @@ -343,6 +336,7 @@ public function prepareQuery(): ModelQueryBuilder
return $builder->from('give_campaigns', 'campaigns')
->select(
'id',
['form_id', 'defaultFormId'],
['campaign_type', 'type'],
['campaign_title', 'title'],
['short_desc', 'shortDescription'],
Expand Down
65 changes: 0 additions & 65 deletions src/Campaigns/Routes/MergeCampaigns.php

This file was deleted.

39 changes: 39 additions & 0 deletions src/Campaigns/Routes/RegisterCampaignRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __invoke()
$this->registerGetCampaign();
$this->registerUpdateCampaign();
$this->registerGetCampaigns();
$this->registerMergeCampaigns();
$this->registerCreateCampaign();
}

Expand Down Expand Up @@ -130,6 +131,44 @@ public function registerUpdateCampaign()
}


/**
* Update Campaign route
*
* @unreleased
*/
public function registerMergeCampaigns()
{
register_rest_route(
CampaignRoute::NAMESPACE,
CampaignRoute::CAMPAIGN . '/merge',
[
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => function (WP_REST_Request $request) {
return $this->campaignRequestController->mergeCampaigns($request);
},
'permission_callback' => function () {
return current_user_can('manage_options');
},
],
'args' => [
'id' => [
'type' => 'integer',
'required' => true,
],
'campaignsToMergeIds' => [
'type' => 'array',
'required' => true,
'items' => [
'type' => 'integer',
],
],
],
]
);
}


/**
* Create Campaign route
*
Expand Down
1 change: 0 additions & 1 deletion src/Campaigns/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private function registerRoutes()
Hooks::addAction('rest_api_init', Routes\GetCampaignsListTable::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\DeleteCampaignListTable::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\GetCampaignStatistics::class, 'registerRoute');
Hooks::addAction('rest_api_init', Routes\MergeCampaigns::class, 'registerRoute');
}

/**
Expand Down
Loading