CommentsManager.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace Muzich\CoreBundle\Managers;
  3. /**
  4. * Gestionnaire de commentaires.
  5. * Les commentaires sont stocké en base sous forme de tableau json afin d'économiser
  6. * relations entre tables. Ce menager per de gérer le tableau de commentaire
  7. * plus facilement.
  8. *
  9. * @author bux
  10. */
  11. class CommentsManager
  12. {
  13. protected $comments;
  14. public function __construct($comments = array())
  15. {
  16. if ($comments == null)
  17. {
  18. $comments = array();
  19. }
  20. $this->comments = $comments;
  21. }
  22. /**
  23. * Ajouter un commentaire au tableau.
  24. *
  25. * @param \Muzich\CoreBundle\Entity\User $user
  26. * @param String $comment Contenu du commentaire
  27. * @param boolean $follow L'utilisateur désire être avertis des nouveaux commentaires
  28. * @param String $date date de l'envoi
  29. */
  30. public function add($user, $comment, $follow = false, $date = null)
  31. {
  32. if (!$date)
  33. {
  34. $date = date('Y-m-d H:i:s').' '.substr(microtime(), 0, 10);
  35. }
  36. $this->comments[] = array(
  37. "u" => array(
  38. "i" => $user->getId(),
  39. "s" => $user->getSlug(),
  40. "n" => $user->getName()
  41. ),
  42. "a" => array(),
  43. "f" => $follow,
  44. "d" => $date,
  45. "c" => $comment
  46. );
  47. if (!$follow)
  48. {
  49. $this->userSetFollowToFalse($user->getId());
  50. }
  51. }
  52. /**
  53. * Retourne le dernier enregistrement commentaire
  54. *
  55. * @return array
  56. */
  57. public function getLast()
  58. {
  59. return $this->get(count($this->comments)-1);
  60. }
  61. /**
  62. * Mise a jour d'un commentaire parmis le tableau. On l'identifie ici par son
  63. * auteur et sa date de publication.
  64. *
  65. * @param \Muzich\CoreBundle\Entity\User $user
  66. * @param date $date (Y-m-d H:i:s u)
  67. * @param string $comment_c
  68. * @param boolean $follow
  69. */
  70. public function update($user, $date, $comment_c, $follow)
  71. {
  72. $comments = array();
  73. $found = false;
  74. foreach ($this->comments as $comment)
  75. {
  76. if ($comment['u']['i'] == $user->getId() && $comment['d'] == $date)
  77. {
  78. $found = true;
  79. $comments[] = array(
  80. "u" => array(
  81. "i" => $user->getId(),
  82. "s" => $user->getSlug(),
  83. "n" => $user->getName()
  84. ),
  85. "a" => $comment['a'],
  86. "e" => date('Y-m-d H:i:s').' '.substr(microtime(), 0, 10),
  87. "f" => $follow,
  88. "d" => $date,
  89. "c" => $comment_c
  90. );
  91. }
  92. else
  93. {
  94. $comments[] = $comment;
  95. }
  96. }
  97. $this->comments = $comments;
  98. if (!$follow && $found)
  99. {
  100. $this->userSetFollowToFalse($user->getId());
  101. }
  102. }
  103. /**
  104. * Suppression d'un commentaire. Si il a été trouvé on retourne vrai.
  105. *
  106. * @param int $user_id
  107. * @param string $date (Y-m-d H:i:s u)
  108. * @return boolean
  109. */
  110. public function delete($user_id, $date)
  111. {
  112. $found = false;
  113. $comments = array();
  114. foreach ($this->comments as $comment)
  115. {
  116. if ($comment['u']['i'] != $user_id || $comment['d'] != $date)
  117. {
  118. $comments[] = $comment;
  119. }
  120. else
  121. {
  122. $found = true;
  123. }
  124. }
  125. $this->comments = $comments;
  126. return $found;
  127. }
  128. public function deleteWithDate($date)
  129. {
  130. foreach ($this->comments as $i => $comment)
  131. {
  132. if ($comment['d'] == $date)
  133. {
  134. unset($this->comments[$i]);
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
  142. * Si le commentaire n'est pas trouvé on retourne null.
  143. *
  144. * @param int $user_id
  145. * @param string $date (Y-m-d H:i:s u)
  146. * @return int
  147. */
  148. public function getIndex($user_id, $date)
  149. {
  150. foreach ($this->comments as $i => $comment)
  151. {
  152. if ($comment['u']['i'] == $user_id && $comment['d'] == $date)
  153. {
  154. return $i;
  155. }
  156. }
  157. return null;
  158. }
  159. /**
  160. * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
  161. * Si le commentaire n'est pas trouvé on retourne null.
  162. *
  163. * @param string $date (Y-m-d H:i:s u)
  164. * @return int
  165. */
  166. public function getIndexWithDate($date)
  167. {
  168. foreach ($this->comments as $i => $comment)
  169. {
  170. if ($comment['d'] == $date)
  171. {
  172. return $i;
  173. }
  174. }
  175. return null;
  176. }
  177. /**
  178. * Retourne un commentaire en fonction de son index dans le tableau.
  179. *
  180. * @return array
  181. */
  182. public function get($index = null)
  183. {
  184. if ($index === null)
  185. {
  186. return $this->comments;
  187. }
  188. return $this->comments[$index];
  189. }
  190. /**
  191. * Regarde si il existe un commentaire pour cet utilisateur où il demande
  192. * a suivre les commentaires.
  193. *
  194. * @param int $user_id
  195. * @return boolean
  196. */
  197. public function userFollow($user_id)
  198. {
  199. foreach ($this->comments as $i => $comment)
  200. {
  201. if ($comment['u']['i'] == $user_id && $comment['f'] == true)
  202. {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. /**
  209. * Passe tout les commentaire de cet utilisateur en follow = false
  210. *
  211. * @param int $user_id
  212. */
  213. private function userSetFollowToFalse($user_id)
  214. {
  215. $comments = array();
  216. foreach ($this->comments as $comment)
  217. {
  218. if ($comment['u']['i'] == $user_id)
  219. {
  220. $comment['f'] = false;
  221. }
  222. $comments[] = $comment;
  223. }
  224. $this->comments = $comments;
  225. }
  226. /**
  227. * Retourne les ids utilisateurs ayant demandé a être avertis des nouveaux
  228. * commentaires.
  229. *
  230. * @return array ids des utilisateurs
  231. */
  232. public function getFollowersIds()
  233. {
  234. $ids = array();
  235. foreach ($this->comments as $comment)
  236. {
  237. if ($comment['f'] == true)
  238. {
  239. if (!in_array($comment['u']['i'], $ids))
  240. {
  241. $ids[] = $comment['u']['i'];
  242. }
  243. }
  244. }
  245. return $ids;
  246. }
  247. /**
  248. * Signalement d'un commentaire par un utilisateur
  249. *
  250. * @param int $user_id id de l'user faisant le signalement
  251. * @param date $date date du commentaire
  252. *
  253. * @return boolean
  254. */
  255. public function alertComment($user_id, $date)
  256. {
  257. foreach ($this->comments as $i => $comment)
  258. {
  259. if ($comment['d'] == $date)
  260. {
  261. if (!in_array($user_id, $comment['a']))
  262. {
  263. $comment['a'][] = $user_id;
  264. $this->comments[$i] = $comment;
  265. return true;
  266. }
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * Retrait du signalement d'un commentaire de la part d'un user.
  273. *
  274. * @param type $user_id id de l'user retirant son signalement
  275. * @param date $date date du commentaire
  276. * @return boolean
  277. */
  278. public function unAlertComment($user_id, $date)
  279. {
  280. foreach ($this->comments as $i => $comment)
  281. {
  282. if ($comment['d'] == $date)
  283. {
  284. if (in_array($user_id, $comment['a']))
  285. {
  286. foreach ($comment['a'] as $ii => $user_alert_id)
  287. {
  288. if ($user_alert_id == $user_id)
  289. {
  290. unset($comment['a'][$ii]);
  291. $this->comments[$i] = $comment;
  292. return true;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. /**
  301. * Retourne le nombre de commentaires signalés.
  302. *
  303. * @return boolean
  304. */
  305. public function countCommentAlert()
  306. {
  307. $count = 0;
  308. foreach ($this->comments as $i => $comment)
  309. {
  310. if (count($comment['a']))
  311. {
  312. $count = $count+1;
  313. }
  314. }
  315. return $count;
  316. }
  317. public function getAlertedComments()
  318. {
  319. $comments = array();
  320. foreach ($this->comments as $i => $comment)
  321. {
  322. if (count($comment['a']))
  323. {
  324. $comments[] = $comment;
  325. }
  326. }
  327. return $comments;
  328. }
  329. public function cleanAlertsOnComment($date)
  330. {
  331. foreach ($this->comments as $i => $comment)
  332. {
  333. if ($comment['d'] == $date)
  334. {
  335. $ids = $comment['a'];
  336. $this->comments[$i]['a'] = array();
  337. return $ids;
  338. }
  339. }
  340. return array();
  341. }
  342. }