1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Services/DatatableService.php

145 lines
5.7 KiB
PHP
Raw Normal View History

2015-11-05 23:37:04 +01:00
<?php namespace App\Services;
use Utils;
use Datatable;
2016-03-16 00:08:00 +01:00
use Auth;
2016-05-23 18:52:20 +02:00
use App\Ninja\Datatables\EntityDatatable;
use Chumper\Datatable\Table;
2015-11-05 23:37:04 +01:00
/**
* Class DatatableService
*/
2015-11-05 23:37:04 +01:00
class DatatableService
{
/**
* @param EntityDatatable $datatable
* @param $query
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
2016-05-23 18:52:20 +02:00
public function createDatatable(EntityDatatable $datatable, $query)
2015-11-05 23:37:04 +01:00
{
$table = Datatable::query($query);
2016-05-23 18:52:20 +02:00
if ($datatable->isBulkEdit) {
2015-11-05 23:37:04 +01:00
$table->addColumn('checkbox', function ($model) {
$can_edit = Auth::user()->hasPermission('edit_all') || (isset($model->user_id) && Auth::user()->id == $model->user_id);
2016-05-23 18:52:20 +02:00
return !$can_edit?'':'<input type="checkbox" name="ids[]" value="' . $model->public_id
2015-11-29 17:00:50 +01:00
. '" ' . Utils::getEntityRowClass($model) . '>';
2015-11-05 23:37:04 +01:00
});
}
2016-05-23 18:52:20 +02:00
foreach ($datatable->columns() as $column) {
2015-11-05 23:37:04 +01:00
// set visible to true by default
if (count($column) == 2) {
$column[] = true;
}
list($field, $value, $visible) = $column;
if ($visible) {
$table->addColumn($field, $value);
2016-05-23 18:52:20 +02:00
$orderColumns[] = $field;
2015-11-05 23:37:04 +01:00
}
}
2016-05-23 18:52:20 +02:00
if (count($datatable->actions())) {
$this->createDropdown($datatable, $table);
2015-11-05 23:37:04 +01:00
}
return $table->orderColumns($orderColumns)->make();
}
/**
* @param EntityDatatable $datatable
* @param Table $table
*/
2016-07-04 09:02:18 +02:00
private function createDropdown(EntityDatatable $datatable, $table)
2015-11-05 23:37:04 +01:00
{
2016-05-23 18:52:20 +02:00
$table->addColumn('dropdown', function ($model) use ($datatable) {
2016-02-17 20:33:15 +01:00
$hasAction = false;
2015-12-13 21:12:54 +01:00
$str = '<center style="min-width:100px">';
2015-11-06 00:14:00 +01:00
2016-03-16 00:08:00 +01:00
$can_edit = Auth::user()->hasPermission('edit_all') || (isset($model->user_id) && Auth::user()->id == $model->user_id);
2016-05-23 18:52:20 +02:00
2015-11-06 00:14:00 +01:00
if (property_exists($model, 'is_deleted') && $model->is_deleted) {
2015-12-09 17:08:24 +01:00
$str .= '<button type="button" class="btn btn-sm btn-danger tr-status">'.trans('texts.deleted').'</button>';
2015-11-06 00:14:00 +01:00
} elseif ($model->deleted_at && $model->deleted_at !== '0000-00-00') {
2015-12-09 17:08:24 +01:00
$str .= '<button type="button" class="btn btn-sm btn-warning tr-status">'.trans('texts.archived').'</button>';
2015-11-06 00:14:00 +01:00
} else {
2015-12-09 17:08:24 +01:00
$str .= '<div class="tr-status"></div>';
2015-11-06 00:14:00 +01:00
}
2016-05-14 23:23:20 +02:00
$dropdown_contents = '';
2015-11-05 23:37:04 +01:00
$lastIsDivider = false;
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
2016-05-23 18:52:20 +02:00
foreach ($datatable->actions() as $action) {
2015-11-05 23:37:04 +01:00
if (count($action)) {
if (count($action) == 2) {
$action[] = function() {
return true;
};
}
list($value, $url, $visible) = $action;
if ($visible($model)) {
2016-03-16 00:08:00 +01:00
if($value == '--divider--'){
$dropdown_contents .= '<li class="divider"></li>';
2016-03-16 00:08:00 +01:00
$lastIsDivider = true;
}
else {
2016-05-12 04:55:37 +02:00
$urlVal = $url($model);
$urlStr = is_string($urlVal) ? $urlVal : $urlVal['url'];
$attributes = '';
if (!empty($urlVal['attributes'])) {
$attributes = ' '.$urlVal['attributes'];
}
2016-05-14 23:23:20 +02:00
$dropdown_contents .= "<li><a href=\"$urlStr\"{$attributes}>{$value}</a></li>";
2016-03-16 00:08:00 +01:00
$hasAction = true;
$lastIsDivider = false;
}
2015-11-05 23:37:04 +01:00
}
} elseif ( ! $lastIsDivider) {
$dropdown_contents .= '<li class="divider"></li>';
2015-11-05 23:37:04 +01:00
$lastIsDivider = true;
}
}
2016-02-17 20:33:15 +01:00
if ( ! $hasAction) {
return '';
}
2016-03-16 00:08:00 +01:00
if ( $can_edit && ! $lastIsDivider) {
$dropdown_contents .= '<li class="divider"></li>';
2015-11-05 23:37:04 +01:00
}
2016-05-23 18:52:20 +02:00
if (($datatable->entityType != ENTITY_USER || $model->public_id) && $can_edit) {
2016-05-14 23:23:20 +02:00
$dropdown_contents .= "<li><a href=\"javascript:archiveEntity({$model->public_id})\">"
. trans("texts.archive_{$datatable->entityType}") . '</a></li>';
2015-11-21 22:10:26 +01:00
}
2016-03-16 00:08:00 +01:00
} else if($can_edit) {
2016-06-20 16:14:43 +02:00
$dropdown_contents .= "<li><a href=\"javascript:restoreEntity({$model->public_id})\">"
. trans("texts.restore_{$datatable->entityType}") . '</a></li>';
2015-11-05 23:37:04 +01:00
}
2016-03-16 00:08:00 +01:00
if (property_exists($model, 'is_deleted') && !$model->is_deleted && $can_edit) {
2016-05-14 23:23:20 +02:00
$dropdown_contents .= "<li><a href=\"javascript:deleteEntity({$model->public_id})\">"
. trans("texts.delete_{$datatable->entityType}") . '</a></li>';
2015-11-05 23:37:04 +01:00
}
2016-05-14 23:23:20 +02:00
if (!empty($dropdown_contents)) {
$str .= '<div class="btn-group tr-action" style="height:auto;display:none">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" style="width:100px">
'.trans('texts.select').' <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">';
$str .= $dropdown_contents . '</ul>';
}
return $str.'</div></center>';
2015-11-05 23:37:04 +01:00
});
}
2016-05-23 18:52:20 +02:00
}