src/Entity/Event/Event.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Event;
  3. use App\Entity\Building\Building;
  4. use App\Entity\Client\Client;
  5. use App\Entity\Extension\BlameableTrait;
  6. use App\Entity\Extension\TimestampableTrait;
  7. use App\Entity\File;
  8. use App\Entity\Gauge\Gauge;
  9. use App\Entity\User;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Doctrine\ORM\Mapping\MappedSuperclass;
  15. /**
  16. * @MappedSuperclass
  17. * @ORM\Entity()
  18. * @ORM\Table(name="events")
  19. * @ORM\InheritanceType("JOINED")
  20. * @ORM\DiscriminatorColumn(name="discr_type", type="string")
  21. * @ORM\DiscriminatorMap({"event" = "Event", "action" = "Action", "notice" = "Notice"})
  22. */
  23. class Event
  24. {
  25. use BlameableTrait;
  26. use TimestampableTrait;
  27. public const CONTEXT_BUILDING = 1;
  28. public const CONTEXT_GAUGE = 2;
  29. public const CONTEXT_CLIENT = 3;
  30. public const CONTEXT_MEDIUM = 4;
  31. /**
  32. * @var null|int
  33. *
  34. * @ORM\Id()
  35. * @ORM\GeneratedValue()
  36. * @ORM\Column(type="integer")
  37. */
  38. protected ?int $id = null;
  39. /**
  40. * @var string|null
  41. *
  42. * @Assert\Type("string")
  43. * @ORM\Column(type="string", nullable=true)
  44. */
  45. protected ?string $name = null;
  46. /**
  47. * @var Client
  48. * @ORM\ManyToOne(targetEntity="App\Entity\Client\Client")
  49. * @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
  50. */
  51. private Client $client;
  52. /**
  53. * datum ke které se akce váže - TODO optat se
  54. * @var \DateTime|null
  55. * @ORM\Column(type="date", name="event_date", nullable=true)
  56. */
  57. protected ?\DateTime $date;
  58. /**
  59. * @var int
  60. *
  61. * @ORM\Column(type="smallint", nullable=true)
  62. */
  63. protected int $state;
  64. /**
  65. * @var Collection|Building[]
  66. *
  67. * @ORM\ManyToMany(targetEntity="App\Entity\Building\Building", inversedBy="events", cascade={"persist"})
  68. */
  69. protected $buildings;
  70. /**
  71. * @var Collection|Gauge[]
  72. *
  73. * @ORM\ManyToMany(targetEntity="App\Entity\Gauge\Gauge", inversedBy="events", cascade={"persist"})
  74. */
  75. protected $gauges;
  76. /**
  77. * @var null|int
  78. *
  79. * @Assert\Type("int")
  80. * @ORM\Column(type="smallint", nullable=true)
  81. */
  82. protected ?int $context = null;
  83. /**
  84. * Medium identifier (e.g., electricity, gas, water) for medium-context events
  85. * @var int|null
  86. *
  87. * @Assert\Type("int")
  88. * @ORM\Column(name="medium_id", type="smallint", nullable=true)
  89. */
  90. protected ?int $mediumId = null;
  91. /**
  92. * @var null|string
  93. *
  94. * @ORM\Column(type="text", nullable=true)
  95. * @Assert\Type(type="string")
  96. */
  97. protected ?string $description = null;
  98. /**
  99. * navázaný dokument z kategorie dokumenty|příloha
  100. * @var File|null
  101. *
  102. * @ORM\ManyToOne(targetEntity="App\Entity\File", inversedBy="events")
  103. * @ORM\JoinColumn(nullable=true)
  104. */
  105. private ?File $file = null;
  106. /**
  107. * SYSTÉMOVÝ KÓD
  108. * @var string|null
  109. * @ORM\Column(type="string", nullable=true)
  110. */
  111. protected ?string $systemCode;
  112. /**
  113. * INTERNÍ KÓD
  114. * @var string|null
  115. * @ORM\Column(type="string", nullable=true)
  116. */
  117. protected ?string $internalCode;
  118. /**
  119. * @var User|null $createdBy
  120. *
  121. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  122. * @ORM\JoinColumn(referencedColumnName="id", nullable=true)
  123. */
  124. protected ?User $guarantor; // todo lepší překlad
  125. /**
  126. * @var null|string
  127. *
  128. * @ORM\Column(type="text", nullable=true)
  129. * @Assert\Type(type="string")
  130. */
  131. protected ?string $note;
  132. /**
  133. * TERMÍN PRO VYŘEŠENÍ AKCE
  134. * @var \DateTimeInterface|null
  135. * @ORM\Column(type="date", nullable=true)
  136. */
  137. protected ?\DateTimeInterface $deadline = null;
  138. /**
  139. * @var bool
  140. * @ORM\Column(type="boolean")
  141. */
  142. protected bool $showInGraph = false;
  143. /**
  144. * @var bool|null
  145. * @ORM\Column(type="boolean", nullable=true)
  146. */
  147. protected ?bool $notificationEnabled = false;
  148. /**
  149. * @var int|null
  150. * @ORM\Column(type="integer", nullable=true)
  151. */
  152. protected ?int $notification;
  153. /**
  154. * @var \DateTime|null
  155. *
  156. * @ORM\Column(name="solved_at", type="datetime", nullable=true)
  157. */
  158. protected ?\DateTime $solvedAt = null;
  159. /**
  160. * @var User|null $updatedBy
  161. *
  162. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  163. * @ORM\JoinColumn(referencedColumnName="id", nullable=true)
  164. */
  165. protected ?User $solvedBy;
  166. /**
  167. * simple array of connected buildings
  168. * @var array
  169. */
  170. private $buildingsArray = [];
  171. /**
  172. * simple array of connected gauges
  173. * @var array
  174. */
  175. private $gaugesArray = [];
  176. /**
  177. * @var null|int
  178. *
  179. * @Assert\Type("int")
  180. * @ORM\Column(type="integer", nullable=true)
  181. */
  182. protected ?int $serialNumber = null;
  183. /**
  184. * File constructor.
  185. */
  186. public function __construct()
  187. {
  188. $this->date = new \DateTime();
  189. $this->buildings = new ArrayCollection();
  190. $this->gauges = new ArrayCollection();
  191. }
  192. /**
  193. * @return int|null
  194. */
  195. public function getId(): ?int
  196. {
  197. return $this->id;
  198. }
  199. /**
  200. * @param int|null $id
  201. * @return Event
  202. */
  203. public function setId(?int $id): Event
  204. {
  205. $this->id = $id;
  206. return $this;
  207. }
  208. /**
  209. * @return string|null
  210. */
  211. public function getName(): ?string
  212. {
  213. return $this->name;
  214. }
  215. /**
  216. * @param string|null $name
  217. * @return Event
  218. */
  219. public function setName(?string $name): Event
  220. {
  221. $this->name = $name;
  222. return $this;
  223. }
  224. /**
  225. * @return int
  226. */
  227. public function getState(): int
  228. {
  229. return $this->state;
  230. }
  231. /**
  232. * @param int $state
  233. * @return Event
  234. */
  235. public function setState(int $state): Event
  236. {
  237. $this->state = $state;
  238. return $this;
  239. }
  240. /**
  241. * @return Building[]|Collection
  242. */
  243. public function getBuildings()
  244. {
  245. return $this->buildings;
  246. }
  247. /**
  248. * @param Building[]|Collection $buildings
  249. * @return Event
  250. */
  251. public function setBuildings($buildings)
  252. {
  253. $this->buildings = $buildings;
  254. return $this;
  255. }
  256. /**
  257. * @return Gauge[]|Collection
  258. */
  259. public function getGauges()
  260. {
  261. return $this->gauges;
  262. }
  263. /**
  264. * @param Gauge[]|Collection $gauges
  265. * @return Event
  266. */
  267. public function setGauges($gauges)
  268. {
  269. $this->gauges = $gauges;
  270. return $this;
  271. }
  272. /**
  273. * @return int|null
  274. */
  275. public function getContext(): ?int
  276. {
  277. return $this->context;
  278. }
  279. /**
  280. * @param int|null $context
  281. * @return Event
  282. */
  283. public function setContext(?int $context): Event
  284. {
  285. $this->context = $context;
  286. return $this;
  287. }
  288. /**
  289. * @return int|null
  290. */
  291. public function getMediumId(): ?int
  292. {
  293. return $this->mediumId;
  294. }
  295. /**
  296. * @param int|null $mediumId
  297. * @return Event
  298. */
  299. public function setMediumId(?int $mediumId): Event
  300. {
  301. $this->mediumId = $mediumId;
  302. return $this;
  303. }
  304. /**
  305. * @return string|null
  306. */
  307. public function getDescription(): ?string
  308. {
  309. return $this->description;
  310. }
  311. /**
  312. * @param string|null $description
  313. * @return Event
  314. */
  315. public function setDescription(?string $description): Event
  316. {
  317. $this->description = $description;
  318. return $this;
  319. }
  320. /**
  321. * @return string|null
  322. */
  323. public function getSystemCode(): ?string
  324. {
  325. return $this->systemCode;
  326. }
  327. /**
  328. * @param string|null $systemCode
  329. * @return Event
  330. */
  331. public function setSystemCode(?string $systemCode): Event
  332. {
  333. $this->systemCode = $systemCode;
  334. return $this;
  335. }
  336. /**
  337. * @return User|null
  338. */
  339. public function getGuarantor(): ?User
  340. {
  341. return $this->guarantor;
  342. }
  343. /**
  344. * @param User|null $guarantor
  345. * @return Event
  346. */
  347. public function setGuarantor(?User $guarantor): Event
  348. {
  349. $this->guarantor = $guarantor;
  350. return $this;
  351. }
  352. /**
  353. * @return string|null
  354. */
  355. public function getNote(): ?string
  356. {
  357. return $this->note;
  358. }
  359. /**
  360. * @param string|null $note
  361. * @return Event
  362. */
  363. public function setNote(?string $note): Event
  364. {
  365. $this->note = $note;
  366. return $this;
  367. }
  368. /**
  369. * @return \DateTimeInterface|null
  370. */
  371. public function getDeadline(): ?\DateTimeInterface
  372. {
  373. return $this->deadline;
  374. }
  375. /**
  376. * @param \DateTimeInterface|null $deadline
  377. * @return Event
  378. */
  379. public function setDeadline(?\DateTimeInterface $deadline): Event
  380. {
  381. $this->deadline = $deadline;
  382. return $this;
  383. }
  384. /**
  385. * @return bool
  386. */
  387. public function isShowInGraph(): bool
  388. {
  389. return $this->showInGraph;
  390. }
  391. /**
  392. * @param bool $showInGraph
  393. * @return Event
  394. */
  395. public function setShowInGraph(bool $showInGraph): Event
  396. {
  397. $this->showInGraph = $showInGraph;
  398. return $this;
  399. }
  400. /**
  401. * @return string|null
  402. */
  403. public function getInternalCode(): ?string
  404. {
  405. return $this->internalCode;
  406. }
  407. /**
  408. * @param string|null $internalCode
  409. * @return Event
  410. */
  411. public function setInternalCode(?string $internalCode): Event
  412. {
  413. $this->internalCode = $internalCode;
  414. return $this;
  415. }
  416. /**
  417. * @return bool|null
  418. */
  419. public function getNotificationEnabled(): ?bool
  420. {
  421. return $this->notificationEnabled;
  422. }
  423. /**
  424. * @param bool|null $notificationEnabled
  425. * @return Event
  426. */
  427. public function setNotificationEnabled(?bool $notificationEnabled): Event
  428. {
  429. $this->notificationEnabled = $notificationEnabled;
  430. return $this;
  431. }
  432. /**
  433. * @return int|null
  434. */
  435. public function getNotification(): ?int
  436. {
  437. return $this->notification;
  438. }
  439. /**
  440. * @param int|null $notification
  441. * @return Event
  442. */
  443. public function setNotification(?int $notification): Event
  444. {
  445. $this->notification = $notification;
  446. return $this;
  447. }
  448. /**
  449. * @return \DateTime|null
  450. */
  451. public function getSolvedAt(): ?\DateTime
  452. {
  453. return $this->solvedAt;
  454. }
  455. /**
  456. * @param \DateTime|null $solvedAt
  457. * @return Event
  458. */
  459. public function setSolvedAt(?\DateTime $solvedAt): Event
  460. {
  461. $this->solvedAt = $solvedAt;
  462. return $this;
  463. }
  464. /**
  465. * @return User|null
  466. */
  467. public function getSolvedBy(): ?User
  468. {
  469. return $this->solvedBy;
  470. }
  471. /**
  472. * @param User|null $solvedBy
  473. * @return Event
  474. */
  475. public function setSolvedBy(?User $solvedBy): Event
  476. {
  477. $this->solvedBy = $solvedBy;
  478. return $this;
  479. }
  480. /**
  481. * @return Client
  482. */
  483. public function getClient(): Client
  484. {
  485. return $this->client;
  486. }
  487. /**
  488. * @param Client $client
  489. * @return Event
  490. */
  491. public function setClient(Client $client): Event
  492. {
  493. $this->client = $client;
  494. return $this;
  495. }
  496. /**
  497. * @return \DateTime|null
  498. */
  499. public function getDate(): ?\DateTime
  500. {
  501. return $this->date;
  502. }
  503. /**
  504. * @param \DateTime|null $date
  505. * @return Event
  506. */
  507. public function setDate(?\DateTime $date): Event
  508. {
  509. $this->date = $date;
  510. return $this;
  511. }
  512. /**
  513. * @return array
  514. */
  515. public function getBuildingsArray(): array
  516. {
  517. return $this->buildingsArray;
  518. }
  519. /**
  520. * @param array $buildingsArray (buildingsArray in array by fileId as key)
  521. * @return self
  522. */
  523. public function setBuildingsArrayByArray(array $buildingsArray): self
  524. {
  525. if (key_exists($this->getId(), $buildingsArray)) {
  526. $this->buildingsArray = $buildingsArray[$this->getId()];
  527. }
  528. return $this;
  529. }
  530. /**
  531. * @return array
  532. */
  533. public function getGaugesArray(): array
  534. {
  535. return $this->gaugesArray;
  536. }
  537. /**
  538. * @param array $gaugesArray (buildingsArray in array by fileId as key)
  539. * @return self
  540. */
  541. public function setGaugesArrayByArray(array $gaugesArray): self
  542. {
  543. if (key_exists($this->getId(), $gaugesArray)) {
  544. $this->gaugesArray = $gaugesArray[$this->getId()];
  545. }
  546. return $this;
  547. }
  548. /**
  549. * @param bool $fromArray
  550. * @return array
  551. */
  552. public function getBuildingsNames(bool $fromArray): array
  553. {
  554. $buildings = [];
  555. if ($fromArray) {
  556. foreach ($this->getBuildingsArray() as $building) {
  557. $buildings[] = $building['name'];
  558. }
  559. } else {
  560. foreach ($this->getBuildings() as $building) {
  561. $buildings[] = $building->getName();
  562. }
  563. }
  564. return $buildings;
  565. }
  566. /**
  567. * @param bool $fromArray
  568. * @return array
  569. */
  570. public function getGaugesNames(bool $fromArray) :array
  571. {
  572. $gauges = [];
  573. if ($fromArray) {
  574. foreach ($this->getGaugesArray() as $gauge) {
  575. $gauges[] = $gauge['buildingName'] . ' - ' . $gauge['name'];
  576. }
  577. } else {
  578. foreach ($this->getGauges() as $gauge) {
  579. $gauges[] = $gauge->getBuilding()->getName() . ' - ' . $gauge->getName();
  580. }
  581. }
  582. return $gauges;
  583. }
  584. /**
  585. * @return int|null
  586. */
  587. public function getSerialNumber(): ?int
  588. {
  589. return $this->serialNumber;
  590. }
  591. /**
  592. * @param int|null $serialNumber
  593. * @return Event
  594. */
  595. public function setSerialNumber(?int $serialNumber): Event
  596. {
  597. $this->serialNumber = $serialNumber;
  598. return $this;
  599. }
  600. /**
  601. * @param bool $noBreak
  602. * @return string
  603. */
  604. public function getOutputCode(bool $noBreak):string
  605. {
  606. $code = '';
  607. if ($this->getInternalCode() !== null) {
  608. $code = $this->getInternalCode();
  609. } elseif ($this->getSystemCode() !== null) {
  610. $code = $this->getSystemCode();
  611. }
  612. return $noBreak ? str_replace('-', '&minus;', $code) : $code;
  613. }
  614. /**
  615. * @return File|null
  616. */
  617. public function getFile(): ?File
  618. {
  619. return $this->file;
  620. }
  621. /**
  622. * @param File|null $file
  623. * @return Event
  624. */
  625. public function setFile(?File $file): Event
  626. {
  627. $this->file = $file;
  628. return $this;
  629. }
  630. /**
  631. * @param Gauge $gauge
  632. * @return $this
  633. */
  634. public function addGauge(Gauge $gauge): self
  635. {
  636. if (!$this->gauges->contains($gauge)) {
  637. $this->gauges[] = $gauge;
  638. $this->addBuilding($gauge->getBuilding());
  639. }
  640. return $this;
  641. }
  642. /**
  643. * @param Gauge $gauge
  644. * @return $this
  645. */
  646. public function removeGauge(Gauge $gauge): self
  647. {
  648. if ($this->gauges->contains($gauge)) {
  649. $this->gauges->removeElement($gauge);
  650. }
  651. return $this;
  652. }
  653. /**
  654. * @param Building $building
  655. * @return $this
  656. */
  657. public function addBuilding(Building $building): self
  658. {
  659. if (!$this->buildings->contains($building)) {
  660. $this->buildings[] = $building;
  661. }
  662. return $this;
  663. }
  664. /**
  665. * @param Building $building
  666. * @return $this
  667. */
  668. public function removeBuilding(Building $building): self
  669. {
  670. if ($this->buildings->contains($building)) {
  671. $this->buildings->removeElement($building);
  672. }
  673. return $this;
  674. }
  675. }