2015-11-05 23:37:04 +01:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
|
|
|
use Utils;
|
|
|
|
use Datatable;
|
|
|
|
|
|
|
|
class DatatableService
|
|
|
|
{
|
|
|
|
public function createDatatable($entityType, $query, $columns, $actions = null, $showCheckbox = true)
|
|
|
|
{
|
|
|
|
$table = Datatable::query($query);
|
|
|
|
$orderColumns = [];
|
|
|
|
|
|
|
|
if ($actions && $showCheckbox) {
|
|
|
|
$table->addColumn('checkbox', function ($model) {
|
2015-11-29 17:00:50 +01:00
|
|
|
return '<input type="checkbox" name="ids[]" value="' . $model->public_id
|
|
|
|
. '" ' . Utils::getEntityRowClass($model) . '>';
|
2015-11-05 23:37:04 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($columns as $column) {
|
|
|
|
// set visible to true by default
|
|
|
|
if (count($column) == 2) {
|
|
|
|
$column[] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($field, $value, $visible) = $column;
|
|
|
|
|
|
|
|
if ($visible) {
|
|
|
|
$table->addColumn($field, $value);
|
|
|
|
$orderColumns[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($actions) {
|
|
|
|
$this->createDropdown($entityType, $table, $actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $table->orderColumns($orderColumns)->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createDropdown($entityType, $table, $actions)
|
|
|
|
{
|
|
|
|
$table->addColumn('dropdown', function ($model) use ($entityType, $actions) {
|
2015-12-13 21:12:54 +01:00
|
|
|
$str = '<center style="min-width:100px">';
|
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
|
|
|
}
|
|
|
|
|
2015-12-13 21:12:54 +01:00
|
|
|
$str .= '<div class="btn-group tr-action" style="height:auto;display:none">
|
2015-12-09 16:54:24 +01:00
|
|
|
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" style="width:100px">
|
2015-11-05 23:37:04 +01:00
|
|
|
'.trans('texts.select').' <span class="caret"></span>
|
|
|
|
</button>
|
|
|
|
<ul class="dropdown-menu" role="menu">';
|
|
|
|
|
|
|
|
$lastIsDivider = false;
|
|
|
|
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
|
|
|
|
foreach ($actions as $action) {
|
|
|
|
if (count($action)) {
|
|
|
|
if (count($action) == 2) {
|
|
|
|
$action[] = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
list($value, $url, $visible) = $action;
|
|
|
|
if ($visible($model)) {
|
|
|
|
$str .= "<li><a href=\"{$url($model)}\">{$value}</a></li>";
|
|
|
|
$lastIsDivider = false;
|
|
|
|
}
|
|
|
|
} elseif ( ! $lastIsDivider) {
|
|
|
|
$str .= "<li class=\"divider\"></li>";
|
|
|
|
$lastIsDivider = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $lastIsDivider) {
|
|
|
|
$str .= "<li class=\"divider\"></li>";
|
|
|
|
}
|
|
|
|
|
2015-11-21 22:10:26 +01:00
|
|
|
if ($entityType != ENTITY_USER || $model->public_id) {
|
2015-11-29 17:00:50 +01:00
|
|
|
$str .= "<li><a href=\"javascript:archiveEntity({$model->public_id})\">"
|
|
|
|
. trans("texts.archive_{$entityType}") . "</a></li>";
|
2015-11-21 22:10:26 +01:00
|
|
|
}
|
2015-11-05 23:37:04 +01:00
|
|
|
} else {
|
2015-11-29 17:00:50 +01:00
|
|
|
$str .= "<li><a href=\"javascript:restoreEntity({$model->public_id})\">"
|
|
|
|
. trans("texts.restore_{$entityType}") . "</a></li>";
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (property_exists($model, 'is_deleted') && !$model->is_deleted) {
|
2015-11-29 17:00:50 +01:00
|
|
|
$str .= "<li><a href=\"javascript:deleteEntity({$model->public_id})\">"
|
|
|
|
. trans("texts.delete_{$entityType}") . "</a></li>";
|
2015-11-05 23:37:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 16:54:24 +01:00
|
|
|
return $str.'</ul></div></center>';
|
2015-11-05 23:37:04 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|