2016-03-16 01:30:39 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-11 01:14:02 +02:00
|
|
|
namespace SilverStripe\Admin;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\ChangeSet;
|
|
|
|
use SilverStripe\ORM\Versioning\ChangeSetItem;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-09-23 01:05:33 +02:00
|
|
|
use SilverStripe\ORM\UnexpectedDataException;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\SecurityToken;
|
|
|
|
use SilverStripe\Security\PermissionProvider;
|
2016-08-11 01:14:02 +02:00
|
|
|
use LogicException;
|
|
|
|
|
2016-03-16 01:30:39 +01:00
|
|
|
/**
|
|
|
|
* Campaign section of the CMS
|
|
|
|
*/
|
|
|
|
class CampaignAdmin extends LeftAndMain implements PermissionProvider {
|
|
|
|
|
|
|
|
private static $allowed_actions = [
|
2016-03-29 06:35:10 +02:00
|
|
|
'set',
|
|
|
|
'sets',
|
2016-03-29 04:38:48 +02:00
|
|
|
'schema',
|
|
|
|
'DetailEditForm',
|
|
|
|
'readCampaigns',
|
2016-03-16 01:30:39 +01:00
|
|
|
'readCampaign',
|
|
|
|
'deleteCampaign',
|
2016-04-11 23:15:04 +02:00
|
|
|
'publishCampaign',
|
2016-03-16 01:30:39 +01:00
|
|
|
];
|
|
|
|
|
2016-05-11 05:24:41 +02:00
|
|
|
private static $menu_priority = 3;
|
2016-03-16 01:30:39 +01:00
|
|
|
|
|
|
|
private static $menu_title = 'Campaigns';
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
private static $tree_class = 'SilverStripe\\ORM\\Versioning\\ChangeSet';
|
2016-04-19 04:35:31 +02:00
|
|
|
|
2016-03-16 01:30:39 +01:00
|
|
|
private static $url_handlers = [
|
2016-03-29 06:35:10 +02:00
|
|
|
'GET sets' => 'readCampaigns',
|
2016-04-17 22:26:24 +02:00
|
|
|
'POST set/$ID/publish' => 'publishCampaign',
|
2016-04-12 00:24:16 +02:00
|
|
|
'GET set/$ID/$Name' => 'readCampaign',
|
2016-03-29 06:35:10 +02:00
|
|
|
'DELETE set/$ID' => 'deleteCampaign',
|
2016-03-16 01:30:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $url_segment = 'campaigns';
|
|
|
|
|
2016-04-12 00:24:16 +02:00
|
|
|
/**
|
|
|
|
* Size of thumbnail width
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $thumbnail_width = 64;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Size of thumbnail height
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $thumbnail_height = 64;
|
|
|
|
|
2016-08-11 01:40:23 +02:00
|
|
|
private static $required_permission_codes = 'CMS_ACCESS_CampaignAdmin';
|
|
|
|
|
2016-03-22 04:27:44 +01:00
|
|
|
public function getClientConfig() {
|
2016-03-23 11:16:03 +01:00
|
|
|
return array_merge(parent::getClientConfig(), [
|
2016-07-14 07:51:01 +02:00
|
|
|
'reactRouter' => true,
|
2016-04-26 02:01:40 +02:00
|
|
|
'form' => [
|
2016-03-23 11:16:03 +01:00
|
|
|
// TODO Use schemaUrl instead
|
2016-04-12 06:47:24 +02:00
|
|
|
'EditForm' => [
|
2016-03-23 11:16:03 +01:00
|
|
|
'schemaUrl' => $this->Link('schema/EditForm')
|
2016-04-12 06:47:24 +02:00
|
|
|
],
|
|
|
|
'DetailEditForm' => [
|
|
|
|
'schemaUrl' => $this->Link('schema/DetailEditForm')
|
2016-04-20 02:19:29 +02:00
|
|
|
],
|
2016-04-12 00:24:16 +02:00
|
|
|
],
|
2016-07-14 07:51:01 +02:00
|
|
|
'itemListViewEndpoint' => [
|
|
|
|
'url' => $this->Link() . 'set/:id/show',
|
|
|
|
'method' => 'get'
|
|
|
|
],
|
2016-04-11 23:15:04 +02:00
|
|
|
'publishEndpoint' => [
|
2016-04-19 01:55:45 +02:00
|
|
|
'url' => $this->Link() . 'set/:id/publish',
|
2016-04-17 22:26:24 +02:00
|
|
|
'method' => 'post'
|
2016-06-22 04:25:29 +02:00
|
|
|
],
|
|
|
|
'treeClass' => $this->config()->tree_class
|
2016-03-23 11:16:03 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function schema($request) {
|
2016-03-23 00:12:48 +01:00
|
|
|
// TODO Hardcoding schema until we can get GridField to generate a schema dynamically
|
2016-06-22 04:25:29 +02:00
|
|
|
$treeClassJS = Convert::raw2js($this->config()->tree_class);
|
2016-07-19 11:58:35 +02:00
|
|
|
$adminURL = Convert::raw2js(AdminRootController::admin_url());
|
2016-03-23 00:12:48 +01:00
|
|
|
$json = <<<JSON
|
|
|
|
{
|
2016-10-09 23:46:19 +02:00
|
|
|
"id": "{$adminURL}campaigns\/schema\/EditForm",
|
2016-03-23 00:12:48 +01:00
|
|
|
"schema": {
|
|
|
|
"name": "EditForm",
|
2016-04-21 04:38:02 +02:00
|
|
|
"id": "Form_EditForm",
|
2016-03-23 00:12:48 +01:00
|
|
|
"action": "schema",
|
|
|
|
"method": "GET",
|
|
|
|
"attributes": {
|
|
|
|
"id": "Form_EditForm",
|
2016-07-19 11:58:35 +02:00
|
|
|
"action": "{$adminURL}campaigns\/EditForm",
|
2016-03-23 00:12:48 +01:00
|
|
|
"method": "POST",
|
|
|
|
"enctype": "multipart\/form-data",
|
2016-04-11 08:43:46 +02:00
|
|
|
"target": null
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
|
|
|
"data": [],
|
|
|
|
"fields": [{
|
|
|
|
"name": "ID",
|
|
|
|
"id": "Form_EditForm_ID",
|
|
|
|
"type": "Hidden",
|
|
|
|
"component": null,
|
2016-09-29 22:10:02 +02:00
|
|
|
"holderId": null,
|
2016-03-23 00:12:48 +01:00
|
|
|
"title": false,
|
|
|
|
"source": null,
|
2016-07-25 07:24:26 +02:00
|
|
|
"extraClass": "hidden form-group--no-label",
|
2016-03-23 00:12:48 +01:00
|
|
|
"description": null,
|
|
|
|
"rightTitle": null,
|
|
|
|
"leftTitle": null,
|
|
|
|
"readOnly": false,
|
|
|
|
"disabled": false,
|
|
|
|
"customValidationMessage": "",
|
|
|
|
"attributes": [],
|
|
|
|
"data": []
|
|
|
|
}, {
|
|
|
|
"name": "ChangeSets",
|
|
|
|
"id": "Form_EditForm_ChangeSets",
|
|
|
|
"type": "Custom",
|
|
|
|
"component": "GridField",
|
2016-09-29 22:10:02 +02:00
|
|
|
"holderId": null,
|
2016-03-23 00:12:48 +01:00
|
|
|
"title": "Campaigns",
|
|
|
|
"source": null,
|
|
|
|
"extraClass": null,
|
|
|
|
"description": null,
|
|
|
|
"rightTitle": null,
|
|
|
|
"leftTitle": null,
|
|
|
|
"readOnly": false,
|
|
|
|
"disabled": false,
|
|
|
|
"customValidationMessage": "",
|
|
|
|
"attributes": [],
|
|
|
|
"data": {
|
2016-06-22 04:25:29 +02:00
|
|
|
"recordType": "{$treeClassJS}",
|
2016-03-29 04:38:48 +02:00
|
|
|
"collectionReadEndpoint": {
|
2016-07-19 11:58:35 +02:00
|
|
|
"url": "{$adminURL}campaigns\/sets",
|
2016-03-22 00:25:23 +01:00
|
|
|
"method": "GET"
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
2016-03-29 04:38:48 +02:00
|
|
|
"itemReadEndpoint": {
|
2016-07-19 11:58:35 +02:00
|
|
|
"url": "{$adminURL}campaigns\/set\/:id",
|
2016-03-22 00:25:23 +01:00
|
|
|
"method": "GET"
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
2016-03-29 04:38:48 +02:00
|
|
|
"itemUpdateEndpoint": {
|
2016-07-19 11:58:35 +02:00
|
|
|
"url": "{$adminURL}campaigns\/set\/:id",
|
2016-03-22 00:25:23 +01:00
|
|
|
"method": "PUT"
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
2016-03-29 04:38:48 +02:00
|
|
|
"itemCreateEndpoint": {
|
2016-07-19 11:58:35 +02:00
|
|
|
"url": "{$adminURL}campaigns\/set\/:id",
|
2016-03-22 00:25:23 +01:00
|
|
|
"method": "POST"
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
2016-03-29 04:38:48 +02:00
|
|
|
"itemDeleteEndpoint": {
|
2016-07-19 11:58:35 +02:00
|
|
|
"url": "{$adminURL}campaigns\/set\/:id",
|
2016-03-22 00:25:23 +01:00
|
|
|
"method": "DELETE"
|
2016-03-23 00:12:48 +01:00
|
|
|
},
|
2016-07-19 11:58:35 +02:00
|
|
|
"editFormSchemaEndpoint": "{$adminURL}campaigns\/schema\/DetailEditForm",
|
2016-03-29 04:38:48 +02:00
|
|
|
"columns": [
|
|
|
|
{"name": "Title", "field": "Name"},
|
2016-06-22 04:25:29 +02:00
|
|
|
{"name": "Changes", "field": "ChangesCount"},
|
2016-03-29 04:38:48 +02:00
|
|
|
{"name": "Description", "field": "Description"}
|
|
|
|
]
|
2016-03-23 00:12:48 +01:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"name": "SecurityID",
|
|
|
|
"id": "Form_EditForm_SecurityID",
|
|
|
|
"type": "Hidden",
|
|
|
|
"component": null,
|
2016-09-29 22:10:02 +02:00
|
|
|
"holderId": null,
|
2016-03-23 00:12:48 +01:00
|
|
|
"title": "Security ID",
|
|
|
|
"source": null,
|
|
|
|
"extraClass": "hidden",
|
|
|
|
"description": null,
|
|
|
|
"rightTitle": null,
|
|
|
|
"leftTitle": null,
|
|
|
|
"readOnly": false,
|
|
|
|
"disabled": false,
|
|
|
|
"customValidationMessage": "",
|
|
|
|
"attributes": [],
|
|
|
|
"data": []
|
|
|
|
}],
|
|
|
|
"actions": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSON;
|
|
|
|
|
2016-03-23 11:16:03 +01:00
|
|
|
$formName = $request->param('ID');
|
|
|
|
if($formName == 'EditForm') {
|
|
|
|
$response = $this->getResponse();
|
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
|
|
|
$response->setBody($json);
|
|
|
|
return $response;
|
|
|
|
} else {
|
|
|
|
return parent::schema($request);
|
|
|
|
}
|
2016-03-23 00:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST endpoint to get a list of campaigns.
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse
|
2016-03-23 00:12:48 +01:00
|
|
|
*/
|
2016-06-22 04:25:29 +02:00
|
|
|
public function readCampaigns() {
|
2016-09-09 08:43:05 +02:00
|
|
|
$response = new HTTPResponse();
|
2016-03-23 00:12:48 +01:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2016-03-29 06:35:10 +02:00
|
|
|
$hal = $this->getListResource();
|
|
|
|
$response->setBody(Convert::array2json($hal));
|
|
|
|
return $response;
|
2016-04-12 00:24:16 +02:00
|
|
|
}
|
2016-03-29 06:35:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list contained as a hal wrapper
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getListResource() {
|
|
|
|
$items = $this->getListItems();
|
|
|
|
$count = $items->count();
|
2016-06-22 04:25:29 +02:00
|
|
|
/** @var string $treeClass */
|
|
|
|
$treeClass = $this->config()->tree_class;
|
2016-03-29 06:35:10 +02:00
|
|
|
$hal = [
|
|
|
|
'count' => $count,
|
|
|
|
'total' => $count,
|
|
|
|
'_links' => [
|
|
|
|
'self' => [
|
|
|
|
'href' => $this->Link('items')
|
|
|
|
]
|
|
|
|
],
|
2016-06-22 04:25:29 +02:00
|
|
|
'_embedded' => [$treeClass => []]
|
2016-03-29 06:35:10 +02:00
|
|
|
];
|
|
|
|
foreach($items as $item) {
|
|
|
|
/** @var ChangeSet $item */
|
|
|
|
$resource = $this->getChangeSetResource($item);
|
2016-06-22 04:25:29 +02:00
|
|
|
$hal['_embedded'][$treeClass][] = $resource;
|
2016-03-29 06:35:10 +02:00
|
|
|
}
|
|
|
|
return $hal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build item resource from a changeset
|
|
|
|
*
|
|
|
|
* @param ChangeSet $changeSet
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getChangeSetResource(ChangeSet $changeSet) {
|
|
|
|
$hal = [
|
|
|
|
'_links' => [
|
|
|
|
'self' => [
|
|
|
|
'href' => $this->SetLink($changeSet->ID)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'ID' => $changeSet->ID,
|
|
|
|
'Name' => $changeSet->Name,
|
|
|
|
'Created' => $changeSet->Created,
|
|
|
|
'LastEdited' => $changeSet->LastEdited,
|
|
|
|
'State' => $changeSet->State,
|
2016-10-12 01:19:07 +02:00
|
|
|
'IsInferred' => $changeSet->IsInferred,
|
2016-04-11 23:15:04 +02:00
|
|
|
'canEdit' => $changeSet->canEdit(),
|
2016-09-23 01:05:33 +02:00
|
|
|
'canPublish' => false,
|
2016-06-22 04:25:29 +02:00
|
|
|
'_embedded' => ['items' => []]
|
2016-03-29 06:35:10 +02:00
|
|
|
];
|
2016-09-23 01:05:33 +02:00
|
|
|
|
|
|
|
// Before presenting the changeset to the client,
|
|
|
|
// synchronise it with new changes.
|
|
|
|
try {
|
|
|
|
$changeSet->sync();
|
|
|
|
$hal['Description'] = $changeSet->getDescription();
|
|
|
|
$hal['canPublish'] = $changeSet->canPublish();
|
|
|
|
|
|
|
|
foreach($changeSet->Changes() as $changeSetItem) {
|
|
|
|
if(!$changeSetItem) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var ChangesetItem $changeSetItem */
|
|
|
|
$resource = $this->getChangeSetItemResource($changeSetItem);
|
|
|
|
$hal['_embedded']['items'][] = $resource;
|
2016-04-07 07:14:12 +02:00
|
|
|
}
|
2016-09-23 01:05:33 +02:00
|
|
|
$hal['ChangesCount'] = count($hal['_embedded']['items']);
|
2016-04-07 07:14:12 +02:00
|
|
|
|
2016-09-23 01:05:33 +02:00
|
|
|
// An unexpected data exception means that the database is corrupt
|
|
|
|
} catch(UnexpectedDataException $e) {
|
|
|
|
$hal['Description'] = 'Corrupt database! ' . $e->getMessage();
|
|
|
|
$hal['ChangesCount'] = '-';
|
2016-03-29 06:35:10 +02:00
|
|
|
}
|
|
|
|
return $hal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build item resource from a changesetitem
|
|
|
|
*
|
|
|
|
* @param ChangeSetItem $changeSetItem
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getChangeSetItemResource(ChangeSetItem $changeSetItem) {
|
2016-05-25 07:09:29 +02:00
|
|
|
$baseClass = DataObject::getSchema()->baseDataClass($changeSetItem->ObjectClass);
|
2016-04-12 00:24:16 +02:00
|
|
|
$baseSingleton = DataObject::singleton($baseClass);
|
|
|
|
$thumbnailWidth = (int)$this->config()->thumbnail_width;
|
|
|
|
$thumbnailHeight = (int)$this->config()->thumbnail_height;
|
2016-03-29 06:35:10 +02:00
|
|
|
$hal = [
|
|
|
|
'_links' => [
|
|
|
|
'self' => [
|
|
|
|
'href' => $this->ItemLink($changeSetItem->ID)
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'ID' => $changeSetItem->ID,
|
|
|
|
'Created' => $changeSetItem->Created,
|
|
|
|
'LastEdited' => $changeSetItem->LastEdited,
|
|
|
|
'Title' => $changeSetItem->getTitle(),
|
|
|
|
'ChangeType' => $changeSetItem->getChangeType(),
|
|
|
|
'Added' => $changeSetItem->Added,
|
2016-04-07 02:48:40 +02:00
|
|
|
'ObjectClass' => $changeSetItem->ObjectClass,
|
|
|
|
'ObjectID' => $changeSetItem->ObjectID,
|
2016-04-12 00:24:16 +02:00
|
|
|
'BaseClass' => $baseClass,
|
|
|
|
'Singular' => $baseSingleton->i18n_singular_name(),
|
|
|
|
'Plural' => $baseSingleton->i18n_plural_name(),
|
|
|
|
'Thumbnail' => $changeSetItem->ThumbnailURL($thumbnailWidth, $thumbnailHeight),
|
2016-03-29 06:35:10 +02:00
|
|
|
];
|
2016-04-27 04:26:39 +02:00
|
|
|
// Get preview urls
|
|
|
|
$previews = $changeSetItem->getPreviewLinks();
|
2016-04-28 01:41:40 +02:00
|
|
|
if($previews) {
|
|
|
|
$hal['_links']['preview'] = $previews;
|
2016-04-27 04:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get edit link
|
|
|
|
$editLink = $changeSetItem->CMSEditLink();
|
|
|
|
if($editLink) {
|
|
|
|
$hal['_links']['edit'] = [
|
|
|
|
'href' => $editLink,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-29 06:35:10 +02:00
|
|
|
// Depending on whether the object was added implicitly or explicitly, set
|
|
|
|
// other related objects.
|
|
|
|
if($changeSetItem->Added === ChangeSetItem::IMPLICITLY) {
|
|
|
|
$referencedItems = $changeSetItem->ReferencedBy();
|
|
|
|
$referencedBy = [];
|
|
|
|
foreach($referencedItems as $referencedItem) {
|
|
|
|
$referencedBy[] = [
|
|
|
|
'href' => $this->SetLink($referencedItem->ID)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if($referencedBy) {
|
|
|
|
$hal['_links']['referenced_by'] = $referencedBy;
|
2016-03-29 04:38:48 +02:00
|
|
|
}
|
2016-03-29 06:35:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $hal;
|
2016-03-29 04:38:48 +02:00
|
|
|
}
|
2016-03-23 00:12:48 +01:00
|
|
|
|
2016-03-29 06:35:10 +02:00
|
|
|
/**
|
|
|
|
* Gets viewable list of campaigns
|
|
|
|
*
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
protected function getListItems() {
|
|
|
|
return ChangeSet::get()
|
2016-04-07 07:14:12 +02:00
|
|
|
->filter('State', ChangeSet::STATE_OPEN)
|
|
|
|
->filterByCallback(function($item) {
|
2016-06-22 04:25:29 +02:00
|
|
|
/** @var ChangeSet $item */
|
2016-04-07 07:14:12 +02:00
|
|
|
return ($item->canView());
|
|
|
|
});
|
2016-03-23 00:12:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:35:10 +02:00
|
|
|
|
2016-03-16 01:30:39 +01:00
|
|
|
/**
|
|
|
|
* REST endpoint to get a campaign.
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-03-16 01:30:39 +01:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse
|
2016-03-16 01:30:39 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function readCampaign(HTTPRequest $request) {
|
|
|
|
$response = new HTTPResponse();
|
2016-04-12 00:24:16 +02:00
|
|
|
|
|
|
|
if ($request->getHeader('Accept') == 'text/json') {
|
2016-04-11 23:15:04 +02:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2016-04-26 00:34:51 +02:00
|
|
|
if (!$request->param('Name')) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 400));
|
2016-04-12 00:24:16 +02:00
|
|
|
}
|
2016-04-07 07:13:54 +02:00
|
|
|
|
2016-06-22 04:25:29 +02:00
|
|
|
/** @var ChangeSet $changeSet */
|
|
|
|
$changeSet = ChangeSet::get()->byID($request->param('ID'));
|
2016-04-26 00:34:51 +02:00
|
|
|
if(!$changeSet) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 404));
|
2016-04-26 00:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!$changeSet->canView()) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 403));
|
2016-04-26 00:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$body = Convert::raw2json($this->getChangeSetResource($changeSet));
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse($body, 200))
|
2016-04-26 00:34:51 +02:00
|
|
|
->addHeader('Content-Type', 'application/json');
|
2016-04-12 00:24:16 +02:00
|
|
|
} else {
|
|
|
|
return $this->index($request);
|
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REST endpoint to delete a campaign.
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-03-16 01:30:39 +01:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse
|
2016-03-16 01:30:39 +01:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function deleteCampaign(HTTPRequest $request) {
|
2016-05-09 06:00:43 +02:00
|
|
|
// Check security ID
|
|
|
|
if (!SecurityToken::inst()->checkRequest($request)) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return new HTTPResponse(null, 400);
|
2016-05-09 06:00:43 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 05:38:48 +02:00
|
|
|
$id = $request->param('ID');
|
|
|
|
if (!$id || !is_numeric($id)) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 400));
|
2016-04-11 23:15:04 +02:00
|
|
|
}
|
2016-03-30 05:38:48 +02:00
|
|
|
|
|
|
|
$record = ChangeSet::get()->byID($id);
|
|
|
|
if(!$record) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 404));
|
2016-03-30 05:38:48 +02:00
|
|
|
}
|
2016-03-16 01:30:39 +01:00
|
|
|
|
2016-03-30 05:38:48 +02:00
|
|
|
if(!$record->canDelete()) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 403));
|
2016-03-30 05:38:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$record->delete();
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 204));
|
2016-03-16 01:30:39 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 23:15:04 +02:00
|
|
|
/**
|
|
|
|
* REST endpoint to publish a {@link ChangeSet} and all of its items.
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-04-11 23:15:04 +02:00
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse
|
2016-04-11 23:15:04 +02:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function publishCampaign(HTTPRequest $request) {
|
2016-04-17 22:26:24 +02:00
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
if(!SecurityToken::inst()->checkRequest($request)) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 400));
|
2016-04-17 22:26:24 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 23:15:04 +02:00
|
|
|
$id = $request->param('ID');
|
|
|
|
if(!$id || !is_numeric($id)) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 400));
|
2016-04-11 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 04:25:29 +02:00
|
|
|
/** @var ChangeSet $record */
|
2016-04-11 23:15:04 +02:00
|
|
|
$record = ChangeSet::get()->byID($id);
|
|
|
|
if(!$record) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 404));
|
2016-04-11 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!$record->canPublish()) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(null, 403));
|
2016-04-11 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$record->publish();
|
|
|
|
} catch(LogicException $e) {
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(json_encode(['status' => 'error', 'message' => $e->getMessage()]), 401))
|
2016-04-11 23:15:04 +02:00
|
|
|
->addHeader('Content-Type', 'application/json');
|
|
|
|
}
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
return (new HTTPResponse(
|
2016-04-11 23:15:04 +02:00
|
|
|
Convert::raw2json($this->getChangeSetResource($record)),
|
|
|
|
200
|
|
|
|
))->addHeader('Content-Type', 'application/json');
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:35:31 +02:00
|
|
|
/**
|
|
|
|
* Url handler for edit form
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-04-19 04:35:31 +02:00
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function DetailEditForm($request) {
|
|
|
|
// Get ID either from posted back value, or url parameter
|
|
|
|
$id = $request->param('ID') ?: $request->postVar('ID');
|
|
|
|
return $this->getDetailEditForm($id);
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:12:48 +01:00
|
|
|
/**
|
|
|
|
* @todo Use GridFieldDetailForm once it can handle structured data and form schemas
|
|
|
|
*
|
2016-04-19 04:35:31 +02:00
|
|
|
* @param int $id
|
2016-03-23 00:12:48 +01:00
|
|
|
* @return Form
|
|
|
|
*/
|
2016-04-12 06:47:24 +02:00
|
|
|
public function getDetailEditForm($id) {
|
2016-04-19 04:35:31 +02:00
|
|
|
// Get record-specific fields
|
|
|
|
$record = null;
|
|
|
|
if($id) {
|
2016-06-22 04:25:29 +02:00
|
|
|
$record = ChangeSet::get()->byID($id);
|
2016-04-26 00:34:51 +02:00
|
|
|
if(!$record || !$record->canView()) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-04-19 04:35:31 +02:00
|
|
|
}
|
2016-04-26 00:34:51 +02:00
|
|
|
|
2016-04-19 04:35:31 +02:00
|
|
|
if(!$record) {
|
|
|
|
$record = ChangeSet::singleton();
|
|
|
|
}
|
2016-04-26 00:34:51 +02:00
|
|
|
|
2016-04-19 04:35:31 +02:00
|
|
|
$fields = $record->getCMSFields();
|
|
|
|
|
|
|
|
// Add standard fields
|
|
|
|
$fields->push(HiddenField::create('ID'));
|
|
|
|
$form = Form::create(
|
2016-03-23 00:12:48 +01:00
|
|
|
$this,
|
|
|
|
'DetailEditForm',
|
2016-04-19 04:35:31 +02:00
|
|
|
$fields,
|
2016-03-23 00:12:48 +01:00
|
|
|
FieldList::create(
|
2016-07-07 00:39:44 +02:00
|
|
|
FormAction::create('save', _t('CMSMain.SAVE', 'Save'))
|
|
|
|
->setIcon('save'),
|
2016-04-26 00:59:04 +02:00
|
|
|
FormAction::create('cancel', _t('LeftAndMain.CANCEL', 'Cancel'))
|
2016-07-07 00:39:44 +02:00
|
|
|
->setUseButtonTag(true)
|
2016-03-23 00:12:48 +01:00
|
|
|
)
|
|
|
|
);
|
2016-06-22 04:25:29 +02:00
|
|
|
|
|
|
|
// Load into form
|
|
|
|
if($id && $record) {
|
|
|
|
$form->loadDataFrom($record);
|
|
|
|
}
|
2016-10-28 05:13:29 +02:00
|
|
|
$form->getValidator()->addRequiredField('Name');
|
2016-04-19 04:35:31 +02:00
|
|
|
// Configure form to respond to validation errors with form schema
|
|
|
|
// if requested via react.
|
2016-10-09 23:46:19 +02:00
|
|
|
$form->setValidationResponseCallback(function() use ($form, $record) {
|
|
|
|
$schemaId = Controller::join_links($this->Link('schema/DetailEditForm'), $record->exists() ? $record->ID : '');
|
|
|
|
return $this->getSchemaResponse($form, $schemaId);
|
2016-04-19 04:35:31 +02:00
|
|
|
});
|
2016-04-26 00:34:51 +02:00
|
|
|
|
2016-04-19 04:35:31 +02:00
|
|
|
return $form;
|
2016-03-23 00:12:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:35:10 +02:00
|
|
|
/**
|
|
|
|
* Gets user-visible url to edit a specific {@see ChangeSet}
|
|
|
|
*
|
|
|
|
* @param $itemID
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function SetLink($itemID) {
|
|
|
|
return Controller::join_links(
|
|
|
|
$this->Link('set'),
|
|
|
|
$itemID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets user-visible url to edit a specific {@see ChangeSetItem}
|
|
|
|
*
|
|
|
|
* @param int $itemID
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function ItemLink($itemID) {
|
|
|
|
return Controller::join_links(
|
|
|
|
$this->Link('item'),
|
|
|
|
$itemID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-11 01:40:23 +02:00
|
|
|
public function providePermissions() {
|
|
|
|
return array(
|
|
|
|
"CMS_ACCESS_CampaignAdmin" => array(
|
|
|
|
'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => static::menu_title())),
|
|
|
|
'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'),
|
|
|
|
'help' => _t(
|
|
|
|
'CampaignAdmin.ACCESS_HELP',
|
|
|
|
'Allow viewing of the campaign publishing section.'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-16 01:30:39 +01:00
|
|
|
}
|