invoiceninja/app/Datatables/MakesActionMenu.php
David Bomba 0c1fc0d904
List views (#2609)
* Wire up Create Entity Button to create route

* Refactor permissions, we must also ensure the user company id and entity id matches at the Gate::

* Add translations for Status filters

* Bug fix for initial list view not displaying

* Apply actions to menu for list items

* Wire up list view actions, individual

* Place permission filters on datatable lists
2019-01-20 16:00:50 +11:00

64 lines
2.7 KiB
PHP

<?php
namespace App\Datatables;
use Illuminate\Support\Collection;
trait MakesActionMenu
{
/**
* Returns all possible datatable actions
* this list will be the single source of truth
* for all Select Actions
*
* @return Collection collection instance of action items
*/
public function actions() :Collection
{
return collect([
['action' => 'view_client_client_id', 'permission' => 'view_client', 'route' => 'clients.show', 'key' => 'client_id', 'name' => trans('texts.view')],
['action' => 'edit_client_client_id', 'permission' => 'edit_client', 'route' => 'clients.edit', 'key' => 'client_id', 'name' => trans('texts.edit')],
['action' => 'create_task_client_id', 'permission' => 'create_task', 'route' => 'tasks.create', 'key' => 'client_id', 'name' => trans('texts.new_task')],
['action' => 'create_invoice_client_id', 'permission' => 'create_invoice', 'route' => 'invoices.create', 'key' => 'client_id', 'name' => trans('texts.new_invoice')],
['action' => 'enter_payment_client_id', 'permission' => 'create_payment', 'route' => 'payments.create', 'key' => 'client_id', 'name' => trans('texts.enter_payment')],
['action' => 'enter_credit_client_id', 'permission' => 'create_credit', 'route' => 'credits.create', 'key' => 'client_id', 'name' => trans('texts.enter_credit')],
['action' => 'enter_expense_client_id', 'permission' => 'create_expense', 'route' => 'expenses.create', 'key' => 'client_id', 'name' => trans('texts.enter_expense')]
]);
}
/**
* Filters the main actions collection down to the requested
* actions for this menu
*
* @param array $actions Array of actions requested
* @param array $permissions Array of user permissions
* @param bool $isAdmin Boolean isAdmin
* @return Collection collection of filtered actions available to the user
*/
public function filterActions(array $actions, array $permissions, bool $isAdmin) :Collection
{
return $this->checkPermissions($this->actions()->whereIn('action', $actions), $permissions, $isAdmin);
}
/**
* Checks the user permissions against the collection and returns
* a Collection of available actions\.
*
* @param Collection $actions collection of possible actions
* @param bool $isAdmin boolean defining if user is an administrator
* @return Collection collection of filtered actions
*/
private function checkPermissions(Collection $actions, array $permissions, bool $is_admin) :Collection
{
if($is_admin === TRUE)
return $actions;
return $actions->whereIn('permission', $permissions);
}
}