phpvms/app/Repositories/UserRepository.php

109 lines
2.6 KiB
PHP
Raw Normal View History

2017-11-30 08:01:07 +08:00
<?php
2017-11-30 08:01:07 +08:00
namespace App\Repositories;
use App\Contracts\Repository;
2017-12-31 10:40:32 +08:00
use App\Models\Enums\UserState;
2018-02-21 12:33:09 +08:00
use App\Models\User;
use App\Models\UserField;
2017-12-23 05:11:27 +08:00
use App\Repositories\Criteria\WhereCriteria;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
2017-11-30 08:01:07 +08:00
class UserRepository extends Repository
2017-11-30 08:01:07 +08:00
{
protected $fieldSearchable = [
'name' => 'like',
'email' => 'like',
'home_airport_id',
'curr_airport_id',
2018-08-27 00:40:04 +08:00
'state',
2017-11-30 08:01:07 +08:00
];
/**
* @return string
*/
2017-11-30 08:01:07 +08:00
public function model()
{
return User::class;
}
/**
* Get all of the fields which has the mapped values
*
2020-08-14 05:44:41 +08:00
* @param User $user
* @param bool $only_public_fields Only include the user's public fields
*
* @return \App\Models\UserField[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getUserFields(User $user, $only_public_fields = null): Collection
{
if (is_bool($only_public_fields)) {
$fields = UserField::where(['private' => !$only_public_fields])->get();
} else {
$fields = UserField::get();
}
2020-08-14 05:44:41 +08:00
return $fields->map(function ($field, $_) use ($user) {
foreach ($user->fields as $userFieldValue) {
if ($userFieldValue->field->slug === $field->slug) {
$field->value = $userFieldValue->value;
}
}
return $field;
});
}
/**
* Number of PIREPs that are pending
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
public function getPendingCount()
{
$where = [
2017-12-31 10:40:32 +08:00
'state' => UserState::PENDING,
];
2020-08-14 05:44:41 +08:00
return $this->orderBy('created_at', 'desc')
->findWhere($where, ['id'])
->count();
}
2017-12-23 05:11:27 +08:00
/**
* Create the search criteria and return this with the stuff pushed
2018-08-27 00:40:04 +08:00
*
2017-12-23 05:11:27 +08:00
* @param Request $request
* @param bool $only_active
2018-08-27 00:40:04 +08:00
*
2017-12-23 05:11:27 +08:00
* @throws \Prettus\Repository\Exceptions\RepositoryException
2018-08-27 00:40:04 +08:00
*
* @return $this
2017-12-23 05:11:27 +08:00
*/
public function searchCriteria(Request $request, bool $only_active = true)
{
$where = [];
if ($only_active) {
2017-12-31 10:40:32 +08:00
$where['state'] = UserState::ACTIVE;
2017-12-23 05:11:27 +08:00
}
if ($request->filled('name')) {
$where['name'] = $request->name;
}
if ($request->filled('email')) {
$where['email'] = $request->email;
}
2018-01-04 22:14:39 +08:00
if ($request->filled('state')) {
$where['state'] = $request->state;
}
2017-12-23 05:11:27 +08:00
$this->pushCriteria(new WhereCriteria($request, $where));
2017-12-23 05:11:27 +08:00
return $this;
}
2017-11-30 08:01:07 +08:00
}