mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #6370 from open-sausages/pulls/4.0/remove-temp-json
ENHANCEMENT Move temporary JSON block into standard component
This commit is contained in:
commit
322d3e18fd
@ -92,123 +92,32 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function schema($request) {
|
public function getEditForm($id = null, $fields = null)
|
||||||
// TODO Hardcoding schema until we can get GridField to generate a schema dynamically
|
{
|
||||||
$treeClassJS = Convert::raw2js($this->config()->tree_class);
|
$fields = new FieldList(
|
||||||
$adminURL = Convert::raw2js(AdminRootController::admin_url());
|
CampaignAdminList::create('ChangeSets')
|
||||||
$json = <<<JSON
|
);
|
||||||
{
|
$actions = new FieldList();
|
||||||
"id": "{$adminURL}campaigns\/schema\/EditForm",
|
$form = Form::create($this, 'EditForm', $fields, $actions);
|
||||||
"schema": {
|
|
||||||
"name": "EditForm",
|
|
||||||
"id": "Form_EditForm",
|
|
||||||
"action": "schema",
|
|
||||||
"method": "GET",
|
|
||||||
"attributes": {
|
|
||||||
"id": "Form_EditForm",
|
|
||||||
"action": "{$adminURL}campaigns\/EditForm",
|
|
||||||
"method": "POST",
|
|
||||||
"enctype": "multipart\/form-data",
|
|
||||||
"target": null
|
|
||||||
},
|
|
||||||
"data": [],
|
|
||||||
"fields": [{
|
|
||||||
"name": "ID",
|
|
||||||
"id": "Form_EditForm_ID",
|
|
||||||
"type": "Hidden",
|
|
||||||
"component": null,
|
|
||||||
"holderId": null,
|
|
||||||
"title": false,
|
|
||||||
"source": null,
|
|
||||||
"extraClass": "hidden form-group--no-label",
|
|
||||||
"description": null,
|
|
||||||
"rightTitle": null,
|
|
||||||
"leftTitle": null,
|
|
||||||
"readOnly": false,
|
|
||||||
"disabled": false,
|
|
||||||
"customValidationMessage": "",
|
|
||||||
"attributes": [],
|
|
||||||
"data": []
|
|
||||||
}, {
|
|
||||||
"name": "ChangeSets",
|
|
||||||
"id": "Form_EditForm_ChangeSets",
|
|
||||||
"type": "Custom",
|
|
||||||
"component": "GridField",
|
|
||||||
"holderId": null,
|
|
||||||
"title": "Campaigns",
|
|
||||||
"source": null,
|
|
||||||
"extraClass": null,
|
|
||||||
"description": null,
|
|
||||||
"rightTitle": null,
|
|
||||||
"leftTitle": null,
|
|
||||||
"readOnly": false,
|
|
||||||
"disabled": false,
|
|
||||||
"customValidationMessage": "",
|
|
||||||
"attributes": [],
|
|
||||||
"data": {
|
|
||||||
"recordType": "{$treeClassJS}",
|
|
||||||
"collectionReadEndpoint": {
|
|
||||||
"url": "{$adminURL}campaigns\/sets",
|
|
||||||
"method": "GET"
|
|
||||||
},
|
|
||||||
"itemReadEndpoint": {
|
|
||||||
"url": "{$adminURL}campaigns\/set\/:id",
|
|
||||||
"method": "GET"
|
|
||||||
},
|
|
||||||
"itemUpdateEndpoint": {
|
|
||||||
"url": "{$adminURL}campaigns\/set\/:id",
|
|
||||||
"method": "PUT"
|
|
||||||
},
|
|
||||||
"itemCreateEndpoint": {
|
|
||||||
"url": "{$adminURL}campaigns\/set\/:id",
|
|
||||||
"method": "POST"
|
|
||||||
},
|
|
||||||
"itemDeleteEndpoint": {
|
|
||||||
"url": "{$adminURL}campaigns\/set\/:id",
|
|
||||||
"method": "DELETE"
|
|
||||||
},
|
|
||||||
"editFormSchemaEndpoint": "{$adminURL}campaigns\/schema\/DetailEditForm",
|
|
||||||
"columns": [
|
|
||||||
{"name": "Title", "field": "Name"},
|
|
||||||
{"name": "Changes", "field": "ChangesCount"},
|
|
||||||
{"name": "Description", "field": "Description"}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
"name": "SecurityID",
|
|
||||||
"id": "Form_EditForm_SecurityID",
|
|
||||||
"type": "Hidden",
|
|
||||||
"component": null,
|
|
||||||
"holderId": null,
|
|
||||||
"title": "Security ID",
|
|
||||||
"source": null,
|
|
||||||
"extraClass": "hidden",
|
|
||||||
"description": null,
|
|
||||||
"rightTitle": null,
|
|
||||||
"leftTitle": null,
|
|
||||||
"readOnly": false,
|
|
||||||
"disabled": false,
|
|
||||||
"customValidationMessage": "",
|
|
||||||
"attributes": [],
|
|
||||||
"data": []
|
|
||||||
}],
|
|
||||||
"actions": []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JSON;
|
|
||||||
|
|
||||||
$formName = $request->param('ID');
|
// Set callback response
|
||||||
if($formName == 'EditForm') {
|
$form->setValidationResponseCallback(function() use ($form) {
|
||||||
$response = $this->getResponse();
|
$schemaId = $this->Link('schema/EditForm');
|
||||||
$response->addHeader('Content-Type', 'application/json');
|
return $this->getSchemaResponse($form, $schemaId);
|
||||||
$response->setBody($json);
|
});
|
||||||
return $response;
|
|
||||||
} else {
|
|
||||||
return parent::schema($request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $form;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function EditForm($request = null)
|
||||||
|
{
|
||||||
|
// Get ID either from posted back value, or url parameter
|
||||||
|
$request = $request ?: $this->getRequest();
|
||||||
|
$id = $request->param('ID') ?: $request->postVar('ID');
|
||||||
|
return $this->getEditForm($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* REST endpoint to get a list of campaigns.
|
* REST endpoint to get a list of campaigns.
|
||||||
*
|
*
|
||||||
* @return HTTPResponse
|
* @return HTTPResponse
|
||||||
|
65
admin/code/CampaignAdminList.php
Normal file
65
admin/code/CampaignAdminList.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace SilverStripe\Admin;
|
||||||
|
|
||||||
|
use SilverStripe\Forms\FormField;
|
||||||
|
use SilverStripe\ORM\Versioning\ChangeSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Warning: Volatile API as placeholder for standard "GridField"
|
||||||
|
*/
|
||||||
|
class CampaignAdminList extends FormField
|
||||||
|
{
|
||||||
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
|
||||||
|
|
||||||
|
protected $schemaComponent = 'GridField';
|
||||||
|
|
||||||
|
public function getSchemaDataDefaults()
|
||||||
|
{
|
||||||
|
$data = parent::getSchemaDataDefaults();
|
||||||
|
|
||||||
|
// Get endpoints from admin
|
||||||
|
$admin = CampaignAdmin::singleton();
|
||||||
|
$data['data']['recordType'] = $admin->config()->get('tree_class');
|
||||||
|
$oneSetAction = $admin->Link("set") . "/:id";
|
||||||
|
$setsAction = $admin->Link("sets");
|
||||||
|
$schemaEndpoint = $admin->Link("schema") . "/DetailEditForm";
|
||||||
|
|
||||||
|
// Merge custom endpoints
|
||||||
|
$data['data']['collectionReadEndpoint'] = [
|
||||||
|
"url" => $setsAction,
|
||||||
|
"method" => "GET",
|
||||||
|
];
|
||||||
|
$data['data']['itemReadEndpoint'] = [
|
||||||
|
"url" => $oneSetAction,
|
||||||
|
"method" => "GET",
|
||||||
|
];
|
||||||
|
$data['data']['itemUpdateEndpoint'] = [
|
||||||
|
"url" => $oneSetAction,
|
||||||
|
"method" => "PUT"
|
||||||
|
];
|
||||||
|
$data['data']['itemCreateEndpoint'] = [
|
||||||
|
"url" => $oneSetAction,
|
||||||
|
"method" => "POST"
|
||||||
|
];
|
||||||
|
$data['data']["itemDeleteEndpoint"] = [
|
||||||
|
"url" => $oneSetAction,
|
||||||
|
"method" => "DELETE"
|
||||||
|
];
|
||||||
|
$data['data']['editFormSchemaEndpoint'] = $schemaEndpoint;
|
||||||
|
|
||||||
|
// Set summary columns
|
||||||
|
$columns = [];
|
||||||
|
foreach(ChangeSet::singleton()->summaryFields() as $field => $label) {
|
||||||
|
$columns[] = [
|
||||||
|
'field' => $field,
|
||||||
|
'name' => $label,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$data['data']['columns'] = $columns;
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
46
admin/tests/CampaignAdminListTest.php
Normal file
46
admin/tests/CampaignAdminListTest.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Admin\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Admin\CampaignAdminList;
|
||||||
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\Form;
|
||||||
|
|
||||||
|
class CampaignAdminListTest extends SapphireTest
|
||||||
|
{
|
||||||
|
public function testSchema() {
|
||||||
|
$fields = new FieldList(
|
||||||
|
$changesets = CampaignAdminList::create('ChangeSets')
|
||||||
|
);
|
||||||
|
$actions = new FieldList();
|
||||||
|
Form::create(new Controller(), 'EditForm', $fields, $actions);
|
||||||
|
|
||||||
|
$schema = $changesets->getSchemaData();
|
||||||
|
|
||||||
|
// Check endpoint urls
|
||||||
|
$this->assertEquals('admin/campaigns/sets', $schema['data']['collectionReadEndpoint']['url']);
|
||||||
|
$this->assertEquals('admin/campaigns/set/:id', $schema['data']['itemReadEndpoint']['url']);
|
||||||
|
$this->assertEquals('admin/campaigns/set/:id', $schema['data']['itemUpdateEndpoint']['url']);
|
||||||
|
$this->assertEquals('admin/campaigns/set/:id', $schema['data']['itemCreateEndpoint']['url']);
|
||||||
|
$this->assertEquals('admin/campaigns/set/:id', $schema['data']['itemDeleteEndpoint']['url']);
|
||||||
|
$this->assertEquals('admin/campaigns/schema/DetailEditForm', $schema['data']['editFormSchemaEndpoint']);
|
||||||
|
|
||||||
|
// Check summary fields
|
||||||
|
$this->assertEquals([
|
||||||
|
[
|
||||||
|
'field' => 'Name',
|
||||||
|
'name' => 'Title',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'field' => 'ChangesCount',
|
||||||
|
'name' => 'Changes',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'field' => 'Description',
|
||||||
|
'name' => 'Description',
|
||||||
|
]
|
||||||
|
], $schema['data']['columns']);
|
||||||
|
}
|
||||||
|
}
|
@ -79,6 +79,12 @@ class ChangeSet extends DataObject
|
|||||||
'SilverStripe\\Assets\\File',
|
'SilverStripe\\Assets\\File',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
private static $summary_fields = [
|
||||||
|
'Name' => 'Title',
|
||||||
|
'ChangesCount' => 'Changes',
|
||||||
|
'Description' => 'Description',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default permission to require for publishers.
|
* Default permission to require for publishers.
|
||||||
* Publishers must either be able to use the campaign admin, or have all admin access.
|
* Publishers must either be able to use the campaign admin, or have all admin access.
|
||||||
@ -535,6 +541,16 @@ class ChangeSet extends DataObject
|
|||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Required to support count display in react gridfield column
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getChangesCount()
|
||||||
|
{
|
||||||
|
return $this->Changes()->count();
|
||||||
|
}
|
||||||
|
|
||||||
public function fieldLabels($includerelations = true)
|
public function fieldLabels($includerelations = true)
|
||||||
{
|
{
|
||||||
$labels = parent::fieldLabels($includerelations);
|
$labels = parent::fieldLabels($includerelations);
|
||||||
|
Loading…
Reference in New Issue
Block a user