mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-02 09:04:35 -04:00
* Adopt Laravel coding style The Laravel framework adopts the PSR-2 coding style with some additions. Laravel apps *should* adopt this coding style as well. However, Shift allows you to customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config to your project. You may use [Shift's .php_cs][2] file as a base. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200 * Shift bindings PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser. * Shift core files * Shift to Throwable * Add laravel/ui dependency * Unindent vendor mail templates * Shift config files * Default config files In an effort to make upgrading the constantly changing config files easier, Shift defaulted them so you can review the commit diff for changes. Moving forward, you should use ENV variables or create a separate config file to allow the core config files to remain automatically upgradeable. * Shift Laravel dependencies * Shift cleanup * Upgrade to Laravel 7 Co-authored-by: Laravel Shift <shift@laravelshift.com>
166 lines
3.3 KiB
PHP
166 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
namespace App\Filters;
|
|
|
|
use App\Models\User;
|
|
//use Illuminate\Database\Query\Builder;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Class QueryFilters.
|
|
*/
|
|
abstract class QueryFilters
|
|
{
|
|
/**
|
|
* active status.
|
|
*/
|
|
const STATUS_ACTIVE = 'active';
|
|
|
|
/**
|
|
* archived status.
|
|
*/
|
|
const STATUS_ARCHIVED = 'archived';
|
|
|
|
/**
|
|
* deleted status.
|
|
*/
|
|
const STATUS_DELETED = 'deleted';
|
|
|
|
/**
|
|
* The request object.
|
|
*
|
|
* @var Request
|
|
*/
|
|
protected $request;
|
|
|
|
/**
|
|
* The builder instance.
|
|
*
|
|
* @var Builder
|
|
*/
|
|
protected $builder;
|
|
|
|
/**
|
|
* Create a new QueryFilters instance.
|
|
*
|
|
* @param Request $request
|
|
*/
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
/**
|
|
* Apply the filters to the builder.
|
|
*
|
|
* @param Builder $builder
|
|
* @return Builder
|
|
*/
|
|
public function apply(Builder $builder)
|
|
{
|
|
$this->builder = $builder;
|
|
|
|
$this->entityFilter();
|
|
|
|
$this->clientFilter();
|
|
|
|
foreach ($this->filters() as $name => $value) {
|
|
if (! method_exists($this, $name)) {
|
|
continue;
|
|
}
|
|
|
|
if (strlen($value)) {
|
|
$this->$name($value);
|
|
} else {
|
|
$this->$name();
|
|
}
|
|
}
|
|
|
|
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
|
|
*/
|
|
public function split($value) : stdClass
|
|
{
|
|
$exploded_array = explode(':', $value);
|
|
|
|
$parts = new stdClass;
|
|
|
|
$parts->value = $exploded_array[0];
|
|
$parts->operator = $this->operatorConvertor($exploded_array[1]);
|
|
|
|
return $parts;
|
|
}
|
|
|
|
/**
|
|
* String to operator convertor.
|
|
*
|
|
* @param string $value
|
|
* @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
|
|
*
|
|
* @param $client_id The client Id
|
|
* @param Illuminate\Database\Query\Builder
|
|
*/
|
|
public function clientFilter()
|
|
{
|
|
if (auth('contact')->user()) {
|
|
return $this->builder->whereClientId(auth('contact')->user()->client->id);
|
|
}
|
|
}
|
|
}
|