src/Entity/File.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Building\Building;
  4. use App\Entity\Client\Client;
  5. use App\Entity\ContractPrice\Contract;
  6. use App\Entity\Event\Event;
  7. use App\Entity\Extension\BlameableTrait;
  8. use App\Entity\Extension\TimestampableTrait;
  9. use App\Entity\Gauge\Gauge;
  10. use App\Entity\Gauge\Invoice;
  11. use App\Repository\FileRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Ramsey\Uuid\Uuid;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Ramsey\Uuid\UuidInterface;
  19. /**
  20. * @ORM\Entity(repositoryClass=FileRepository::class)
  21. * @ORM\Table(name="files")
  22. */
  23. class File
  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 SHARED_WITH_NO_ONE = 0; // nikoliv
  31. public const SHARED_WITH_ALL_CLIENT_USERS = 1; // se všemi uživateli města
  32. public const SHARED_WITH_WORKERS = 2; // s pracovníky
  33. public const SHARED_WITH_WORKERS_MANAGERS = 3; // s pracovníky-manažery
  34. public const SHARED_WITH_GUESTS = 4; // s hosty
  35. public const SHARED_WITH_GUEST_EXPERT = 5; // s hosty-experty
  36. public static array $allSharingOptions = [
  37. File::SHARED_WITH_NO_ONE,
  38. File::SHARED_WITH_ALL_CLIENT_USERS,
  39. File::SHARED_WITH_WORKERS,
  40. File::SHARED_WITH_WORKERS_MANAGERS,
  41. File::SHARED_WITH_GUESTS,
  42. File::SHARED_WITH_GUEST_EXPERT
  43. ];
  44. /**
  45. * @var null|int
  46. *
  47. * @ORM\Id()
  48. * @ORM\GeneratedValue()
  49. * @ORM\Column(type="integer")
  50. */
  51. private ?int $id = null;
  52. /**
  53. * @var null|FileCategory
  54. *
  55. * @ORM\ManyToOne(targetEntity="App\Entity\FileCategory", inversedBy="files")
  56. */
  57. private ?FileCategory $category;
  58. /**
  59. * @var Collection|Building[]
  60. *
  61. * @ORM\ManyToMany(targetEntity="App\Entity\Building\Building", inversedBy="files", cascade={"persist"})
  62. */
  63. private $buildings;
  64. /**
  65. * @var Collection|Gauge[]
  66. *
  67. * @ORM\ManyToMany(targetEntity="App\Entity\Gauge\Gauge", inversedBy="files", cascade={"persist"})
  68. */
  69. private $gauges;
  70. /**
  71. * @var Collection|Contract[]
  72. *
  73. * @ORM\OneToMany(targetEntity="App\Entity\ContractPrice\Contract", mappedBy="file", cascade={"persist"})
  74. */
  75. private $contracts;
  76. /**
  77. * @var null|int
  78. *
  79. * @Assert\Type("int")
  80. * @ORM\Column(type="smallint", nullable=true)
  81. */
  82. private ?int $context = null;
  83. /**
  84. * @var string
  85. *
  86. * @Assert\NotBlank()
  87. * @Assert\Type("string")
  88. * @ORM\Column(type="string")
  89. */
  90. private string $fileName;
  91. /**
  92. * @var string
  93. *
  94. * @Assert\NotBlank()
  95. * @Assert\Type("string")
  96. * @ORM\Column(type="string")
  97. */
  98. private string $mimeType;
  99. /**
  100. * @var float|null
  101. *
  102. * @Assert\Type("float")
  103. * @ORM\Column(type="float", nullable=true)
  104. */
  105. private ?float $size;
  106. /**
  107. * @var string
  108. *
  109. * @Assert\NotBlank()
  110. * @Assert\Type("string")
  111. * @ORM\Column(type="string")
  112. */
  113. private string $path;
  114. /**
  115. * @var null|string
  116. *
  117. * @Assert\Type("string")
  118. * @ORM\Column(type="string", nullable=true)
  119. */
  120. private ?string $name;
  121. /**
  122. * @var Client|null
  123. * @ORM\ManyToOne(targetEntity="App\Entity\Client\Client", inversedBy="files")
  124. * @ORM\JoinColumn(name="client_id", nullable=true)
  125. * @Groups({"list"})
  126. */
  127. private ?Client $client = null;
  128. /**
  129. * @var null|\DateTimeInterface
  130. *
  131. * @ORM\Column(type="datetime", nullable=true)
  132. */
  133. private ?\DateTimeInterface $initDate;
  134. /**
  135. * @var null|\DateTimeInterface
  136. *
  137. * @ORM\Column(type="datetime", nullable=true)
  138. */
  139. private ?\DateTimeInterface $validTo;
  140. /**
  141. * @var null|string
  142. *
  143. * @ORM\Column(type="text", nullable=true)
  144. * @Assert\Type(type="string")
  145. */
  146. private ?string $note;
  147. /**
  148. * @var int
  149. * @ORM\Column(type="smallint", nullable=false)
  150. */
  151. private int $sharedWith = self::SHARED_WITH_NO_ONE;
  152. /**
  153. * @var bool
  154. * @ORM\Column(type="boolean")
  155. */
  156. private bool $hasEvent = false;
  157. /**
  158. * The internal primary identity key.
  159. *
  160. * @var UuidInterface
  161. *
  162. * @ORM\Column(type="uuid", unique=true)
  163. */
  164. protected UuidInterface $token;
  165. /**
  166. * simple array of connected buildings
  167. * @var array
  168. */
  169. private $buildingsArray = [];
  170. /**
  171. * simple array of connected gauges
  172. * @var array
  173. */
  174. private $gaugesArray = [];
  175. /**
  176. * @var Collection|Event[]
  177. *
  178. * @ORM\OneToMany(targetEntity="App\Entity\Event\Event", mappedBy="file")
  179. */
  180. private $events;
  181. /**
  182. * @var Collection|Invoice[]
  183. *
  184. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Invoice", mappedBy="file")
  185. */
  186. private $invoices;
  187. /**
  188. * @var Collection|Building[]
  189. *
  190. * @ORM\OneToMany(targetEntity="App\Entity\Building\Building", mappedBy="photo")
  191. */
  192. private $buildingPhotos;
  193. /**
  194. * @var Collection|Gauge[]
  195. *
  196. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\Gauge", mappedBy="photo")
  197. */
  198. private $gaugePhotos;
  199. /**
  200. * @var bool
  201. * @ORM\Column(type="boolean", options={"default" : 0})
  202. */
  203. private bool $common = false;
  204. /**
  205. * File constructor.
  206. */
  207. public function __construct()
  208. {
  209. $this->buildings = new ArrayCollection();
  210. $this->gauges = new ArrayCollection();
  211. $this->contracts = new ArrayCollection();
  212. $this->events = new ArrayCollection();
  213. $this->invoices = new ArrayCollection();
  214. $this->buildingPhotos = new ArrayCollection();
  215. $this->gaugePhotos = new ArrayCollection();
  216. $this->token = Uuid::uuid4();
  217. }
  218. public function __toString()
  219. {
  220. return $this->getName() ?? '';
  221. }
  222. /**
  223. * Returns data that will be consumed by an API controller and then returned via JSON to the client.
  224. *
  225. * @return array
  226. */
  227. public function getApiData(): array
  228. {
  229. return [
  230. 'id' => $this->getId(),
  231. 'fileName' => $this->getFileName(),
  232. 'size' => $this->getSize(),
  233. 'mimeType' => $this->getMimeType(),
  234. 'token' => $this->getToken(),
  235. 'name' => $this->getName()
  236. ];
  237. }
  238. /**
  239. * @return int|null
  240. */
  241. public function getId(): ?int
  242. {
  243. return $this->id;
  244. }
  245. /**
  246. * @return int|null
  247. */
  248. public function getContext(): ?int
  249. {
  250. return $this->context;
  251. }
  252. /**
  253. * @param int|null $context
  254. * @return $this
  255. */
  256. public function setContext(?int $context): self
  257. {
  258. $this->context = $context;
  259. return $this;
  260. }
  261. public function getFileName(): string
  262. {
  263. return $this->fileName;
  264. }
  265. /**
  266. * @param string $fileName
  267. * @return $this
  268. */
  269. public function setFileName(string $fileName): self
  270. {
  271. $this->fileName = $fileName;
  272. return $this;
  273. }
  274. /**
  275. * @return string|null
  276. */
  277. public function getName(): ?string
  278. {
  279. return $this->name;
  280. }
  281. /**
  282. * @param string|null $name
  283. * @return $this
  284. */
  285. public function setName(?string $name): self
  286. {
  287. $this->name = $name;
  288. return $this;
  289. }
  290. /**
  291. * @return \DateTimeInterface|null
  292. */
  293. public function getInitDate(): ?\DateTimeInterface
  294. {
  295. return $this->initDate;
  296. }
  297. /**
  298. * @param \DateTimeInterface|null $initDate
  299. * @return $this
  300. */
  301. public function setInitDate(?\DateTimeInterface $initDate): self
  302. {
  303. $this->initDate = $initDate;
  304. return $this;
  305. }
  306. /**
  307. * @return \DateTimeInterface|null
  308. */
  309. public function getValidTo(): ?\DateTimeInterface
  310. {
  311. return $this->validTo;
  312. }
  313. /**
  314. * @param \DateTimeInterface|null $validTo
  315. * @return $this
  316. */
  317. public function setValidTo(?\DateTimeInterface $validTo): self
  318. {
  319. $this->validTo = $validTo;
  320. return $this;
  321. }
  322. /**
  323. * @return string|null
  324. */
  325. public function getNote(): ?string
  326. {
  327. return $this->note;
  328. }
  329. /**
  330. * @param string|null $note
  331. * @return $this
  332. */
  333. public function setNote(?string $note): self
  334. {
  335. $this->note = $note;
  336. return $this;
  337. }
  338. /**
  339. * @return null|FileCategory
  340. */
  341. public function getCategory(): ?FileCategory
  342. {
  343. return $this->category;
  344. }
  345. /**
  346. * @param null|FileCategory $category
  347. * @return $this
  348. */
  349. public function setCategory(?FileCategory $category): self
  350. {
  351. $this->category = $category;
  352. return $this;
  353. }
  354. /**
  355. * @return Collection|Building[]
  356. */
  357. public function getBuildings(): Collection
  358. {
  359. return $this->buildings;
  360. }
  361. /**
  362. * @param Building[]|Collection $buildings
  363. * @return File
  364. */
  365. public function setBuildings($buildings)
  366. {
  367. $this->buildings = $buildings;
  368. return $this;
  369. }
  370. /**
  371. * @param Gauge[]|Collection $gauges
  372. * @return File
  373. */
  374. public function setGauges($gauges): self
  375. {
  376. $this->gauges = is_array($gauges)
  377. ? new ArrayCollection($gauges)
  378. : $gauges
  379. ;
  380. foreach ($gauges as $gauge) {
  381. $this->addBuilding($gauge->getBuilding());
  382. }
  383. return $this;
  384. }
  385. /**
  386. * Sets gauges collection without modifying building associations.
  387. * Used to restore gauges from snapshot without triggering addBuilding() side-effects.
  388. *
  389. * @param Gauge[]|Collection $gauges
  390. */
  391. public function setGaugesWithoutBuildingSync($gauges): self
  392. {
  393. $this->gauges = is_array($gauges)
  394. ? new ArrayCollection($gauges)
  395. : $gauges
  396. ;
  397. return $this;
  398. }
  399. /**
  400. * @param Building $building
  401. * @return $this
  402. */
  403. public function addBuilding(Building $building): self
  404. {
  405. if (!$this->buildings->contains($building)) {
  406. $this->buildings[] = $building;
  407. }
  408. return $this;
  409. }
  410. /**
  411. * @param Building $building
  412. * @return $this
  413. */
  414. public function removeBuilding(Building $building): self
  415. {
  416. if ($this->buildings->contains($building)) {
  417. $this->buildings->removeElement($building);
  418. }
  419. return $this;
  420. }
  421. /**
  422. * @return Collection|Gauge[]
  423. */
  424. public function getGauges(): Collection
  425. {
  426. return $this->gauges;
  427. }
  428. /**
  429. * @return Collection|Contract[]
  430. */
  431. public function getContracts(): Collection
  432. {
  433. return $this->contracts;
  434. }
  435. /**
  436. * @param Gauge $gauge
  437. * @return $this
  438. */
  439. public function addGauge(Gauge $gauge): self
  440. {
  441. if (!$this->gauges->contains($gauge)) {
  442. $this->gauges[] = $gauge;
  443. $this->addBuilding($gauge->getBuilding());
  444. }
  445. return $this;
  446. }
  447. /**
  448. * @param Gauge $gauge
  449. * @return $this
  450. */
  451. public function removeGauge(Gauge $gauge): self
  452. {
  453. if ($this->gauges->contains($gauge)) {
  454. $this->gauges->removeElement($gauge);
  455. }
  456. return $this;
  457. }
  458. /**
  459. * @return string
  460. */
  461. public function getMimeType(): string
  462. {
  463. return $this->mimeType;
  464. }
  465. /**
  466. * @param string $mimeType
  467. * @return $this
  468. */
  469. public function setMimeType(string $mimeType): self
  470. {
  471. $this->mimeType = $mimeType;
  472. return $this;
  473. }
  474. /**
  475. * @return float|null
  476. */
  477. public function getSize(): ?float
  478. {
  479. return $this->size;
  480. }
  481. /**
  482. * @param float|null $size
  483. * @return File
  484. */
  485. public function setSize(?float $size): File
  486. {
  487. $this->size = $size;
  488. return $this;
  489. }
  490. /**
  491. * @return string
  492. */
  493. public function getPath(): string
  494. {
  495. return $this->path;
  496. }
  497. /**
  498. * @param string $path
  499. * @return $this
  500. */
  501. public function setPath(string $path): self
  502. {
  503. $this->path = $path;
  504. return $this;
  505. }
  506. /**
  507. * @return Client|null
  508. */
  509. public function getClient(): ?Client
  510. {
  511. return $this->client;
  512. }
  513. /**
  514. * @param Client|null $client
  515. * @return File
  516. */
  517. public function setClient(?Client $client): File
  518. {
  519. $this->client = $client;
  520. return $this;
  521. }
  522. /**
  523. * @return int
  524. */
  525. public function getSharedWith(): int
  526. {
  527. return $this->sharedWith;
  528. }
  529. /**
  530. * @param int $sharedWith
  531. * @return File
  532. */
  533. public function setSharedWith(int $sharedWith): File
  534. {
  535. $this->sharedWith = $sharedWith;
  536. return $this;
  537. }
  538. /**
  539. * @return bool
  540. */
  541. public function isHasEvent(): bool
  542. {
  543. return $this->hasEvent;
  544. }
  545. /**
  546. * @param bool $hasEvent
  547. * @return File
  548. */
  549. public function setHasEvent(bool $hasEvent): File
  550. {
  551. $this->hasEvent = $hasEvent;
  552. return $this;
  553. }
  554. /**
  555. * @return UuidInterface
  556. */
  557. public function getToken(): UuidInterface
  558. {
  559. return $this->token;
  560. }
  561. /**
  562. * @param UuidInterface $token
  563. * @return File
  564. */
  565. public function setToken(UuidInterface $token): File
  566. {
  567. $this->token = $token;
  568. return $this;
  569. }
  570. /**
  571. * @param bool $fromArray
  572. * @return array
  573. */
  574. public function getBuildingsNames(bool $fromArray): array
  575. {
  576. $buildings = [];
  577. if ($fromArray) {
  578. foreach ($this->getBuildingsArray() as $building) {
  579. $buildings[] = $building['name'];
  580. }
  581. } else {
  582. foreach ($this->getBuildings() as $building) {
  583. $buildings[] = $building->getName();
  584. }
  585. }
  586. return $buildings;
  587. }
  588. /**
  589. * @param bool $fromArray
  590. * @return array
  591. */
  592. public function getGaugesNames(bool $fromArray): array
  593. {
  594. $gauges = [];
  595. if ($fromArray) {
  596. foreach ($this->getGaugesArray() as $gauge) {
  597. $gauges[] = $gauge['name'];
  598. }
  599. } else {
  600. foreach ($this->getGauges() as $gauge) {
  601. $gauges[] = $gauge->getName();
  602. }
  603. }
  604. return $gauges;
  605. }
  606. /**
  607. * @param bool $fromArray
  608. * @return array
  609. */
  610. public function getGaugesFilterString(): array
  611. {
  612. $gauges = [];
  613. foreach ($this->getGaugesArray() as $gauge) {
  614. $gauges[] = 'gauge' . $gauge['id'];
  615. }
  616. return $gauges;
  617. }
  618. /**
  619. * @param string $roleName
  620. * @return array
  621. */
  622. public static function getSharedWithOptionsByRole(string $roleName): array
  623. {
  624. $sharedWith = [File::SHARED_WITH_ALL_CLIENT_USERS];
  625. switch ($roleName) {
  626. case Role::ADMIN:
  627. case Role::ADMIN_MANAGER:
  628. case Role::MANAGER:
  629. case Role::CLIENT_BOSS:
  630. $sharedWith = File::$allSharingOptions;
  631. break;
  632. case Role::WORKER_MANAGER:
  633. $sharedWith[] = File::SHARED_WITH_WORKERS_MANAGERS;
  634. $sharedWith[] = File::SHARED_WITH_WORKERS;
  635. break;
  636. case Role::WORKER:
  637. $sharedWith[] = File::SHARED_WITH_WORKERS;
  638. break;
  639. case Role::GUEST:
  640. $sharedWith[] = File::SHARED_WITH_GUESTS;
  641. break;
  642. case Role::GUEST_EXPERT:
  643. $sharedWith[] = File::SHARED_WITH_GUEST_EXPERT;
  644. $sharedWith[] = File::SHARED_WITH_GUESTS;
  645. break;
  646. }
  647. return $sharedWith;
  648. }
  649. /**
  650. * @return array
  651. */
  652. public function getBuildingsArray(): array
  653. {
  654. return $this->buildingsArray;
  655. }
  656. /**
  657. * @param array $buildingsArray (buildingsArray in array by fileId as key)
  658. * @return File
  659. */
  660. public function setBuildingsArrayByArray(array $buildingsArray): File
  661. {
  662. if (array_key_exists($this->getId(), $buildingsArray)) {
  663. $this->buildingsArray = $buildingsArray[$this->getId()];
  664. }
  665. return $this;
  666. }
  667. /**
  668. * @return array
  669. */
  670. public function getGaugesArray(): array
  671. {
  672. return $this->gaugesArray;
  673. }
  674. /**
  675. * @param array $gaugesArray (buildingsArray in array by fileId as key)
  676. * @return File
  677. */
  678. public function setGaugesArrayByArray(array $gaugesArray): File
  679. {
  680. if (array_key_exists($this->getId(), $gaugesArray)) {
  681. $this->gaugesArray = $gaugesArray[$this->getId()];
  682. }
  683. return $this;
  684. }
  685. /**
  686. * @return Event[]|Collection
  687. */
  688. public function getEvents()
  689. {
  690. return $this->events;
  691. }
  692. /**
  693. * @param Event[]|Collection $events
  694. * @return File
  695. */
  696. public function setEvents($events): File
  697. {
  698. $this->events = $events;
  699. return $this;
  700. }
  701. /**
  702. * @return bool
  703. */
  704. public function isCommon(): bool
  705. {
  706. return $this->common;
  707. }
  708. /**
  709. * @param bool $common
  710. * @return File
  711. */
  712. public function setCommon(bool $common): File
  713. {
  714. $this->common = $common;
  715. return $this;
  716. }
  717. /**
  718. * @return Invoice[]|Collection
  719. */
  720. public function getInvoices()
  721. {
  722. return $this->invoices;
  723. }
  724. /**
  725. * @return Building[]|Collection
  726. */
  727. public function getBuildingPhotos()
  728. {
  729. return $this->buildingPhotos;
  730. }
  731. /**
  732. * @return Gauge[]|Collection
  733. */
  734. public function getGaugePhotos()
  735. {
  736. return $this->gaugePhotos;
  737. }
  738. /**
  739. * method returns mapping dependencies (eg. check before deletion)
  740. * @return bool
  741. */
  742. public function hasDependencies(): bool
  743. {
  744. $dependencies = [
  745. 'invoices' => $this->getInvoices()->count(),
  746. 'events' => $this->getEvents()->count(),
  747. 'buildingPhotos' => $this->getBuildingPhotos()->count(),
  748. 'gaugePhotos' => $this->getGaugePhotos()->count()
  749. ];
  750. return array_sum($dependencies) > 0;
  751. }
  752. /**
  753. * @return string|null
  754. */
  755. public function getFullCategoryName(): ?string
  756. {
  757. if ($this->getCategory() !== null) {
  758. $catName = $this->getCategory()->getName();
  759. $parentCat = $this->getCategory()->getParent();
  760. return ($parentCat === null) ? $catName : sprintf('%s - %s', $parentCat->getName(), $catName);
  761. }
  762. return null;
  763. }
  764. }