From 2cf5fbcfce3904df95904f367cb3a6a34745a674 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 29 Mar 2016 17:35:10 +1300 Subject: [PATCH] Implement readCampaigns() Renamed "item/items" urls to "set/sets" to distinguish between change sets and change set items. --- admin/code/CampaignAdmin.php | 346 +++++++++++++---------------- model/versioning/ChangeSet.php | 10 + model/versioning/ChangeSetItem.php | 1 - 3 files changed, 163 insertions(+), 194 deletions(-) diff --git a/admin/code/CampaignAdmin.php b/admin/code/CampaignAdmin.php index 02e1b5807..8c1ea860d 100644 --- a/admin/code/CampaignAdmin.php +++ b/admin/code/CampaignAdmin.php @@ -9,8 +9,8 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { private static $allowed_actions = [ - 'item', - 'items', + 'set', + 'sets', 'schema', 'DetailEditForm', 'readCampaigns', @@ -25,11 +25,11 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { private static $menu_title = 'Campaigns'; private static $url_handlers = [ - 'GET items' => 'readCampaigns', - 'POST item/$ID' => 'createCampaign', - 'GET item/$ID' => 'readCampaign', - 'PUT item/$ID' => 'updateCampaign', - 'DELETE item/$ID' => 'deleteCampaign', + 'GET sets' => 'readCampaigns', + 'POST set/$ID' => 'createCampaign', + 'GET set/$ID' => 'readCampaign', + 'PUT set/$ID' => 'updateCampaign', + 'DELETE set/$ID' => 'deleteCampaign', ]; private static $url_segment = 'campaigns'; @@ -101,23 +101,23 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { "data": { "recordType": "ChangeSet", "collectionReadEndpoint": { - "url": "admin\/campaigns\/items", + "url": "admin\/campaigns\/sets", "method": "GET" }, "itemReadEndpoint": { - "url": "admin\/campaigns\/item\/:id", + "url": "admin\/campaigns\/set\/:id", "method": "GET" }, "itemUpdateEndpoint": { - "url": "admin\/campaigns\/item\/:id", + "url": "admin\/campaigns\/set\/:id", "method": "PUT" }, "itemCreateEndpoint": { - "url": "admin\/campaigns\/item\/:id", + "url": "admin\/campaigns\/set\/:id", "method": "POST" }, "itemDeleteEndpoint": { - "url": "admin\/campaigns\/item\/:id", + "url": "admin\/campaigns\/set\/:id", "method": "DELETE" }, "editFormSchemaEndpoint": "admin\/campaigns\/schema\/DetailEditForm", @@ -186,186 +186,116 @@ JSON; public function readCampaigns(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); - $json = <<setBody($json); - + $hal = $this->getListResource(); + $response->setBody(Convert::array2json($hal)); return $response; + } + + /** + * Get list contained as a hal wrapper + * + * @return array + */ + protected function getListResource() { + $items = $this->getListItems(); + $count = $items->count(); + $hal = [ + 'count' => $count, + 'total' => $count, + '_links' => [ + 'self' => [ + 'href' => $this->Link('items') + ] + ], + '_embedded' => ['ChangeSets' => []] + ]; + foreach($items as $item) { + /** @var ChangeSet $item */ + $resource = $this->getChangeSetResource($item); + $hal['_embedded']['ChangeSets'][] = $resource; + } + 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, + '_embedded' => ['ChangeSetItems' => []] + ]; + foreach($changeSet->Changes() as $changeSetItem) { + /** @var ChangesetItem $changeSetItem */ + $resource = $this->getChangeSetItemResource($changeSetItem); + $hal['_embedded']['ChangeSetItems'][] = $resource; + } + return $hal; + } + + /** + * Build item resource from a changesetitem + * + * @param ChangeSetItem $changeSetItem + * @return array + */ + protected function getChangeSetItemResource(ChangeSetItem $changeSetItem) { + $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, + ]; + // 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; + } + } + + return $hal; + } + + + + /** + * Gets viewable list of campaigns + * + * @return SS_List + */ + protected function getListItems() { + return ChangeSet::get() + ->filter('State', ChangeSet::STATE_OPEN); + } + + /** * REST endpoint to get a campaign. * @@ -420,14 +350,44 @@ JSON; return Form::create( $this, 'DetailEditForm', - FieldList::create( - TextField::create('Name'), - TextAreaField::create('Description') - ), + ChangeSet::singleton()->getCMSFields(), FieldList::create( FormAction::create('save', 'Save') ) ); } + /** + * 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 + ); + } + + /** + * + */ + public function FindReferencedChanges() { + + } + } diff --git a/model/versioning/ChangeSet.php b/model/versioning/ChangeSet.php index 64a264c7c..91fa8479f 100644 --- a/model/versioning/ChangeSet.php +++ b/model/versioning/ChangeSet.php @@ -328,4 +328,14 @@ class ChangeSet extends DataObject { // Default permissions return (bool)Permission::checkMember($member, $this->config()->required_permission); } + + public function getCMSFields() { + $fields = new FieldList(); + $fields->merge([ + TextField::create('Name'), + ReadonlyField::create('State') + ]); + $this->extend('updateCMSFields', $fields); + return $fields; + } } diff --git a/model/versioning/ChangeSetItem.php b/model/versioning/ChangeSetItem.php index 3bd663ee9..76a7173a6 100644 --- a/model/versioning/ChangeSetItem.php +++ b/model/versioning/ChangeSetItem.php @@ -5,7 +5,6 @@ /** * A single line in a changeset * - * @property string $ReferencedBy * @property string $Added * @property string $ObjectClass * @property int $ObjectID