286 lines
6.8 KiB
PHP
286 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Traits\Sant\Relationships;
|
|
|
|
use App\Models\Chaturmas;
|
|
use Carbon\Carbon;
|
|
use App\Models\Sant;
|
|
use App\Models\User;
|
|
use App\Models\Thana;
|
|
use App\Models\Vihar;
|
|
use App\Models\Dharma;
|
|
use App\Models\Sampraday;
|
|
use App\Constant\Constant;
|
|
use App\Models\SantFollower;
|
|
use App\Models\SantRelation;
|
|
use App\Models\KarmaPointsTransaction;
|
|
use App\Models\SantLocation;
|
|
use App\Models\SantTemp;
|
|
|
|
/**
|
|
* Trait SantRelationships
|
|
*
|
|
*/
|
|
trait SantRelationships
|
|
{
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function guru()
|
|
{
|
|
return $this->belongsTo(Sant::class, 'guru_id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function vihars()
|
|
{
|
|
return $this->hasMany(Vihar::class, 'sant_id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function dharm()
|
|
{
|
|
return $this->belongsTo(Dharma::class, 'dharma_id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function sampraday()
|
|
{
|
|
return $this->belongsTo(Sampraday::class, 'sampraday_id');
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function santFather()
|
|
{
|
|
return $this->hasMany(SantRelation::class,);
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function father()
|
|
{
|
|
return $this->hasMany(SantRelation::class)->where('type', 'father');
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function mother()
|
|
{
|
|
return $this->hasMany(SantRelation::class)->where('type', 'mother');
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function shishya()
|
|
{
|
|
return $this->hasMany(Sant::class, 'guru_id');
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function maharaj()
|
|
{
|
|
return $this->hasMany(Sant::class, 'guru_id')->where('gender', Constant::MALE);
|
|
}
|
|
|
|
/**
|
|
* Get the santFather associated with the user.
|
|
*/
|
|
public function mahasatiJi()
|
|
{
|
|
return $this->hasMany(Sant::class, 'guru_id')->where('gender', Constant::FEMALE);;
|
|
}
|
|
|
|
/**
|
|
* Get the sant followers.
|
|
*/
|
|
public function followers()
|
|
{
|
|
return $this->belongsToMany(User::class, 'sant_followers', 'sant_id', 'user_id')->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Get the sant followings.
|
|
*/
|
|
public function followings()
|
|
{
|
|
return $this->belongsToMany(User::class, 'sant_followers', 'user_id', 'sant_id')->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function createdBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function updatedBy()
|
|
{
|
|
return $this->belongsTo(User::class, 'updated_by', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function viharsCreatedBy()
|
|
{
|
|
return $this->hasMany(Vihar::class, 'created_by', 'created_by');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function santTemp()
|
|
{
|
|
return $this->belongsTo(SantTemp::class, 'id', 'sant_id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function editedLocation()
|
|
{
|
|
return $this->belongsTo(SantLocation::class, 'id', 'sant_id');
|
|
}
|
|
|
|
public function chaturmas()
|
|
{
|
|
return $this->hasMany(Chaturmas::class, 'sant_id', 'id');
|
|
}
|
|
|
|
public function vihar()
|
|
{
|
|
return $this->hasMany(Vihar::class, 'sant_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function location()
|
|
{
|
|
return $this->hasOne(Vihar::class, 'sant_id', 'id')->where(function ($query) {
|
|
$query->where(function ($subQuery) {
|
|
$subQuery->where('start_date', '=', Carbon::now()->toDateString())
|
|
->where('start_time', '<=', Carbon::now()->subHour(2)->toTimeString());
|
|
})->orWhere(function ($subQuery) {
|
|
$subQuery->where('start_date', '<', Carbon::now()->toDateString())
|
|
->where('start_time', '=', Carbon::now()->toTimeString());
|
|
});
|
|
})->latest('start_time')->latest('start_date');
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function pastLocation()
|
|
{
|
|
return $this->hasOne(Vihar::class, 'sant_id', 'id')->where(function ($query) {
|
|
$query->where(function ($subQuery) {
|
|
$subQuery->where('start_date', '=', Carbon::now()->toDateString())
|
|
->where('start_time', '<=', Carbon::now()->subHour(2)->toTimeString());
|
|
})->orWhere(function ($subQuery) {
|
|
$subQuery->where('start_date', '<', Carbon::now()->toDateString())
|
|
->where('start_time', '>=', '00:00:00');
|
|
});
|
|
})->latest('start_date')->latest('start_time');
|
|
}
|
|
|
|
/**
|
|
* add points for for registration.
|
|
*/
|
|
public function addPoints()
|
|
{
|
|
return $this->morphMany(KarmaPointsTransaction::class, 'pointable');
|
|
}
|
|
|
|
/**
|
|
* Determine if the given team is the current team.
|
|
*
|
|
* @param mixed $team
|
|
* @return bool
|
|
*/
|
|
public function isCurrentThana($thana)
|
|
{
|
|
return $thana->id === $this->currentThana->id;
|
|
}
|
|
|
|
/**
|
|
* Get the current team of the user's context.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function currentThana()
|
|
{
|
|
// if (is_null($this->current_team_id) && $this->id) {
|
|
// $this->switchTeam($this->personalTeam());
|
|
// }
|
|
|
|
return $this->belongsTo(Thana::class, 'current_team_id');
|
|
}
|
|
|
|
/**
|
|
* Get all of the teams the user owns or belongs to.
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
// public function allTeams()
|
|
// {
|
|
// return $this->ownedThana->merge($this->teams)->sortBy('name');
|
|
// }
|
|
|
|
/**
|
|
* Get all of the teams the user owns.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function ownedThana()
|
|
{
|
|
return $this->hasOne(Thana::class);
|
|
}
|
|
|
|
/**
|
|
* Get all of the teams the user belongs to.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
// public function thanas()
|
|
// {
|
|
// return $this->belongsToMany(Thana::class, 'thana_members')
|
|
// ->withTimestamps();
|
|
// }
|
|
|
|
/**
|
|
* Determine if the user belongs to the given team.
|
|
*
|
|
* @param mixed $team
|
|
* @return bool
|
|
*/
|
|
// public function belongsToTeam($team)
|
|
// {
|
|
// if (is_null($team)) {
|
|
// return false;
|
|
// }
|
|
|
|
// return $this->ownsTeam($team) || $this->thanas->contains(function ($t) use ($team) {
|
|
// return $t->id === $team->id;
|
|
// });
|
|
// }
|
|
}
|