Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch-edit redirects #789

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions application/controllers/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,31 @@ public function batchEditAction()

$delete = (boolean) $this->_getParam('submit-batch-delete');

$params = json_decode($this->_getParam('params'), true) ?: array();
unset($params['admin']);
unset($params['module']);
unset($params['controller']);
unset($params['action']);
unset($params['submit_search']);
unset($params['page']);
$redirectTo = 'items/browse'. (empty($params) ? '' : '?'. http_build_query($params));

$batchAll = (boolean) $this->_getParam('batch-all');
// Process all searched items.
if ($batchAll) {
$params = json_decode($this->_getParam('params'), true) ?: array();
unset($params['admin']);
unset($params['module']);
unset($params['controller']);
unset($params['action']);
unset($params['submit_search']);
unset($params['page']);

$totalRecords = $this->_helper->db->count($params);

if (empty($totalRecords)) {
$this->_helper->flashMessenger(__('No item to batch edit.'), 'error');
$this->_helper->redirector('browse', 'items', null, $params);
$this->_helper->redirector->gotoUrl($redirectTo);
return;
}

// Special check to avoid the deletion of all the base.
if ($delete && total_records('Item') == $totalRecords) {
$this->_helper->flashMessenger(__('The deletion of all items is forbidden.'), 'error');
$this->_helper->redirector('browse', 'items', null, $params);
$this->_helper->redirector->gotoUrl($redirectTo);
return;
}

Expand All @@ -253,7 +255,7 @@ public function batchEditAction()
$itemIds = $this->_getParam('items');
if (empty($itemIds)) {
$this->_helper->flashMessenger(__('You must choose some items to batch edit.'), 'error');
$this->_helper->redirector('browse', 'items');
$this->_helper->redirector->gotoUrl($redirectTo);
return;
}

Expand All @@ -278,6 +280,7 @@ public function batchEditSaveAction()
return $this->_batchEditAllSave();
}

$redirectToSelection = true;
$itemIds = $this->_getParam('items');
if ($itemIds) {
$metadata = $this->_getParam('metadata');
Expand Down Expand Up @@ -354,6 +357,7 @@ public function batchEditSaveAction()
$dispatcher->send('Job_ItemBatchEdit', $options);

if ($delete) {
$redirectToSelection = false;
$message = __('The items were successfully deleted!');
} else {
$message = __('The items were successfully changed!');
Expand All @@ -364,14 +368,19 @@ public function batchEditSaveAction()
$this->_helper->flashMessenger(__('No item to batch edit.'), 'error');
}

$this->_helper->redirector('browse', 'items');
if ($redirectToSelection && !empty($itemIds)) {
$this->_helper->redirector->gotoUrl('items/browse?'. http_build_query(array('range' => implode(',', (array) $itemIds))));
} else {
$this->_helper->redirector('browse', 'items');
}
}

/**
* Processes batch edit all information. Only accessible via POST.
*/
protected function _batchEditAllSave()
{
$redirectToSelection = true;
// Get the record ids filtered to Omeka_Db_Table::applySearchFilters().
$params = json_decode($this->_getParam('params'), true) ?: array();
$totalRecords = $this->_helper->db->count($params);
Expand Down Expand Up @@ -419,6 +428,7 @@ protected function _batchEditAllSave()
$dispatcher->sendLongRunning('Job_ItemBatchEditAll', $options);

if ($delete) {
$redirectToSelection = false;
$message = __('The items are checked and deleted one by one in the background.');
} else {
$message = __('The items are checked and changed one by one in the background.');
Expand All @@ -430,6 +440,10 @@ protected function _batchEditAllSave()
$this->_helper->flashMessenger(__('No item to batch edit.'), 'error');
}

$this->_helper->redirector('browse', 'items');
if ($redirectToSelection && !empty($params)) {
$this->_helper->redirector->gotoUrl('items/browse?'. http_build_query($params));
} else {
$this->_helper->redirector('browse', 'items');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function testBatchEditSaveSuperUser()
$this->assertEquals(3, count($item->getTags()));
}

$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("The items were successfully changed!", $messages['success']);
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testBatchEditSaveAdminUser()
$this->assertEquals(3, count($item->getTags()));
}

$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("The items were successfully changed!", $messages['success']);
Expand Down Expand Up @@ -255,7 +255,7 @@ public function testBatchEditContributorUserSaveAllowedData()
$this->assertEquals(3, count($item->getTags()));
}

$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("The items were successfully changed!", $messages['success']);
Expand All @@ -274,7 +274,7 @@ public function testBatchEditContributorUserSaveDisallowedData()
$this->_makePost();

$this->dispatch('/items/batch-edit-save');
$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("User is not allowed", $messages['error'][0]);
Expand Down Expand Up @@ -303,7 +303,11 @@ public function testBatchDeletePermissions($userRole, $succeeds)
$this->_authenticateUser($this->_users[$userRole]);
$this->_makePost($post);
$this->dispatch('/items/batch-edit-save');
$this->assertRedirectTo('/items/browse');
if ($succeeds) {
$this->assertRedirectTo('/items/browse');
} else {
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
}
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();

Expand Down Expand Up @@ -354,7 +358,7 @@ public function testBatchEditMakeNotPublicNotFeatured()
$this->assertFalse($item->isFeatured());
}

$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("The items were successfully changed!", $messages['success']);
Expand Down Expand Up @@ -396,7 +400,7 @@ public function testBatchEditRemoveMetadata()
$this->assertNull($item->item_type_id);
}

$this->assertRedirectTo('/items/browse');
$this->assertRedirectTo('/items/browse?range='.urlencode(implode(',', $itemIds)));
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$messages = $flash->getCurrentMessages();
$this->assertContains("The items were successfully changed!", $messages['success']);
Expand Down