<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ExerciseRepository")
* @Vich\Uploadable
*/
class Exercise implements TranslatableInterface
{
use TranslatableTrait;
const DIFICULTY_TYPE_HIGH = "HIGH";
const DIFICULTY_TYPE_MEDIUM = "MEDIUM";
const DIFICULTY_TYPE_LOW = "LOW";
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="exercises")
* @ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Material", inversedBy="exercises")
* @ORM\JoinColumn(nullable=false)
*/
private $materials;
/**
* @ORM\OneToMany(targetEntity="App\Entity\NoteExercise", mappedBy="exercise", orphanRemoval=true)
*/
private $notes;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @Assert\Length(
* max = "50M",
* maxMessage = "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}."
* )
*
* @Vich\UploadableField(mapping="exercise_images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime
*/
private $updatedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $poster;
/**
* @Vich\UploadableField(mapping="exercise_images", fileNameProperty="poster")
* @var File
*/
private $posterFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $dificulty;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $intensity;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $duration;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ExerciseCalendar", mappedBy="exercise", orphanRemoval=true)
*/
private $exerciseCalendars;
/**
* @ORM\Column(type="integer")
*/
private $customer_id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="exercises")
* @ORM\JoinColumn(nullable=false)
*/
private $customer;
/**
* @ORM\Column(type="integer", nullable=false)
*/
private $category_id;
/**
* @ORM\OneToMany(targetEntity=ExerciseQualification::class, mappedBy="exercise")
*/
private $exerciseQualifications;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $is_shared;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $deletedAt;
private $stars;
public function __construct()
{
$this->exerciseCalendars = new ArrayCollection();
$this->materials = new ArrayCollection();
$this->exerciseQualifications = new ArrayCollection();
$this->stars = new ArrayCollection();
$this->notes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCustomerId(): ?int
{
return $this->customer_id;
}
public function setCustomerId(?int $customer): self
{
$this->customer_id = $customer;
return $this;
}
public function getCustomer(): ?Customer
{
return $this->customer;
}
public function setCategoryId(?int $category): self
{
$this->category_id = $category;
return $this;
}
public function getCategoryId(): ?int
{
return $this->category_id;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
* @return Exercise
*/
public function setUpdatedAt(\DateTime $updatedAt): Exercise
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @param File|null $image
* @return Exercise
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($image) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
return $this;
}
public function getImageFile()
{
return $this->imageFile;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
/**
* @param File|null $poster
* @return Exercise
*/
public function setPosterFile(File $poster = null)
{
$this->posterFile = $poster;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($poster) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
return $this;
}
public function getPosterFile()
{
return $this->posterFile;
}
public function getPoster(): ?string
{
return $this->poster;
}
public function setPoster(?string $poster): self
{
$this->poster = $poster;
return $this;
}
public function getDificulty(): ?string
{
return $this->dificulty;
}
public function setDificulty(?string $dificulty): self
{
$this->dificulty = $dificulty;
return $this;
}
public function getIntensity(): ?string
{
return $this->intensity;
}
public function setIntensity(?string $intensity): self
{
$this->intensity = $intensity;
return $this;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): self
{
$this->duration = $duration;
return $this;
}
public function getIsShared(): ?int
{
return $this->is_shared;
}
public function setIsShared(?int $isShared): self
{
$this->is_shared = $isShared;
return $this;
}
/**
* @return \DateTime
*/
public function getDeletedAt(): \DateTime
{
return $this->deletedAt;
}
/**
* @param \DateTime $deletedAt
* @return Exercise
*/
public function setDeletedAt(\DateTime $deletedAt): Exercise
{
$this->deletedAt = $deletedAt;
return $this;
}
public function __call($method, $arguments)
{
$method = ('get' === substr($method, 0, 3) || 'set' === substr($method, 0, 3)) ? $method : 'get' . ucfirst($method);
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __get($name)
{
$method = 'get' . ucfirst($name);
$arguments = [];
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __toArray($imagePath = "")
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"image" => !empty($this->getImage()) ? $imagePath . $this->getImage() : "",
"poster" => !empty($this->getPoster()) ? $imagePath . $this->getPoster() : "",
"description" => $this->getDescription(),
"category" => $this->getCategory()->getId(),
"dificulty" => $this->getDificulty(),
"intensity" => $this->getIntensity(),
"duration" => $this->getDuration(),
"isShared" => $this->getIsShared(),
"materials" => $this->getMaterialsArray(),
"customer" => $this->getCustomerId(),
"category" => $this->getCategoryId(),
"notes" => $this->getNotesArray()
];
}
/**
* @return Collection|ExerciseCalendar[]
*/
public function getExerciseCalendars(): Collection
{
return $this->exerciseCalendars;
}
public function addExerciseCalendar(ExerciseCalendar $exerciseCalendar): self
{
if (!$this->exerciseCalendars->contains($exerciseCalendar)) {
$this->exerciseCalendars[] = $exerciseCalendar;
$exerciseCalendar->setExercise($this);
}
return $this;
}
public function removeExerciseCalendar(ExerciseCalendar $exerciseCalendar): self
{
if ($this->exerciseCalendars->contains($exerciseCalendar)) {
$this->exerciseCalendars->removeElement($exerciseCalendar);
// set the owning side to null (unless already changed)
if ($exerciseCalendar->getExercise() === $this) {
$exerciseCalendar->setExercise(null);
}
}
return $this;
}
/**
* @return Collection|Material[]
*/
public function getMaterials(): Collection
{
return $this->materials;
}
/**
* @return array
*/
public function getMaterialsArray()
{
$materials = $this->materials;
$output = [];
foreach ($materials as $material) {
$output[] = $material->__toArray();
}
return $output;
}
/**
* @return Collection|NoteExercise[]
*/
public function getNotes(): Collection
{
return $this->notes;
}
/**
* @return array
*/
public function getNotesArray()
{
$notes = $this->notes;
$output = [];
foreach ($notes as $note) {
$output[] = $note->__toArray();
}
return $output;
}
public function addMaterial(Material $material): self
{
if (!$this->materials->contains($material)) {
$this->materials[] = $material;
}
return $this;
}
public function removeMaterial(Material $material): self
{
if ($this->materials->contains($material)) {
$this->materials->removeElement($material);
// set the owning side to null (unless already changed)
if ($material->getExercise() === $this) {
$material->setExercise(null);
}
}
return $this;
}
public function removeMaterials(): self
{
$materials = $this->materials;
foreach ($materials as $material) {
$this->materials->removeElement($material);
if ($material->getExercisesUser() === $this) {
$material->removeExerciseUser($this);
}
}
return $this;
}
public function getExerciseQualifications(): Collection
{
return $this->exerciseQualifications;
}
public function addExerciseQualification(ExerciseQualification $exerciseQualification): self
{
if (!$this->exerciseQualifications->contains($exerciseQualification)) {
$this->exerciseQualifications[] = $exerciseQualification;
$exerciseQualification->setExercise($this);
}
return $this;
}
public function removeExerciseQualification(ExerciseQualification $exerciseQualification): self
{
if ($this->exerciseQualifications->contains($exerciseQualification)) {
$this->exerciseQualifications->removeElement($exerciseQualification);
// set the owning side to null (unless already changed)
if ($exerciseQualification->getExercise() === $this) {
$exerciseQualification->setExercise(null);
}
}
return $this;
}
public function getStars(){
$this->stars = 0;
foreach ($this->exerciseQualifications as $key => $value) {
$this->stars = $this->stars + $value->getQuantity();
}
if(!$this->stars){
return $this->stars;
}
return round($this->stars / count($this->exerciseQualifications)) ;
}
public function getMedia(){
$this->stars = 0;
foreach ($this->exerciseQualifications as $key => $value) {
$this->stars = $this->stars + $value->getQuantity();
}
return $this->stars / count($this->exerciseQualifications);
}
}