AttendanceBookingReason.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Booking;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Delete;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\Metadata\Patch;
  9. use ApiPlatform\Metadata\Post;
  10. use ApiPlatform\Metadata\Put;
  11. use App\Attribute\OrganizationDefaultValue;
  12. use App\Entity\Organization\Organization;
  13. use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. /**
  18. * Motif d'absence ou de retard.
  19. *
  20. * Security :
  21. *
  22. * @see \App\Doctrine\Education\AttendanceBookingReasonExtension.php
  23. */
  24. #[ApiResource(
  25. operations: [
  26. new Get(
  27. security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\') and object.getOrganization().getId() == user.getOrganization().getId()'
  28. ),
  29. new Put(
  30. security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
  31. ),
  32. new Patch(
  33. security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
  34. ),
  35. new Delete(
  36. security: 'is_granted(\'ROLE_ORGANIZATION\') and object.getOrganization().getId() == user.getOrganization().getId()'
  37. ),
  38. new GetCollection(
  39. security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
  40. ),
  41. new Post(
  42. security: 'is_granted(\'ROLE_ORGANIZATION\')'
  43. )]
  44. )]
  45. #[Auditable]
  46. #[ORM\Table(name: 'AttendanceBookingReason')]
  47. #[OrganizationDefaultValue(fieldName: 'organization')]
  48. #[ORM\Entity]
  49. class AttendanceBookingReason
  50. {
  51. #[ORM\Id]
  52. #[ORM\Column]
  53. #[ORM\GeneratedValue]
  54. private ?int $id = null;
  55. #[ORM\ManyToOne(inversedBy: 'attendanceBookingReasons')]
  56. #[ORM\JoinColumn(nullable: false)]
  57. protected Organization $organization;
  58. #[ORM\Column(length: 255, nullable: false)]
  59. private string $reason;
  60. /**
  61. * Pas de cascade remove ici, les absences / présences existent indépendamment de ce motif.
  62. *
  63. * @var Collection<int, AttendanceBooking>
  64. */
  65. #[ORM\OneToMany(targetEntity: AttendanceBooking::class, mappedBy: 'reason', cascade: ['persist'])]
  66. private Collection $attendanceBookings;
  67. public function __construct()
  68. {
  69. $this->attendanceBookings = new ArrayCollection();
  70. }
  71. public function getId(): ?int
  72. {
  73. return $this->id;
  74. }
  75. public function setId(?int $id): self
  76. {
  77. $this->id = $id;
  78. return $this;
  79. }
  80. public function getOrganization(): Organization
  81. {
  82. return $this->organization;
  83. }
  84. public function setOrganization(?Organization $organization): self
  85. {
  86. $this->organization = $organization;
  87. return $this;
  88. }
  89. public function getReason(): string
  90. {
  91. return $this->reason;
  92. }
  93. public function setReason(string $reason): self
  94. {
  95. $this->reason = $reason;
  96. return $this;
  97. }
  98. public function getAttendanceBookings(): Collection
  99. {
  100. return $this->attendanceBookings;
  101. }
  102. public function addAttendanceBookings(AttendanceBooking $attendanceBooking): self
  103. {
  104. if (!$this->attendanceBookings->contains($attendanceBooking)) {
  105. $this->attendanceBookings[] = $attendanceBooking;
  106. $attendanceBooking->setReason($this);
  107. }
  108. return $this;
  109. }
  110. public function removeAttendanceBooking(AttendanceBooking $attendanceBooking): self
  111. {
  112. if ($this->attendanceBookings->removeElement($attendanceBooking)) {
  113. // set the owning side to null (unless already changed)
  114. if ($attendanceBooking->getReason() === $this) {
  115. $attendanceBooking->setReason(null);
  116. }
  117. }
  118. return $this;
  119. }
  120. }