247 lines
9.3 KiB
PHP
247 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Models\Ticket;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Controller extends BaseController
|
|
{
|
|
use AuthorizesRequests;
|
|
use DispatchesJobs;
|
|
use ValidatesRequests;
|
|
|
|
/**
|
|
* Controller
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware(function ($request, $next) {
|
|
$ignore = [
|
|
// * Support Web
|
|
9,
|
|
// * Gen-Game- Web
|
|
15,
|
|
// * Store LDC
|
|
19,
|
|
// * CRM
|
|
27,
|
|
// * LDC - WEB
|
|
55,
|
|
];
|
|
|
|
$ticketsNew = Ticket::where('status', TicketStatusEnum::new);
|
|
$ticketsWaiting = Ticket::where('status', TicketStatusEnum::waiting);
|
|
$ticketsInProgress = Ticket::whereNotIn(
|
|
'status',
|
|
[
|
|
TicketStatusEnum::new,
|
|
TicketStatusEnum::estimateTodo,
|
|
TicketStatusEnum::waiting,
|
|
TicketStatusEnum::production,
|
|
TicketStatusEnum::archived,
|
|
TicketStatusEnum::takenInAccount
|
|
]
|
|
);
|
|
|
|
/** @var \App\Models\User|null */
|
|
$user = Auth::user();
|
|
if (!is_null($user) and !$user->isAdmin()) {
|
|
$ticketsNew = $ticketsNew->where('is_internal', false)->whereNotIn('site_id', $ignore);
|
|
$ticketsWaiting = $ticketsWaiting->where('is_internal', false)->whereNotIn('site_id', $ignore);
|
|
$ticketsInProgress = $ticketsInProgress->where('is_internal', false)->whereNotIn('site_id', $ignore);
|
|
}
|
|
|
|
View::share('ticketsNew', $ticketsNew->count());
|
|
View::share('ticketsWaiting', $ticketsWaiting->count());
|
|
View::share('ticketsInProgress', $ticketsInProgress->count());
|
|
|
|
return $next($request);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Build Search query on specified fields
|
|
* splitting search into words or using whole sentence
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query The eloquent query builder.
|
|
* @param string $search The query string.
|
|
* @param callable $searchQuery The query string.
|
|
* @param string ...$fields Either a simple array or a string
|
|
* in case of a string the two fields
|
|
* would be concatenate to make the search.
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
protected function searchQuery(
|
|
Builder $query,
|
|
string $search,
|
|
callable $searchQuery = null,
|
|
string ...$fields
|
|
): Builder {
|
|
if (count($fields)) {
|
|
// Search using words.
|
|
$words = explode(' ', $search);
|
|
$query->where(function ($query) use ($searchQuery, $words, $search, $fields) {
|
|
foreach ($words as $word) {
|
|
if (!strlen($word)) {
|
|
continue;
|
|
}
|
|
$this->searchOnFields($query, $word, $fields);
|
|
} //end foreach
|
|
// Search whole using whole sentence.
|
|
$this->searchOnFields($query, $search, $fields);
|
|
if ($searchQuery) {
|
|
$searchQuery($query);
|
|
}
|
|
});
|
|
} //end if
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Build query search on all fields
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param string $search
|
|
* @param array $fields
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
private function searchOnFields(
|
|
Builder $query,
|
|
string $search,
|
|
array $fields
|
|
): \Illuminate\Database\Eloquent\Builder {
|
|
$table = $query->getModel()->getTable();
|
|
foreach ($fields as $field) {
|
|
$field = str_replace('?', '\?', $field);
|
|
if (is_array($field)) {
|
|
$query->orWhereRaw(
|
|
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($field)->map(function ($q) use ($table) {
|
|
return "`$table`.`$q`";
|
|
})->implode(', \' \', ')),
|
|
['%' . htmlspecialchars($search) . '%']
|
|
);
|
|
return $query;
|
|
}
|
|
// Get models woth enum's label.
|
|
if (is_array($this->getSearchValue($query, $search, $field))) {
|
|
foreach ($this->getSearchValue($query, $search, $field) as $array) {
|
|
$query->orWhereRaw(
|
|
"UPPER(`$table`.`$field`) LIKE UPPER(?)",
|
|
['%' . htmlspecialchars($array->value) . '%']
|
|
);
|
|
}
|
|
return $query;
|
|
}
|
|
// Get models with relation's name/label.
|
|
if (Str::endsWith($field, '_id')) {
|
|
$query->orWhereHas(Str::remove('_id', $field), function ($queryRelation) use ($field, $search) {
|
|
if (Schema::hasColumn(Str::remove('_id', $field) . 's', 'name')) {
|
|
$queryRelation->where('name', 'like', '%' . $search . '%');
|
|
} elseif (Schema::hasColumn(Str::remove('_id', $field) . 's', 'label')) {
|
|
$queryRelation->where('label', 'like', '%' . $search . '%');
|
|
}
|
|
});
|
|
return $query;
|
|
}
|
|
$query->orWhereRaw(
|
|
"UPPER(`$table`.`$field`) LIKE UPPER(?)",
|
|
['%' . htmlspecialchars($this->getSearchValue($query, $search, $field)) . '%']
|
|
);
|
|
} //end foreach
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Get the search value (on string or Enum).
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param string $search
|
|
* @param string $field
|
|
* @return string|array
|
|
*/
|
|
protected function getSearchValue(Builder $query, string $search, string $field): string|array
|
|
{
|
|
$cast = $query->getModel()->getCasts()[$field] ?? null;
|
|
if (
|
|
count($query->getModel()->getCasts()) and // Check if the model has casts.
|
|
$cast and // Check if there is cast about specific field.
|
|
enum_exists($cast) // Check if the cast is an enum.
|
|
) {
|
|
/** @var \App\Traits\Enums $cast */
|
|
// @phpstan-ignore-next-line
|
|
$cases = collect($cast::toArray());
|
|
$enum = $cases->filter(function ($item) use ($search) {
|
|
return preg_match('/' . Str::lower($search) . '/', Str::lower($item->label));
|
|
});
|
|
return $enum->toArray();
|
|
} else {
|
|
return $search;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sort columns with a query
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param array $ignore
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
protected function sortQuery(Builder $query, array $ignore = []): Builder
|
|
{
|
|
$rName = request()->route()->getName();
|
|
$table = $query->getModel()->getTable();
|
|
if (request()->sort_col) {
|
|
Session::put("$rName.sort_col", request()->sort_col);
|
|
Session::put("$rName.sorted", true);
|
|
}
|
|
if (request()->sort_way) {
|
|
Session::put("$rName.sort_way", request()->sort_way);
|
|
Session::put("$rName.sorted", true);
|
|
}
|
|
if (request()->rst) {
|
|
Session::remove("$rName.sorted");
|
|
Session::remove("$rName.sort_col");
|
|
Session::remove("$rName.sort_way");
|
|
}
|
|
if (
|
|
request()->sort_col &&
|
|
request()->sort_way &&
|
|
Schema::hasColumn($table, request()->sort_col) &&
|
|
(!isset($ignore) || (isset($ignore) && !in_array(request()->sort_col, $ignore)))
|
|
) {
|
|
$query = $query->orderBy("$table." . request()->sort_col, request()->sort_way);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* Customize pagination with cache or config (default).
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param integer|null $pagination
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator
|
|
*/
|
|
protected function customPaginate(Builder $query, $pagination): LengthAwarePaginator
|
|
{
|
|
if ($pagination) {
|
|
Cache::put('pagination', $pagination);
|
|
};
|
|
$query = $query->paginate(
|
|
(Cache::get('pagination')) ? intval(Cache::get('pagination')) : config('pagination.default')
|
|
);
|
|
return $query;
|
|
}
|
|
}
|