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:
Ingo Schommer 2016-12-08 23:24:58 +13:00 committed by GitHub
commit 322d3e18fd
4 changed files with 151 additions and 115 deletions

View File

@ -92,123 +92,32 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider {
]);
}
public function schema($request) {
// TODO Hardcoding schema until we can get GridField to generate a schema dynamically
$treeClassJS = Convert::raw2js($this->config()->tree_class);
$adminURL = Convert::raw2js(AdminRootController::admin_url());
$json = <<<JSON
{
"id": "{$adminURL}campaigns\/schema\/EditForm",
"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;
public function getEditForm($id = null, $fields = null)
{
$fields = new FieldList(
CampaignAdminList::create('ChangeSets')
);
$actions = new FieldList();
$form = Form::create($this, 'EditForm', $fields, $actions);
$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);
}
}
// Set callback response
$form->setValidationResponseCallback(function() use ($form) {
$schemaId = $this->Link('schema/EditForm');
return $this->getSchemaResponse($form, $schemaId);
});
/**
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.
*
* @return HTTPResponse

View 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;
}
}

View 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']);
}
}

View File

@ -79,6 +79,12 @@ class ChangeSet extends DataObject
'SilverStripe\\Assets\\File',
);
private static $summary_fields = [
'Name' => 'Title',
'ChangesCount' => 'Changes',
'Description' => 'Description',
];
/**
* Default permission to require for publishers.
* 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;
}
/**
* Required to support count display in react gridfield column
*
* @return int
*/
public function getChangesCount()
{
return $this->Changes()->count();
}
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);