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

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