src/Entity/Page.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use DateTime;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use App\Misc\StringUtils;
  8. use App\Validation as ExcAssert;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Validator\Constraints\UrlValidator;
  11. /**
  12. * Page
  13. *
  14. * @ORM\Table(name="Page")
  15. * @ORM\Entity(repositoryClass="App\Entity\PageRepository")
  16. */
  17. class Page
  18. {
  19. // /!\ Do not change constants names, they're used as string in templates!
  20. // shared types
  21. public const TYPE_CATEGORIE = 'categorie';
  22. public const TYPE_LOCAL = 'local';
  23. public const TYPE_ACTU = 'actu';
  24. public const TYPE_NEWSLETTER = 'newsletter';
  25. // single types
  26. public const TYPE_LOCAL_INDEX = 'local_index';
  27. public const TYPE_INDEX = 'index';
  28. public const TYPE_AIDE = 'aide';
  29. public const TYPE_CGUV = 'cguv';
  30. public const TYPE_QUALITY = 'quality';
  31. public const TYPE_LEGAL = 'legal';
  32. public const TYPE_LINKS = 'links';
  33. public const TYPE_PRIX = 'prix';
  34. public static $miscPages = array(
  35. self::TYPE_INDEX,
  36. self::TYPE_LINKS,
  37. self::TYPE_CGUV,
  38. self::TYPE_LEGAL,
  39. self::TYPE_QUALITY,
  40. self::TYPE_LOCAL_INDEX,
  41. self::TYPE_AIDE,
  42. self::TYPE_PRIX,
  43. );
  44. public static function createWithId($id)
  45. {
  46. $instance = new self();
  47. $instance->id = $id;
  48. return $instance;
  49. }
  50. /**
  51. * @var integer
  52. *
  53. * @ORM\Column(name="id", type="integer")
  54. * @ORM\Id
  55. * @ORM\GeneratedValue(strategy="AUTO")
  56. */
  57. private $id;
  58. /**
  59. * @var string
  60. *
  61. * @ORM\Column(name="type", type="string", length=255, nullable=true)
  62. */
  63. private $type;
  64. /**
  65. * @var DateTime
  66. *
  67. * @ORM\Column(name="creation_date", type="datetime")
  68. */
  69. private $creationDate;
  70. /**
  71. * @var DateTime
  72. *
  73. * @ORM\Column(name="modified_date", type="datetime")
  74. */
  75. private $modifiedDate;
  76. /**
  77. * @var string
  78. *
  79. * @ORM\Column(name="title", type="string", length=255)
  80. */
  81. private $title;
  82. /**
  83. * @var string
  84. *
  85. * @ORM\Column(name="menu_label", type="string", length=255)
  86. */
  87. private $menuLabel;
  88. /**
  89. * @var string
  90. *
  91. * @ORM\Column(name="page_title", type="string", length=255)
  92. */
  93. private $pageTitle;
  94. /**
  95. * @var string
  96. *
  97. * @ORM\Column(name="page_description", type="string", length=255)
  98. * @Assert\Length(
  99. * max=220,
  100. * maxMessage="La longueur de la description doit être inférieure à 140 caractères"
  101. * )
  102. */
  103. private $pageDescription;
  104. /**
  105. * @var text
  106. *
  107. * @ORM\Column(name="bandeau_text_1", type="text", nullable=true)
  108. */
  109. private $bandeauText1;
  110. /**
  111. * @var text
  112. *
  113. * @ORM\Column(name="bandeau_text_2", type="text", nullable=true)
  114. */
  115. private $bandeauText2;
  116. /**
  117. * @var text
  118. *
  119. * @ORM\Column(name="bandeau_text_3", type="text", nullable=true)
  120. */
  121. private $bandeauText3;
  122. /**
  123. * @var string
  124. *
  125. * @ORM\Column(name="content", type="text")
  126. */
  127. private $content;
  128. /**
  129. * @var int
  130. *
  131. * @ORM\Column(name="page_order", type="integer", nullable=true)
  132. */
  133. private $order = null;
  134. /**
  135. * @var Page
  136. *
  137. * @ORM\ManyToOne(targetEntity="Page")
  138. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
  139. */
  140. private $parent = null;
  141. /**
  142. * @var Url
  143. *
  144. * @ORM\OneToOne(targetEntity="Url", inversedBy="page", cascade={"remove"})
  145. * @ORM\JoinColumn(name="url_id", referencedColumnName="id", nullable=true)
  146. */
  147. private $url;
  148. /**
  149. * @var string
  150. */
  151. private $transientUrl;
  152. /**
  153. * @var string
  154. * @ORM\Column(name="robots", type="string", nullable=true)
  155. */
  156. private $robots;
  157. /**
  158. * @var Collection
  159. *
  160. * @ORM\OneToMany(targetEntity="PageMeta", mappedBy="page", cascade={"persist","remove"})
  161. */
  162. private $metas;
  163. public function __construct()
  164. {
  165. $this->creationDate = new DateTime();
  166. $this->modifiedDate = new DateTime();
  167. $this->metas = new ArrayCollection();
  168. }
  169. /**
  170. * Get id
  171. *
  172. * @return integer
  173. */
  174. public function getId()
  175. {
  176. return $this->id;
  177. }
  178. /**
  179. * Set type
  180. *
  181. * @param string $type
  182. *
  183. * @return Page
  184. */
  185. public function setType($type)
  186. {
  187. $this->type = $type;
  188. return $this;
  189. }
  190. /**
  191. * Get type
  192. *
  193. * @return string
  194. */
  195. public function getType()
  196. {
  197. return $this->type;
  198. }
  199. /**
  200. * Set creationDate
  201. *
  202. * @param \DateTime $creationDate
  203. *
  204. * @return Page
  205. */
  206. public function setCreationDate($creationDate)
  207. {
  208. $this->creationDate = $creationDate;
  209. return $this;
  210. }
  211. /**
  212. * Get creationDate
  213. *
  214. * @return \DateTime
  215. */
  216. public function getCreationDate()
  217. {
  218. return $this->creationDate;
  219. }
  220. /**
  221. * Set updateDate
  222. *
  223. * @param \DateTime $modifiedDate
  224. *
  225. * @return Page
  226. */
  227. public function setModifiedDate($modifiedDate)
  228. {
  229. $this->modifiedDate = $modifiedDate;
  230. return $this;
  231. }
  232. /**
  233. * Get updateDate
  234. *
  235. * @return \DateTime
  236. */
  237. public function getModifiedDate()
  238. {
  239. return $this->modifiedDate;
  240. }
  241. /**
  242. * Set title
  243. *
  244. * @param string $title
  245. *
  246. * @return Page
  247. */
  248. public function setTitle($title)
  249. {
  250. $this->title = $title;
  251. if (!$this->menuLabel) {
  252. $this->menuLabel = $title;
  253. }
  254. if (!$this->pageTitle) {
  255. $this->pageTitle = $title;
  256. }
  257. return $this;
  258. }
  259. /**
  260. * Get title
  261. *
  262. * @return string
  263. */
  264. public function getTitle()
  265. {
  266. return $this->title;
  267. }
  268. /**
  269. * Set menuLabel
  270. *
  271. * @param string $menuLabel
  272. *
  273. * @return Page
  274. */
  275. public function setMenuLabel($menuLabel)
  276. {
  277. $this->menuLabel = $menuLabel;
  278. if (!$this->title) {
  279. $this->title = $menuLabel;
  280. }
  281. return $this;
  282. }
  283. /**
  284. * Get menuLabel
  285. *
  286. * @return string
  287. */
  288. public function getMenuLabel()
  289. {
  290. return $this->menuLabel;
  291. }
  292. /**
  293. * Set pageTitle
  294. *
  295. * @param string $pageTitle
  296. *
  297. * @return Page
  298. */
  299. public function setPageTitle($pageTitle)
  300. {
  301. $this->pageTitle = $pageTitle;
  302. return $this;
  303. }
  304. /**
  305. * Get pageTitle
  306. *
  307. * @return string
  308. */
  309. public function getPageTitle()
  310. {
  311. return $this->pageTitle;
  312. }
  313. public function getSanitizedTitle()
  314. {
  315. return StringUtils::sanitizeString($this->pageTitle);
  316. }
  317. /**
  318. * Set pageDescription
  319. *
  320. * @param string $pageDescription
  321. *
  322. * @return Page
  323. */
  324. public function setPageDescription($pageDescription)
  325. {
  326. $this->pageDescription = $pageDescription;
  327. return $this;
  328. }
  329. /**
  330. * Get pageDescription
  331. *
  332. * @return string
  333. */
  334. public function getPageDescription()
  335. {
  336. return $this->pageDescription;
  337. }
  338. /**
  339. * Set bandeauText1
  340. *
  341. * @param string $bandeauText1
  342. * @return Page
  343. */
  344. public function setBandeauText1($bandeauText1)
  345. {
  346. $this->bandeauText1 = $bandeauText1;
  347. return $this;
  348. }
  349. /**
  350. * Get bandeauText1
  351. *
  352. * @return string
  353. */
  354. public function getBandeauText1()
  355. {
  356. return $this->bandeauText1;
  357. }
  358. /**
  359. * Set bandeauText2
  360. *
  361. * @param string $bandeauText2
  362. * @return Page
  363. */
  364. public function setBandeauText2($bandeauText2)
  365. {
  366. $this->bandeauText2 = $bandeauText2;
  367. return $this;
  368. }
  369. /**
  370. * Get bandeauText2
  371. *
  372. * @return string
  373. */
  374. public function getBandeauText2()
  375. {
  376. return $this->bandeauText2;
  377. }
  378. /**
  379. * Set bandeauText3
  380. *
  381. * @param string $bandeauText3
  382. * @return Page
  383. */
  384. public function setBandeauText3($bandeauText3)
  385. {
  386. $this->bandeauText3 = $bandeauText3;
  387. return $this;
  388. }
  389. /**
  390. * Get bandeauText3
  391. *
  392. * @return string
  393. */
  394. public function getBandeauText3()
  395. {
  396. return $this->bandeauText3;
  397. }
  398. /**
  399. * Set content
  400. *
  401. * @param string $content
  402. *
  403. * @return Page
  404. */
  405. public function setContent($content)
  406. {
  407. $this->content = $content;
  408. return $this;
  409. }
  410. /**
  411. * Get content
  412. *
  413. * @return string
  414. */
  415. public function getContent()
  416. {
  417. return $this->content;
  418. }
  419. public function getExcerpt($max)
  420. {
  421. // '/\s+?(\S+)?$/', '',
  422. $text = trim(strip_tags($this->content));
  423. if (strlen($text) > $max) {
  424. return preg_replace(
  425. '/\s+?(\S*)?$/',
  426. '',
  427. substr($text, 0, $max)
  428. ) . '…';
  429. }
  430. return $text;
  431. }
  432. /**
  433. * @return int
  434. */
  435. public function getOrder()
  436. {
  437. return $this->order;
  438. }
  439. /**
  440. * @param int $order
  441. */
  442. public function setOrder($order)
  443. {
  444. $this->order = $order;
  445. }
  446. /**
  447. * @return Url
  448. */
  449. public function getUrl()
  450. {
  451. return $this->url;
  452. }
  453. /**
  454. * @param Url $url
  455. */
  456. public function setUrl($url)
  457. {
  458. $this->url = $url;
  459. }
  460. /**
  461. * @return Page
  462. */
  463. public function getParent()
  464. {
  465. return $this->parent;
  466. }
  467. /**
  468. * @param Page $parent
  469. */
  470. public function setParent($parent)
  471. {
  472. $this->parent = $parent;
  473. }
  474. /**
  475. * @return PageMeta[]
  476. */
  477. public function getMetas()
  478. {
  479. return $this->metas;
  480. }
  481. public function getMeta($key)
  482. {
  483. /** @var PageMeta $meta */
  484. foreach ($this->metas as $meta) {
  485. if ($meta->getKey() == $key) {
  486. return $meta->getValue();
  487. }
  488. }
  489. return null;
  490. }
  491. public function getMetaEntity($key)
  492. {
  493. foreach ($this->metas as $meta) {
  494. if ($meta->getKey() == $key) {
  495. return $meta;
  496. }
  497. }
  498. return null;
  499. }
  500. public function setMeta($key, $value)
  501. {
  502. $meta = $this->getMetaEntity($key);
  503. if (!$meta) {
  504. $meta = new PageMeta();
  505. $meta->setKey($key);
  506. $this->addMeta($meta);
  507. $meta->setPage($this);
  508. }
  509. $meta->setValue($value);
  510. return $this;
  511. }
  512. /**
  513. * @param PageMeta[] $metas
  514. */
  515. public function setMetas($metas)
  516. {
  517. $this->metas = $metas;
  518. }
  519. /**
  520. * Add metas
  521. *
  522. * @param PageMeta $meta
  523. *
  524. * @return Page
  525. */
  526. public function addMeta(PageMeta $meta)
  527. {
  528. $this->metas[] = $meta;
  529. return $this;
  530. }
  531. /**
  532. * Remove metas
  533. *
  534. * @param PageMeta $metas
  535. */
  536. public function removeMeta(PageMeta $metas)
  537. {
  538. $this->metas->removeElement($metas);
  539. }
  540. /**
  541. * @return string
  542. */
  543. public function getTransientUrl()
  544. {
  545. return $this->transientUrl;
  546. }
  547. /**
  548. * @param string $transientUrl
  549. */
  550. public function setTransientUrl($transientUrl)
  551. {
  552. $this->transientUrl = $transientUrl;
  553. }
  554. public function __toString()
  555. {
  556. return $this->menuLabel;
  557. }
  558. /**
  559. * @return string
  560. */
  561. public function getRobots()
  562. {
  563. return $this->robots;
  564. }
  565. /**
  566. * @param string $robots
  567. */
  568. public function setRobots($robots)
  569. {
  570. $this->robots = $robots;
  571. }
  572. }