195 lines
5.8 KiB
PHP
195 lines
5.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* AVSDev UF Organisations (https://avsdev.uk)
|
|
*
|
|
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
|
|
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
|
|
*/
|
|
|
|
namespace UserFrosting\Sprinkle\Organisations\Database\Models;
|
|
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
|
|
|
/**
|
|
* Organisation Class.
|
|
*
|
|
* Represents a organisation object as stored in the database.
|
|
*
|
|
* @author Craig Williams (https://avsdev.uk)
|
|
*
|
|
* @property string $slug
|
|
* @property string $name
|
|
* @property string $description
|
|
* @property timestamp $created_at
|
|
* @property timestamp $updated_at
|
|
* @property timestamp $deleted_at
|
|
*/
|
|
class Organisation extends Model
|
|
{
|
|
/**
|
|
* @var string The name of the table for the current model.
|
|
*/
|
|
protected $table = 'organisations';
|
|
|
|
/**
|
|
* Fields that should be mass-assignable when creating a new Organisation.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'slug',
|
|
'name',
|
|
'description',
|
|
'deleted_at',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $dates = [
|
|
'deleted_at',
|
|
];
|
|
|
|
/**
|
|
* The accessors to append to the model's array form.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $appends = [
|
|
'member_count',
|
|
'admin_count',
|
|
'non_admin_count'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden from toArray/toJson.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'organisation_id'
|
|
];
|
|
|
|
/**
|
|
* @var bool Enable timestamps for this class.
|
|
*/
|
|
public $timestamps = true;
|
|
|
|
|
|
/**
|
|
* Get a count of members within this organisation (includes admins).
|
|
*/
|
|
public function getMemberCountAttribute()
|
|
{
|
|
return $this->members()->count();
|
|
}
|
|
|
|
/**
|
|
* Get a count of administrators within this organisation.
|
|
*/
|
|
public function getAdminCountAttribute()
|
|
{
|
|
return $this->administrators()->count();
|
|
}
|
|
|
|
/**
|
|
* Get a count of members within this organisation (excludes admins).
|
|
*/
|
|
public function getNonAdminCountAttribute()
|
|
{
|
|
return $this->members(true)->count();
|
|
}
|
|
|
|
/**
|
|
* Get a list of members within this organisation.
|
|
*/
|
|
public function members($exclude_admins = false)
|
|
{
|
|
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
|
$classMapper = static::$ci->classMapper;
|
|
|
|
$qry = $this
|
|
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
|
|
->withTimestamps();
|
|
|
|
if ($exclude_admins) {
|
|
return $qry->where('flag_admin', false);
|
|
} else {
|
|
return $qry;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a list of administrators within this organisation.
|
|
*/
|
|
public function administrators()
|
|
{
|
|
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
|
|
$classMapper = static::$ci->classMapper;
|
|
|
|
return $this
|
|
->belongsToMany(
|
|
$classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id'
|
|
)
|
|
->where('flag_admin', true)
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Joins the organisation's member count, so we can do things like sort, search, paginate, etc.
|
|
*
|
|
* @param Builder $query
|
|
*
|
|
* @return Builder
|
|
*/
|
|
public function scopeJoinMemberCounts($query)
|
|
{
|
|
$memberCountsInner = DB::table('organisation_members')
|
|
->selectRaw('organisation_id, COUNT(*) AS member_count')
|
|
->groupBy('organisation_id');
|
|
$memberCounts = DB::table('organisations')
|
|
->leftJoinSub($memberCountsInner, 'member_counts_inner', function ($join) {
|
|
$join->on('member_counts_inner.organisation_id', '=', 'organisations.id');
|
|
})
|
|
->select('id AS organisation_id')
|
|
->selectRaw('COALESCE(member_count, 0) AS member_count');
|
|
|
|
$adminCountsInner = DB::table('organisation_members')
|
|
->selectRaw('organisation_id, COUNT(*) AS admin_count')
|
|
->where('flag_admin', true)
|
|
->groupBy('organisation_id');
|
|
$adminCounts = DB::table('organisations')
|
|
->leftJoinSub($adminCountsInner, 'admin_counts_inner', function ($join) {
|
|
$join->on('admin_counts_inner.organisation_id', '=', 'organisations.id');
|
|
})
|
|
->select('id AS organisation_id')
|
|
->selectRaw('COALESCE(admin_count, 0) AS admin_count');
|
|
|
|
$nonAdminCountsInner = DB::table('organisation_members')
|
|
->selectRaw('organisation_id, COUNT(*) AS non_admin_count')
|
|
->where('flag_admin', false)
|
|
->groupBy('organisation_id');
|
|
$nonAdminCounts = DB::table('organisations')
|
|
->leftJoinSub($nonAdminCountsInner, 'non_admin_counts_inner', function ($join) {
|
|
$join->on('non_admin_counts_inner.organisation_id', '=', 'organisations.id');
|
|
})
|
|
->select('id AS organisation_id')
|
|
->selectRaw('COALESCE(non_admin_count, 0) AS non_admin_count');
|
|
|
|
return $query
|
|
->leftJoinSub($memberCounts, 'member_counts', function ($join) {
|
|
$join->on('member_counts.organisation_id', '=', 'organisations.id');
|
|
})
|
|
->leftJoinSub($adminCounts, 'admin_counts', function ($join) {
|
|
$join->on('admin_counts.organisation_id', '=', 'organisations.id');
|
|
})
|
|
->leftJoinSub($nonAdminCounts, 'non_admin_counts', function ($join) {
|
|
$join->on('non_admin_counts.organisation_id', '=', 'organisations.id');
|
|
});
|
|
}
|
|
}
|