1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 23:44:27 +02:00

Extract Flarum\Group namespace

This commit is contained in:
Franz Liedke
2017-06-24 14:05:00 +02:00
parent 11bf3e34b7
commit c22219ec20
24 changed files with 69 additions and 71 deletions

135
src/Group/Group.php Executable file
View File

@@ -0,0 +1,135 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Group;
use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Database\AbstractModel;
use Flarum\Group\Event\Created;
use Flarum\Group\Event\Deleting;
use Flarum\Group\Event\Renamed;
/**
* @property int $id
* @property string $name_singular
* @property string $name_plural
* @property string|null $color
* @property string|null $icon
* @property \Illuminate\Database\Eloquent\Collection $users
* @property \Illuminate\Database\Eloquent\Collection $permissions
*/
class Group extends AbstractModel
{
use EventGeneratorTrait;
use ScopeVisibilityTrait;
/**
* {@inheritdoc}
*/
protected $table = 'groups';
/**
* The ID of the administrator group.
*/
const ADMINISTRATOR_ID = 1;
/**
* The ID of the guest group.
*/
const GUEST_ID = 2;
/**
* The ID of the member group.
*/
const MEMBER_ID = 3;
/**
* The ID of the mod group.
*/
const MODERATOR_ID = 4;
/**
* Boot the model.
*
* @return void
*/
public static function boot()
{
parent::boot();
static::deleted(function (Group $group) {
$group->raise(new Deleting($group));
$group->permissions()->delete();
});
}
/**
* Create a new group.
*
* @param string $nameSingular
* @param string $namePlural
* @param string $color
* @param string $icon
* @return static
*/
public static function build($nameSingular, $namePlural, $color, $icon)
{
$group = new static;
$group->name_singular = $nameSingular;
$group->name_plural = $namePlural;
$group->color = $color;
$group->icon = $icon;
$group->raise(new Created($group));
return $group;
}
/**
* Rename the group.
*
* @param string $nameSingular
* @param string $namePlural
* @return $this
*/
public function rename($nameSingular, $namePlural)
{
$this->name_singular = $nameSingular;
$this->name_plural = $namePlural;
$this->raise(new Renamed($this));
return $this;
}
/**
* Define the relationship with the group's users.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany('Flarum\User\User', 'users_groups');
}
/**
* Define the relationship with the group's permissions.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permissions()
{
return $this->hasMany('Flarum\Group\Permission');
}
}