src/Entity/Category.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use DateTimeImmutable;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  10. #[ORM\HasLifecycleCallbacks]
  11. #[ORM\Table(name'categories')]
  12. class Category {
  13.   #[ORM\Id]
  14.   #[ORM\GeneratedValue]
  15.   #[ORM\Column]
  16.   private ?int $id null;
  17.     public function getThumbUploadPath(): ?string {
  18.         return $_ENV['CATEGORY_THUMB_PATH'] . '/' .  date('Y/m/d/');
  19.     }
  20.     public function getImageUploadPath(): ?string {
  21.         return $_ENV['CATEGORY_IMAGE_PATH'] . '/' .  date('Y/m/d/');
  22.     }
  23.   #[ORM\Column(length255)]
  24.   private ?string $title null;
  25.   #[ORM\Column(length255nullabletrue,)]
  26.   private ?string $title2 null;
  27.   #[ORM\Column(length255nullabletrue,)]
  28.   private ?string $title3 null;
  29.   #[ORM\ManyToOne(fetch'LAZY')]
  30.   #[ORM\JoinColumn(nullabletrue)]
  31.   private ?User $editBy null;
  32.   #[ORM\Column]
  33.   private bool $isSuspended false;
  34.     #[ORM\Column]
  35.     private bool $isImages false;
  36.   #[ORM\Column(typeTypes::TEXTnullabletrue,)]
  37.   private ?string $description null;
  38.   #[ORM\Column(length255nullabletrue)]
  39.   private ?string $metaTitle;
  40.   #[ORM\Column(length255nullabletrue)]
  41.   private ?string $metaDescription;
  42.   #[ORM\Column(length255nullabletrue)]
  43.   private ?string $metaKeywords;
  44.   #[ORM\Column]
  45.   private DateTimeImmutable $created;
  46.   #[ORM\Column]
  47.   private DateTimeImmutable $updated;
  48.   #[ORM\ManyToOne(targetEntityself::class, fetch"LAZY")]
  49.   private ?self $parent null;
  50.   #[ORM\OneToMany(mappedBy'category'targetEntityProduct::class, fetch"LAZY")]
  51.   private Collection $products;
  52.   #[ORM\ManyToOne(fetch"LAZY"inversedBy'category')]
  53.   private ?Choice $choice null;
  54.   #[ORM\ManyToOne(inversedBy'categories')]
  55.   private ?Image $slika null;
  56.   public function __construct() {
  57.     $this->products = new ArrayCollection();
  58.   }
  59.   #[ORM\PrePersist]
  60.   public function prePersist(): void {
  61.     $this->created = new DateTimeImmutable();
  62.     $this->updated = new DateTimeImmutable();
  63.   }
  64.   #[ORM\PreUpdate]
  65.   public function preUpdate(): void {
  66.     $this->updated = new DateTimeImmutable();
  67.   }
  68.   public function getId(): ?int {
  69.     return $this->id;
  70.   }
  71.   public function getTitle(): ?string {
  72.     return $this->title;
  73.   }
  74.   function reverseParenthesizedOrder($inputString) {
  75.     // Regularni izraz za pronalaženje teksta unutar zagrada
  76.     $pattern '/\(([^)]+)\)/';
  77.     // Funkcija za obradu podudaranja
  78.     $callback = function ($matches) use (&$prefix) {
  79.       // Razdvajamo string unutar zagrada koristeći '/' kao delimiter
  80.       $parts explode(' /'$matches[1]);
  81.       // Dodajemo prefiks na osnovu broja elemenata
  82.       if (count($parts) == 1) {
  83.         $prefix '-';
  84.       } elseif (count($parts) == 2) {
  85.         $prefix '--';
  86.       } elseif (count($parts) >= 3) {
  87.         $prefix '---';
  88.       }
  89.       // Ako ima više od jednog elementa, obrćemo redosled
  90.       if (count($parts) > 1) {
  91.         $parts array_reverse($parts);
  92.       }
  93.       // Ponovo spajamo elemente u string koristeći '/' kao delimiter
  94.       return '(' implode(' /'$parts) . ')';
  95.     };
  96.     // Zamena teksta unutar zagrada koristeći regularni izraz i callback funkciju
  97.     $reversedString preg_replace_callback($pattern$callback$inputString);
  98.     // Dodajemo prefiks na početak ulaznog stringa
  99.     $reversedString $prefix $reversedString;
  100.     return $reversedString;
  101.   }
  102.   public function getTitleForSale(): ?string {
  103.     $naziv $this->title;
  104.     if (!is_null($this->parent)) {
  105.       $naziv $naziv ' u (' $this->parent->title;
  106.       if (!is_null($this->parent->parent)) {
  107.         $naziv $naziv ' / ' $this->parent->parent->title;
  108.         if (!is_null($this->parent->parent->parent)) {
  109.           $naziv $naziv ' / ' $this->parent->parent->parent->title;
  110.         }
  111.       }
  112.       $naziv $naziv ')';
  113.     }
  114.     return $this->reverseParenthesizedOrder($naziv);
  115.   }
  116.   public function setTitle(string $title): self {
  117.     $this->title $title;
  118.     return $this;
  119.   }
  120.   /**
  121.    * @return User|null
  122.    */
  123.   public function getEditBy(): ?User {
  124.     return $this->editBy;
  125.   }
  126.   /**
  127.    * @param User|null $editBy
  128.    */
  129.   public function setEditBy(?User $editBy): void {
  130.     $this->editBy $editBy;
  131.   }
  132.   /**
  133.    * @return DateTimeImmutable
  134.    */
  135.   public function getCreated(): DateTimeImmutable {
  136.     return $this->created;
  137.   }
  138.   /**
  139.    * @param DateTimeImmutable $created
  140.    */
  141.   public function setCreated(DateTimeImmutable $created): void {
  142.     $this->created $created;
  143.   }
  144.   /**
  145.    * @return DateTimeImmutable
  146.    */
  147.   public function getUpdated(): DateTimeImmutable {
  148.     return $this->updated;
  149.   }
  150.   /**
  151.    * @param DateTimeImmutable $updated
  152.    */
  153.   public function setUpdated(DateTimeImmutable $updated): void {
  154.     $this->updated $updated;
  155.   }
  156.   /**
  157.    * @return bool
  158.    */
  159.   public function isSuspended(): bool {
  160.     return $this->isSuspended;
  161.   }
  162.   /**
  163.    * @param bool $isSuspended
  164.    */
  165.   public function setIsSuspended(bool $isSuspended): void {
  166.     $this->isSuspended $isSuspended;
  167.   }
  168.   public function getBadgeByStatus(): string {
  169.     if ($this->isSuspended) {
  170.       return '<span class="badge bg-warning text-primary">Deaktivirana</span>';
  171.     }
  172.     return '<span class="badge bg-primary text-white">Aktivna</span>';
  173.   }
  174.   /**
  175.    * @return string|null
  176.    */
  177.   public function getMetaTitle(): ?string {
  178.     return $this->metaTitle;
  179.   }
  180.   /**
  181.    * @param string|null $metaTitle
  182.    */
  183.   public function setMetaTitle(?string $metaTitle): void {
  184.     $this->metaTitle $metaTitle;
  185.   }
  186.   /**
  187.    * @return string|null
  188.    */
  189.   public function getMetaDescription(): ?string {
  190.     return $this->metaDescription;
  191.   }
  192.   /**
  193.    * @param string|null $metaDescription
  194.    */
  195.   public function setMetaDescription(?string $metaDescription): void {
  196.     $this->metaDescription $metaDescription;
  197.   }
  198.   /**
  199.    * @return string|null
  200.    */
  201.   public function getMetaKeywords(): ?string {
  202.     return $this->metaKeywords;
  203.   }
  204.   /**
  205.    * @param string|null $metaKeywords
  206.    */
  207.   public function setMetaKeywords(?string $metaKeywords): void {
  208.     $this->metaKeywords $metaKeywords;
  209.   }
  210.   public function getParent(): ?self {
  211.     return $this->parent;
  212.   }
  213.   public function setParent(?self $parent): self {
  214.     $this->parent $parent;
  215.     return $this;
  216.   }
  217.   /**
  218.    * @return string|null
  219.    */
  220.   public function getDescription(): ?string {
  221.     return $this->description;
  222.   }
  223.   /**
  224.    * @param string|null $description
  225.    */
  226.   public function setDescription(?string $description): void {
  227.     $this->description $description;
  228.   }
  229.   /**
  230.    * @return Collection<int, Product>
  231.    */
  232.   public function getProducts(): Collection {
  233.     return $this->products;
  234.   }
  235.   public function addProduct(Product $product): self {
  236.     if (!$this->products->contains($product)) {
  237.       $this->products->add($product);
  238.       $product->setCategory($this);
  239.     }
  240.     return $this;
  241.   }
  242.   public function removeProduct(Product $product): self {
  243.     if ($this->products->removeElement($product)) {
  244.       // set the owning side to null (unless already changed)
  245.       if ($product->getCategory() === $this) {
  246.         $product->setCategory(null);
  247.       }
  248.     }
  249.     return $this;
  250.   }
  251.   /**
  252.    * @return string|null
  253.    */
  254.   public function getTitle2(): ?string {
  255.     return $this->title2;
  256.   }
  257.   /**
  258.    * @param string|null $title2
  259.    */
  260.   public function setTitle2(?string $title2): void {
  261.     $this->title2 $title2;
  262.   }
  263.   public function getChoice(): ?Choice {
  264.     return $this->choice;
  265.   }
  266.   public function setChoice(?Choice $choice): self {
  267.     $this->choice $choice;
  268.     return $this;
  269.   }
  270.   /**
  271.    * @return string|null
  272.    */
  273.   public function getTitle3(): ?string {
  274.     return $this->title3;
  275.   }
  276.   /**
  277.    * @param string|null $title3
  278.    */
  279.   public function setTitle3(?string $title3): void {
  280.     $this->title3 $title3;
  281.   }
  282.     public function isImages(): bool {
  283.         return $this->isImages;
  284.     }
  285.     public function setIsImages(bool $isImages): void {
  286.         $this->isImages $isImages;
  287.     }
  288.     public function getSlika(): ?Image
  289.     {
  290.         return $this->slika;
  291.     }
  292.     public function setSlika(?Image $slika): self
  293.     {
  294.         $this->slika $slika;
  295.         return $this;
  296.     }
  297. }