/Entity/Category.php

https://gitlab.com/Incolab/IncolabForumBundle · PHP · 599 lines · 224 code · 77 blank · 298 comment · 3 complexity · 1857c74f8bffe9f2784a655600a44691 MD5 · raw file

  1. <?php
  2. namespace Incolab\ForumBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * Category
  6. *
  7. * @ORM\Table(name="forum_category")
  8. * @ORM\Entity(repositoryClass="Incolab\ForumBundle\Repository\CategoryRepository")
  9. */
  10. class Category
  11. {
  12. /**
  13. * @var int
  14. *
  15. * @ORM\Column(name="id", type="integer")
  16. * @ORM\Id
  17. * @ORM\GeneratedValue(strategy="AUTO")
  18. */
  19. private $id;
  20. /**
  21. * @var string
  22. *
  23. * @ORM\Column(name="name", type="string", length=255)
  24. */
  25. private $name;
  26. /**
  27. * @var string
  28. *
  29. * @ORM\Column(name="description", type="text")
  30. */
  31. private $description;
  32. /**
  33. * @var int
  34. *
  35. * @ORM\ManyToOne(targetEntity="Incolab\ForumBundle\Entity\Category", cascade={"persist"}, inversedBy="childs")
  36. */
  37. private $parent;
  38. /**
  39. * @var string
  40. *
  41. * @ORM\Column(name="read_roles", type="array", options={"default"="a:0:{}"})
  42. */
  43. private $readRoles;
  44. /**
  45. * @var string
  46. *
  47. * @ORM\Column(name="write_roles", type="array", options={"default"="a:0:{}"})
  48. */
  49. private $writeRoles;
  50. /**
  51. *
  52. * @ORM\OneToMany(targetEntity="Incolab\ForumBundle\Entity\Category", mappedBy="parent")
  53. */
  54. private $childs;
  55. /**
  56. * @var string
  57. *
  58. * @ORM\Column(name="slug", type="string", length=255, unique=true)
  59. */
  60. private $slug;
  61. /**
  62. * @var int
  63. *
  64. * @ORM\Column(name="position", type="integer")
  65. */
  66. private $position;
  67. /**
  68. * @var int
  69. *
  70. * @ORM\OneToMany(targetEntity="Incolab\ForumBundle\Entity\Topic", mappedBy="category")
  71. */
  72. private $topics;
  73. /**
  74. * @var int
  75. *
  76. * @ORM\Column(name="num_topics", type="integer")
  77. */
  78. private $numTopics;
  79. /**
  80. * @var int
  81. *
  82. * @ORM\Column(name="num_posts", type="integer")
  83. */
  84. private $numPosts;
  85. /**
  86. * @var \stdClass
  87. *
  88. * @ORM\ManyToOne(targetEntity="Incolab\ForumBundle\Entity\Topic", cascade="persist")
  89. */
  90. private $lastTopic;
  91. /**
  92. * @var \stdClass
  93. *
  94. * @ORM\ManyToOne(targetEntity="Incolab\ForumBundle\Entity\Post", cascade="persist")
  95. */
  96. private $lastPost;
  97. /**
  98. * Constructor
  99. */
  100. public function __construct()
  101. {
  102. $this->childs = new \Doctrine\Common\Collections\ArrayCollection();
  103. $this->topics = new \Doctrine\Common\Collections\ArrayCollection();
  104. $this->numPosts = 0;
  105. $this->numTopics = 0;
  106. }
  107. /**
  108. * Get id
  109. *
  110. * @return integer
  111. */
  112. public function getId()
  113. {
  114. return $this->id;
  115. }
  116. /**
  117. * Set name
  118. *
  119. * @param string $name
  120. *
  121. * @return Category
  122. */
  123. public function setName($name)
  124. {
  125. $this->name = $name;
  126. return $this;
  127. }
  128. /**
  129. * Get name
  130. *
  131. * @return string
  132. */
  133. public function getName()
  134. {
  135. return $this->name;
  136. }
  137. /**
  138. * Set description
  139. *
  140. * @param string $description
  141. *
  142. * @return Category
  143. */
  144. public function setDescription($description)
  145. {
  146. $this->description = $description;
  147. return $this;
  148. }
  149. /**
  150. * Get description
  151. *
  152. * @return string
  153. */
  154. public function getDescription()
  155. {
  156. return $this->description;
  157. }
  158. /**
  159. * Set slug
  160. *
  161. * @param string $slug
  162. *
  163. * @return Category
  164. */
  165. public function setSlug($slug)
  166. {
  167. $this->slug = $slug;
  168. return $this;
  169. }
  170. /**
  171. * Get slug
  172. *
  173. * @return string
  174. */
  175. public function getSlug()
  176. {
  177. return $this->slug;
  178. }
  179. /**
  180. * Set position
  181. *
  182. * @param integer $position
  183. *
  184. * @return Category
  185. */
  186. public function setPosition($position)
  187. {
  188. $this->position = $position;
  189. return $this;
  190. }
  191. /**
  192. * Get position
  193. *
  194. * @return integer
  195. */
  196. public function getPosition()
  197. {
  198. return $this->position;
  199. }
  200. /**
  201. * Increments the category position
  202. */
  203. public function incrementPosition()
  204. {
  205. $this->position++;
  206. return $this;
  207. }
  208. /**
  209. * Decrements the category position
  210. */
  211. public function decrementPosition()
  212. {
  213. $this->position--;
  214. return $this;
  215. }
  216. /**
  217. * Set numTopics
  218. *
  219. * @param integer $numTopics
  220. *
  221. * @return Category
  222. */
  223. public function setNumTopics($numTopics)
  224. {
  225. $this->numTopics = $numTopics;
  226. return $this;
  227. }
  228. /**
  229. * Get numTopics
  230. *
  231. * @return integer
  232. */
  233. public function getNumTopics()
  234. {
  235. return $this->numTopics;
  236. }
  237. /**
  238. * Increments the number of topics
  239. */
  240. public function incrementNumTopics()
  241. {
  242. $this->numTopics++;
  243. return $this;
  244. }
  245. /**
  246. * Decrements the number of topics
  247. */
  248. public function decrementNumTopics()
  249. {
  250. $this->numTopics--;
  251. return $this;
  252. }
  253. /**
  254. * Set numPosts
  255. *
  256. * @param integer $numPosts
  257. *
  258. * @return Category
  259. */
  260. public function setNumPosts($numPosts)
  261. {
  262. $this->numPosts = $numPosts;
  263. return $this;
  264. }
  265. /**
  266. * Get numPosts
  267. *
  268. * @return integer
  269. */
  270. public function getNumPosts()
  271. {
  272. return $this->numPosts;
  273. }
  274. /**
  275. * Increments the number of posts
  276. */
  277. public function incrementNumPosts()
  278. {
  279. $this->numPosts++;
  280. return $this;
  281. }
  282. /**
  283. * Decrements the number of posts
  284. */
  285. public function decrementNumPosts()
  286. {
  287. $this->numPosts--;
  288. return $this;
  289. }
  290. /**
  291. * Set parent
  292. *
  293. * @param \Incolab\ForumBundle\Entity\Category $parent
  294. *
  295. * @return Category
  296. */
  297. public function setParent(\Incolab\ForumBundle\Entity\Category $parent = null)
  298. {
  299. $this->parent = $parent;
  300. return $this;
  301. }
  302. /**
  303. * Get parent
  304. *
  305. * @return \Incolab\ForumBundle\Entity\Category
  306. */
  307. public function getParent()
  308. {
  309. return $this->parent;
  310. }
  311. /**
  312. * Add child
  313. *
  314. * @param \Incolab\ForumBundle\Entity\Category $child
  315. *
  316. * @return Category
  317. */
  318. public function addChild(\Incolab\ForumBundle\Entity\Category $child)
  319. {
  320. $this->childs[] = $child;
  321. return $this;
  322. }
  323. /**
  324. * Remove child
  325. *
  326. * @param \Incolab\ForumBundle\Entity\Category $child
  327. */
  328. public function removeChild(\Incolab\ForumBundle\Entity\Category $child)
  329. {
  330. $this->childs->removeElement($child);
  331. }
  332. /**
  333. * Get childs
  334. *
  335. * @return \Doctrine\Common\Collections\Collection
  336. */
  337. public function getChilds()
  338. {
  339. return $this->childs;
  340. }
  341. /**
  342. * Get child by slug
  343. *
  344. * @return Category
  345. */
  346. public function getChildBySlug($slug)
  347. {
  348. foreach ($this->childs as $child) {
  349. if ($child->getSlug() === $slug){
  350. return $child;
  351. }
  352. }
  353. }
  354. /**
  355. * Add topic
  356. *
  357. * @param \Incolab\ForumBundle\Entity\Topic $topic
  358. *
  359. * @return Category
  360. */
  361. public function addTopic(\Incolab\ForumBundle\Entity\Topic $topic)
  362. {
  363. $this->topics[] = $topic;
  364. return $this;
  365. }
  366. /**
  367. * Remove topic
  368. *
  369. * @param \Incolab\ForumBundle\Entity\Topic $topic
  370. */
  371. public function removeTopic(\Incolab\ForumBundle\Entity\Topic $topic)
  372. {
  373. $this->topics->removeElement($topic);
  374. }
  375. /**
  376. * Get topics
  377. *
  378. * @return \Doctrine\Common\Collections\Collection
  379. */
  380. public function getTopics()
  381. {
  382. return $this->topics;
  383. }
  384. /**
  385. * Set lastTopic
  386. *
  387. * @param \Incolab\ForumBundle\Entity\Topic $lastTopic
  388. *
  389. * @return Category
  390. */
  391. public function setLastTopic(\Incolab\ForumBundle\Entity\Topic $lastTopic = null)
  392. {
  393. $this->lastTopic = $lastTopic;
  394. return $this;
  395. }
  396. /**
  397. * Get lastTopic
  398. *
  399. * @return \Incolab\ForumBundle\Entity\Topic
  400. */
  401. public function getLastTopic()
  402. {
  403. return $this->lastTopic;
  404. }
  405. /**
  406. * Set lastPost
  407. *
  408. * @param \Incolab\ForumBundle\Entity\Post $lastPost
  409. *
  410. * @return Category
  411. */
  412. public function setLastPost(\Incolab\ForumBundle\Entity\Post $lastPost = null)
  413. {
  414. $this->lastPost = $lastPost;
  415. return $this;
  416. }
  417. /**
  418. * Get lastPost
  419. *
  420. * @return \Incolab\ForumBundle\Entity\Post
  421. */
  422. public function getLastPost()
  423. {
  424. return $this->lastPost;
  425. }
  426. /**
  427. * Add readRoles
  428. *
  429. * @param array $readRole
  430. *
  431. * @return Category
  432. */
  433. public function addReadRole($readRole)
  434. {
  435. $readRole = strtoupper($readRole);
  436. if (!in_array($readRole, $this->readRoles, true)) {
  437. $this->readRoles[] = $readRole;
  438. }
  439. return $this;
  440. }
  441. /**
  442. * Set readRoles
  443. *
  444. * @param array $readRoles
  445. *
  446. * @return Category
  447. */
  448. public function setReadRoles(array $readRoles)
  449. {
  450. $this->readRoles = array();
  451. foreach ($readRoles as $role) {
  452. $this->addReadRole($role);
  453. }
  454. return $this;
  455. }
  456. /**
  457. * Get readRoles
  458. *
  459. * @return array
  460. */
  461. public function getReadRoles()
  462. {
  463. return $this->readRoles;
  464. }
  465. /*
  466. * @param string $readRole
  467. *
  468. * @return boolean
  469. */
  470. public function hasReadRole($readRole)
  471. {
  472. return in_array(strtoupper($readRole), $this->getReadRoles(), true);
  473. }
  474. /**
  475. * Add writeRoles
  476. *
  477. * @param array $writeRole
  478. *
  479. * @return Category
  480. */
  481. public function addWriteRole($writeRole)
  482. {
  483. $writeRole = strtoupper($writeRole);
  484. if (!in_array($writeRole, $this->writeRoles, true)) {
  485. $this->writeRoles[] = $writeRole;
  486. }
  487. return $this;
  488. }
  489. /**
  490. * Set writeRoles
  491. *
  492. * @param array $writeRoles
  493. *
  494. * @return Category
  495. */
  496. public function setWriteRoles($writeRoles)
  497. {
  498. $this->writeRoles = array();
  499. foreach ($writeRoles as $role) {
  500. $this->addWriteRole($role);
  501. }
  502. return $this;
  503. }
  504. /**
  505. * Get writeRoles
  506. *
  507. * @return array
  508. */
  509. public function getWriteRoles()
  510. {
  511. return $this->writeRoles;
  512. }
  513. /*
  514. * @param string $writeRole
  515. *
  516. * @return boolean
  517. */
  518. public function hasWriteRole($writeRole)
  519. {
  520. return in_array(strtoupper($writeRole), $this->getWriteRoles(), true);
  521. }
  522. }