MediaVideo.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MediaVideoRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. #[ORM\Table(name: "media_video")]
  7. #[ORM\Entity(repositoryClass: MediaVideoRepository::class)]
  8. #[ORM\HasLifecycleCallbacks] // Permet d’utiliser des événements
  9. class MediaVideo
  10. {
  11. #[ORM\Column(name: "id", type: "integer")]
  12. #[ORM\Id]
  13. #[ORM\GeneratedValue(strategy: "AUTO")]
  14. private $id;
  15. #[ORM\Column(name: "type", type: "string", length: 191)]
  16. private $type;
  17. #[ORM\Column(name: "identif", type: "string", length: 191)]
  18. private $identif;
  19. #[Assert\Regex(
  20. pattern: "#^(http|https)://(youtu.be|www.youtube.com|www.dailymotion.com|vimeo.com|video.fdlibre.eu)/#",
  21. match: true,
  22. message: "L'url doit correspondre à l'url d'une vidéo Youtube, DailyMotion, Vimeo ou Peertube(fdlibre)"
  23. )]
  24. private $url;
  25. public function setFromTmdb(string $type, string $identif): void
  26. {
  27. $this->setType($type);
  28. $this->setIdentif($identif);
  29. }
  30. public function getId(): ?int
  31. {
  32. return $this->id;
  33. }
  34. public function getUrl(): ?string
  35. {
  36. return $this->url();
  37. }
  38. public function setUrl(string $url): self
  39. {
  40. $this->url = $url;
  41. $this->extractIdentif();
  42. return $this;
  43. }
  44. public function setType(string $type): self
  45. {
  46. $this->type = $type;
  47. return $this;
  48. }
  49. public function getType(): ?string
  50. {
  51. return $this->type;
  52. }
  53. public function setIdentif(string $identif): self
  54. {
  55. $this->identif = $identif;
  56. return $this;
  57. }
  58. public function getIdentif(): ?string
  59. {
  60. return $this->identif;
  61. }
  62. /////////////////////
  63. // Méthodes
  64. private function peertubeId($url): void
  65. {
  66. $tableau = explode("/", $url);
  67. $id = $tableau[count($tableau)-1];
  68. $this->setIdentif($id);
  69. $this->setType('peertube');
  70. }
  71. private function youtubeId($url): void
  72. {
  73. $tableaux = explode("=", $url); // découpe l’url en deux avec le signe ‘=’
  74. $this->setIdentif($tableaux[1]); // ajoute l’identifiant à l’attribut identif
  75. $this->setType('youtube'); // signale qu’il s’agit d’une video youtube et l’inscrit dans l’attribut $type
  76. }
  77. private function youtubeCourteId($url): void
  78. {
  79. $tableaux = explode("/", $url); // on découpe l’url grâce au « / »
  80. $id = $tableaux[count($tableaux)-1]; // on retient la dernière partie qui contient l’identifiant
  81. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  82. $this->setType('youtube'); // signale qu’il s’agit d’une video youtube et l’inscrit dans l’attribut $type
  83. }
  84. private function dailymotionId($url): void
  85. {
  86. $cas = explode("/", $url); // On sépare la première partie de l'url des 2 autres
  87. $idb = $cas[4]; // On récupère la partie qui nous intéressent
  88. $bp = explode("_", $idb); // On sépare l'identifiant du reste
  89. $id = $bp[0]; // On récupère l'identifiant
  90. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  91. $this->setType('dailymotion'); // signale qu’il s’agit d’une video dailymotion et l’inscrit dans l’attribut $type
  92. }
  93. private function vimeoId($url): void
  94. {
  95. $tableaux = explode("/", $url); // on découpe l’url grâce au « / »
  96. $id = $tableaux[count($tableaux)-1]; // on reticent la dernière partie qui contient l’identifiant
  97. $this->setIdentif($id); // ajoute l’identifiant à l’attribut identif
  98. $this->setType('vimeo'); // signale qu’il s’agit d’une video vimeo et l’inscrit dans l’attribut $type
  99. }
  100. //#[ORM\PrePersist()] // Les trois événements suivant s’exécutent avant que l’entité soit enregistrée
  101. //#[ORM\PreUpdate()]
  102. //#[ORM\PreFlush()]
  103. public function extractIdentif(): void
  104. {
  105. $url = $this->url; // on récupère l’url
  106. if (preg_match("#^(http|https)://video.fdlibre.eu/#", $url)) // Si c'est peertube fdlibre
  107. {
  108. $this->peertubeId($url);
  109. }
  110. if (preg_match("#^(http|https)://www.youtube.com/#", $url)) // Si c’est une url Youtube on execute la fonction correspondante
  111. {
  112. $this->youtubeId($url);
  113. }
  114. if (preg_match("#^(http|https)://youtu.be/#", $url)) // Si c’est une url Youtube on execute la fonction correspondante
  115. {
  116. $this->youtubeCourteId($url);
  117. }
  118. else if((preg_match("#^(http|https)://www.dailymotion.com/#", $url))) // Si c’est une url Dailymotion on execute la fonction correspondante
  119. {
  120. $this->dailymotionId($url);
  121. }
  122. else if((preg_match("#^(http|https)://vimeo.com/#", $url))) // Si c’est une url Vimeo on execute la fonction correspondante
  123. {
  124. $this->vimeoId($url);
  125. }
  126. }
  127. public function embedUrl(): ?string
  128. {
  129. $control = $this->getType(); // on récupère le type de la vidéo
  130. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  131. if ($control == 'youtube') {
  132. $embed = "https://www.youtube-nocookie.com/embed/" . $id;
  133. return $embed;
  134. } else if ($control == 'dailymotion') {
  135. $embed = "https://www.dailymotion.com/embed/video/" . $id;
  136. return $embed;
  137. } else if ($control == 'vimeo') {
  138. $embed = "https://player.vimeo.com/video/" . $id;
  139. return $embed;
  140. } else if ($control == 'peertube') {
  141. $embed = "https://video.fdlibre.eu/videos/embed/" . $id;
  142. return $embed;
  143. } else {
  144. return null;
  145. }
  146. }
  147. public function url(): ?string
  148. {
  149. $control = $this->getType(); // on récupère le type de la vidéo
  150. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  151. if ($control == 'youtube') {
  152. $embed = "https://www.youtube.com/watch?v=" . $id;
  153. return $embed;
  154. } else if ($control == 'dailymotion') {
  155. $embed = "https://www.dailymotion.com/video/" . $id;
  156. return $embed;
  157. } else if ($control == 'vimeo') {
  158. $embed = "https://vimeo.com/" . $id;
  159. return $embed;
  160. } else if ($control == 'peertube') {
  161. $embed = "https://video.fdlibre.eu/videos/watch/" . $id;
  162. return $embed;
  163. } else {
  164. return null;
  165. }
  166. }
  167. public function image(): ?string
  168. {
  169. $control = $this->getType(); // on récupère le type de la vidéo
  170. $id = strip_tags($this->getIdentif()); // on récupère son identifiant
  171. if ($control == 'youtube') {
  172. $image = 'https://img.youtube.com/vi/' . $id . '/hqdefault.jpg';
  173. return $image;
  174. } else if ($control == 'dailymotion') {
  175. $image = 'https://www.dailymotion.com/thumbnail/150x120/video/' . $id . '';
  176. return $image;
  177. } else if ($control == 'vimeo') {
  178. $hash = unserialize(file_get_contents("https://vimeo.com/api/v2/video/" . $id . ".php"));
  179. $image = $hash[0]['thumbnail_small'];
  180. return $image;
  181. } else {
  182. return null;
  183. }
  184. }
  185. public function video(): string
  186. {
  187. $video = "<iframe src='".$this->embedUrl()."' allowfullscreen></iframe>";
  188. return $video;
  189. }
  190. }