invoiceninja/app/Filters/UserFilters.php
2023-11-02 12:06:34 +11:00

172 lines
4.4 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Filters;
use Illuminate\Database\Eloquent\Builder;
/**
* UserFilters.
*/
class UserFilters extends QueryFilters
{
/**
* Filter based on search text.
*
* @param string $filter
* @return Builder
* @deprecated
*/
public function filter(string $filter = ''): Builder
{
if (strlen($filter) == 0) {
return $this->builder;
}
return $this->builder->where(function ($query) use ($filter) {
$query->where('first_name', 'like', '%'.$filter.'%')
->orWhere('last_name', 'like', '%'.$filter.'%')
->orWhere('email', 'like', '%'.$filter.'%')
->orWhere('signature', 'like', '%'.$filter.'%');
});
}
/**
* Sorts the list based on $sort.
*
* @param string $sort formatted as column|asc
* @return Builder
*/
public function sort(string $sort = ''): Builder
{
$sort_col = explode('|', $sort);
if (!is_array($sort_col) || count($sort_col) != 2) {
return $this->builder;
}
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
/**
* Filters the query by the users company ID.
*
* @return Builder
*/
public function entityFilter()
{
return $this->builder->whereHas('company_users', function ($q) {
$q->where('company_id', '=', auth()->user()->company()->id);
});
}
/**
* Filters users that have been removed from the
* company, but not deleted from the system.
*
* @return void
*/
public function hideRemovedUsers()
{
return $this->builder->whereHas('company_users', function ($q) {
$q->where('company_id', '=', auth()->user()->company()->id)->whereNull('deleted_at');
});
}
/**
* Overrides the base with() function as no company ID
* exists on the user table
*
* @param string $value Hashed ID of the user to return back in the dataset
*
* @return Builder
*/
public function with(string $value = ''): Builder
{
if (strlen($value) == 0) {
return $this->builder;
}
return $this->builder
->orWhere($this->with_property, $value)
->orderByRaw("{$this->with_property} = ? DESC", [$value])
->where('account_id', auth()->user()->account_id);
}
public function sending_users(string $value = ''): Builder
{
if (strlen($value) == 0 || $value != 'true') {
return $this->builder;
}
return $this->builder->whereNotNull('oauth_user_refresh_token');
}
/**
* Exclude a list of user_ids, can pass multiple
* user IDs by separating them with a comma.
*
* @param string $user_id
* @return Builder
*/
public function without(string $user_id = ''): Builder
{
if (strlen($user_id) == 0) {
return $this->builder;
}
$user_array = $this->transformKeys(explode(',', $user_id));
return $this->builder->where(function ($query) use ($user_array) {
$query->whereNotIn('id', $user_array);
});
}
/**
* Filters the list based on the status
* archived, active, deleted.
*
* @param string $filter
* @return Builder
*/
public function status(string $filter = ''): Builder
{
if (strlen($filter) == 0) {
return $this->builder;
}
$filters = explode(',', $filter);
return $this->builder->where(function ($query) use ($filters) {
/** @var \App\Models\User $user */
$user = auth()->user();
if (in_array(self::STATUS_ACTIVE, $filters)) {
$query->orWhereNull('deleted_at');
}
if (in_array(self::STATUS_ARCHIVED, $filters)) {
$query->orWhereNotNull('deleted_at')->where('is_deleted', 0);
}
if (in_array(self::STATUS_DELETED, $filters)) {
$query->orWhere('is_deleted', 1);
}
});
}
}