src/Entity/Course.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JMS\Serializer\Annotation as JMS;
  7. use App\Commons\Entity\GenericEntity;
  8. use App\User\Entity\Client;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\CourseRepository")
  11.  *
  12.  * @JMS\ExclusionPolicy("all")
  13.  */
  14. class Course extends GenericEntity
  15. {
  16.     /**
  17.      * @ORM\ManyToOne(targetEntity="App\User\Entity\Client")
  18.      *
  19.      * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
  20.      */
  21.     protected $client;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      *
  25.      * @JMS\Expose
  26.      *
  27.      * @JMS\Groups({"public"})
  28.      */
  29.     protected $title;
  30.     /**
  31.      * @ORM\Column(type="string", length=255, nullable=true)
  32.      *
  33.      * @JMS\Expose
  34.      *
  35.      * @JMS\Groups({"public"})
  36.      */
  37.     protected $subtitle;
  38.     /**
  39.      * @ORM\Column(type="string", length=255, options={"default" : "Course"})
  40.      *
  41.      * @JMS\Expose
  42.      *
  43.      * @JMS\Groups({"public", "personal"})
  44.      */
  45.     protected $courseNature 'Course';
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity="App\Entity\CourseSubscription")
  48.      *
  49.      * @JMS\Expose
  50.      *
  51.      * @JMS\Groups({"public"})
  52.      */
  53.     protected $subscription;
  54.     /**
  55.      * @ORM\Column(type="text", nullable=true)
  56.      *
  57.      * @JMS\Expose
  58.      *
  59.      * @JMS\Groups({"public"})
  60.      */
  61.     protected $description;
  62.     /**
  63.      * @ORM\Column(type="text", nullable=true)
  64.      *
  65.      * @JMS\Expose
  66.      *
  67.      * @JMS\Groups({"public"})
  68.      */
  69.     protected $searchwords;
  70.     /**
  71.      * @ORM\Column(type="float")
  72.      *
  73.      * @JMS\Expose
  74.      *
  75.      * @JMS\Groups({"public"})
  76.      */
  77.     protected $price;
  78.     /**
  79.      * @ORM\Column(type="string", length=20, nullable=true)
  80.      *
  81.      * @JMS\Expose
  82.      *
  83.      * @JMS\Groups({"public"})
  84.      */
  85.     protected $rabatt;
  86.     /**
  87.      * @ORM\Column(type="float", nullable=true)
  88.      */
  89.     protected $taxRate 0;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity="CourseOccurrence", mappedBy="course", cascade={"all"})
  92.      *
  93.      * @JMS\Expose
  94.      *
  95.      * @JMS\Groups({"public"})
  96.      */
  97.     protected $occurrences;
  98.     /**
  99.      * @ORM\ManyToOne(targetEntity="App\Entity\Category")
  100.      *
  101.      * @JMS\Expose
  102.      *
  103.      * @JMS\Groups({"public"})
  104.      */
  105.     protected $category;
  106.     /**
  107.      * @ORM\OneToMany(targetEntity="App\Entity\CourseImage", mappedBy="course", orphanRemoval=true, cascade={"all"})
  108.      *
  109.      * @ORM\OrderBy({"orderId" = "ASC"})
  110.      *
  111.      * @JMS\Expose
  112.      *
  113.      * @JMS\Groups({"public"})
  114.      */
  115.     protected $images;
  116.     /**
  117.      * @ORM\Column(type="string", length=20, nullable=true)
  118.      *
  119.      * @JMS\Expose
  120.      *
  121.      * @JMS\Groups({"public"})
  122.      */
  123.     protected $number;
  124.     /**
  125.      * @ORM\Column(type="float", nullable=true)
  126.      *
  127.      * @JMS\Expose
  128.      *
  129.      * @JMS\Groups({"public"})
  130.      */
  131.     protected $materialCost 0.0;
  132.     /**
  133.      * @ORM\Column(type="integer", nullable=true)
  134.      *
  135.      * @JMS\Expose
  136.      *
  137.      * @JMS\Groups({"public"})
  138.      */
  139.     protected $targetAgeMin;
  140.     /**
  141.      * @ORM\Column(type="integer", nullable=true)
  142.      *
  143.      * @JMS\Expose
  144.      *
  145.      * @JMS\Groups({"public"})
  146.      */
  147.     protected $targetAgeMax;
  148.     /**
  149.      * @ORM\OneToMany(targetEntity="App\Entity\CourseText", mappedBy="course", orphanRemoval=true, cascade={"all"})
  150.      *
  151.      * @ORM\OrderBy({"orderId" = "ASC"})
  152.      *
  153.      * @JMS\Expose
  154.      *
  155.      * @JMS\Groups({"detail"})
  156.      */
  157.     protected $texts;
  158.     /**
  159.      * @ORM\ManyToOne(targetEntity="App\Entity\CourseSeries", inversedBy="courses")
  160.      *
  161.      * @JMS\Expose
  162.      *
  163.      * @JMS\Groups({"public"})
  164.      */
  165.     protected $series;
  166.     /**
  167.      * @ORM\ManyToOne(targetEntity="App\Entity\CourseType")
  168.      *
  169.      * @JMS\Expose
  170.      *
  171.      * @JMS\Groups({"public", "personal"})
  172.      */
  173.     protected $type;
  174.     /**
  175.      * @ORM\Column(type="text", nullable=true)
  176.      */
  177.     protected $invoiceUpperComment;
  178.     /**
  179.      * @ORM\Column(type="text", nullable=true)
  180.      */
  181.     protected $invoiceLowerComment;
  182.     /**
  183.      * @ORM\Column(type="text", nullable=true)
  184.      */
  185.     protected $invoiceLowerCommentDebit;
  186.     /**
  187.      * @ORM\OneToMany(
  188.      *   targetEntity=CourseProvider::class,
  189.      *   mappedBy="course",
  190.      *   cascade={"persist"},
  191.      *   orphanRemoval=true
  192.      * )
  193.      *
  194.      * DO NOT EXPOSE - use getProvidersApi() instead to avoid circular references
  195.      */
  196.     private Collection $courseProviders;
  197.     /**
  198.      * @JMS\Expose
  199.      *
  200.      * @JMS\Groups({"public","detail"})
  201.      *
  202.      * @JMS\Type("array") // dein $testFields ist ein Array von Arrays
  203.      */
  204.     public $fields = [];
  205.     // Existing properties and methods
  206.     public function __toString(): string
  207.     {
  208.         // Passe das Feld an: name, title o.รค.
  209.         return (string) ($this->getTitle() ?? ('Titel #'.($this->getId() ?? '')));
  210.     }
  211.     /**
  212.      * Set fields for the course.
  213.      */
  214.     public function setFields(array $fields): void
  215.     {
  216.         $this->fields $fields;
  217.     }
  218.     /**
  219.      * Get fields for the course.
  220.      */
  221.     public function getFields(): array
  222.     {
  223.         return $this->fields;
  224.     }
  225.     public function __construct()
  226.     {
  227.         $this->occurrences = new ArrayCollection();
  228.         $this->images = new ArrayCollection();
  229.         $this->texts = new ArrayCollection();
  230.         $this->courseProviders = new ArrayCollection();
  231.         $this->fields = []; // <-- Array lassen
  232.     }
  233.     /** @return Collection<int, CourseProvider> */
  234.     public function getCourseProviders(): Collection
  235.     {
  236.         return $this->courseProviders;
  237.     }
  238.     public function addCourseProvider(CourseProvider $cp): self
  239.     {
  240.         if (!$this->courseProviders->contains($cp)) {
  241.             $this->courseProviders->add($cp);
  242.             $cp->setCourse($this);
  243.         }
  244.         return $this;
  245.     }
  246.     public function removeCourseProvider(CourseProvider $cp): self
  247.     {
  248.         if ($this->courseProviders->removeElement($cp)) {
  249.             if ($cp->getCourse() === $this) {
  250.                 $cp->setCourse(null);
  251.             }
  252.         }
  253.         return $this;
  254.     }
  255.     /** Komfort: Direkt die Provider-Liste ableiten */
  256.     /** @return Collection<int, Provider> */
  257.     public function getProviders(): Collection
  258.     {
  259.         return new ArrayCollection(
  260.             $this->courseProviders
  261.                  ->map(fn (CourseProvider $cp) => $cp->getProvider())
  262.                  ->toArray()
  263.         );
  264.     }
  265.     public function getClient(): ?Client
  266.     {
  267.         return $this->client;
  268.     }
  269.     /**
  270.      * @param Client $client
  271.      */
  272.     public function setClient($client): self
  273.     {
  274.         $this->client $client;
  275.         return $this;
  276.     }
  277.     public function getTitle(): ?string
  278.     {
  279.         // $title = str_replace('&shy;', '', $this->title);
  280.         //   return $title;
  281.         return $this->title;
  282.     }
  283.     public function setTitle(string $title): self
  284.     {
  285.         $this->title $title;
  286.         return $this;
  287.     }
  288.     public function getSubtitle(): ?string
  289.     {
  290.         return $this->subtitle;
  291.     }
  292.     public function setSubtitle(?string $subtitle): self
  293.     {
  294.         $this->subtitle $subtitle;
  295.         return $this;
  296.     }
  297.     public function getDescription(): ?string
  298.     {
  299.         return $this->description;
  300.     }
  301.     public function setDescription(?string $description): self
  302.     {
  303.         $this->description $description;
  304.         return $this;
  305.     }
  306.     public function getSearchwords(): ?string
  307.     {
  308.         return $this->searchwords;
  309.     }
  310.     public function setSearchwords(?string $searchwords): self
  311.     {
  312.         $this->searchwords $searchwords;
  313.         return $this;
  314.     }
  315.     public function getPrice(): ?float
  316.     {
  317.         return $this->price;
  318.     }
  319.     public function setPrice(float $price): self
  320.     {
  321.         $this->price $price;
  322.         return $this;
  323.     }
  324.     public function getTaxRate(): float
  325.     {
  326.         return null === $this->taxRate 0.0 $this->taxRate;
  327.     }
  328.     public function setTaxRate(?float $taxRate): self
  329.     {
  330.         $this->taxRate $taxRate;
  331.         return $this;
  332.     }
  333.     /**
  334.      * @JMS\Expose
  335.      *
  336.      * @JMS\Groups({"detail"})
  337.      *
  338.      * @JMS\VirtualProperty()
  339.      *
  340.      * @JMS\Type("array<App\Entity\CourseOccurrence>")
  341.      *
  342.      * @return array<App\Entity\CourseOccurrence>
  343.      */
  344.     public function getOccurrences(bool $onlyPublished falsebool $isBookable false): ArrayCollection
  345.     {
  346.         $occurrences = new ArrayCollection();
  347.         foreach ($this->occurrences as $occurrence) {
  348.                     $occurrences->add($occurrence);
  349.         }
  350.         return $occurrences;
  351.     }
  352.     /**
  353.      * Liefert Provider als flaches Array fรผr die API.
  354.      * Umgeht damit Gruppen-/Serializer-Probleme.
  355.      *
  356.      * @JMS\VirtualProperty()
  357.      *
  358.      * @JMS\SerializedName("providers")
  359.      *
  360.      * @JMS\Groups({"public","detail"})
  361.      */
  362.     public function getProvidersApi(): array
  363.     {
  364.         $out = [];
  365.         foreach ($this->getProviders() as $p) {
  366.             $out[] = [
  367.                 'id' => $p->getId(),
  368.                 'company' => $p->getCompany(),
  369.                 'street' => $p->getStreet(),
  370.                 'streetNumber' => $p->getStreetNumber(),
  371.                 'postalCode' => $p->getPostalcode(),
  372.                 'city' => $p->getCity(),
  373.                 'web' => $p->getWeb(),
  374.                 'phone' => $p->getPhone(),
  375.             ];
  376.         }
  377.         return $out;
  378.     }
  379.     public function getAllOccurrences(bool $onlyPublished falsebool $isBookable false): ArrayCollection
  380.     {
  381.         $occurrences = new ArrayCollection();
  382.         foreach ($this->occurrences as $occurrence) {
  383.             if ($onlyPublished && !$occurrence->getPublished()) {
  384.                 continue;
  385.             }
  386.             if ($isBookable && !$occurrence->isBookable()) {
  387.                 continue;
  388.             }
  389.             // Beispiel fรผr zusรคtzliche Berechnungen
  390.             // $freeSlots = $occurrence->getSlots() - $occurrence->getBookedSlots();
  391.             // $occurrence->setFreeSlots($freeSlots); // Beispielmethodenaufruf
  392.             $occurrences->add($occurrence);
  393.         }
  394.         return $occurrences;
  395.     }
  396.     /**
  397.      * @param ArrayCollection<CourseOccurrence> $occurrences
  398.      */
  399.     public function setOccurrences($occurrences): self
  400.     {
  401.         $this->occurrences $occurrences;
  402.         return $this;
  403.     }
  404.     public function addOccurrence(CourseOccurrence $occurrence): self
  405.     {
  406.         $occurrence->setCourse($this);
  407.         $this->occurrences->add($occurrence);
  408.         return $this;
  409.     }
  410.     /**
  411.      * @return Course
  412.      */
  413.     public function removeOccurrence(CourseOccurrence $occurrence)
  414.     {
  415.         if ($this->occurrences->contains($occurrence)) {
  416.             $this->occurrences->removeElement($occurrence);
  417.             if ($occurrence->getCourse() === $this) {
  418.                 $occurrence->setCourse(null);
  419.             }
  420.         }
  421.         return $this;
  422.     }
  423.     public function getCategory(): ?Category
  424.     {
  425.         return $this->category;
  426.     }
  427.     public function setCategory(?Category $category): self
  428.     {
  429.         $this->category $category;
  430.         return $this;
  431.     }
  432.     public function getCourseNature(): string
  433.     {
  434.         return $this->courseNature;
  435.     }
  436.     public function setCourseNature($courseNature)
  437.     {
  438.         $this->courseNature $courseNature;
  439.     }
  440.     /**
  441.      * @return Collection|CourseImage[]
  442.      */
  443.     public function getImages(): Collection
  444.     {
  445.         return $this->images;
  446.     }
  447.     public function addImage(CourseImage $image): self
  448.     {
  449.         if (!$this->images->contains($image)) {
  450.             $this->images[] = $image;
  451.             $image->setCourse($this);
  452.         }
  453.         return $this;
  454.     }
  455.     public function removeImage(CourseImage $image): self
  456.     {
  457.         if ($this->images->contains($image)) {
  458.             $this->images->removeElement($image);
  459.             // set the owning side to null (unless already changed)
  460.             if ($image->getCourse() === $this) {
  461.                 $image->setCourse(null);
  462.             }
  463.         }
  464.         return $this;
  465.     }
  466.     public function getNumber(): ?string
  467.     {
  468.         return $this->number;
  469.     }
  470.     public function setNumber(?string $number): self
  471.     {
  472.         $this->number $number;
  473.         return $this;
  474.     }
  475.     public function getMaterialCost(): ?float
  476.     {
  477.         return $this->materialCost;
  478.     }
  479.     public function setMaterialCost(?float $materialCost): self
  480.     {
  481.         $this->materialCost $materialCost;
  482.         return $this;
  483.     }
  484.     public function getTargetAgeMin(): ?int
  485.     {
  486.         return $this->targetAgeMin;
  487.     }
  488.     public function setTargetAgeMin(?int $targetAgeMin): self
  489.     {
  490.         $this->targetAgeMin $targetAgeMin;
  491.         return $this;
  492.     }
  493.     public function getTargetAgeMax(): ?int
  494.     {
  495.         return $this->targetAgeMax;
  496.     }
  497.     public function setTargetAgeMax(?int $targetAgeMax): self
  498.     {
  499.         $this->targetAgeMax $targetAgeMax;
  500.         return $this;
  501.     }
  502.     /**
  503.      * @return Collection|CourseText[]
  504.      */
  505.     public function getTexts(): Collection
  506.     {
  507.         return $this->texts;
  508.     }
  509.     public function addText(CourseText $text): self
  510.     {
  511.         if (!$this->texts->contains($text)) {
  512.             $this->texts[] = $text;
  513.             $text->setCourse($this);
  514.         }
  515.         return $this;
  516.     }
  517.     public function removeText(CourseText $text): self
  518.     {
  519.         if ($this->texts->contains($text)) {
  520.             $this->texts->removeElement($text);
  521.             // set the owning side to null (unless already changed)
  522.             if ($text->getCourse() === $this) {
  523.                 $text->setCourse(null);
  524.             }
  525.         }
  526.         return $this;
  527.     }
  528.     public function getSeries(): ?CourseSeries
  529.     {
  530.         return $this->series;
  531.     }
  532.     public function setSeries(?CourseSeries $series): self
  533.     {
  534.         $this->series $series;
  535.         return $this;
  536.     }
  537.     public function getSubscription(): ?CourseSubscription
  538.     {
  539.         return $this->subscription;
  540.     }
  541.     public function setSubscription(?CourseSubscription $subscription): self
  542.     {
  543.         $this->subscription $subscription;
  544.         return $this;
  545.     }
  546.     /**
  547.      * @JMS\Expose
  548.      *
  549.      * @JMS\Groups({"public", "personal"})
  550.      *
  551.      * @JMS\VirtualProperty()
  552.      *
  553.      * @JMS\Type("int")
  554.      */
  555.     public function getId(): ?int
  556.     {
  557.         return parent::getId();
  558.     }
  559.     public function getType(): ?CourseType
  560.     {
  561.         return $this->type;
  562.     }
  563.     /**
  564.      * @return CourseType
  565.      */
  566.     public function setType(?CourseType $type): self
  567.     {
  568.         $this->type $type;
  569.         return $this;
  570.     }
  571.     public function getInvoiceUpperComment(): ?string
  572.     {
  573.         return $this->invoiceUpperComment;
  574.     }
  575.     public function setInvoiceUpperComment(?string $invoiceUpperComment): self
  576.     {
  577.         $this->invoiceUpperComment $invoiceUpperComment;
  578.         return $this;
  579.     }
  580.     public function getInvoiceLowerComment(): ?string
  581.     {
  582.         return $this->invoiceLowerComment;
  583.     }
  584.     public function setInvoiceLowerComment(?string $invoiceLowerComment): self
  585.     {
  586.         $this->invoiceLowerComment $invoiceLowerComment;
  587.         return $this;
  588.     }
  589.     public function getInvoiceLowerCommentDebit(): ?string
  590.     {
  591.         return $this->invoiceLowerCommentDebit;
  592.     }
  593.     public function setInvoiceLowerCommentDebit(?string $invoiceLowerCommentDebit): self
  594.     {
  595.         $this->invoiceLowerCommentDebit $invoiceLowerCommentDebit;
  596.         return $this;
  597.     }
  598.     /**
  599.      * @JMS\Expose
  600.      *
  601.      * @JMS\Groups({"public"})
  602.      *
  603.      * @JMS\VirtualProperty()
  604.      *
  605.      * @JMS\SerializedName("type")
  606.      *
  607.      * @JMS\Type("string")
  608.      *
  609.      * @return int
  610.      */
  611.     public function getTypeSlug()
  612.     {
  613.         return $this->getType() ? $this->getType()->getSlug() : null;
  614.     }
  615.     public function isArchive(): ?bool
  616.     {
  617.         $onlyPublished true;
  618.         $isBookable false;
  619.         if (count($this->getAllOccurrences($onlyPublished$isBookable)) < 1) {
  620.             return true;
  621.         }
  622.         $now = new \DateTime();
  623.         foreach ($this->getAllOccurrences($onlyPublished$isBookable) as $occurrence) {
  624.             $diff $now->diff($occurrence->getEnd());
  625.             if (== $diff->invert || $diff->$_ENV['DAYS_TO_ARCHIVE']) {
  626.                 return false;
  627.             }
  628.         }
  629.         return true;
  630.     }
  631. }