<?php
namespace App\Entity;
use App\Repository\ObjectiveTargetRepository;
use App\Traits\DateTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=ObjectiveTargetRepository::class)
* @ORM\HasLifecycleCallbacks()
* @Serializer\ExclusionPolicy("ALL")
* @UniqueEntity("slug")
*/
class ObjectiveTarget
{
public const MODULE_NAME = 'objective_target';
public const OBJECTIVE_CA_TYPES_GLOBAL = "objectives_global";
public const OBJECTIVE_CA_TYPES_ROLES = "objectives_roles";
public const OBJECTIVE_CA_TYPES_JOBS = "objectives_jobs";
public const OBJECTIVE_CA_TYPES_INDIVIDUAL = "objectives_individual";
public const OBJECTIVE_CA_TYPES = [
self::OBJECTIVE_CA_TYPES_GLOBAL => "Global",
self::OBJECTIVE_CA_TYPES_ROLES => "Par roles",
self::OBJECTIVE_CA_TYPES_JOBS => "Par jobs",
self::OBJECTIVE_CA_TYPES_INDIVIDUAL => "Par utilisateur",
];
public const OBJECTIVE_CA_TYPES_ARRAY = [
self::OBJECTIVE_CA_TYPES_INDIVIDUAL,
self::OBJECTIVE_CA_TYPES_JOBS,
self::OBJECTIVE_CA_TYPES_ROLES,
self::OBJECTIVE_CA_TYPES_GLOBAL,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @Expose
* @Groups({"objective_target"})
*/
private ?int $id = null;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank()
* @Assert\Regex(
* pattern = "/^[a-z0-9]+(?:-[a-z0-9]+)*$/",
* message = "Le slug doit contenir uniquement des lettres minuscules, chiffres et tirets."
* )
*
* @Expose
* @Groups({"objective_target", "objective_target_score"})
*/
private ?string $slug = null;
/**
* @ORM\Column(type="string")
*
* @Expose
* @Groups({"objective_target", "objective_target_score"})
*/
private ?string $label = null;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="ownerObjectiveTargets")
* @ORM\JoinColumn(nullable=false)
*
* @Expose
* @Groups({"objective_target"})
*/
private ?User $owner = null;
/**
* @ORM\ManyToMany(targetEntity=User::class, mappedBy="objectiveTargets")
*
* @Expose
* @Groups({"objective_target"})
*/
private Collection $users;
/**
* @ORM\OneToMany(targetEntity=ObjectiveTargetScore::class, mappedBy="objectiveTarget")
*/
private Collection $objectiveTargetScores;
/**
* @ORM\Column(type="string", length=255)
*
* @Expose
* @Groups({"objective_target"})
*/
private ?string $objectiveType = null;
/**
* @ORM\Column(type="json", nullable=true)
*
* @Expose
* @Groups({"objective_target"})
*/
private ?array $applyChoices = null;
/**
* @ORM\Column(type="float")
*
* @Expose
* @Groups({"objective_target", "objective_target_score"})
*/
private ?float $completionValue = null;
/**
* @ORM\Column(type="boolean", options={"default":true})
*
* @Expose
* @Groups({"objective_target"})
*/
private bool $enabled = true;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @Expose
* @Groups({"objective_target"})
*/
private ?\DateTimeInterface $dateStart = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @Expose
* @Groups({"objective_target"})
*/
private ?\DateTimeInterface $dateEnd = null;
/**
* @ORM\OneToMany(targetEntity=ObjectiveTargetStep::class, mappedBy="objectiveTarget", cascade={"persist", "remove"}, orphanRemoval=true)
* @Assert\Valid()
* @ORM\OrderBy({"orderItem" = "ASC"})
*/
private Collection $objectiveTargetSteps;
use DateTrait;
public function __construct()
{
$this->users = new ArrayCollection();
$this->objectiveTargetScores = new ArrayCollection();
$this->objectiveTargetSteps = new ArrayCollection();
}
public function __toString(): string
{
return $this->label ?? '';
}
public function getId(): ?int
{
return $this->id;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setObjectiveTarget($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getObjectiveTarget() === $this) {
$user->setObjectiveTarget(null);
}
}
return $this;
}
/**
* @return Collection<int, ObjectiveTargetScore>
*/
public function getObjectiveTargetScores(): Collection
{
return $this->objectiveTargetScores;
}
public function addObjectiveTargetScores(ObjectiveTargetScore $objectiveTargetScore): self
{
if (!$this->objectiveTargetScores->contains($objectiveTargetScore)) {
$this->objectiveTargetScores[] = $objectiveTargetScore;
$objectiveTargetScore->setObjectiveTarget($this);
}
return $this;
}
public function removeObjectiveTargetScores(ObjectiveTargetScore $objectiveTargetScore): self
{
if ($this->objectiveTargetScores->removeElement($objectiveTargetScore)) {
// set the owning side to null (unless already changed)
if ($objectiveTargetScore->getObjectiveTarget() === $this) {
$objectiveTargetScore->setObjectiveTarget(null);
}
}
return $this;
}
/**
* @return int
*
* @VirtualProperty
* @SerializedName("count_users")
*
* @Expose
* @Groups({"objective_target"})
*/
public function countUsers(): int
{
return count($this->users);
}
public function getObjectiveType(): ?string
{
return $this->objectiveType;
}
public function setObjectiveType(string $objectiveType): self
{
$this->objectiveType = $objectiveType;
return $this;
}
public function getApplyChoices(): ?array
{
return $this->applyChoices;
}
public function setApplyChoices(?array $applyChoices): self
{
$this->applyChoices = $applyChoices;
return $this;
}
public function getCompletionValue(): ?float
{
return $this->completionValue;
}
public function setCompletionValue(float $completionValue): self
{
$this->completionValue = $completionValue;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): ObjectiveTarget
{
$this->enabled = $enabled;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(?\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(?\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
return $this;
}
/**
* @return Collection<int, ObjectiveTargetStep>
*/
public function getObjectiveTargetSteps(): Collection
{
return $this->objectiveTargetSteps;
}
public function addObjectiveTargetStep(objectiveTargetStep $objectiveTargetStep): self
{
if (!$this->objectiveTargetScores->contains($objectiveTargetStep)) {
$this->objectiveTargetSteps[] = $objectiveTargetStep;
$objectiveTargetStep->setObjectiveTarget($this);
}
return $this;
}
public function removeObjectiveTargetStep(objectiveTargetStep $objectiveTargetStep): self
{
if ($this->objectiveTargetSteps->removeElement($objectiveTargetStep)) {
// set the owning side to null (unless already changed)
if ($objectiveTargetStep->getObjectiveTarget() === $this) {
$objectiveTargetStep->setObjectiveTarget(null);
}
}
return $this;
}
}