src/Aqarmap/Bundle/ListingBundle/Entity/Section.php line 28

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Entity;
  3. use App\Enum\Listing\SectionTypeEnum;
  4. use Aqarmap\Bundle\ExchangeBundle\Entity\ExchangeRequest;
  5. use Aqarmap\Bundle\ListingBundle\Constant\ListingStatus;
  6. use Aqarmap\Bundle\UserBundle\Entity\UserInterest;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use Gedmo\Translatable\Translatable;
  12. use JMS\Serializer\Annotation as Serializer;
  13. /**
  14. * Section.
  15. */
  16. #[ORM\Cache]
  17. #[ORM\Entity(repositoryClass: \Aqarmap\Bundle\ListingBundle\Repository\SectionRepository::class)]
  18. #[ORM\Table(name: 'sections')]
  19. #[Gedmo\TranslationEntity(class: SectionTranslation::class)]
  20. #[Serializer\ExclusionPolicy('all')]
  21. class Section implements Translatable, \Stringable
  22. {
  23. /**
  24. * @var int
  25. */
  26. #[ORM\Column(name: 'id', type: 'integer')]
  27. #[ORM\Id]
  28. #[ORM\GeneratedValue(strategy: 'AUTO')]
  29. #[Serializer\Groups(['MyListings', 'Default', 'Search', 'DefaultV4', 'SearchV4', 'SlugResolver', 'listingDetails', 'leadsBank', 'UserFavoriteListings'])]
  30. #[Serializer\Expose]
  31. private $id;
  32. /**
  33. * @var string
  34. */
  35. #[ORM\Column(name: 'title', type: 'string', length: 100)]
  36. #[Gedmo\Translatable]
  37. #[Serializer\Groups(['Default', 'Search', 'DefaultV4', 'SlugResolver', 'listingDetails', 'MyListings', 'MyLeads', 'leadsBank', 'UserFavoriteListings', 'SearchV4'])]
  38. #[Serializer\Expose]
  39. private $title;
  40. /**
  41. * @var string
  42. */
  43. #[ORM\Column(name: 'description', type: 'text', length: 512, nullable: true)]
  44. #[Gedmo\Translatable]
  45. #[Serializer\Groups(['Default', 'DefaultV4'])]
  46. #[Serializer\Expose]
  47. private $description;
  48. /**
  49. * @var string
  50. */
  51. #[ORM\Column(name: 'meta_title', type: 'string', length: 100)]
  52. #[Gedmo\Translatable]
  53. #[Serializer\Groups(['Default'])]
  54. #[Serializer\Expose]
  55. private $metaTitle;
  56. /**
  57. * @var string
  58. */
  59. #[ORM\Column(name: 'meta_description', type: 'text', length: 512, nullable: true)]
  60. #[Gedmo\Translatable]
  61. #[Serializer\Groups(['Default', 'DefaultV4'])]
  62. #[Serializer\Expose]
  63. private $metaDescription;
  64. /**
  65. * @var bool
  66. */
  67. #[ORM\Column(name: 'main', type: 'boolean')]
  68. #[Serializer\Groups(['Default'])]
  69. #[Serializer\Expose]
  70. private $main = false;
  71. /**
  72. * @var bool
  73. */
  74. #[ORM\Column(name: 'searchable', type: 'boolean')]
  75. #[Serializer\Groups(['Default'])]
  76. #[Serializer\Expose]
  77. private $searchable = false;
  78. #[ORM\Column(name: 'type', type: 'string', length: 20, enumType: SectionTypeEnum::class)]
  79. private ?SectionTypeEnum $type = null;
  80. #[ORM\Column(length: 128, unique: true)]
  81. #[Gedmo\Slug(fields: ['title'], updatable: false)]
  82. #[Serializer\Groups(['Default', 'DefaultV4', 'SlugResolver', 'SearchV4', 'listingDetails', 'UserFavoriteListings'])]
  83. #[Serializer\Expose]
  84. private $slug;
  85. #[ORM\OneToMany(targetEntity: SectionTranslation::class, mappedBy: 'object', cascade: ['persist', 'remove'])]
  86. private $translations;
  87. #[ORM\OneToMany(targetEntity: Listing::class, mappedBy: 'section', fetch: 'EXTRA_LAZY')]
  88. protected $listings;
  89. #[ORM\OneToMany(targetEntity: \Aqarmap\Bundle\NeighborhoodBundle\Entity\LocationStatistics::class, mappedBy: 'section', fetch: 'EXTRA_LAZY')]
  90. protected $locationStatistics;
  91. #[ORM\OneToMany(targetEntity: PriceFilter::class, mappedBy: 'section', fetch: 'EXTRA_LAZY')]
  92. protected $priceFilter;
  93. #[ORM\OneToMany(targetEntity: Rule::class, mappedBy: 'section', fetch: 'EXTRA_LAZY')]
  94. protected $rules;
  95. #[ORM\OneToMany(targetEntity: UserInterest::class, mappedBy: 'section')]
  96. protected $interests;
  97. #[ORM\OneToMany(targetEntity: ExchangeRequest::class, mappedBy: 'section', fetch: 'EXTRA_LAZY')]
  98. protected $exchangeRequests;
  99. #[Gedmo\Locale] // Used locale to override Translation listener`s locale
  100. private $locale;
  101. #[ORM\Column(name: 'reference_id', type: 'integer', nullable: true)]
  102. private $referenceId;
  103. /**
  104. * @var string
  105. */
  106. #[ORM\Column(name: 'meta_search_description', type: 'text', length: 512, nullable: true)]
  107. #[Gedmo\Translatable]
  108. private $metaSearchDescription;
  109. /**
  110. * @var array
  111. */
  112. private $v4Translations = [];
  113. /**
  114. * Constructor.
  115. */
  116. public function __construct()
  117. {
  118. $this->translations = new ArrayCollection();
  119. $this->listings = new ArrayCollection();
  120. $this->locationStatistics = new ArrayCollection();
  121. $this->priceFilter = new ArrayCollection();
  122. $this->rules = new ArrayCollection();
  123. $this->interests = new ArrayCollection();
  124. $this->exchangeRequests = new ArrayCollection();
  125. }
  126. // ---------------------------------------------------------------------
  127. /**
  128. * Get id.
  129. *
  130. * @return int
  131. */
  132. public function getId()
  133. {
  134. return $this->id;
  135. }
  136. // ---------------------------------------------------------------------
  137. /**
  138. * Set title.
  139. *
  140. * @param string $title
  141. *
  142. * @return Section
  143. */
  144. public function setTitle($title)
  145. {
  146. $this->title = $title;
  147. return $this;
  148. }
  149. /**
  150. * Get title.
  151. *
  152. * @return string
  153. */
  154. public function getTitle()
  155. {
  156. return $this->title;
  157. }
  158. // ---------------------------------------------------------------------
  159. /**
  160. * Set description.
  161. *
  162. * @param string $description
  163. *
  164. * @return Section
  165. */
  166. public function setDescription($description)
  167. {
  168. $this->title = $description;
  169. return $this;
  170. }
  171. /**
  172. * Get description.
  173. *
  174. * @return string
  175. */
  176. public function getDescription()
  177. {
  178. return $this->description;
  179. }
  180. // ---------------------------------------------------------------------
  181. /**
  182. * Set main.
  183. *
  184. * @param bool $main
  185. *
  186. * @return Section
  187. */
  188. public function setMain($main)
  189. {
  190. $this->main = $main;
  191. return $this;
  192. }
  193. /**
  194. * Get main.
  195. *
  196. * @return bool
  197. */
  198. public function getMain()
  199. {
  200. return $this->main;
  201. }
  202. // ---------------------------------------------------------------------
  203. /**
  204. * Set searchable.
  205. *
  206. * @param bool $searchable
  207. *
  208. * @return Section
  209. */
  210. public function setSearchable($searchable)
  211. {
  212. $this->searchable = $searchable;
  213. return $this;
  214. }
  215. /**
  216. * Get searchable.
  217. *
  218. * @return bool
  219. */
  220. public function getSearchable()
  221. {
  222. return $this->searchable;
  223. }
  224. /**
  225. * @return bool
  226. */
  227. #[Serializer\VirtualProperty]
  228. #[Serializer\Groups(['DefaultV4'])]
  229. #[Serializer\Expose]
  230. public function isSearchable()
  231. {
  232. return (bool) $this->searchable;
  233. }
  234. /**
  235. * @return bool
  236. */
  237. #[Serializer\VirtualProperty]
  238. #[Serializer\Groups(['DefaultV4'])]
  239. #[Serializer\Expose]
  240. public function isMain()
  241. {
  242. return (bool) $this->main;
  243. }
  244. // ---------------------------------------------------------------------
  245. /**
  246. * Set type.
  247. *
  248. * @return Section
  249. */
  250. public function setType(?SectionTypeEnum $type)
  251. {
  252. $this->type = $type;
  253. return $this;
  254. }
  255. /**
  256. * Get type.
  257. */
  258. public function getType(): ?SectionTypeEnum
  259. {
  260. return $this->type;
  261. }
  262. /**
  263. * @return string
  264. */
  265. #[Serializer\VirtualProperty]
  266. #[Serializer\Groups(['DefaultV4'])]
  267. #[Serializer\SerializedName('inheritanceType')]
  268. #[Serializer\Expose]
  269. public function getInheritanceType()
  270. {
  271. return str_replace('-', '_', $this->getSlug());
  272. }
  273. // ---------------------------------------------------------------------
  274. /**
  275. * Set slug.
  276. *
  277. * @param string $slug
  278. *
  279. * @return Section
  280. */
  281. public function setSlug($slug)
  282. {
  283. $this->slug = $slug;
  284. return $this;
  285. }
  286. /**
  287. * Get slug.
  288. *
  289. * @return string
  290. */
  291. public function getSlug()
  292. {
  293. return $this->slug;
  294. }
  295. // ---------------------------------------------------------------------
  296. /**
  297. * Get translations.
  298. *
  299. * @return ArrayCollection
  300. */
  301. public function getTranslations()
  302. {
  303. return $this->translations;
  304. }
  305. public function getTranslatableLocale()
  306. {
  307. return $this->locale;
  308. }
  309. /**
  310. * Add translation.
  311. *
  312. * @return Section
  313. */
  314. public function addTranslation(SectionTranslation $translation)
  315. {
  316. if (!$this->translations->contains($translation)) {
  317. $this->translations->add($translation);
  318. $translation->setObject($this);
  319. }
  320. return $this;
  321. }
  322. /**
  323. * Remove translation.
  324. *
  325. * @return Section
  326. */
  327. public function removeTranslation(SectionTranslation $translation)
  328. {
  329. if ($this->translations->contains($translation)) {
  330. $this->translations->removeElement($translation);
  331. }
  332. return $this;
  333. }
  334. // ---------------------------------------------------------------------
  335. /**
  336. * Add listings.
  337. *
  338. * @return Section
  339. */
  340. public function addListing(Listing $listings)
  341. {
  342. $this->listings[] = $listings;
  343. return $this;
  344. }
  345. /**
  346. * Remove listings.
  347. */
  348. public function removeListing(Listing $listings): void
  349. {
  350. $this->listings->removeElement($listings);
  351. }
  352. /**
  353. * Get listings.
  354. *
  355. * @return \Doctrine\Common\Collections\Collection
  356. */
  357. public function getListings()
  358. {
  359. return $this->listings;
  360. }
  361. /**
  362. * @param array $locations
  363. *
  364. * @return \Doctrine\Common\Collections\Collection
  365. */
  366. public function getLocationListings($locations)
  367. {
  368. $criteria = Criteria::create()
  369. ->where(Criteria::expr()->in('location', $locations))
  370. ->andWhere(Criteria::expr()->eq('status', ListingStatus::LIVE))
  371. ->orderBy(['featured' => Criteria::DESC, 'publishedAt' => Criteria::DESC])
  372. ->setMaxResults(6);
  373. return $this->getListings()->matching($criteria);
  374. }
  375. // ---------------------------------------------------------------------
  376. /**
  377. * Add priceFilter.
  378. *
  379. * @return Section
  380. */
  381. public function addPriceFilter(PriceFilter $priceFilter)
  382. {
  383. $this->priceFilter[] = $priceFilter;
  384. return $this;
  385. }
  386. /**
  387. * Remove priceFilter.
  388. */
  389. public function removePriceFilter(PriceFilter $priceFilter): void
  390. {
  391. $this->priceFilter->removeElement($priceFilter);
  392. }
  393. /**
  394. * Get priceFilter.
  395. *
  396. * @return \Doctrine\Common\Collections\Collection
  397. */
  398. public function getPriceFilter()
  399. {
  400. return $this->priceFilter;
  401. }
  402. // ---------------------------------------------------------------------
  403. /**
  404. * Add rules.
  405. *
  406. * @return Section
  407. */
  408. public function addRule(Rule $rules)
  409. {
  410. $this->rules[] = $rules;
  411. return $this;
  412. }
  413. /**
  414. * Remove rules.
  415. */
  416. public function removeRule(Rule $rules): void
  417. {
  418. $this->rules->removeElement($rules);
  419. }
  420. /**
  421. * Get rules.
  422. *
  423. * @return \Doctrine\Common\Collections\Collection
  424. */
  425. public function getRules()
  426. {
  427. return $this->rules;
  428. }
  429. // ---------------------------------------------------------------------
  430. /**
  431. * Add locationStatistic.
  432. *
  433. * @return Section
  434. */
  435. public function addLocationStatistic(\Aqarmap\Bundle\NeighborhoodBundle\Entity\LocationStatistics $locationStatistic)
  436. {
  437. $this->locationStatistics[] = $locationStatistic;
  438. return $this;
  439. }
  440. /**
  441. * Remove locationStatistic.
  442. */
  443. public function removeLocationStatistic(\Aqarmap\Bundle\NeighborhoodBundle\Entity\LocationStatistics $locationStatistic): void
  444. {
  445. $this->locationStatistics->removeElement($locationStatistic);
  446. }
  447. /**
  448. * Get locationStatistics.
  449. *
  450. * @return \Doctrine\Common\Collections\Collection
  451. */
  452. public function getLocationStatistics()
  453. {
  454. return $this->locationStatistics;
  455. }
  456. /**
  457. * Add interest.
  458. *
  459. * @return Section
  460. */
  461. public function addInterest(UserInterest $interest)
  462. {
  463. $this->interests[] = $interest;
  464. return $this;
  465. }
  466. /**
  467. * Remove interest.
  468. */
  469. public function removeInterest(UserInterest $interest): void
  470. {
  471. $this->interests->removeElement($interest);
  472. }
  473. /**
  474. * Get interests.
  475. *
  476. * @return \Doctrine\Common\Collections\Collection
  477. */
  478. public function getInterests()
  479. {
  480. return $this->interests;
  481. }
  482. public function setTranslatableLocale($locale): void
  483. {
  484. $this->locale = $locale;
  485. }
  486. /**
  487. * Add exchangeRequest.
  488. *
  489. * @return Section
  490. */
  491. public function addExchangeRequest(ExchangeRequest $exchangeRequest)
  492. {
  493. $this->exchangeRequests[] = $exchangeRequest;
  494. return $this;
  495. }
  496. /**
  497. * Remove exchangeRequest.
  498. */
  499. public function removeExchangeRequest(ExchangeRequest $exchangeRequest): void
  500. {
  501. $this->exchangeRequests->removeElement($exchangeRequest);
  502. }
  503. /**
  504. * Get exchangeRequests.
  505. *
  506. * @return \Doctrine\Common\Collections\Collection
  507. */
  508. public function getExchangeRequests()
  509. {
  510. return $this->exchangeRequests;
  511. }
  512. public function __toString(): string
  513. {
  514. return $this->title;
  515. }
  516. /**
  517. * @return string
  518. */
  519. public function getMetaDescription()
  520. {
  521. return $this->metaDescription;
  522. }
  523. /**
  524. * @param string $metaDescription
  525. */
  526. public function setMetaDescription($metaDescription): void
  527. {
  528. $this->metaDescription = $metaDescription;
  529. }
  530. /**
  531. * @return string
  532. */
  533. public function getMetaTitle()
  534. {
  535. return $this->metaTitle;
  536. }
  537. /**
  538. * @param string $metaTitle
  539. *
  540. * @return $this
  541. */
  542. public function setMetaTitle($metaTitle)
  543. {
  544. $this->metaTitle = $metaTitle;
  545. return $this;
  546. }
  547. /**
  548. * @return string|null
  549. */
  550. public function getMetaSearchDescription()
  551. {
  552. return $this->metaSearchDescription;
  553. }
  554. /**
  555. * @return Section
  556. */
  557. public function setMetaSearchDescription(string $metaSearchDescription)
  558. {
  559. $this->metaSearchDescription = $metaSearchDescription;
  560. return $this;
  561. }
  562. /**
  563. * @return int|null
  564. */
  565. #[Serializer\VirtualProperty]
  566. #[Serializer\Groups(['Default'])]
  567. #[Serializer\Expose]
  568. public function getReferenceId()
  569. {
  570. return $this->id;
  571. }
  572. /**
  573. * @return self
  574. */
  575. public function setReferenceId(?int $referenceId)
  576. {
  577. $this->referenceId = $referenceId;
  578. return $this;
  579. }
  580. #[Serializer\VirtualProperty]
  581. #[Serializer\SerializedName('referenceId')]
  582. #[Serializer\Expose]
  583. #[Serializer\Groups(['DefaultV4'])]
  584. public function getReferencedV4Id()
  585. {
  586. return $this->getId();
  587. }
  588. /**
  589. * @param array $translations
  590. *
  591. * @return self
  592. */
  593. public function setV4Translations($translations = [])
  594. {
  595. $this->v4Translations = $translations;
  596. return $this;
  597. }
  598. /**
  599. * Get translations.
  600. *
  601. * @return array
  602. */
  603. #[Serializer\VirtualProperty]
  604. #[Serializer\Groups(['DefaultV4', 'MyListing'])]
  605. #[Serializer\SerializedName('translations')]
  606. #[Serializer\Expose]
  607. public function getV4Translations()
  608. {
  609. return $this->v4Translations;
  610. }
  611. }