Container.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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'
  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. ];
  25. protected static function booted(): void
  26. {
  27. static::addGlobalScope(new CompanyScope);
  28. }
  29. public function scopeAllowed(Builder $query, string $id = null)
  30. {
  31. $query->where(function (Builder $query) {
  32. return $query->where('acl', ContainerACL::PRIVATE->value)->where('created_by', Auth::id());
  33. })->orWhere(function (Builder $query) {
  34. return $query->where('acl', ContainerACL::CUSTOM->value)->where('whitelist', 'like', '%,' . Auth::id() . ',%');
  35. });
  36. }
  37. public function library(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  38. {
  39. return $this->belongsTo(Library::class, "library_id");
  40. }
  41. public function namingRule(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  42. {
  43. return $this->belongsTo(NamingRule::class);
  44. }
  45. public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  46. {
  47. return $this->belongsTo(User::class, 'created_by');
  48. }
  49. public function content(int $version = 0): \Illuminate\Database\Eloquent\Relations\HasOne
  50. {
  51. $version = $version > 0 ? $version : $this->version;
  52. return $this->hasOne(ContainerContent::class, "container_id")->where("version", $version);
  53. }
  54. public function folder()
  55. {
  56. return $this->hasMany(Folder::class, "object_id")->where("object_type", ActionObjectType::CONTAINER->value);
  57. }
  58. public function files()
  59. {
  60. return $this->hasMany(File::class, "object_id")->where("object_type", ActionObjectType::CONTAINER->value);
  61. }
  62. }