Added organisation members table & associated table column data
This commit is contained in:
@@ -24,12 +24,15 @@ return [
|
||||
|
||||
'EDIT' => 'Edit organistion',
|
||||
'UPDATE' => 'Details updated for organistion <strong>{{name}}</strong>',
|
||||
|
||||
|
||||
'DELETE' => 'Delete organisation',
|
||||
'DELETE_CONFIRM' => 'Are you sure you want to delete the organisation <strong>{{name}}</strong>?',
|
||||
'DELETE_YES' => 'Yes, delete organisation',
|
||||
'DELETION_SUCCESSFUL' => 'Successfully deleted organisation <strong>{{name}}</strong>',
|
||||
|
||||
'TOTAL_MEMBER_COUNT' => '# Members <sub>(inc admins)</sub>',
|
||||
'ADMIN_COUNT' => '# Admins',
|
||||
|
||||
'NAME' => [
|
||||
1 => 'Organisation name',
|
||||
|
||||
|
||||
58
src/Database/Migrations/v001/OrganisationMembersTable.php
Normal file
58
src/Database/Migrations/v001/OrganisationMembersTable.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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\Migrations\v001;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use UserFrosting\Sprinkle\Organisations\Database\Models\Organisation;
|
||||
use UserFrosting\Sprinkle\Organisations\Database\Models\OrganisationMember;
|
||||
use UserFrosting\Sprinkle\Core\Database\Migration;
|
||||
use UserFrosting\Sprinkle\Core\Facades\Seeder;
|
||||
|
||||
/**
|
||||
* Organisation members table migration
|
||||
* "Organisations" are intended to replace groups by providing logical grouping where users may be in none or more organisations.
|
||||
* The organisation members table is used to hold the many-to-many mapping as well as indicate if a user has admin privileges over the organisation.
|
||||
* Version 1.0.0.
|
||||
*
|
||||
* @author Craig Williams (https://avsdev.uk)
|
||||
*/
|
||||
class OrganisationMembersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (!$this->schema->hasTable('organisation_members')) {
|
||||
$this->schema->create('organisation_members', function (Blueprint $table) {
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->integer('organisation_id')->unsigned();
|
||||
$table->boolean('flag_admin');
|
||||
$table->timestamps();
|
||||
|
||||
$table->engine = 'InnoDB';
|
||||
$table->collation = 'utf8_unicode_ci';
|
||||
$table->charset = 'utf8';
|
||||
$table->foreign('user_id')->references('id')->on('users');
|
||||
$table->foreign('organisation_id')->references('id')->on('organisations');
|
||||
$table->index('user_id');
|
||||
$table->index('organisation_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->schema->drop('organisation_members');
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace UserFrosting\Sprinkle\Organisations\Database\Models;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as DB;
|
||||
use UserFrosting\Sprinkle\Core\Database\Models\Model;
|
||||
|
||||
/**
|
||||
@@ -53,8 +54,123 @@ class Organisation extends Model
|
||||
'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)
|
||||
{
|
||||
$memberCounts = DB::table('organisation_members')
|
||||
->selectRaw('organisation_id, count(*) as member_count')
|
||||
->groupBy('organisation_id');
|
||||
|
||||
$adminCounts = DB::table('organisation_members')
|
||||
->selectRaw('organisation_id, count(*) as admin_count')
|
||||
->where('flag_admin', true)
|
||||
->groupBy('organisation_id');
|
||||
|
||||
$nonAdminCounts = DB::table('organisation_members')
|
||||
->selectRaw('organisation_id, count(*) as non_admin_count')
|
||||
->where('flag_admin', false)
|
||||
->groupBy('organisation_id');
|
||||
|
||||
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');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,15 @@ class OrganisationSprunje extends Sprunje
|
||||
protected $sortable = [
|
||||
'name',
|
||||
'description',
|
||||
'member_count',
|
||||
'admin_count'
|
||||
];
|
||||
|
||||
protected $filterable = [
|
||||
'name',
|
||||
'description',
|
||||
'member_count',
|
||||
'admin_count'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -37,6 +41,6 @@ class OrganisationSprunje extends Sprunje
|
||||
*/
|
||||
protected function baseQuery()
|
||||
{
|
||||
return $this->classMapper->createInstance('organisation')->newQuery();
|
||||
return $this->classMapper->createInstance('organisation')->newQuery()->joinMemberCounts();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
<tr>
|
||||
<th class="sorter-metatext" data-column-name="name" data-column-template="#organisation-table-column-info" data-priority="1">{{translate('ORGANISATION')}} <i class="fas fa-sort"></i></th>
|
||||
<th class="sorter-metatext" data-column-name="description" data-column-template="#organisation-table-column-description" data-priority="2">{{translate("DESCRIPTION")}} <i class="fas fa-sort"></i></th>
|
||||
<th class="sorter-metanum" data-column-name="member_count" data-column-template="#organisation-table-column-memberCount" data-priority="2">{{translate("ORGANISATION.TOTAL_MEMBER_COUNT")}} <i class="fas fa-sort"></i></th>
|
||||
<th class="sorter-metanum" data-column-name="admin_count" data-column-template="#organisation-table-column-adminCount" data-priority="2">{{translate("ORGANISATION.ADMIN_COUNT")}} <i class="fas fa-sort"></i></th>
|
||||
<th data-column-template="#organisation-table-column-actions" data-sorter="false" data-filter="false" data-priority="1">{{translate("ACTIONS")}}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
@@ -43,6 +45,18 @@
|
||||
</td>
|
||||
</script>
|
||||
|
||||
<script id="organisation-table-column-memberCount" type="text/x-handlebars-template">
|
||||
<td>
|
||||
{{row.member_count}}
|
||||
</td>
|
||||
</script>
|
||||
|
||||
<script id="organisation-table-column-adminCount" type="text/x-handlebars-template">
|
||||
<td>
|
||||
{{row.admin_count}}
|
||||
</td>
|
||||
</script>
|
||||
|
||||
<script id="organisation-table-column-actions" type="text/x-handlebars-template">
|
||||
<td>
|
||||
<div class="btn-group">
|
||||
|
||||
Reference in New Issue
Block a user