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

Create campaign tables #7519

Merged
merged 11 commits into from
Aug 27, 2024
64 changes: 64 additions & 0 deletions src/Campaigns/Migrations/Tables/CreateCampaignFormsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Give\Campaigns\Migrations\Tables;

use Give\Framework\Database\DB;
use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
* Creates give_campaign_forms table
*/
class CreateCampaignFormsTable extends Migration
{
/**
* @inheritdoc
*/
public static function id(): string
{
return 'give-campaigns-create-give-campaign-forms-table';
}

/**
* @inheritdoc
*/
public static function title(): string
{
return 'Create give_campaign_forms table';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-08-26 00:00:01');
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run(): void
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaign_forms';
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
campaign_id INT UNSIGNED NOT NULL,
form_id INT UNSIGNED NOT NULL,
PRIMARY KEY (campaign_id),
KEY form_id (form_id)
) $charset";

try {
DB::delta($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException("An error occurred while creating the $table table", 0, $exception);
}
}
}
77 changes: 77 additions & 0 deletions src/Campaigns/Migrations/Tables/CreateCampaignsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Give\Campaigns\Migrations\Tables;

use Give\Framework\Database\DB;
use Give\Framework\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;

/**
* @unreleased
* Creates give_campaigns table
*/
class CreateCampaignsTable extends Migration
{
/**
* @inheritdoc
*/
public static function id(): string
{
return 'give-campaigns-create-give-core-campaigns-table';
}

/**
* @inheritdoc
*/
public static function title(): string
{
return 'Create give_campaigns table from core';
}

/**
* @inheritdoc
*/
public static function timestamp(): string
{
return strtotime('2024-08-26 00:00:00');
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @inheritDoc
* @throws DatabaseMigrationException
*/
public function run()
{
global $wpdb;

$table = $wpdb->prefix . 'give_campaigns';
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
$charset = DB::get_charset_collate();

$sql = "CREATE TABLE $table (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
form_id INT NOT NULL,
campaign_type VARCHAR(12) NOT NULL DEFAULT '',
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
campaign_title TEXT NOT NULL,
campaign_url TEXT NOT NULL,
short_desc TEXT NOT NULL,
long_desc TEXT NOT NULL,
campaign_logo TEXT NOT NULL,
campaign_image TEXT NOT NULL,
primary_color VARCHAR(7) NOT NULL,
secondary_color VARCHAR(7) NOT NULL,
campaign_goal INT UNSIGNED NOT NULL,
status VARCHAR(12) NOT NULL,
start_date DATETIME NULL,
end_date DATETIME NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset";

try {
DB::delta($sql);
} catch (DatabaseQueryException $exception) {
throw new DatabaseMigrationException("An error occurred while creating the $table table", 0, $exception);
}
}
}
32 changes: 31 additions & 1 deletion src/Campaigns/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Give\Campaigns;

use Give\Campaigns\Migrations\Tables\CreateCampaignFormsTable;
use Give\Campaigns\Migrations\Tables\CreateCampaignsTable;
use Give\Framework\Migrations\MigrationsRegister;
use Give\Helpers\Hooks;
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface;

Expand All @@ -16,7 +19,7 @@ class ServiceProvider implements ServiceProviderInterface
*/
public function register(): void
{
//
$this->registerTableNames();
}

/**
Expand All @@ -28,9 +31,36 @@ public function boot(): void
// Hooks::addAction('init', Actions\MyAction::class);
// Hooks::addAction('rest_api_init', Controllers\MyEndpoint::class);

$this->registerMigrations();
$this->registerMenus();
}


/**
* @unreleased
*/
private function registerMigrations(): void
{
give(MigrationsRegister::class)->addMigrations(
[
CreateCampaignsTable::class,
CreateCampaignFormsTable::class,
]
);
}


/**
* @unreleased
*/
private function registerTableNames(): void
{
global $wpdb;

$wpdb->give_campaigns = $wpdb->prefix . 'give_campaigns';
$wpdb->give_campaign_forms = $wpdb->prefix . 'give_campaign_forms';
}

/**
* @unreleased
*/
Expand Down
Loading