CommentsManager.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /**
  129. * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
  130. * Si le commentaire n'est pas trouvé on retourne null.
  131. *
  132. * @param int $user_id
  133. * @param string $date (Y-m-d H:i:s u)
  134. * @return int
  135. */
  136. public function getIndex($user_id, $date)
  137. {
  138. foreach ($this->comments as $i => $comment)
  139. {
  140. if ($comment['u']['i'] == $user_id && $comment['d'] == $date)
  141. {
  142. return $i;
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * Retourne un commentaire en fonction de son index dans le tableau.
  149. *
  150. * @return array
  151. */
  152. public function get($index = null)
  153. {
  154. if ($index === null)
  155. {
  156. return $this->comments;
  157. }
  158. return $this->comments[$index];
  159. }
  160. /**
  161. * Regarde si il existe un commentaire pour cet utilisateur où il demande
  162. * a suivre les commentaires.
  163. *
  164. * @param int $user_id
  165. * @return boolean
  166. */
  167. public function userFollow($user_id)
  168. {
  169. foreach ($this->comments as $i => $comment)
  170. {
  171. if ($comment['u']['i'] == $user_id && $comment['f'] == true)
  172. {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. /**
  179. * Passe tout les commentaire de cet utilisateur en follow = false
  180. *
  181. * @param int $user_id
  182. */
  183. private function userSetFollowToFalse($user_id)
  184. {
  185. $comments = array();
  186. foreach ($this->comments as $comment)
  187. {
  188. if ($comment['u']['i'] == $user_id)
  189. {
  190. $comment['f'] = false;
  191. }
  192. $comments[] = $comment;
  193. }
  194. $this->comments = $comments;
  195. }
  196. /**
  197. * Retourne les ids utilisateurs ayant demandé a être avertis des nouveaux
  198. * commentaires.
  199. *
  200. * @return array ids des utilisateurs
  201. */
  202. public function getFollowersIds()
  203. {
  204. $ids = array();
  205. foreach ($this->comments as $comment)
  206. {
  207. if ($comment['f'] == true)
  208. {
  209. if (!in_array($comment['u']['i'], $ids))
  210. {
  211. $ids[] = $comment['u']['i'];
  212. }
  213. }
  214. }
  215. return $ids;
  216. }
  217. /**
  218. * Signalement d'un commentaire par un utilisateur
  219. *
  220. * @param int $user_id id de l'user faisant le signalement
  221. * @param date $date date du commentaire
  222. *
  223. * @return boolean
  224. */
  225. public function alertComment($user_id, $date)
  226. {
  227. foreach ($this->comments as $i => $comment)
  228. {
  229. if ($comment['d'] == $date)
  230. {
  231. if (!in_array($user_id, $comment['a']))
  232. {
  233. $comment['a'][] = $user_id;
  234. $this->comments[$i] = $comment;
  235. return true;
  236. }
  237. }
  238. }
  239. return false;
  240. }
  241. /**
  242. * Retrait du signalement d'un commentaire de la part d'un user.
  243. *
  244. * @param type $user_id id de l'user retirant son signalement
  245. * @param date $date date du commentaire
  246. * @return boolean
  247. */
  248. public function unAlertComment($user_id, $date)
  249. {
  250. foreach ($this->comments as $i => $comment)
  251. {
  252. if ($comment['d'] == $date)
  253. {
  254. if (in_array($user_id, $comment['a']))
  255. {
  256. foreach ($comment['a'] as $ii => $user_alert_id)
  257. {
  258. if ($user_alert_id == $user_id)
  259. {
  260. unset($comment['a'][$ii]);
  261. $this->comments[$i] = $comment;
  262. return true;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * Permet de savoir si un ou plusieurs des commentaires ont été signalé.
  272. *
  273. * @return boolean
  274. */
  275. public function hasCommentAlert()
  276. {
  277. foreach ($this->comments as $i => $comment)
  278. {
  279. if (count($comment['a']))
  280. {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. }