class AddToCampaignHandler (View source)

Class AddToCampaignHandler - handle the AddToCampaign action.

This is a class designed to be delegated to by a Form action handler method in the EditForm of a LeftAndMain child class.

Add To Campaign can be seen as an item action like "publish" or "rollback", but unlike those actions it needs one additional piece of information to execute, the ChangeSet ID.

So this handler does one of two things to respond to the action request, depending on whether the ChangeSet ID was included in the submitted data

  • If it was, perform the Add To Campaign action (as per any other action)
  • If it wasn't, return a form to get the ChangeSet ID and then repeat this action submission

To use, you'd add an action to your LeftAndMain subclass, like this:

function addtocampaign($data, $form) {
    $handler = AddToCampaignHandler::create($form, $data);
    return $handler->addToCampaign();
}

and add an AddToCampaignHandler_FormAction to the EditForm, possibly through getCMSActions

Traits

A class that can be instantiated or replaced via DI

Properties

protected Controller $controller

Parent controller for this form

protected array $data

The submitted form data

protected string $name

Form name to use

Methods

public static 
create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

public static 
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

public
__construct(Controller $controller = null, array|DataObject $data = [], string $name = 'AddToCampaignForm')

AddToCampaignHandler constructor.

protected
ChangeSet>
getAvailableChangeSets()

Get what ChangeSets are available for an item to be added to by this user

protected
ChangeSet>
getInChangeSets($object)

Get changesets that a given object is already in

protected
getObject(int $id, string $class)

Safely get a DataObject from a client-supplied ID and ClassName, checking: argument validity; existence; and canView permissions.

public
Form(DataObject $object)

Builds a Form that mirrors the parent editForm, but with an extra field to collect the ChangeSet ID

public
addToCampaign(DataObject $object, array|int $data)

Performs the actual action of adding the object to the ChangeSet, once the ChangeSet ID is known

protected
string
getFormAlert(ArrayList $inChangeSets, ArrayList $candidateChangeSets, bool $canCreate)

Get descriptive alert to display at the top of the form

protected
int
getOrCreateCampaign(array|int $data)

Find or build campaign from posted data

protected
validationResult(string $message, string $field = null)

Raise validation error

Details

static Injectable create(mixed ...$args)

An implementation of the factory method, allows you to create an instance of a class

This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.

This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();

Parameters

mixed ...$args

Return Value

Injectable

static Injectable singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

__construct(Controller $controller = null, array|DataObject $data = [], string $name = 'AddToCampaignForm')

AddToCampaignHandler constructor.

Parameters

Controller $controller

Controller for this form

array|DataObject $data

The data submitted as part of that form

string $name

Form name

protected ChangeSet> getAvailableChangeSets()

Get what ChangeSets are available for an item to be added to by this user

Return Value

ChangeSet>

protected ChangeSet> getInChangeSets($object)

Get changesets that a given object is already in

Parameters

$object

Return Value

ChangeSet>

protected DataObject getObject(int $id, string $class)

Safely get a DataObject from a client-supplied ID and ClassName, checking: argument validity; existence; and canView permissions.

Parameters

int $id

The ID of the DataObject

string $class

The Class of the DataObject

Return Value

DataObject

The referenced DataObject

Exceptions

HTTPResponse_Exception

Form Form(DataObject $object)

Builds a Form that mirrors the parent editForm, but with an extra field to collect the ChangeSet ID

Parameters

DataObject $object

The object we're going to be adding to whichever ChangeSet is chosen

Return Value

Form

HTTPResponse addToCampaign(DataObject $object, array|int $data)

Performs the actual action of adding the object to the ChangeSet, once the ChangeSet ID is known

Parameters

DataObject $object

The object to add to the ChangeSet

array|int $data

Post data for this campaign form, or the ID of the campaign to add to

Return Value

HTTPResponse

Exceptions

ValidationException

protected string getFormAlert(ArrayList $inChangeSets, ArrayList $candidateChangeSets, bool $canCreate)

Get descriptive alert to display at the top of the form

Parameters

ArrayList $inChangeSets

List of changesets this item exists in

ArrayList $candidateChangeSets

List of changesets this item could be added to

bool $canCreate

Return Value

string

protected int getOrCreateCampaign(array|int $data)

Find or build campaign from posted data

Parameters

array|int $data

Return Value

int

Exceptions

ValidationException

protected ValidationException validationResult(string $message, string $field = null)

Raise validation error

Parameters

string $message
string $field

Return Value

ValidationException