2017-11-30 08:01:07 +08:00
|
|
|
<?php
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
namespace App\Repositories;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
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;
|
2020-08-12 05:48:51 +08:00
|
|
|
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;
|
2020-08-12 05:48:51 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2017-11-30 08:01:07 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class UserRepository extends Repository
|
2017-11-30 08:01:07 +08:00
|
|
|
{
|
|
|
|
protected $fieldSearchable = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'name' => 'like',
|
2017-12-02 00:53:33 +08:00
|
|
|
'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
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-11-30 08:01:07 +08:00
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return User::class;
|
|
|
|
}
|
2017-12-23 04:48:15 +08:00
|
|
|
|
2020-08-12 05:48:51 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2020-08-12 05:48:51 +08:00
|
|
|
*
|
|
|
|
* @return \App\Models\UserField[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
|
|
|
|
*/
|
2021-08-31 00:55:33 +08:00
|
|
|
public function getUserFields(User $user, $only_public_fields = null): Collection
|
2020-08-12 05:48:51 +08:00
|
|
|
{
|
2021-08-31 00:55:33 +08:00
|
|
|
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) {
|
2020-08-12 05:48:51 +08:00
|
|
|
foreach ($user->fields as $userFieldValue) {
|
|
|
|
if ($userFieldValue->field->slug === $field->slug) {
|
|
|
|
$field->value = $userFieldValue->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-23 04:48:15 +08:00
|
|
|
/**
|
|
|
|
* Number of PIREPs that are pending
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 04:48:15 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getPendingCount()
|
|
|
|
{
|
|
|
|
$where = [
|
2017-12-31 10:40:32 +08:00
|
|
|
'state' => UserState::PENDING,
|
2017-12-23 04:48:15 +08:00
|
|
|
];
|
|
|
|
|
2020-08-14 05:44:41 +08:00
|
|
|
return $this->orderBy('created_at', 'desc')
|
2018-03-20 09:50:40 +08:00
|
|
|
->findWhere($where, ['id'])
|
|
|
|
->count();
|
2017-12-23 04:48:15 +08:00
|
|
|
}
|
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
|
2018-03-20 09:50:40 +08:00
|
|
|
* @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 = [];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
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));
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-23 05:11:27 +08:00
|
|
|
return $this;
|
|
|
|
}
|
2017-11-30 08:01:07 +08:00
|
|
|
}
|