Container.php 2.4 KB

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