src/Entity/News.php line 672

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Utils\File\FileServiceTrait;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\NewsRepository")
  11.  * @Vich\Uploadable
  12.  */
  13. class News implements \JsonSerializable
  14. {
  15.     use FileServiceTrait;
  16.     const FILE_PICTURE = "picture";
  17.     const FILES = [
  18.         self::FILE_PICTURE => [
  19.             'types' => ['gif', 'jpg', 'jpeg', 'png', 'tif', 'svg'],
  20.             'dir' => '/news',
  21.             'size' => 5*1024*1024,
  22.         ]
  23.     ];
  24.     /**
  25.      * @ORM\Id()
  26.      * @ORM\GeneratedValue()
  27.      * @ORM\Column(type="integer")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="news")
  32.      * @ORM\JoinColumn(nullable=false)
  33.      */
  34.     private $company;
  35.     /**
  36.      * @ORM\ManyToMany(targetEntity="App\Entity\NewsCategory", inversedBy="news")
  37.      */
  38.     private $categories;
  39.     /**
  40.      * @ORM\Column(type="string", length=255)
  41.      */
  42.     private $title;
  43.     /**
  44.      * @ORM\Column(type="string", length=255)
  45.      */
  46.     private $author;
  47.     /**
  48.      * @ORM\Column(type="text")
  49.      */
  50.     private $description;
  51.     /**
  52.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  53.      */
  54.     private $createdAt;
  55.     /**
  56.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  57.      */
  58.     private $publishedAt;
  59.     /**
  60.      * @ORM\Column(type="datetime", nullable=true)
  61.      */
  62.     private $updatedAt;
  63.     /**
  64.      * @ORM\Column(type="boolean", options={"default":"0"})
  65.      */
  66.     private $featured = FALSE;
  67.     /**
  68.      * @ORM\Column(type="boolean", options={"default":"0"})
  69.      */
  70.     private $companyFeatured = FALSE;
  71.     /**
  72.      * @ORM\Column(type="boolean", options={"default": "0"})
  73.      */
  74.     private $active = FALSE;
  75.     /**
  76.      * @ORM\Column(type="string", length=255, nullable=true)
  77.      * @var string|null
  78.      */
  79.     private $thumbnail;
  80.      /**
  81.      * @Vich\UploadableField(mapping="thumbnails", fileNameProperty="thumbnail")
  82.      * @var File|null
  83.      */
  84.     private $pictureFile;
  85.     /**
  86.      * @ORM\Column(type="string", length=255, nullable=true)
  87.      */
  88.     private $slug;
  89.     /**
  90.      * @ORM\Column(type="boolean", options={"default": "0"})
  91.      */
  92.     private $main = FALSE;
  93.     /**
  94.      * @ORM\Column(type="integer", nullable=true)
  95.      */
  96.     private $oldId;
  97.     /**
  98.      * @ORM\Column(type="string", length=255, nullable=true)
  99.      */
  100.     private $oldUrl;
  101.      /**
  102.      * @ORM\Column(type="string", length=255, nullable=true)
  103.      */
  104.     private $canonicalUrl;
  105.    /**
  106.      * @ORM\Column(type="integer", nullable=true)
  107.      */
  108.     private $views;
  109.     /**
  110.      * @ORM\Column(type="integer", nullable=true)
  111.      */
  112.     private $contentChars;
  113.     /**
  114.      * @ORM\Column(type="integer", nullable=true)
  115.      */
  116.     private $contentWords;
  117.     /**
  118.      * @ORM\Column(type="string", length=1024, nullable=true)
  119.      */
  120.     public $keywords;
  121.     public function __construct() {
  122.         $this->setCreatedAt(new \DateTime());
  123.         $this->updatedAt = (new \DateTime());
  124.         $this->categories = new ArrayCollection();
  125.     }
  126.     /**
  127.      * @return int|null
  128.      */
  129.     public function getId(): ?int
  130.     {
  131.         return $this->id;
  132.     }
  133.     /**
  134.      * @return Company
  135.      */
  136.     public function getCompany(): ?Company {
  137.         return $this->company;
  138.     }
  139.     /**
  140.      * @param Company $company
  141.      *
  142.      * @return self
  143.      */
  144.     public function setCompany(Company $company): self {
  145.         $this->company = $company;
  146.         return $this;
  147.     }
  148.     /**
  149.      * @return Collection|NewsCategory[]
  150.      */
  151.     public function getCategories(): Collection
  152.     {
  153.         return $this->categories;
  154.     }
  155.     public function addCategory(NewsCategory $category): self
  156.     {
  157.         if (!$this->categories->contains($category)) {
  158.             $this->categories[] = $category;
  159.         }
  160.         return $this;
  161.     }
  162.     public function removeCategory(NewsCategory $category): self
  163.     {
  164.         if ($this->categories->contains($category)) {
  165.             $this->categories->removeElement($category);
  166.         }
  167.         return $this;
  168.     }
  169.     public function setCategories(ArrayCollection $categories): self
  170.     {
  171.         $this->categories = $categories;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return string|null
  176.      */
  177.     public function getTitle(): ?string
  178.     {
  179.         return $this->title;
  180.     }
  181.     /**
  182.      * @param string $title
  183.      *
  184.      * @return self
  185.      */
  186.     public function setTitle(string $title): self
  187.     {
  188.         $this->title = $title;
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return string|null
  193.      */
  194.     public function getAuthor(): ?string
  195.     {
  196.         return $this->author;
  197.     }
  198.     /**
  199.      * @param string $author
  200.      *
  201.      * @return self
  202.      */
  203.     public function setAuthor(string $author): self
  204.     {
  205.         $this->author = $author;
  206.         return $this;
  207.     }
  208.     /**
  209.      * @return string|null
  210.      */
  211.     public function getDescription(): ?string
  212.     {
  213.         return $this->description;
  214.     }
  215.     /**
  216.      * @param string|null $description
  217.      *
  218.      * @return self
  219.      */
  220.     public function setDescription(?string $description): self
  221.     {
  222.         $this->description = $description;
  223.         return $this;
  224.     }
  225.     /**
  226.      * @return \DateTime
  227.      */
  228.     public function getCreatedAt(): \DateTime {
  229.         return $this->createdAt;
  230.     }
  231.     /**
  232.      * @param \DateTime $createdAt
  233.      *
  234.      * @return self
  235.      */
  236.     public function setCreatedAt(\DateTime $createdAt): self {
  237.         $this->createdAt = $createdAt;
  238.         return $this;
  239.     }
  240.     /**
  241.      * @return \DateTime
  242.      */
  243.     public function getPublishedAt(): ?\DateTime {
  244.         return $this->publishedAt;
  245.     }
  246.     /**
  247.      * @param \DateTime $publishedAt
  248.      *
  249.      * @return self
  250.      */
  251.     public function setPublishedAt(\DateTime $publishedAt): self {
  252.         $this->publishedAt = $publishedAt;
  253.         return $this;
  254.     }
  255.     /**
  256.      * @return bool
  257.      */
  258.     public function getFeatured(): bool {
  259.         return $this->featured;
  260.     }
  261.     /**
  262.      * @param bool $featured
  263.      *
  264.      * @return self
  265.      */
  266.     public function setFeatured(bool $featured): self {
  267.         $this->featured = $featured;
  268.         return $this;
  269.     }
  270.     /**
  271.      * @return bool
  272.      */
  273.     public function getCompanyFeatured(): bool {
  274.         return $this->companyFeatured;
  275.     }
  276.     /**
  277.      * @param bool $companyFeatured
  278.      *
  279.      * @return self
  280.      */
  281.     public function setCompanyFeatured(bool $companyFeatured): self {
  282.         $this->companyFeatured = $companyFeatured;
  283.         return $this;
  284.     }
  285.     /**
  286.      * @return bool
  287.      */
  288.     public function getActive() {
  289.         return $this->active;
  290.     }
  291.     /**
  292.      * @param bool $active
  293.      *
  294.      * @return self
  295.      */
  296.     public function setActive(bool $active): self {
  297.         $this->active = $active;
  298.         return $this;
  299.     }
  300.     /**
  301.      * Get the value of thumbnail
  302.      */ 
  303.     public function getThumbnail()
  304.     {
  305.         return $this->thumbnail;
  306.     }
  307.     /**
  308.      * Set the value of thumbnail
  309.      *
  310.      * @return  self
  311.      */ 
  312.     public function setThumbnail(?string $thumbnail)
  313.     {
  314.         $this->thumbnail = $thumbnail;
  315.         return $this;
  316.     }
  317.     /**
  318.      * Get the value of pictureFile
  319.      */ 
  320.     public function getPictureFile()
  321.     {
  322.         return $this->pictureFile;
  323.     }
  324.     /**
  325.      * Set the value of pictureFile
  326.      *
  327.      * @return  self
  328.      */ 
  329.     public function setPictureFile(?UploadedFile $pictureFile = null)
  330.     {
  331.         $this->pictureFile = $pictureFile;
  332.       
  333.         if ($pictureFile) {
  334.             $this->setPicture($pictureFile, null);
  335.             $this->updatedAt = (new \DateTime());
  336.         }
  337.         return $this;
  338.     }
  339.     /**
  340.      * @return string|null
  341.      */
  342.     public function getSlug(): ?string
  343.     {
  344.         return $this->slug;
  345.     }
  346.     /**
  347.      * @param string $slug
  348.      *
  349.      * @return self
  350.      */
  351.     public function setSlug(string $slug): self
  352.     {
  353.         $this->slug = $slug;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @return bool
  358.      */
  359.     public function getMain() {
  360.         return $this->main;
  361.     }
  362.     /**
  363.      * @param bool $main
  364.      *
  365.      * @return self
  366.      */
  367.     public function setMain(bool $main): self {
  368.         $this->main = $main;
  369.         return $this;
  370.     }
  371.     /**
  372.      * @return string|null
  373.      */
  374.     public function getCanonicalUrl(): ?string
  375.     {
  376.         return $this->canonicalUrl;
  377.     }
  378.     /**
  379.      * @param string $slug
  380.      *
  381.      * @return self
  382.      */
  383.     public function setCanonicalUrl(?string $canonicalUrl): self
  384.     {
  385.         $this->canonicalUrl = $canonicalUrl;
  386.         return $this;
  387.     }
  388.     /**
  389.      * @return string|null
  390.      */
  391.     public function getPicturePreview(): ?string {
  392.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  393.         
  394.         if ($file) {
  395.             $filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
  396.             return $filePath;
  397.         }
  398.         return null;
  399.     }
  400.     /**
  401.      * @return File|null
  402.      */
  403.     public function getPicture(){
  404.         return $this->getFileService()->getFile($this, self::FILE_PICTURE);
  405.     }
  406.     /**
  407.      * @return self
  408.      */
  409.     public function setPicture(?UploadedFile $file, $author = null): self {
  410.         if ($file) {
  411.             $userId = $this->getCompany() ? $this->getCompany()->getUser()->getId() : 1;
  412.             $oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  413.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE, true, $author, $userId);
  414.             
  415.             if ($newFile && $oldFile) {
  416.                 $this->getFileService()->delete($oldFile);
  417.             }
  418.         }
  419.         return $this;
  420.     }
  421.     /**
  422.      * @param UploadedFile|null $file
  423.      *
  424.      * @return File
  425.      */
  426.     public function uploadPicture(?UploadedFile $file): File {
  427.         $newFile = null;
  428.         if ($file) {
  429.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE, false);
  430.         }
  431.         return $newFile;
  432.     }
  433.     /**
  434.      * @return self
  435.      */
  436.     public function removePicture(): self{
  437.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  438.         if ($file) {
  439.             $this->getFileService()->delete($file);
  440.         }
  441.         return $this;
  442.     }
  443.     /**
  444.      * @return int|null
  445.      */
  446.     public function getOldId(): ?int
  447.     {
  448.         return $this->oldId;
  449.     }
  450.     /**
  451.      * @param string $oldId
  452.      *
  453.      * @return self
  454.      */
  455.     public function setOldId(string $oldId): self
  456.     {
  457.         $this->oldId = $oldId;
  458.         return $this;
  459.     }
  460.     /**
  461.      * @return string|null
  462.      */
  463.     public function getOldUrl(): ?string
  464.     {
  465.         return $this->oldUrl;
  466.     }
  467.     /**
  468.      * @param string $oldUrl
  469.      *
  470.      * @return self
  471.      */
  472.     public function setOldUrl(string $oldUrl): self
  473.     {
  474.         $this->oldUrl = $oldUrl;
  475.         return $this;
  476.     }
  477.     /**
  478.      * @return int
  479.      */
  480.     public function getViews(): ?int
  481.     {
  482.         return $this->views;
  483.     }
  484.     /**
  485.      * @return self
  486.      */
  487.     public function setViews(string $views): self
  488.     {
  489.         $this->views = $views;
  490.         return $this;
  491.     }
  492.     /**
  493.      * @return self
  494.      */
  495.     public function addViews(): self
  496.     {
  497.         $this->views++;
  498.         return $this;
  499.     }
  500.     /**
  501.      * @return int
  502.      */
  503.     public function getContentChars(): ?int
  504.     {
  505.         return $this->contentChars;
  506.     }
  507.     
  508.     /**
  509.      * @return self
  510.      */
  511.     public function setContentChars(string $contentChars): self
  512.     {
  513.         $this->contentChars = $contentChars;
  514.         return $this;
  515.     }
  516.     /**
  517.      * @return int
  518.      */
  519.     public function getContentWords(): ?int
  520.     {
  521.         return $this->contentWords;
  522.     }
  523.     
  524.     /**
  525.      * @return self
  526.      */
  527.     public function setContentWords(string $contentWords): self
  528.     {
  529.         $this->contentWords = $contentWords;
  530.         return $this;
  531.     }
  532.     /**
  533.      * Get the value of keywords
  534.      */ 
  535.     public function getKeywords(): ?string
  536.     {
  537.         return $this->keywords;
  538.     }
  539.     /**
  540.      * Set the value of keywords
  541.      *
  542.      * @return  self
  543.      */ 
  544.     public function setKeywords(?string $keywords)
  545.     {
  546.         $this->keywords = $keywords;
  547.         return $this;
  548.     }
  549.     public function __toString() {
  550.         return $this->getTitle();
  551.     }
  552.     /**
  553.      * Specify data which should be serialized to JSON
  554.      *
  555.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  556.      * @return mixed data which can be serialized by <b>json_encode</b>,
  557.      * which is a value of any type other than a resource.
  558.      * @since 5.4.0
  559.      */
  560.     public function jsonSerialize() {
  561.         return [
  562.             'id' => $this->getId(),
  563.             'picture' => $this->getPicture(),
  564.             'company' => $this->getCompany(),
  565.             'categories' => $this->getCategories()->getValues(),
  566.             'title' => $this->getTitle(),
  567.             'author' => $this->getAuthor(),
  568.             'descriptions' => $this->getDescription(),
  569.             'createdAt' => $this->getCreatedAt(),
  570.             'publishedAt' => $this->getPublishedAt(),
  571.             'featured' => $this->getFeatured(),
  572.             'companyFeatured' => $this->getCompanyFeatured(),
  573.             'main' => $this->getMain(),
  574.             'active' => $this->getActive(),
  575.             'slug' => $this->getSlug(),
  576.             'oldId' => $this->getOldId(),
  577.             'canonicalUrl' => $this->getCanonicalUrl(),
  578.             'keywords' => explode(',',$this->getKeywords())
  579.         ];
  580.     }
  581. }