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

Feature: add "Make as default" action to the forms list view #7606

Merged
4 changes: 3 additions & 1 deletion src/Campaigns/Actions/LoadCampaignDetailsAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

/**
* @unreleased
Expand Down Expand Up @@ -33,12 +34,13 @@ 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' => $defaultForm ? $defaultForm->settings->formTitle : null,
'defaultForm' => $defaultFormTitle,
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved
]
);

Expand Down
6 changes: 5 additions & 1 deletion src/Campaigns/Controllers/CampaignRequestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Give\Campaigns\Controllers;

use Exception;
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\Repositories\CampaignRepository;
use Give\Campaigns\ValueObjects\CampaignGoalType;
use Give\Campaigns\ValueObjects\CampaignRoute;
use Give\Campaigns\ValueObjects\CampaignStatus;
use Give\Campaigns\ValueObjects\CampaignType;
use Give\Framework\Exceptions\Primitives\Exception;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
Expand Down Expand Up @@ -124,6 +124,10 @@ public function updateCampaign(WP_REST_Request $request)
case 'goalType':
$campaign->goalType = new CampaignGoalType($value);
break;
case 'defaultFormId':
give(CampaignRepository::class)->updateDefaultCampaignForm($campaign,
$request->get_param('defaultFormId'));
break;
default:
if ($campaign->hasProperty($key)) {
$campaign->$key = $value;
Expand Down
27 changes: 25 additions & 2 deletions src/Campaigns/Models/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Framework\Models\Contracts\ModelCrud;
use Give\Framework\Models\Contracts\ModelHasFactory;
Expand Down Expand Up @@ -64,12 +65,22 @@ class Campaign extends Model implements ModelCrud, ModelHasFactory

/**
* @unreleased
*
* @return DonationForm | LegacyDonationForm
*/
public function defaultForm(): ?DonationForm
public function defaultForm()
{
return $this->forms()
$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;
}

/**
Expand All @@ -84,6 +95,18 @@ public function forms(): ModelQueryBuilder
})->where('campaign_forms.campaign_id', $this->id);
}

/**
* @unreleased
*/
public function legacyForms(): ModelQueryBuilder
glaubersilva marked this conversation as resolved.
Show resolved Hide resolved
{
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);
}

/**
* @unreleased
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Campaigns/Routes/RegisterCampaignRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public function getSchema(): array
],
'description' => esc_html__('Campaign goal type', 'give'),
],
'defaultFormId' => [
'type' => 'integer',
'description' => esc_html__('Default campaign form ID', 'give'),
],
],
'required' => ['id', 'title', 'goal', 'goalType'],
'allOf' => [
Expand Down
4 changes: 4 additions & 0 deletions src/DonationForms/V2/Endpoints/ListDonationForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\DonationForms\V2\Endpoints;

use Give\Campaigns\Models\Campaign;
use Give\DonationForms\V2\ListTable\DonationFormsListTable;
use Give\Framework\Database\DB;
use Give\Framework\QueryBuilder\JoinQueryBuilder;
Expand Down Expand Up @@ -135,12 +136,15 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response
$this->listTable->items($forms, $this->request->get_param('locale') ?? '');
$items = $this->listTable->getItems();

$defaultCampaignForm = ($campaignId = $this->request->get_param('campaignId')) ? Campaign::find($campaignId)->defaultForm() : false;

foreach ($items as $i => &$item) {
$item['name'] = get_the_title($item['id']);
$item['edit'] = get_edit_post_link($item['id'], 'edit');
$item['permalink'] = get_permalink($item['id']);
$item['v3form'] = (bool)give_get_meta($item['id'], 'formBuilderSettings');
$item['status_raw'] = $forms[$i]->status->getValue();
$item['isDefaultCampaignForm'] = $defaultCampaignForm && $item['id'] === $defaultCampaignForm->id;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {useContext} from 'react';
import {ShowConfirmModalContext} from '@givewp/components/ListTable/ListTablePage';
import {Interweave} from 'interweave';
import {OnboardingContext} from './Onboarding';
import {UpgradeModalContent} from "./Migration";
import {UpgradeModalContent} from './Migration';
import apiFetch from '@wordpress/api-fetch';
import {addQueryArgs} from '@wordpress/url';

const donationFormsApi = new ListTableApi(window.GiveDonationForms);

Expand Down Expand Up @@ -51,11 +53,35 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat
};

const confirmUpgradeModal = (event) => {
showConfirmModal(__('Upgrade', 'give'), UpgradeModalContent, async (selected) => {
const response = await donationFormsApi.fetchWithArgs('/migrate/' + item.id, {}, 'POST');
await mutate(parameters);
return response;
});
};

const urlParams = new URLSearchParams(window.location.search);
const isCampaignDetailsPage =
urlParams.get('id') && urlParams.get('page') && 'give-campaigns' === urlParams.get('page');
const campaignId = urlParams.get('id');

const confirmDefaultCampaignFormModal = (event) => {
showConfirmModal(
__('Upgrade', 'give'),
UpgradeModalContent,
async (selected) => {
const response = await donationFormsApi.fetchWithArgs("/migrate/" + item.id, {}, 'POST');
__('Make as default', 'give'),
(selected) => (
<p>
{__('Really make the following campaign form default?', 'give')}
<br />
<Interweave content={item?.title} />
</p>
),
async () => {
const response = await apiFetch({
path: addQueryArgs('/give-api/v2/campaigns/' + campaignId, {
defaultFormId: item.id,
}),
method: 'PATCH',
});
await mutate(parameters);
return response;
}
Expand Down Expand Up @@ -99,12 +125,22 @@ export function DonationFormsRowActions({data, item, removeRow, addRow, setUpdat
displayText={__('Duplicate', 'give')}
hiddenText={item?.name}
/>
{!item.v3form && (<RowAction
onClick={confirmUpgradeModal}
actionId={item.id}
displayText={__('Upgrade', 'give')}
hiddenText={item?.name}
/>)}
{!item.v3form && (
<RowAction
onClick={confirmUpgradeModal}
actionId={item.id}
displayText={__('Upgrade', 'give')}
hiddenText={item?.name}
/>
)}
{isCampaignDetailsPage && !item.isDefaultCampaignForm && (
<RowAction
onClick={confirmDefaultCampaignFormModal}
actionId={item.id}
displayText={__('Make as default', 'give')}
hiddenText={item?.name}
/>
)}
</>
)}
</>
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/DonationForms/Endpoints/TestListDonationForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function getMockRequest(): WP_REST_Request
}

/**
* @unreleased Add support to isDefaultCampaignForm key
* @since 2.25.0
*
* @param array $donationForms
Expand All @@ -115,6 +116,8 @@ public function getMockColumns(array $donationForms, string $sortDirection = 'de
$expectedItem['v3form'] = false;
$expectedItem['status_raw'] = $donationForm->status->getValue();

$expectedItem['isDefaultCampaignForm'] = false;

$expectedItems[] = $expectedItem;
}

Expand Down
Loading