2022-09-20 21:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use App\Models\Forms\Form;
|
2023-12-02 14:51:08 +01:00
|
|
|
use App\Models\Traits\CachableAttributes;
|
|
|
|
|
use App\Models\Traits\CachesAttributes;
|
2022-09-20 21:59:52 +02:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-07-04 17:21:36 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2022-09-20 21:59:52 +02:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
class Workspace extends Model implements CachableAttributes
|
2022-09-20 21:59:52 +02:00
|
|
|
{
|
2024-02-23 11:54:12 +01:00
|
|
|
use CachesAttributes;
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
public const MAX_FILE_SIZE_FREE = 5000000; // 5 MB
|
|
|
|
|
|
|
|
|
|
public const MAX_FILE_SIZE_PRO = 50000000; // 50 MB
|
|
|
|
|
|
|
|
|
|
public const MAX_DOMAIN_PRO = 1;
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'icon',
|
|
|
|
|
'user_id',
|
2023-11-29 14:53:08 +01:00
|
|
|
'custom_domain',
|
2024-09-24 12:16:20 +02:00
|
|
|
'settings'
|
2022-09-20 21:59:52 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
|
'is_pro',
|
2024-04-15 16:14:21 +02:00
|
|
|
'is_trialing',
|
2024-02-23 11:54:12 +01:00
|
|
|
'is_enterprise',
|
2022-09-20 21:59:52 +02:00
|
|
|
];
|
|
|
|
|
|
2024-09-24 12:16:20 +02:00
|
|
|
protected function casts(): array
|
2024-06-10 16:10:14 +02:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'custom_domains' => 'array',
|
2024-09-24 12:16:20 +02:00
|
|
|
'settings' => 'array'
|
2024-06-10 16:10:14 +02:00
|
|
|
];
|
|
|
|
|
}
|
2023-11-29 14:53:08 +01:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
protected $cachableAttributes = [
|
|
|
|
|
'is_pro',
|
|
|
|
|
'is_enterprise',
|
|
|
|
|
'is_risky',
|
|
|
|
|
'submissions_count',
|
|
|
|
|
'max_file_size',
|
2024-02-23 11:54:12 +01:00
|
|
|
'custom_domain_count',
|
2023-12-02 14:51:08 +01:00
|
|
|
];
|
2022-09-20 21:59:52 +02:00
|
|
|
|
2023-11-01 16:58:10 +01:00
|
|
|
public function getMaxFileSizeAttribute()
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
if (!pricing_enabled()) {
|
2023-11-01 16:58:10 +01:00
|
|
|
return self::MAX_FILE_SIZE_PRO;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('max_file_size', 15 * 60, function (): int {
|
2023-12-02 14:51:08 +01:00
|
|
|
// Return max file size depending on subscription
|
|
|
|
|
foreach ($this->owners as $owner) {
|
|
|
|
|
if ($owner->is_subscribed) {
|
|
|
|
|
if ($license = $owner->activeLicense()) {
|
|
|
|
|
// In case of special License
|
|
|
|
|
return $license->max_file_size;
|
|
|
|
|
}
|
2023-11-01 16:58:10 +01:00
|
|
|
}
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return self::MAX_FILE_SIZE_PRO;
|
2023-11-01 16:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return self::MAX_FILE_SIZE_FREE;
|
|
|
|
|
});
|
2023-11-01 16:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-29 14:53:08 +01:00
|
|
|
public function getCustomDomainCountLimitAttribute()
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
if (!pricing_enabled()) {
|
2023-11-29 14:53:08 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('custom_domain_count', 15 * 60, function (): ?int {
|
2023-12-02 14:51:08 +01:00
|
|
|
foreach ($this->owners as $owner) {
|
|
|
|
|
if ($owner->is_subscribed) {
|
|
|
|
|
if ($license = $owner->activeLicense()) {
|
|
|
|
|
// In case of special License
|
|
|
|
|
return $license->custom_domain_limit_count;
|
|
|
|
|
}
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2023-12-03 15:33:29 +01:00
|
|
|
return self::MAX_DOMAIN_PRO;
|
2023-11-29 14:53:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-02 14:51:08 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getIsProAttribute()
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
if (!pricing_enabled()) {
|
2023-12-02 14:51:08 +01:00
|
|
|
return true; // If no paid plan so TRUE for ALL
|
2023-11-29 14:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('is_pro', 15 * 60, function (): bool {
|
2023-12-02 14:51:08 +01:00
|
|
|
// Make sure at least one owner is pro
|
|
|
|
|
foreach ($this->owners as $owner) {
|
|
|
|
|
if ($owner->is_subscribed) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return false;
|
|
|
|
|
});
|
2023-11-29 14:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 16:14:21 +02:00
|
|
|
public function getIsTrialingAttribute()
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
if (!pricing_enabled()) {
|
2024-04-15 16:14:21 +02:00
|
|
|
return false; // If no paid plan so FALSE for ALL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->remember('is_trialing', 15 * 60, function (): bool {
|
|
|
|
|
// Make sure at least one owner is pro
|
|
|
|
|
foreach ($this->owners as $owner) {
|
|
|
|
|
if ($owner->onTrial()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
public function getIsEnterpriseAttribute()
|
|
|
|
|
{
|
2024-07-04 17:21:36 +02:00
|
|
|
if (!pricing_enabled()) {
|
2023-08-30 09:58:29 +02:00
|
|
|
return true; // If no paid plan so TRUE for ALL
|
|
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('is_enterprise', 15 * 60, function (): bool {
|
2023-12-02 14:51:08 +01:00
|
|
|
// Make sure at least one owner is pro
|
|
|
|
|
foreach ($this->owners as $owner) {
|
|
|
|
|
if ($owner->has_enterprise_subscription) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return false;
|
|
|
|
|
});
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 09:58:29 +02:00
|
|
|
public function getIsRiskyAttribute()
|
|
|
|
|
{
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('is_risky', 15 * 60, function (): bool {
|
2023-12-02 14:51:08 +01:00
|
|
|
// A workspace is risky if all of his users are risky
|
|
|
|
|
foreach ($this->owners as $owner) {
|
2024-06-10 16:10:14 +02:00
|
|
|
if (!$owner->is_risky) {
|
2023-12-02 14:51:08 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-30 09:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return true;
|
|
|
|
|
});
|
2023-08-30 09:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getSubmissionsCountAttribute()
|
|
|
|
|
{
|
2024-02-23 11:54:12 +01:00
|
|
|
return $this->remember('submissions_count', 15 * 60, function (): int {
|
2023-12-02 14:51:08 +01:00
|
|
|
$total = 0;
|
|
|
|
|
foreach ($this->forms as $form) {
|
|
|
|
|
$total += $form->submissions_count;
|
|
|
|
|
}
|
2023-08-30 09:58:29 +02:00
|
|
|
|
2023-12-02 14:51:08 +01:00
|
|
|
return $total;
|
|
|
|
|
});
|
2023-08-30 09:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
/**
|
|
|
|
|
* Relationships
|
|
|
|
|
*/
|
|
|
|
|
public function users()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 17:21:36 +02:00
|
|
|
public function invites()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserInvite::class);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
public function owners()
|
|
|
|
|
{
|
|
|
|
|
return $this->users()->wherePivot('role', 'admin');
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 17:21:36 +02:00
|
|
|
public function billingOwners(): Collection
|
|
|
|
|
{
|
|
|
|
|
return $this->owners->filter(fn ($owner) => $owner->is_subscribed);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
public function forms()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Form::class);
|
|
|
|
|
}
|
2024-12-30 14:35:23 +01:00
|
|
|
|
|
|
|
|
public function isReadonlyUser(?User $user)
|
|
|
|
|
{
|
|
|
|
|
return $user ? $this->users()
|
|
|
|
|
->wherePivot('user_id', $user->id)
|
|
|
|
|
->wherePivot('role', User::ROLE_READONLY)
|
|
|
|
|
->exists() : false;
|
|
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|