Container.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Enums\ContainerACL;
  4. use App\Models\Scopes\CompanyScope;
  5. use EloquentFilter\Filterable;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. use Illuminate\Support\Facades\Auth;
  11. class Container extends Model
  12. {
  13. use HasFactory, SoftDeletes, Filterable;
  14. protected $fillable = [
  15. 'name', 'library_id', 'naming_rule_id', 'naming_rules', 'mailto', 'email_subject', 'acl', 'whitelist',
  16. 'doc_stage', 'doc_type'
  17. ];
  18. protected $casts = [
  19. 'mailto' => 'array',
  20. 'naming_rules' => 'array',
  21. ];
  22. protected static function booted(): void
  23. {
  24. static::addGlobalScope(new CompanyScope);
  25. }
  26. public function scopeAllowed(Builder $query, string $id = null)
  27. {
  28. $query->where(function (Builder $query) {
  29. return $query->where('acl', ContainerACL::PRIVATE->value)->where('created_by', Auth::id());
  30. })->orWhere(function (Builder $query) {
  31. return $query->where('acl', ContainerACL::CUSTOM->value)->where('whitelist', 'like', '%,' . Auth::id() . ',%');
  32. });
  33. }
  34. public function library(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  35. {
  36. return $this->belongsTo(Library::class, "library_id");
  37. }
  38. public function namingRule(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  39. {
  40. return $this->belongsTo(NamingRule::class);
  41. }
  42. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  43. {
  44. return $this->belongsTo(User::class, 'created_by');
  45. }
  46. public function content(int $version = 0): \Illuminate\Database\Eloquent\Relations\HasOne
  47. {
  48. $version = $version > 0 ? $version : $this->version;
  49. return $this->hasOne(ContainerContent::class, "container_id")->where("version", $version);
  50. }
  51. }