2019-12-04 17:30:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2022-03-09 17:28:23 +01:00
|
|
|
use Illuminate\Database\Query\Builder;
|
2019-12-04 17:30:46 +01:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2022-03-09 17:28:23 +01:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2019-12-04 17:30:46 +01:00
|
|
|
|
|
|
|
|
class Controller extends BaseController
|
|
|
|
|
{
|
2022-03-08 09:42:26 +01:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
use DispatchesJobs;
|
|
|
|
|
use ValidatesRequests;
|
2022-03-09 15:31:35 +01:00
|
|
|
|
2022-03-09 17:28:23 +01:00
|
|
|
/**
|
|
|
|
|
* Controller
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
|
// * Flush poultry search if timed out
|
|
|
|
|
/** @var \Illuminate\Support\Carbon|null $time */
|
|
|
|
|
if ($time = session()->get('poultrySearch.time')) {
|
|
|
|
|
if ($time->lessThan(now()->subMinutes(30))) {
|
|
|
|
|
session()->forget('poultrySearch');
|
|
|
|
|
} else {
|
|
|
|
|
// * Refresh search session
|
|
|
|
|
Session::put('poultrySearch.time', now());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
session()->forget('poultrySearch');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 15:31:35 +01:00
|
|
|
/**
|
|
|
|
|
* Handle search for model.index
|
|
|
|
|
*
|
|
|
|
|
* @param Builder $query The eloquent query builder.
|
2022-03-09 17:28:23 +01:00
|
|
|
* @param string $table The query table string.
|
2022-03-09 15:31:35 +01:00
|
|
|
* @param string $search The query string.
|
|
|
|
|
* @phpcs:ignore Squiz.Commenting.FunctionComment.ParamCommentFullStop
|
|
|
|
|
* @param string|array ...$field Either a simple array or a string.
|
|
|
|
|
* In case of a string the two fields would be concatenate to make the search
|
|
|
|
|
* @return Builder
|
|
|
|
|
*/
|
|
|
|
|
protected function searchQuery(
|
|
|
|
|
Builder $query,
|
2022-03-09 17:28:23 +01:00
|
|
|
string $table,
|
2022-03-09 15:31:35 +01:00
|
|
|
string $search,
|
|
|
|
|
...$field
|
|
|
|
|
): Builder {
|
|
|
|
|
if (count($field)) {
|
|
|
|
|
$query = $query->where(function ($q) use ($field, $search, $table) {
|
|
|
|
|
foreach ($field as $f) {
|
|
|
|
|
$f = str_replace('?', '\?', $f);
|
|
|
|
|
if (is_array($f)) {
|
|
|
|
|
$q->orWhereRaw(
|
|
|
|
|
sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) {
|
|
|
|
|
return "`$table`.`$q`";
|
|
|
|
|
})->implode(', \' \', ')),
|
|
|
|
|
["%$search%"]
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$q->orWhereRaw("UPPER(`$table`.`$f`) LIKE UPPER(?)", ["%$search%"]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
2019-12-04 17:30:46 +01:00
|
|
|
}
|