<?php
namespace App\Entity\Formation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Image;
use App\Repository\Formation\ActivityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Traits\Actions;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OrderBy;
#[ORM\Entity(repositoryClass: ActivityRepository::class)]
#[ORM\Table(name: 'hs_tc_formation_activity')]
#[ApiResource]
class Activity implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: Category::class, cascade: ['persist'], inversedBy: 'activites')]
private $category;
#[ORM\ManyToOne(targetEntity: Domaine::class, cascade: ['persist'])]
private $domaine;
#[ORM\Column(name: "title", type: "string", length: 255, unique: true)]
private $title;
#[ORM\Column(name: "title_ar", type: "string", length: 255, nullable: true)]
private ?string $titleAr = null;
#[ORM\Column(name: "diploma", type: "string", length: 255, nullable: true)]
private ?string $diploma;
#[ORM\Column(name: 'slug', type: 'string', length: 255)]
private $slug;
#[ORM\OneToMany(mappedBy: "activity", targetEntity: "Unite")]
#[ORM\OrderBy(["ref" => "ASC"])]
private ?Collection $unites = null;
#[ORM\OneToMany(mappedBy: "activity", targetEntity: "Classe")]
private ?Collection $classes = null;
#[ORM\Column(name: 'forder', type: 'integer', nullable: true)]
private ?int $forder;
#[ORM\Column(name: 'featured', type: 'boolean')]
private bool $featured = false;
#[ORM\OneToMany(mappedBy: 'activity', targetEntity: Image::class, cascade: ['all'])]
private ?Collection $images = null;
private $file;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
$this->featured=false;
$this->unites = new ArrayCollection();
$this->images = new ArrayCollection();
}
/**
* Set title
*
* @param string $title
*
* @return Activity
*/
public function setTitle($title)
{
$this->title = $title;
$this->setSlug($this->slugify($title));
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string
*/
public function getTitleAr(): ?string
{
return $this->titleAr;
}
/**
* @param string $titleAr
*/
public function setTitleAr(?string $titleAr): Activity
{
$this->titleAr = $titleAr;
return $this;
}
/**
* Set diploma
*
* @param string $diploma
*
* @return Activity
*/
public function setDiploma($diploma)
{
$this->diploma = $diploma;
return $this;
}
/**
* Get diploma
*
* @return string
*/
public function getDiploma()
{
return $this->diploma;
}
public function getDiplomaText(): ?string
{
$diplomaChoices = [
'Aucun' => 'Aucun diplôme',
'FC' => 'Formation Continue',
'AP Comptabilité' => 'Aptitude Professionnelle : Comptabilité',
'AP AMPP' => 'Aptitude Professionnelle : Assistant Médicale',
'QP AC' => 'Qualification Professionnelle : Attestation de Conduite',
'CEL Fr A1' => 'Certificat de langue : Français Niveau A1',
'CEL Fr A2' => 'Certificat de langue : Français Niveau A2',
'CEL Fr B1' => 'Certificat de langue : Français Niveau B1',
'CEL Fr B2' => 'Certificat de langue : Français Niveau B2',
'CEL Fr A1+' => 'Certificat de langue : Français Niveau A1+',
'CEL Fr A2+' => 'Certificat de langue : Français Niveau A2+',
'CEL Fr B1+' => 'Certificat de langue : Français Niveau B1+',
'CEL Fr B2+' => 'Certificat de langue : Français Niveau B2+',
'CEL Fr C1' => 'Certificat de langue : Français Niveau C1',
'CEL Fr C2' => 'Certificat de langue : Français Niveau C2',
'CEL En A1' => 'Certificat de langue : Anglais Niveau A1',
'CEL En A2' => 'Certificat de langue : Anglais Niveau A2',
'CEL En B1' => 'Certificat de langue : Anglais Niveau B1',
'CEL En B2' => 'Certificat de langue : Anglais Niveau B2',
'CEL En A1+' => 'Certificat de langue : Anglais Niveau A1+',
'CEL En A2+' => 'Certificat de langue : Anglais Niveau A2+',
'CEL En B1+' => 'Certificat de langue : Anglais Niveau B1+',
'CEL En B2+' => 'Certificat de langue : Anglais Niveau B2+',
'CEL En C1' => 'Certificat de langue : Anglais Niveau C1',
'CEL En C2' => 'Certificat de langue : Anglais Niveau C2',
'CEL Es A1' => 'Certificat de langue : Espagnole Niveau A1',
'CEL Es A2' => 'Certificat de langue : Espagnole Niveau A2',
'CEL Es B1' => 'Certificat de langue : Espagnole Niveau B1',
'CEL Es B2' => 'Certificat de langue : Espagnole Niveau B2',
'CEL Es A1+' => 'Certificat de langue : Espagnole Niveau A1+',
'CEL Es A2+' => 'Certificat de langue : Espagnole Niveau A2+',
'CEL Es B1+' => 'Certificat de langue : Espagnole Niveau B1+',
'CEL Es B2+' => 'Certificat de langue : Espagnole Niveau B2+',
'CEL Es C1' => 'Certificat de langue : Espagnole Niveau C1',
'CEL Es C2' => 'Certificat de langue : Espagnole Niveau C2',
'CEL De A1' => 'Certificat de langue : Allemand Niveau A1',
'CEL De A2' => 'Certificat de langue : Allemand Niveau A2',
'CEL De B1' => 'Certificat de langue : Allemand Niveau B1',
'CEL De B2' => 'Certificat de langue : Allemand Niveau B2',
'CEL De A1+' => 'Certificat de langue : Allemand Niveau A1+',
'CEL De A2+' => 'Certificat de langue : Allemand Niveau A2+',
'CEL De B1+' => 'Certificat de langue : Allemand Niveau B1+',
'CEL De B2+' => 'Certificat de langue : Allemand Niveau B2+',
'CEL De C1' => 'Certificat de langue : Allemand Niveau C1',
'CEL De C2' => 'Certificat de langue : Allemand Niveau C2',
];
return $diplomaChoices[$this->diploma] ?? 'Aucun diplôme';
}
public function getLevel(): ?string
{
// Vérifie si le diplôme est un certificat de langue
if (strpos($this->diploma, 'CEL ') === 0) {
// Utilise une expression régulière pour extraire le niveau (A1, A2, B1, etc.)
if (preg_match('/CEL \w+ (\w+\+?)/', $this->diploma, $matches)) {
return $matches[1]; // Retourne le niveau (A1, A2, etc.)
}
}
return null; // Retourne null si ce n'est pas un certificat de langue
}
public function getLangue(): ?string
{
// Vérifie si le diplôme est un certificat de langue
if (strpos($this->diploma, 'CEL ') === 0) {
// Utilise une expression régulière pour extraire le code de langue (Fr, En, Es, De)
if (preg_match('/CEL (\w+) \w+/', $this->diploma, $matches)) {
// // Convertit le code de langue en nom complet
// $langueCodes = [
// 'Fr' => 'Français',
// 'En' => 'Anglais',
// 'Es' => 'Espagnole',
// 'De' => 'Allemand', // Allemand ajouté
// ];
return $matches[1] ?? null; // Retourne la langue ou null si non trouvé
}
}
return null; // Retourne null si ce n'est pas un certificat de langue
}
public function getSlug()
{
return $this->slug;
}
public function setSlug($slug)
{
$this->slug = $slug;
}
public function __toString(): string
{
return (string) $this->title;
}
public function getName(){
return $this->diploma.' en '.$this->title;
}
/**
* Add unite
*
*
* @return Activity
*/
public function addUnite(Unite $unite)
{
$unite->setActivity($this);
$this->unites[] = $unite;
return $this;
}
/**
* Remove unite
*/
public function removeUnite(Unite $unite)
{
$this->unites->removeElement($unite);
}
/**
* Get unites
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUnites()
{
return $this->unites;
}
/**
* Add classe
*
*
* @return Activity
*/
public function addClasse(Classe $classe)
{
$classe->setActivity($this);
$this->classes[] = $classe;
return $this;
}
/**
* Remove classe
*/
public function removeClasse(Classe $classe)
{
$this->classes->removeElement($classe);
}
/**
* Get classes
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getClasses()
{
return $this->classes;
}
public function getClasseOpned()
{
/** @var Classe $classe */
foreach($this->classes as $classe){
if($classe->isOpened()) return $classe;
}
return null;
}
/**
* @return mixed
*/
public function getForder()
{
return $this->forder;
}
/**
* @return Activity
*/
public function setForder(mixed $forder)
{
$this->forder = $forder;
return $this;
}
/**
* Add image
*
*
* @return Activity
*/
public function addImage(Image $image)
{
$this->images[] = $image;
return $this;
}
/**
* Remove image
*/
public function removeImage(Image $image)
{
$this->images->removeElement($image);
}
/**
* Get images
*
* @return Collection
*/
public function getImages()
{
if($this->images->isEmpty()){
$imageVide=new Image();
$imageVide->setUrl("aucun.jpg");
$this->images->add($imageVide);
}
return $this->images;
}
/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* @param mixed $file
* @return Activity
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory($category): void
{
$this->category = $category;
}
/**
* @return mixed
*/
public function getDomaine()
{
return $this->domaine;
}
/**
* @param mixed $domaine
*/
public function setDomaine($domaine): void
{
$this->domaine = $domaine;
}
public function isFeatured(): bool
{
return $this->featured;
}
public function setFeatured(bool $featured): void
{
$this->featured = $featured;
}
public function getButtonFeatured()
{
$confirm="data-toggle='confirmation'
data-btn-ok-label='Oui'
data-btn-ok-icon='glyphicon glyphicon-ok'
data-btn-ok-class='btn-success'
data-btn-cancel-label='Non'
data-btn-cancel-icon='glyphicon glyphicon-remove'
data-btn-cancel-class='btn-danger'
data-title='Changer statut ?'
data-popout='true'
data-content='Voulez-vous vraiment changer le statut de cet élément ?' ";
if($this->featured)
$icon= "<i class=\"text-green fa fa-toggle-on\"> Oui</i>";
else
$icon= "<i class=\"text-red fa fa-toggle-off\"> Non</i>";
$btn="<a ".$confirm." href='javascript:void(0)' class='boledit' data-boledit-id='".$this->id."' data-boledit-route='".$this->getRoute('featured')."'>".$icon."</a> </td>";
return $btn;
}
public function slugify($title)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', (string) $title);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^\-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
}