src/Entity/ObjectiveTarget.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ObjectiveTargetRepository;
  4. use App\Traits\DateTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use JMS\Serializer\Annotation as Serializer;
  9. use JMS\Serializer\Annotation\Expose;
  10. use JMS\Serializer\Annotation\Groups;
  11. use JMS\Serializer\Annotation\SerializedName;
  12. use JMS\Serializer\Annotation\VirtualProperty;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. /**
  16. * @ORM\Entity(repositoryClass=ObjectiveTargetRepository::class)
  17. * @ORM\HasLifecycleCallbacks()
  18. * @Serializer\ExclusionPolicy("ALL")
  19. * @UniqueEntity("slug")
  20. */
  21. class ObjectiveTarget
  22. {
  23. public const OBJECTIVE_CA_TYPES_GLOBAL = "objectives_global";
  24. public const OBJECTIVE_CA_TYPES_ROLES = "objectives_roles";
  25. public const OBJECTIVE_CA_TYPES_JOBS = "objectives_jobs";
  26. public const OBJECTIVE_CA_TYPES_INDIVIDUAL = "objectives_individual";
  27. public const OBJECTIVE_CA_TYPES = [
  28. self::OBJECTIVE_CA_TYPES_GLOBAL => "Global",
  29. self::OBJECTIVE_CA_TYPES_ROLES => "Par roles",
  30. self::OBJECTIVE_CA_TYPES_JOBS => "Par jobs",
  31. self::OBJECTIVE_CA_TYPES_INDIVIDUAL => "Par utilisateur",
  32. ];
  33. public const OBJECTIVE_CA_TYPES_ARRAY = [
  34. self::OBJECTIVE_CA_TYPES_INDIVIDUAL,
  35. self::OBJECTIVE_CA_TYPES_JOBS,
  36. self::OBJECTIVE_CA_TYPES_ROLES,
  37. self::OBJECTIVE_CA_TYPES_GLOBAL,
  38. ];
  39. /**
  40. * @ORM\Id
  41. * @ORM\GeneratedValue
  42. * @ORM\Column(type="integer")
  43. *
  44. * @Expose
  45. * @Groups({"objective_target"})
  46. */
  47. private ?int $id = null;
  48. /**
  49. * @ORM\Column(type="string")
  50. *
  51. * @Assert\NotBlank()
  52. * @Assert\Regex(
  53. * pattern = "/^[a-z0-9]+(?:-[a-z0-9]+)*$/",
  54. * message = "Le slug doit contenir uniquement des lettres minuscules, chiffres et tirets."
  55. * )
  56. *
  57. * @Expose
  58. * @Groups({"objective_target", "objective_target_score"})
  59. */
  60. private ?string $slug = null;
  61. /**
  62. * @ORM\Column(type="string")
  63. *
  64. * @Expose
  65. * @Groups({"objective_target", "objective_target_score"})
  66. */
  67. private ?string $label = null;
  68. /**
  69. * @ORM\ManyToOne(targetEntity=User::class, inversedBy="ownerObjectiveTargets")
  70. * @ORM\JoinColumn(nullable=false)
  71. *
  72. * @Expose
  73. * @Groups({"objective_target"})
  74. */
  75. private ?User $owner = null;
  76. /**
  77. * @ORM\ManyToMany(targetEntity=User::class, mappedBy="objectiveTargets")
  78. *
  79. * @Expose
  80. * @Groups({"objective_target"})
  81. */
  82. private Collection $users;
  83. /**
  84. * @ORM\OneToMany(targetEntity=ObjectiveTargetScore::class, mappedBy="objectiveTarget")
  85. */
  86. private Collection $objectiveTargetScores;
  87. /**
  88. * @ORM\Column(type="string", length=255)
  89. *
  90. * @Expose
  91. * @Groups({"objective_target"})
  92. */
  93. private ?string $objectiveType = null;
  94. /**
  95. * @ORM\Column(type="json", nullable=true)
  96. *
  97. * @Expose
  98. * @Groups({"objective_target"})
  99. */
  100. private ?array $applyChoices = null;
  101. /**
  102. * @ORM\Column(type="float")
  103. *
  104. * @Expose
  105. * @Groups({"objective_target", "objective_target_score"})
  106. */
  107. private ?float $completionValue = null;
  108. /**
  109. * @ORM\Column(type="boolean", options={"default":true})
  110. *
  111. * @Expose
  112. * @Groups({"objective_target"})
  113. */
  114. private bool $enabled = true;
  115. /**
  116. * @ORM\Column(type="datetime", nullable=true)
  117. *
  118. * @Expose
  119. * @Groups({"objective_target"})
  120. */
  121. private ?\DateTimeInterface $dateStart = null;
  122. /**
  123. * @ORM\Column(type="datetime", nullable=true)
  124. *
  125. * @Expose
  126. * @Groups({"objective_target"})
  127. */
  128. private ?\DateTimeInterface $dateEnd = null;
  129. /**
  130. * @ORM\OneToMany(targetEntity=ObjectiveTargetStep::class, mappedBy="objectiveTarget", cascade={"persist", "remove"}, orphanRemoval=true)
  131. * @Assert\Valid()
  132. * @ORM\OrderBy({"orderItem" = "ASC"})
  133. */
  134. private Collection $objectiveTargetSteps;
  135. /**
  136. * @ORM\Column(type="boolean", options={"default":true})
  137. *
  138. * @Expose
  139. * @Groups({"objective_target"})
  140. */
  141. private bool $scoredByUser = true;
  142. use DateTrait;
  143. public function __construct()
  144. {
  145. $this->users = new ArrayCollection();
  146. $this->objectiveTargetScores = new ArrayCollection();
  147. $this->objectiveTargetSteps = new ArrayCollection();
  148. }
  149. public function __toString(): string
  150. {
  151. return $this->label ?? '';
  152. }
  153. public function getId(): ?int
  154. {
  155. return $this->id;
  156. }
  157. public function getSlug(): ?string
  158. {
  159. return $this->slug;
  160. }
  161. public function setSlug(string $slug): self
  162. {
  163. $this->slug = $slug;
  164. return $this;
  165. }
  166. public function getLabel(): ?string
  167. {
  168. return $this->label;
  169. }
  170. public function setLabel(string $label): self
  171. {
  172. $this->label = $label;
  173. return $this;
  174. }
  175. public function getOwner(): ?User
  176. {
  177. return $this->owner;
  178. }
  179. public function setOwner(?User $owner): self
  180. {
  181. $this->owner = $owner;
  182. return $this;
  183. }
  184. /**
  185. * @return Collection<int, User>
  186. */
  187. public function getUsers(): Collection
  188. {
  189. return $this->users;
  190. }
  191. public function addUser(User $user): self
  192. {
  193. if (!$this->users->contains($user)) {
  194. $this->users[] = $user;
  195. $user->setObjectiveTarget($this);
  196. }
  197. return $this;
  198. }
  199. public function removeUser(User $user): self
  200. {
  201. if ($this->users->removeElement($user)) {
  202. // set the owning side to null (unless already changed)
  203. if ($user->getObjectiveTarget() === $this) {
  204. $user->setObjectiveTarget(null);
  205. }
  206. }
  207. return $this;
  208. }
  209. /**
  210. * @return Collection<int, ObjectiveTargetScore>
  211. */
  212. public function getObjectiveTargetScores(): Collection
  213. {
  214. return $this->objectiveTargetScores;
  215. }
  216. public function addObjectiveTargetScores(ObjectiveTargetScore $objectiveTargetScore): self
  217. {
  218. if (!$this->objectiveTargetScores->contains($objectiveTargetScore)) {
  219. $this->objectiveTargetScores[] = $objectiveTargetScore;
  220. $objectiveTargetScore->setObjectiveTarget($this);
  221. }
  222. return $this;
  223. }
  224. public function removeObjectiveTargetScores(ObjectiveTargetScore $objectiveTargetScore): self
  225. {
  226. if ($this->objectiveTargetScores->removeElement($objectiveTargetScore)) {
  227. // set the owning side to null (unless already changed)
  228. if ($objectiveTargetScore->getObjectiveTarget() === $this) {
  229. $objectiveTargetScore->setObjectiveTarget(null);
  230. }
  231. }
  232. return $this;
  233. }
  234. /**
  235. * @return int
  236. *
  237. * @VirtualProperty
  238. * @SerializedName("count_users")
  239. *
  240. * @Expose
  241. * @Groups({"objective_target"})
  242. */
  243. public function countUsers(): int
  244. {
  245. return count($this->users);
  246. }
  247. public function getObjectiveType(): ?string
  248. {
  249. return $this->objectiveType;
  250. }
  251. public function setObjectiveType(string $objectiveType): self
  252. {
  253. $this->objectiveType = $objectiveType;
  254. return $this;
  255. }
  256. public function getApplyChoices(): ?array
  257. {
  258. return $this->applyChoices;
  259. }
  260. public function setApplyChoices(?array $applyChoices): self
  261. {
  262. $this->applyChoices = $applyChoices;
  263. return $this;
  264. }
  265. public function getCompletionValue(): ?float
  266. {
  267. return $this->completionValue;
  268. }
  269. public function setCompletionValue(float $completionValue): self
  270. {
  271. $this->completionValue = $completionValue;
  272. return $this;
  273. }
  274. public function getEnabled(): ?bool
  275. {
  276. return $this->enabled;
  277. }
  278. public function setEnabled(bool $enabled): ObjectiveTarget
  279. {
  280. $this->enabled = $enabled;
  281. return $this;
  282. }
  283. public function getDateStart(): ?\DateTimeInterface
  284. {
  285. return $this->dateStart;
  286. }
  287. public function setDateStart(?\DateTimeInterface $dateStart): self
  288. {
  289. $this->dateStart = $dateStart;
  290. return $this;
  291. }
  292. public function getDateEnd(): ?\DateTimeInterface
  293. {
  294. return $this->dateEnd;
  295. }
  296. public function setDateEnd(?\DateTimeInterface $dateEnd): self
  297. {
  298. $this->dateEnd = $dateEnd;
  299. return $this;
  300. }
  301. /**
  302. * @return Collection<int, ObjectiveTargetStep>
  303. */
  304. public function getObjectiveTargetSteps(): Collection
  305. {
  306. return $this->objectiveTargetSteps;
  307. }
  308. public function addObjectiveTargetStep(objectiveTargetStep $objectiveTargetStep): self
  309. {
  310. if (!$this->objectiveTargetScores->contains($objectiveTargetStep)) {
  311. $this->objectiveTargetSteps[] = $objectiveTargetStep;
  312. $objectiveTargetStep->setObjectiveTarget($this);
  313. }
  314. return $this;
  315. }
  316. public function removeObjectiveTargetStep(objectiveTargetStep $objectiveTargetStep): self
  317. {
  318. if ($this->objectiveTargetSteps->removeElement($objectiveTargetStep)) {
  319. // set the owning side to null (unless already changed)
  320. if ($objectiveTargetStep->getObjectiveTarget() === $this) {
  321. $objectiveTargetStep->setObjectiveTarget(null);
  322. }
  323. }
  324. return $this;
  325. }
  326. public function getScoredByUser(): ?bool
  327. {
  328. return $this->scoredByUser;
  329. }
  330. public function setScoredByUser(bool $scoredByUser): ObjectiveTarget
  331. {
  332. $this->scoredByUser = $scoredByUser;
  333. return $this;
  334. }
  335. }