diff --git a/admin/code/CampaignAdmin.php b/admin/code/CampaignAdmin.php index 8be248cd6..edfaddf21 100644 --- a/admin/code/CampaignAdmin.php +++ b/admin/code/CampaignAdmin.php @@ -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 = <<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 diff --git a/admin/code/CampaignAdminList.php b/admin/code/CampaignAdminList.php new file mode 100644 index 000000000..3f88397ac --- /dev/null +++ b/admin/code/CampaignAdminList.php @@ -0,0 +1,65 @@ +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; + } +} diff --git a/admin/tests/CampaignAdminListTest.php b/admin/tests/CampaignAdminListTest.php new file mode 100644 index 000000000..95beba067 --- /dev/null +++ b/admin/tests/CampaignAdminListTest.php @@ -0,0 +1,46 @@ +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']); + } +} diff --git a/src/ORM/Versioning/ChangeSet.php b/src/ORM/Versioning/ChangeSet.php index d191bce87..9a76babb1 100644 --- a/src/ORM/Versioning/ChangeSet.php +++ b/src/ORM/Versioning/ChangeSet.php @@ -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);