<?php
namespace App\Entity;
use App\Repository\CommentaireRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Controller\UploadController;
use Doctrine\ORM\Mapping as ORM;
use App\Controller\ExtensionController;
/**
* @ORM\Entity(repositoryClass=CommentaireRepository::class)
*/
class Commentaire
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="commentaires")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity=Commentaire::class, inversedBy="commentaires")
*/
private $commentaireRep;
/**
* @ORM\ManyToOne(targetEntity=Article::class, inversedBy="commentaires")
* @ORM\JoinColumn(nullable=false)
*/
private $article;
/**
* @ORM\OneToMany(targetEntity=Commentaire::class, mappedBy="commentaireRep")
*/
private $commentaires;
/**
* @ORM\OneToMany(targetEntity=Fichier::class, mappedBy="commentaire", orphanRemoval=true)
*/
private $fichiers;
/**
* @ORM\OneToMany(targetEntity=ReportCommentaire::class, mappedBy="commentaire", orphanRemoval=true)
*/
private $reportCommentaires;
/**
* @ORM\Column(type="boolean")
*/
private $verified;
public function __construct()
{
$this->commentaires = new ArrayCollection();
$this->fichiers = new ArrayCollection();
$this->reportCommentaires = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getDate(): ?\DateTimeImmutable
{
return $this->date;
}
public function setDate(\DateTimeImmutable $date): self
{
$this->date = $date;
return $this;
}
public function getCommentaireRep(): ?self
{
return $this->commentaireRep;
}
public function setCommentaireRep(?self $commentaireRep): self
{
$this->commentaireRep = $commentaireRep;
return $this;
}
public function addReponse(self $commentaire): self
{
if (!$this->commentaires->contains($commentaire)) {
$this->commentaires[] = $commentaire;
$commentaire->setCommentaireRep($this);
}
return $this;
}
public function removeReponse(self $commentaire): self
{
if ($this->commentaires->removeElement($commentaire)) {
// set the owning side to null (unless already changed)
if ($commentaire->getCommentaireRep() === $this) {
$commentaire->setCommentaireRep(null);
}
}
return $this;
}
public function getArticle(): ?Article
{
return $this->article;
}
public function setArticle(?Article $article): self
{
$this->article = $article;
return $this;
}
public function toArray(): Array
{
$controllerExt = new ExtensionController();
$extImage = $controllerExt->getExtImage();
$extPDF = $controllerExt->getExtPDF();
$rep = null;
$mainComment = null;
if($this->commentaireRep != null){
$rep = $this->commentaireRep->toArray();
}
if($this->commentaireRep != null){
$mainComment = $this->commentaireRep->toArray();
}
$controller = new UpLoadController();
$count = count($this->commentaires);
$tmp = [
'id' => $this->id,
'user' => $this->user->toArray(),
'date' => $this->getDateFomatFR(),
'heure' => $this->getHeureFomatFR(),
'content' => $this->content,
'timestamp'=> intval($this->date->getTimestamp()),
'commentaireRep' => $rep,
'mainComment' => $mainComment,
'nbRep' => $count,
// 'images' => [],
'image' => [],
'pdf' => [],
'fichier' => [],
'verified' => $this->verified
];
foreach($this->getFichiers() as $fichier)
{
$arrayFichier = $fichier->toArray();
$tmp["fichier"][] = $arrayFichier;
if(in_array($arrayFichier["ext"], $extImage)){
// $tmp['images'][] = $arrayFichier;
$tmp['image'][] = $arrayFichier;
}
if(in_array($arrayFichier["ext"], $extPDF)){
$tmp['pdf'][] = $arrayFichier;
}
}
// foreach($this->getFichiers() as $fichier)
// {
// $arrayFichier = $fichier->toArray();
// $tmp['test'][] = $arrayFichier["ext"];
// }
return $tmp;
}
public function getDateFomatFR()
{
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
date_default_timezone_set('Europe/Paris');
$date = $this->date->getTimestamp();
$res = strftime('%A %d %B %Y',$date);
return $res;
}
public function getHeureFomatFR()
{
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8');
date_default_timezone_set('Europe/Paris');
$date = $this->date->getTimestamp();
$res = strftime('%HH%M',$date);
return $res;
}
/**
* @return Collection|self[]
*/
public function getCommentaires(): Collection
{
return $this->commentaires;
}
public function addCommentaire(self $commentaire): self
{
if (!$this->commentaires->contains($commentaire)) {
$this->commentaires[] = $commentaire;
$commentaire->setCommentaireRep($this);
}
return $this;
}
public function removeCommentaire(self $commentaire): self
{
if ($this->commentaires->removeElement($commentaire)) {
// set the owning side to null (unless already changed)
if ($commentaire->getCommentaireRep() === $this) {
$commentaire->setCommentaireRep(null);
}
}
return $this;
}
/**
* @return Collection|Fichier[]
*/
public function getFichiers(): Collection
{
return $this->fichiers;
}
public function addFichier(Fichier $fichier): self
{
if (!$this->fichiers->contains($fichier)) {
$this->fichiers[] = $fichier;
$fichier->setCommentaire($this);
}
return $this;
}
public function removeFichier(Fichier $fichier): self
{
if ($this->fichiers->removeElement($fichier)) {
// set the owning side to null (unless already changed)
if ($fichier->getCommentaire() === $this) {
$fichier->setCommentaire(null);
}
}
return $this;
}
/**
* @return Collection|ReportCommentaire[]
*/
public function getReportCommentaires(): Collection
{
return $this->reportCommentaires;
}
public function addReportCommentaire(ReportCommentaire $reportCommentaire): self
{
if (!$this->reportCommentaires->contains($reportCommentaire)) {
$this->reportCommentaires[] = $reportCommentaire;
$reportCommentaire->setCommentaire($this);
}
return $this;
}
public function removeReportCommentaire(ReportCommentaire $reportCommentaire): self
{
if ($this->reportCommentaires->removeElement($reportCommentaire)) {
// set the owning side to null (unless already changed)
if ($reportCommentaire->getCommentaire() === $this) {
$reportCommentaire->setCommentaire(null);
}
}
return $this;
}
public function to_arraytabcom(): Array
{
$tmp = [
'id' => $this->id,
'user' => $this->user->to_arraytab(),
'content' => $this->content,
'date' => $this->date,
'commentaireRep' => $this->commentaireRep,
'article' => $this->article,
'commentaires' => $this->commentaires,
'fichiers' => $this->fichiers,
'reportCommentaires' => $this->reportCommentaires,
'verified' => $this->verified
];
return $tmp;
}
public function getVerified(): ?bool
{
return $this->verified;
}
public function setVerified(bool $verified): self
{
$this->verified = $verified;
return $this;
}
}