Customer names stored with ي/ك were missed when typing ی/ک; fold both sides of LIKE. Co-authored-by: Cursor <cursoragent@cursor.com>
69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\DB\Te;
|
|
|
|
|
|
use App\Support\PersianText;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
|
|
|
class QuerySelector
|
|
{
|
|
public static function run(Request $request)
|
|
{
|
|
|
|
if (!$request->has('action')) {
|
|
throw new BadRequestException();
|
|
}
|
|
$input = $request->all();
|
|
if (!isset($input['query']) && !isset($input['default'])) {
|
|
return response()->json(null);
|
|
}
|
|
$result = QuerySelector::searchQuery($request->all());
|
|
return response()->json($result);
|
|
}
|
|
|
|
public static function searchQuery($input)
|
|
{
|
|
|
|
$conf = Arr::get(config('query'), $input['action'], null);
|
|
// dd($conf);
|
|
if (is_null($conf)) {
|
|
return null;
|
|
}
|
|
$conf = (object)$conf;
|
|
if (isset($conf->fn) && isset($input['query'])) {
|
|
return call_user_func($conf->fn, $input['query'], $input['extra'] ?? null);
|
|
}
|
|
$cols = explode(',', $conf->cols);
|
|
if (isset($input['query'])) {
|
|
$needle = PersianText::likePattern(strtolower((string) $input['query']));
|
|
$query = $conf->model::whereRaw(
|
|
PersianText::sqlFoldExpr('lower(`'.$cols[0].'`)').' like ?',
|
|
[$needle]
|
|
);
|
|
}
|
|
|
|
|
|
if (isset($input['default'])) {
|
|
if (is_array($input['default'])) {
|
|
$query = $conf->model::whereIn('id', $input['default']);
|
|
} else {
|
|
$query = $conf->model::whereId($input['default']);
|
|
}
|
|
}
|
|
if (isset($conf->where)) {
|
|
foreach ($conf->where as $ww) {
|
|
$query->where(...$ww);
|
|
}
|
|
}
|
|
if (isset($conf->select)) {
|
|
$select = (is_callable($conf->select)) ? call_user_func($conf->select) : $conf->select;
|
|
return $query->select($select)->limit(20)->get()->toArray();
|
|
} else {
|
|
return $query->select(array_merge(['id'], $cols))->limit(20)->get()->toArray();
|
|
}
|
|
}
|
|
}
|