Skip to content

Commit

Permalink
fixed #3 added delete button on list and edit-form
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jul 11, 2016
1 parent 9d5ce92 commit 15339fc
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 28 deletions.
38 changes: 38 additions & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,44 @@ public function putAction(Request $request, $uuid)
);
}

/**
* Deletes multiple documents.
*
* @param Request $request
*
* @return Response
*/
public function cdeleteAction(Request $request)
{
$ids = array_filter(explode(',', $request->get('ids', '')));

$documentManager = $this->getDocumentManager();
foreach ($ids as $id) {
$document = $documentManager->find($id);
$documentManager->remove($document);
}
$documentManager->flush();

return $this->handleView($this->view(null));
}

/**
* Deletes multiple documents.
*
* @param string $id
*
* @return Response
*/
public function deleteAction($id)
{
$documentManager = $this->getDocumentManager();
$document = $documentManager->find($id);
$documentManager->remove($document);
$documentManager->flush();

return $this->handleView($this->view(null));
}

/**
* Persists the document using the given information.
*
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/dist/components/articles/edit/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Resources/public/dist/components/articles/list/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 29 additions & 16 deletions Resources/public/js/components/articles/edit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ define(['jquery', 'underscore'], function($, _) {
},

header: function() {
var buttons = {
save: {
parent: 'saveWithOptions'
},
template: {
options: {
dropdownOptions: {
url: '/admin/articles/templates?type=' + (this.options.type || this.data.type),
callback: function(item) {
this.template = item.template;
this.sandbox.emit('sulu.tab.template-change', item);
}.bind(this)
}
}
}
};

if (!!this.options.id) {
buttons.delete = {};
}

return {
tabs: {
url: '/admin/content-navigations?alias=article',
Expand All @@ -46,22 +67,7 @@ define(['jquery', 'underscore'], function($, _) {
},

toolbar: {
buttons: {
save: {
parent: 'saveWithOptions'
},
template: {
options: {
dropdownOptions: {
url: '/admin/articles/templates?type=' + (this.options.type || this.data.type),
callback: function(item) {
this.template = item.template;
this.sandbox.emit('sulu.tab.template-change', item);
}.bind(this)
}
}
}
}
buttons: buttons
}
};
},
Expand All @@ -76,6 +82,7 @@ define(['jquery', 'underscore'], function($, _) {
this.sandbox.on('sulu.header.back', this.toList.bind(this));
this.sandbox.on('sulu.tab.dirty', this.enableSave.bind(this));
this.sandbox.on('sulu.toolbar.save', this.save.bind(this));
this.sandbox.on('sulu.toolbar.delete', this.deleteItem.bind(this));
this.sandbox.on('sulu.tab.data-changed', this.setData.bind(this));

this.sandbox.on('sulu.header.language-changed', function(item) {
Expand All @@ -84,6 +91,12 @@ define(['jquery', 'underscore'], function($, _) {
}.bind(this));
},

deleteItem: function() {
this.sandbox.util.save('/admin/api/articles/' + this.options.id, 'DELETE').then(function() {
this.toList();
}.bind(this));
},

toEdit: function(locale, id) {
this.sandbox.emit('sulu.router.navigate', 'articles/' + (locale || this.options.locale) + '/edit:' + (id || this.options.id), true, true);
},
Expand Down
16 changes: 6 additions & 10 deletions Resources/public/js/components/articles/list/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,10 @@ define(['underscore'], function(_) {
},

deleteItems: function(ids) {
for (var i = 0, length = ids.length; i < length; i++) {
this.deleteItem(ids[i]);
}
},

deleteItem: function(id) {
this.sandbox.util.save('/admin/api/articles/' + id, 'DELETE').then(function() {
this.sandbox.emit('husky.datagrid.news.record.remove', id);
this.sandbox.util.save('/admin/api/articles?ids=' + ids.join(','), 'DELETE').then(function() {
_.each(ids, function(id) {
this.sandbox.emit('husky.datagrid.articles.record.remove', id);
}.bind(this));
}.bind(this));
},

Expand All @@ -180,13 +176,13 @@ define(['underscore'], function(_) {
},

bindCustomEvents: function() {
this.sandbox.on('husky.datagrid.news.number.selections', function(number) {
this.sandbox.on('husky.datagrid.articles.number.selections', function(number) {
var postfix = number > 0 ? 'enable' : 'disable';
this.sandbox.emit('sulu.header.toolbar.item.' + postfix, 'deleteSelected', false);
}.bind(this));

this.sandbox.on('sulu.toolbar.delete', function() {
this.sandbox.emit('husky.datagrid.news.items.get-selected', this.deleteItems.bind(this));
this.sandbox.emit('husky.datagrid.articles.items.get-selected', this.deleteItems.bind(this));
}.bind(this));

this.sandbox.on('sulu.header.language-changed', function(item) {
Expand Down
42 changes: 42 additions & 0 deletions Tests/Functional/Controller/ArticleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,48 @@ function ($item) {
$this->assertContains([$article2['id'], $article2['title']], $items);
}

public function testDelete()
{
$this->purgeIndex();

$article = $this->testPost('Sulu');
$this->flush();

$client = $this->createAuthenticatedClient();
$client->request('DELETE', '/api/articles/' . $article['id']);
$this->flush();

$this->assertHttpStatusCode(204, $client->getResponse());

$client->request('GET', '/api/articles?locale=de&type=blog');

$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(0, $response['total']);
$this->assertCount(0, $response['_embedded']['articles']);
}

public function testCDelete()
{
$this->purgeIndex();

$article1 = $this->testPost('Sulu');
$this->flush();
$article2 = $this->testPost('Sulu is awesome', 'simple');
$this->flush();

$client = $this->createAuthenticatedClient();
$client->request('DELETE', '/api/articles?ids=' . implode(',', [$article1['id'], $article2['id']]));
$this->flush();

$this->assertHttpStatusCode(204, $client->getResponse());

$client->request('GET', '/api/articles?locale=de&type=blog');

$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(0, $response['total']);
$this->assertCount(0, $response['_embedded']['articles']);
}

private function purgeIndex()
{
/** @var IndexerInterface $indexer */
Expand Down

0 comments on commit 15339fc

Please sign in to comment.