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