PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/AppBundle/Entity/Event.php

https://gitlab.com/aredhel-bazaar/la-taverne-du-hasard
PHP | 466 lines | 174 code | 121 blank | 171 comment | 3 complexity | a6f4bee1c666013f04496c3d9424448e MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Silvesius
  5. * Date: 06/10/2016
  6. * Time: 19:41
  7. */
  8. namespace AppBundle\Entity;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13. * @ORM\Entity(repositoryClass="AppBundle\Repository\EventRepository")
  14. * @ORM\Table(name="event", indexes={})
  15. * @ORM\Cache()
  16. */
  17. class Event
  18. {
  19. const DATE_FORMAT_ICAL = 'Ymd\THis\Z';
  20. const MAX_PER_ADMIN_PAGE = 20;
  21. const TYPE_OPEN_EVENT = 'open_event';
  22. const TYPE_CAKE_EVENT = 'cake_event';
  23. /**
  24. * @var integer
  25. *
  26. * @ORM\Column(name="id", type="integer")
  27. * @ORM\Id
  28. * @ORM\GeneratedValue(strategy="IDENTITY")
  29. */
  30. private $id;
  31. /**
  32. * @var string
  33. *
  34. * @ORM\Column(name="name", type="string", length=100)
  35. */
  36. private $name;
  37. /**
  38. * @ORM\ManyToOne(targetEntity="User")
  39. * @ORM\JoinColumn(name="fk_organizer_id", referencedColumnName="id", nullable=true)
  40. */
  41. private $fkOrganizer;
  42. /**
  43. * @var string
  44. *
  45. * @ORM\Column(name="type", type="string", length=50, nullable=true)
  46. */
  47. private $type;
  48. /**
  49. * @var \DateTime
  50. *
  51. * @ORM\Column(name="start_date", type="datetime")
  52. */
  53. private $startDate;
  54. /**
  55. * @var \DateTime
  56. *
  57. * @ORM\Column(name="end_date", type="datetime")
  58. */
  59. private $endDate;
  60. /**
  61. * @var integer
  62. *
  63. * @ORM\Column(name="places", type="integer", length=2)
  64. */
  65. private $places = 0;
  66. /**
  67. * @var string
  68. *
  69. * @ORM\Column(name="content", type="text")
  70. */
  71. private $content;
  72. /**
  73. * @ORM\ManyToOne(targetEntity="User")
  74. * @ORM\JoinColumn(name="fk_user_id", referencedColumnName="id")
  75. */
  76. private $fkUser;
  77. /**
  78. * @ORM\OneToMany(targetEntity="Subscriber", mappedBy="fkEvent", cascade={"remove"}, fetch="EAGER")
  79. */
  80. private $subscribers;
  81. /**
  82. * @var \DateTime
  83. *
  84. * @ORM\Column(name="creation_date", type="datetime")
  85. */
  86. private $creationDate;
  87. /**
  88. * @var \DateTime
  89. *
  90. * @ORM\Column(name="modification_date", type="datetime")
  91. */
  92. private $modificationDate;
  93. public function __construct()
  94. {
  95. $this->subscribers = new ArrayCollection();
  96. }
  97. /**
  98. * @return int
  99. */
  100. public function getId()
  101. {
  102. return $this->id;
  103. }
  104. /**
  105. * @param int $id
  106. *
  107. * @return $this
  108. */
  109. public function setId(int $id)
  110. {
  111. $this->id = $id;
  112. return $this;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getName()
  118. {
  119. return $this->name;
  120. }
  121. /**
  122. * @param string $name
  123. *
  124. * @return $this
  125. */
  126. public function setName(string $name)
  127. {
  128. $this->name = $name;
  129. return $this;
  130. }
  131. /**
  132. * @return User
  133. */
  134. public function getFkOrganizer()
  135. {
  136. return $this->fkOrganizer;
  137. }
  138. /**
  139. * @param mixed $fkOrganizer
  140. *
  141. * @return $this
  142. */
  143. public function setFkOrganizer($fkOrganizer)
  144. {
  145. $this->fkOrganizer = $fkOrganizer;
  146. return $this;
  147. }
  148. /**
  149. * @return \DateTime
  150. */
  151. public function getStartDate()
  152. {
  153. return $this->startDate;
  154. }
  155. /**
  156. * @param \DateTime $startDate
  157. *
  158. * @return $this
  159. */
  160. public function setStartDate(\DateTime $startDate)
  161. {
  162. $this->startDate = $startDate;
  163. return $this;
  164. }
  165. /**
  166. * @return \DateTime
  167. */
  168. public function getEndDate()
  169. {
  170. return $this->endDate;
  171. }
  172. /**
  173. * @param \DateTime $endDate
  174. *
  175. * @return $this
  176. */
  177. public function setEndDate(\DateTime $endDate)
  178. {
  179. $this->endDate = $endDate;
  180. return $this;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getType()
  186. {
  187. return $this->type;
  188. }
  189. /**
  190. * @param string $type
  191. *
  192. * @return Event
  193. */
  194. public function setType(string $type): Event
  195. {
  196. $this->type = $type;
  197. return $this;
  198. }
  199. /**
  200. * @return int
  201. */
  202. public function getPlaces()
  203. {
  204. return $this->places;
  205. }
  206. /**
  207. * @param int $places
  208. *
  209. * @return $this
  210. */
  211. public function setPlaces(int $places)
  212. {
  213. $this->places = $places;
  214. return $this;
  215. }
  216. /**
  217. * @return string
  218. */
  219. public function getContent()
  220. {
  221. return $this->content;
  222. }
  223. /**
  224. * @param string $content
  225. *
  226. * @return $this
  227. */
  228. public function setContent(string $content)
  229. {
  230. $this->content = $content;
  231. return $this;
  232. }
  233. /**
  234. * @return mixed
  235. */
  236. public function getFkUser()
  237. {
  238. return $this->fkUser;
  239. }
  240. /**
  241. * @param mixed $fkUser
  242. *
  243. * @return $this
  244. */
  245. public function setFkUser($fkUser)
  246. {
  247. $this->fkUser = $fkUser;
  248. return $this;
  249. }
  250. /**
  251. * @return \DateTime
  252. */
  253. public function getCreationDate()
  254. {
  255. return $this->creationDate;
  256. }
  257. /**
  258. * @param \DateTime $creationDate
  259. *
  260. * @return $this
  261. */
  262. public function setCreationDate(\DateTime $creationDate)
  263. {
  264. $this->creationDate = $creationDate;
  265. return $this;
  266. }
  267. /**
  268. * @return \DateTime
  269. */
  270. public function getModificationDate()
  271. {
  272. return $this->modificationDate;
  273. }
  274. /**
  275. * @param \DateTime $modificationDate
  276. *
  277. * @return $this
  278. */
  279. public function setModificationDate(\DateTime $modificationDate)
  280. {
  281. $this->modificationDate = $modificationDate;
  282. return $this;
  283. }
  284. /**
  285. * @return mixed
  286. */
  287. public function getSubscribers()
  288. {
  289. return $this->subscribers;
  290. }
  291. public function addSubscriber(Subscriber $subscriber)
  292. {
  293. $subscriber->setFkEvent($this);
  294. $this->subscribers[] = $subscriber;
  295. return $this;
  296. }
  297. public function removeSubscriber(Subscriber $subscriber)
  298. {
  299. $this->subscribers->removeElement($subscriber);
  300. }
  301. /**
  302. * @param mixed $subscribers
  303. *
  304. * @return Event
  305. */
  306. public function setSubscribers($subscribers)
  307. {
  308. $this->subscribers = $subscribers;
  309. return $this;
  310. }
  311. public function isOpen()
  312. {
  313. $subscribers = $this->getSubscribers();
  314. $nb_subscribers = 0;
  315. /** @var Subscriber $subscriber */
  316. foreach( $subscribers as $subscriber ) {
  317. if( $subscriber->isPresent() ) {
  318. $nb_subscribers++;
  319. }
  320. }
  321. return (
  322. ($this->getPlaces() > $nb_subscribers || $this->getPlaces() == 0)
  323. &&
  324. $this->getStartDate() > new \DateTime()
  325. );
  326. }
  327. /**
  328. * Retourne le flux iCal pour l'évènement
  329. *
  330. * @return string
  331. */
  332. public function getICalFormat()
  333. {
  334. $iCal = 'BEGIN:VCALENDAR'."\n";
  335. $iCal .= 'VERSION:2.0'."\n";
  336. $iCal .= 'PRODID:-//lataverneduhasard/calendrier v1.0//FR'."\n";
  337. $iCal .= 'BEGIN:VEVENT'."\n";
  338. $iCal .= 'DTSTART:'.$this->getStartDate()->format(self::DATE_FORMAT_ICAL)."\n";
  339. $iCal .= 'DTEND:'.$this->getEndDate()->format(self::DATE_FORMAT_ICAL)."\n";
  340. $iCal .= 'SUMMARY:'.$this->getName()."\n";
  341. $iCal .= 'END:VEVENT'."\n";
  342. $iCal .= 'END:VCALENDAR'."\n";
  343. return $iCal;
  344. }
  345. }