Merge pull request #1612 from cuneytsenturk/master

BulkAction re-factoring for module use core bulk action
This commit is contained in:
Cüneyt Şentürk 2020-09-22 00:38:38 +03:00 committed by GitHub
commit 0701b129af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 136 additions and 61 deletions

View File

@ -34,6 +34,7 @@ abstract class BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download'
], ],
]; ];

View File

@ -15,11 +15,15 @@ class Items extends BulkAction
'enable' => [ 'enable' => [
'name' => 'general.enable', 'name' => 'general.enable',
'message' => 'bulk_actions.message.enable', 'message' => 'bulk_actions.message.enable',
'path' => ['group' => 'common', 'type' => 'items'],
'type' => '*',
'permission' => 'update-common-items', 'permission' => 'update-common-items',
], ],
'disable' => [ 'disable' => [
'name' => 'general.disable', 'name' => 'general.disable',
'message' => 'bulk_actions.message.disable', 'message' => 'bulk_actions.message.disable',
'path' => ['group' => 'common', 'type' => 'items'],
'type' => '*',
'permission' => 'update-common-items', 'permission' => 'update-common-items',
], ],
'delete' => [ 'delete' => [
@ -30,6 +34,7 @@ class Items extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -33,6 +33,7 @@ class Bills extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -14,6 +14,7 @@ class Payments extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
'delete' => [ 'delete' => [
'name' => 'general.delete', 'name' => 'general.delete',

View File

@ -29,6 +29,7 @@ class Vendors extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -29,6 +29,7 @@ class Customers extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -39,6 +39,7 @@ class Invoices extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -19,6 +19,7 @@ class Revenues extends BulkAction
'export' => [ 'export' => [
'name' => 'general.export', 'name' => 'general.export',
'message' => 'bulk_actions.message.export', 'message' => 'bulk_actions.message.export',
'type' => 'download',
], ],
]; ];

View File

@ -0,0 +1,22 @@
<?php
namespace App\Events\Common;
use Illuminate\Queue\SerializesModels;
class BulkActionsAdding
{
use SerializesModels;
public $bulk_action;
/**
* Create a new event instance.
*
* @param $bulk_action
*/
public function __construct($bulk_action)
{
$this->bulk_action = $bulk_action;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Http\ViewComposers; namespace App\Http\ViewComposers;
use Akaunting\Module\Module; use Akaunting\Module\Module;
use App\Events\Common\BulkActionsAdding;
use Date; use Date;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\View\View; use Illuminate\View\View;
@ -67,10 +68,19 @@ class Index
$class_name = 'App\BulkActions\\' . $file_name; $class_name = 'App\BulkActions\\' . $file_name;
} }
if (!class_exists($class_name)) { if (class_exists($class_name)) {
return; event(new BulkActionsAdding(app($class_name)));
$bulk_actions = app($class_name)->actions;
} else {
$b = new \stdClass();
$b->actions = [];
event(new BulkActionsAdding($b));
$bulk_actions = $b->actions;
} }
$view->with(['bulk_actions' => app($class_name)->actions]); $view->with(['bulk_actions' => $bulk_actions]);
} }
} }

View File

@ -12,6 +12,8 @@ export default class BulkAction {
this['value'] = '*'; this['value'] = '*';
// Select action message // Select action message
this['message'] = ''; this['message'] = '';
// Action type
this['type'] = '';
// Bulk action view status // Bulk action view status
this['show'] = false; this['show'] = false;
// Bulk action modal status // Bulk action modal status
@ -63,6 +65,18 @@ export default class BulkAction {
this.message = ''; this.message = '';
} }
this.path = document.getElementsByName("bulk_action_path")[0].getAttribute('value');
if (event.target.options[event.target.options.selectedIndex].dataset.path) {
this.path = event.target.options[event.target.options.selectedIndex].dataset.path;
}
this.type = '*';
if (event.target.options[event.target.options.selectedIndex].dataset.type) {
this.type = event.target.options[event.target.options.selectedIndex].dataset.type;
}
return this.message; return this.message;
} }
@ -72,72 +86,79 @@ export default class BulkAction {
return; return;
} }
var path = document.getElementsByName("bulk_action_path")[0].getAttribute('value');
this.loading = true; this.loading = true;
if (this.value != 'export') { // bwfore version 2.0.23
window.axios.post(path, { if (this.value == 'export') {
'handle': this.value, this.type = 'download';
'selected': this.selected }
})
.then(response => {
if (response.data.redirect) {
window.location.reload(false);
}
})
.catch(error => {
//this.loading = false;
//this.modal = false;
//window.location.reload(false); switch (this.type) {
}) case 'download':
.finally(function () { let download_promise = Promise.resolve(window.axios({
//window.location.reload(false); url: this.path,
}); method: 'POST',
} else { data:{
window.axios({ 'handle': this.value,
url: path, 'selected': this.selected
method: 'POST', },
data:{ responseType: 'blob',
}));
download_promise.then((response) => {
const blob = new Blob([response.data], {type: response.data.type});
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const contentDisposition = response.headers['content-disposition'];
let fileName = 'unknown';
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename=(.+)/);
if (fileNameMatch.length === 2) {
fileName = fileNameMatch[1];
}
}
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
this.loading = false;
this.modal = false;
this.value = '*';
this.clear();
});
break;
default:
let type_promise = Promise.resolve(window.axios.post(this.path, {
'handle': this.value, 'handle': this.value,
'selected': this.selected 'selected': this.selected
}, }));
responseType: 'blob',
}).then((response) => {
console.log(response.data);
const blob = new Blob([response.data], {type: response.data.type});
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url; type_promise.then(response => {
if (response.data.redirect) {
const contentDisposition = response.headers['content-disposition']; window.location.reload(false);
let fileName = 'unknown';
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename=(.+)/);
if (fileNameMatch.length === 2) {
fileName = fileNameMatch[1];
} }
} })
.catch(error => {
//this.loading = false;
//this.modal = false;
link.setAttribute('download', fileName); //window.location.reload(false);
})
document.body.appendChild(link); .finally(function () {
//window.location.reload(false);
link.click(); });
link.remove();
window.URL.revokeObjectURL(url);
this.loading = false;
this.modal = false;
this.value = '*';
this.clear();
});
} }
} }

View File

@ -48,6 +48,16 @@
@if(!empty($action['message'])) @if(!empty($action['message']))
data-message="{{ trans_choice($action['message'], 2, ['type' => $text]) }}" data-message="{{ trans_choice($action['message'], 2, ['type' => $text]) }}"
@endif @endif
@if(isset($action['path']) && !empty($action['path']))
data-path="{{ route('bulk-actions.action', $action['path']) }}"
@else
data-path=""
@endif
@if(isset($action['type']) && !empty($action['type']))
data-type="{{ $action['type'] }}"
@else
data-type=""
@endif
>{{ trans($action['name']) }}</option> >{{ trans($action['name']) }}</option>
@endif @endif
@endforeach @endforeach