Container.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. 'created_at' => 'datetime:Y-m-d H:i:s',
  22. 'updated_at' => 'datetime:Y-m-d H:i:s',
  23. ];
  24. protected static function booted(): void
  25. {
  26. static::addGlobalScope(new CompanyScope);
  27. }
  28. public function scopeAllowed(Builder $query, string $id = null)
  29. {
  30. $query->where(function (Builder $query) {
  31. return $query->where('acl', ContainerACL::PRIVATE->value)->where('created_by', Auth::id());
  32. })->orWhere(function (Builder $query) {
  33. return $query->where('acl', ContainerACL::CUSTOM->value)->where('whitelist', 'like', '%,' . Auth::id() . ',%');
  34. });
  35. }
  36. public function library(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  37. {
  38. return $this->belongsTo(Library::class, "library_id");
  39. }
  40. public function namingRule(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  41. {
  42. return $this->belongsTo(NamingRule::class);
  43. }
  44. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  45. {
  46. return $this->belongsTo(User::class, 'created_by');
  47. }
  48. public function content(int $version = 0): \Illuminate\Database\Eloquent\Relations\HasOne
  49. {
  50. $version = $version > 0 ? $version : $this->version;
  51. return $this->hasOne(ContainerContent::class, "container_id")->where("version", $version);
  52. }
  53. }