'readCampaigns', 'POST set/$ID' => 'createCampaign', 'GET set/$ID' => 'readCampaign', 'PUT set/$ID' => 'updateCampaign', 'DELETE set/$ID' => 'deleteCampaign', ]; private static $url_segment = 'campaigns'; public function getClientConfig() { return array_merge(parent::getClientConfig(), [ 'forms' => [ // TODO Use schemaUrl instead 'editForm' => [ 'schemaUrl' => $this->Link('schema/EditForm') ] ] ]); } public function schema($request) { // TODO Hardcoding schema until we can get GridField to generate a schema dynamically $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); } } /** * REST endpoint to create a campaign. * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse */ public function createCampaign(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $response->setBody(Convert::raw2json(['campaign' => 'create'])); return $response; } /** * REST endpoint to get a list of campaigns. * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse */ public function readCampaigns(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/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. * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse */ public function readCampaign(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $response->setBody(''); return $response; } /** * REST endpoint to update a campaign. * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse */ public function updateCampaign(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $response->setBody(Convert::raw2json(['campaign' => 'update'])); return $response; } /** * REST endpoint to delete a campaign. * * @param SS_HTTPRequest $request * * @return SS_HTTPResponse */ public function deleteCampaign(SS_HTTPRequest $request) { $response = new SS_HTTPResponse(); $response->addHeader('Content-Type', 'application/json'); $response->setBody(Convert::raw2json(['campaign' => 'delete'])); return $response; } /** * @todo Use GridFieldDetailForm once it can handle structured data and form schemas * * @return Form */ public function getDetailEditForm() { return Form::create( $this, 'DetailEditForm', 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() { } }