128 lines
3.5 KiB
PHP
128 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Str;
|
|
use Stancl\Tenancy\Contracts\Tenant as TenantContract;
|
|
use Stancl\Tenancy\Database\Concerns\HasDomains;
|
|
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
|
|
|
|
/**
|
|
* Settings.
|
|
*
|
|
* @property string $id Tenant id.
|
|
* @property string $theme Tenant theme.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
|
|
*
|
|
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
|
|
*/
|
|
class Tenant extends BaseTenant implements TenantContract
|
|
{
|
|
use HasDomains;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = ['id', 'theme'];
|
|
|
|
/**
|
|
* Define the custom columns (that won't be stored in the data JSON column)
|
|
* by overriding the getCustomColumns() method.
|
|
*
|
|
* @see https://tenancyforlaravel.com/docs/v3/tenants
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getCustomColumns(): array
|
|
{
|
|
return [
|
|
'id',
|
|
'theme',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::assertThemeIsUnique($model->theme);
|
|
$model->id = Str::slug($model->theme);
|
|
});
|
|
static::created(function (self $model) {
|
|
(new Settings())->setDefaultSettings($model);
|
|
(new StaticPage())->setDefaultPages($model);
|
|
self::resetCache();
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::assertThemeIsUnique($model->theme, $model->getKey());
|
|
$model->id = Str::slug($model->theme);
|
|
});
|
|
static::updated(function () {
|
|
self::resetCache();
|
|
});
|
|
static::deleted(function () {
|
|
self::resetCache();
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Asserts using validation that the theme is unique
|
|
*
|
|
* @param string|null $theme
|
|
* @param string|null $id
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException If theme already exists.
|
|
*/
|
|
private static function assertThemeIsUnique(string|null $theme, ?string $id = null): void
|
|
{
|
|
$table = (new self())->getTable();
|
|
ToolboxHelper::assertFieldIsUnique($table, 'theme', $theme, $id);
|
|
ToolboxHelper::assertFieldIsUnique($table, 'id', Str::slug($theme), $id);
|
|
}
|
|
|
|
/**
|
|
* Reset all cache elements related to this model.
|
|
*
|
|
* @return void
|
|
*/
|
|
private static function resetCache(): void
|
|
{
|
|
Cache::forget('tenantsGlobal');
|
|
self::setTenantsCached();
|
|
}
|
|
|
|
/**
|
|
* Set all tenants in cache.
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public static function setTenantsCached(): \Illuminate\Support\Collection
|
|
{
|
|
return Cache::remember('tenantsGlobal', 86400, function () {
|
|
return Tenant::get();
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Has One Relationship Method.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function setting(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(Settings::class);
|
|
}
|
|
}
|