invoiceninja/app/Filters/QueryFilters.php

302 lines
6.8 KiB
PHP
Raw Permalink Normal View History

<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-29 09:21:40 +11:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
namespace App\Filters;
//use Illuminate\Database\Query\Builder;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
2022-12-21 17:41:36 +11:00
use Illuminate\Support\Carbon;
2019-01-27 10:22:57 +11:00
/**
* Class QueryFilters.
2019-01-27 10:22:57 +11:00
*/
abstract class QueryFilters
{
use MakesHash;
2019-01-27 10:22:57 +11:00
/**
* active status.
2019-01-27 10:22:57 +11:00
*/
const STATUS_ACTIVE = 'active';
2019-03-28 21:20:08 +11:00
2019-01-27 10:22:57 +11:00
/**
* archived status.
2019-01-27 10:22:57 +11:00
*/
const STATUS_ARCHIVED = 'archived';
2019-03-28 21:20:08 +11:00
2019-01-27 10:22:57 +11:00
/**
* deleted status.
2019-01-27 10:22:57 +11:00
*/
const STATUS_DELETED = 'deleted';
/**
* The request object.
*
* @var Request
*/
protected $request;
/**
* The builder instance.
*
* @var Builder
*/
protected $builder;
2022-07-27 19:40:39 +02:00
/**
* The "with" filter property column.
2023-02-16 12:36:09 +11:00
*
2022-07-27 19:40:39 +02:00
* var string
*/
protected $with_property = 'id';
/**
* Create a new QueryFilters instance.
*
* @param Request $request
*/
2019-03-28 21:07:45 +11:00
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Apply the filters to the builder.
*
* @param Builder $builder
* @return Builder
*/
2019-03-28 21:07:45 +11:00
public function apply(Builder $builder)
{
2019-03-28 21:07:45 +11:00
$this->builder = $builder;
2019-03-28 21:16:41 +11:00
$this->entityFilter();
2019-03-28 21:20:08 +11:00
$this->clientFilter();
foreach ($this->filters() as $name => $value) {
if (! method_exists($this, $name)) {
continue;
}
2022-05-27 12:17:59 +10:00
if (is_string($value) && strlen($value)) {
$this->$name($value);
} else {
$this->$name();
}
}
2022-01-11 09:31:08 +11:00
// nlog('[Search] SQL: ' . $this->builder->toSql() . " Bindings: " . implode(', ', $this->builder->getBindings()));
2020-01-07 20:59:24 +11:00
return $this->builder->withTrashed();
}
/**
* Get all request filters data.
*
* @return array
*/
public function filters()
{
return $this->request->all();
}
/**
* Explodes the value by delimiter.
*
* @param string $value
* @return stdClass
*/
2020-12-15 20:38:12 +11:00
public function split($value) : \stdClass
{
$exploded_array = explode(':', $value);
2020-12-15 20:38:12 +11:00
$parts = new \stdClass;
$parts->value = $exploded_array[0];
$parts->operator = $this->operatorConvertor($exploded_array[1]);
return $parts;
}
2023-01-16 10:05:44 +11:00
/**
* Filters the list based on the status
* archived, active, deleted.
*
* @param string filter
* @return Builder
*/
2023-01-30 07:08:26 +01:00
public function status(string $filter = ''): Builder
2023-01-16 10:05:44 +11:00
{
if (strlen($filter) == 0) {
return $this->builder;
}
$filters = explode(',', $filter);
return $this->builder->where(function ($query) use ($filters) {
if (in_array(self::STATUS_ACTIVE, $filters)) {
$query->orWhereNull('deleted_at');
}
if (in_array(self::STATUS_ARCHIVED, $filters)) {
$query->orWhere(function ($query) {
2023-02-16 12:36:09 +11:00
$query->whereNotNull('deleted_at')->where('is_deleted', 0);
2023-01-16 10:05:44 +11:00
});
}
if (in_array(self::STATUS_DELETED, $filters)) {
$query->orWhere('is_deleted', 1);
}
});
}
/**
* String to operator convertor.
*
2020-10-28 21:10:49 +11:00
* @param string $operator
* @return string
*/
private function operatorConvertor(string $operator) : string
{
switch ($operator) {
case 'lt':
return '<';
break;
case 'gt':
return '>';
break;
case 'lte':
return '<=';
break;
case 'gte':
return '>=';
break;
case 'eq':
return '=';
break;
default:
return '=';
break;
}
}
/**
* Filters the query by the contact's client_id.
*
* -Can only be used on contact routes
*
2020-10-28 21:10:49 +11:00
* @return
*/
public function clientFilter()
{
if (auth()->guard('contact')->user()) {
2022-10-30 10:28:59 +11:00
return $this->builder->where('client_id', auth()->guard('contact')->user()->client->id);
}
}
2021-05-11 21:04:32 +10:00
2022-12-21 20:04:41 +11:00
public function created_at($value = '')
2021-05-11 21:04:32 +10:00
{
2023-02-16 12:36:09 +11:00
if ($value == '') {
2022-12-21 20:04:41 +11:00
return $this->builder;
2023-02-16 12:36:09 +11:00
}
2022-10-30 10:28:59 +11:00
2023-02-16 12:36:09 +11:00
try {
if (is_numeric($value)) {
2022-12-21 20:04:41 +11:00
$created_at = Carbon::createFromTimestamp((int)$value);
2023-02-16 12:36:09 +11:00
} else {
2022-12-21 20:04:41 +11:00
$created_at = Carbon::parse($value);
}
2022-12-21 17:45:38 +11:00
2022-12-21 20:04:41 +11:00
return $this->builder->where('created_at', '>=', $created_at);
2023-02-16 12:36:09 +11:00
} catch(\Exception $e) {
2022-12-21 20:04:41 +11:00
return $this->builder;
}
2021-05-11 21:04:32 +10:00
}
2021-05-13 20:18:30 +10:00
2023-02-10 09:25:26 +11:00
public function updated_at($value = '')
{
2023-02-16 12:36:09 +11:00
if ($value == '') {
2023-02-10 09:25:26 +11:00
return $this->builder;
2023-02-16 12:36:09 +11:00
}
2023-02-10 09:25:26 +11:00
try {
if (is_numeric($value)) {
$created_at = Carbon::createFromTimestamp((int)$value);
} else {
$created_at = Carbon::parse($value);
}
return $this->builder->where('updated_at', '>=', $created_at);
} catch (\Exception $e) {
return $this->builder;
}
}
2021-05-13 20:18:30 +10:00
public function is_deleted($value)
{
if ($value == 'true') {
2021-10-18 10:25:43 +11:00
return $this->builder->where('is_deleted', $value)->withTrashed();
}
2021-05-13 20:18:30 +10:00
2021-10-17 22:30:48 +11:00
return $this->builder->where('is_deleted', $value);
2021-05-13 20:18:30 +10:00
}
2023-01-30 07:08:26 +01:00
public function client_id(string $client_id = ''): Builder
{
if (strlen($client_id) == 0) {
return $this->builder;
}
2022-01-19 13:14:38 +11:00
return $this->builder->where('client_id', $this->decodePrimaryKey($client_id));
}
2023-01-30 07:08:26 +01:00
public function vendor_id(string $vendor_id = ''): Builder
2023-01-04 00:39:52 +11:00
{
if (strlen($vendor_id) == 0) {
return $this->builder;
}
return $this->builder->where('vendor_id', $this->decodePrimaryKey($vendor_id));
}
2021-05-13 20:18:30 +10:00
public function filter_deleted_clients($value)
{
if ($value == 'true') {
2021-05-13 20:18:30 +10:00
return $this->builder->whereHas('client', function (Builder $query) {
$query->where('is_deleted', 0);
2021-05-13 20:18:30 +10:00
});
}
return $this->builder;
}
2021-10-17 22:30:48 +11:00
public function with_trashed($value)
{
if ($value == 'false') {
2021-10-31 22:21:27 +11:00
return $this->builder->where('is_deleted', 0);
2021-10-17 22:30:48 +11:00
}
return $this->builder;
}
2022-07-27 19:40:39 +02:00
public function with(string $value): Builder
{
return $this->builder
->orWhere($this->with_property, $value)
->orderByRaw("{$this->with_property} = ? DESC", [$value])
->company();
2022-07-27 19:40:39 +02:00
}
}