Files

218 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2026-01-24 14:03:11 +00:00
<?php
namespace App\DB\Te;
use App\DB\Model;
use App\Excel\TableExport;
use App\User;
use Carbon\Carbon;
use Hekmatinasser\Verta\Verta;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\Request;
class TableEngine
{
/////////
protected $max = 5000, $cols, $type, $selection, $selectionHidden;
/** @var Search */
protected $search;
/** @var Scope */
protected $scope;
/** @var Model */
protected $query;
/** @var TableEngineInput */
protected $input;
/**
* TableEngine constructor.
* @param $filters
*/
public function __construct(&$query, $inp, $i18nPrefix)
{
$this->query = $query;
$this->i18nPrefix = $i18nPrefix;
$this->input = TableEngineInput::create($inp);
$this->filterQuery();
}
public static function create(&$query, $inp, $i18nPrefix = '')
{
return new TableEngine($query, $inp, $i18nPrefix);
}
public static function viewPrint($id)
{
view()->addNamespace('TE', app_path('DB/Te'));
$json = json_decode(Storage::disk('public')->get($id . '.json', ""));
return view('TE::views.print', ['records' => $json->records, 'meta' => $json->meta, 'Arr' => Arr::class]);
}
public function initSearch($cols)
{
if ($this->input->hasSearch()) {
Search::query($this->query, $cols, $this->input->search);
}
return $this;
}
public function hidden($cols)
{
$this->selection = $cols;
$this->selectionHidden = true;
return $this;
}
public function select($cols)
{
$this->selection = $cols;
return $this;
}
public function batchOperation($func)
{
if ($this->input->hasSelectCommand() && $this->input->hasSelectItems()) {
call_user_func($func,
(object)['command' => $this->input->selectCommand,
'items' => $this->input->selectItems]
);
}
return $this;
}
public function execute()
{
$this->query->get();
return $this->query;
}
public function total()
{
$this->query->get();
return $this->query;
}
public function export(&$export = null)
{
$ret = [
'query' => [
'limit' => $this->input->limit,
'filters' => $this->input->filters,
'filtersOpt' => $this->input->filtersOpt,
'search' => $this->input->search,
'key' => $this->input->key,
'select' => $this->input->columns,
'total' => $this->query->count(),
'sort' => $this->input->sort,
'page' => $this->input->page
]
];
$this->sortQuery();
if (!($this->input->op == 'excel' || $this->input->op == 'json' || $this->input->op == 'print')) {
$this->query
->limit($this->input->limit)
->offset(($this->input->page - 1) * $this->input->limit);
}
if (is_null($this->selectionHidden)) {
$ret['rows'] = $this->query->select($this->selection)->get();
} else {
$e = $this->query->get();
foreach ($e as $model) {
$model->addHidden($this->selection);
};
$ret['rows'] = $e->toArray();
}
if ($this->input->op == 'excel' || $this->input->op == 'json' || $this->input->op == 'print') {
if ($this->input->op == 'excel') {
$time = md5(time()) . '.xlsx';
$ret['url'] = url('storage/excel/' . $time);
Excel::store(new ExcelCollection($ret['rows'], $this->input->getShowColumns($this->i18nPrefix)), 'excel/' . $time);
} else if ($this->input->op == 'json' || $this->input->op == 'print') {
$json = new ExcelCollection($ret['rows'], $this->input->getShowColumns($this->i18nPrefix));
$time = md5(time()) . '.json';
$exp = [
'records' => $json->collection()->toArray(),
'meta' => [
'title' => $this->input->reportTitle,
'columns' => $this->input->columnTitles,
'created_at' => Verta::now()->formatJalaliDatetime()
]
];
Storage::disk('public')->put($time, json_encode($exp));
if ($this->input->op == 'print') {
$ret['url'] = url('site/te/print/' . Str::before($time, '.json'));
} else {
$ret['url'] = url('storage/' . $time);
}
}
unset($ret['rows']);
}
if (is_null($export)) {
return $ret;
}
$export = $ret;
}
protected function sortQuery()
{
if ($this->input->hasSort()) {
$r = explode('.', $this->input->sort->key);
if (sizeof($r) > 1) {
$inp = $this->input->sort->order;
$this->query->with([$r[0] => function ($q) use ($r, $inp) {
return $q->orderBy($r[1], ($this->input->sort->order == 'descending') ? 'DESC' : 'ASC');
}]);
} else {
$this->query->orderBy(
$this->input->sort->key,
($this->input->sort->order == 'descending') ? 'DESC' : 'ASC');
}
} else {
$this->query->orderBy('id', 'DESC');
}
}
protected function filterQuery()
{
if ($this->input->hasFilters()) {
$this->query->where(function ($q) {
return Scope::init($q, $this->input->filters)->run();
});
}
}
protected function getWhere($op)
{
return Arr::get(['or' => 'orWhere', 'and' => 'where'], $op, 'where');
}
}