/** * @Route("/forum/mod/{action}/{threadId}-{slug}/{messageId}", defaults={"messageId" = null}, requirements={"messageId" = "\d+"}, name="forum_mod") * @Template() */ public function modAction($action, $threadId, $messageId = null) { if (!$this->getUser()) { return $this->createAccessDeniedResponse(); } $em = $this->getDoctrine()->getManager(); $thread = $em->createQueryBuilder()->select('t, c')->from('EtuModuleForumBundle:Thread', 't')->leftJoin('t.category', 'c')->where('t.id = :id')->andWhere('t.state != 300')->setParameter('id', $threadId)->getQuery()->getSingleResult(); $category = $thread->getCategory(); $categoryId = $category->getId(); $parents = $em->createQueryBuilder()->select('c')->from('EtuModuleForumBundle:Category', 'c')->where('c.left <= :left')->andWhere('c.right >= :right')->setParameter('left', $category->getLeft())->setParameter('right', $category->getRight())->orderBy('c.depth')->getQuery()->getResult(); $return = array(); switch ($action) { case 'remove': $checker = new PermissionsChecker($this->getUser()); if (!$checker->canDelete($category)) { $return = $this->createAccessDeniedResponse(); } if ($messageId == null) { $messages = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.thread = :thread')->setParameter('thread', $thread)->orderBy('m.createdAt')->getQuery()->getResult(); foreach ($messages as $message) { $category->setCountMessages($category->getCountMessages() - 1); $thread->setCountMessages($thread->getCountMessages() - 1); $em->remove($message); } $category->setCountThreads($category->getCountThreads() - 1); $em->remove($thread); } $message = $em->getRepository('EtuModuleForumBundle:Message')->find($messageId); if ($message->getCreatedAt() == $thread->getCreatedAt()) { $messages = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.thread = :thread')->setParameter('thread', $thread)->orderBy('m.createdAt')->getQuery()->getResult(); foreach ($messages as $message) { $category->setCountMessages($category->getCountMessages() - 1); $thread->setCountMessages($thread->getCountMessages() - 1); $em->remove($message); } $category->setCountThreads($category->getCountThreads() - 1); $em->remove($thread); } else { $thread->setCountMessages($thread->getCountMessages() - 1); $category->setCountMessages($thread->getCountMessages() - 1); $em->remove($message); } $em->flush(); $category = $em->getRepository('EtuModuleForumBundle:Category')->find($categoryId); $thread = $em->getRepository('EtuModuleForumBundle:Category')->find($threadId); $getLastMessage = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.category = :category')->setParameter('category', $category)->orderBy('m.createdAt', 'DESC')->setMaxResults(1)->getQuery(); try { $getLastMessage = $getLastMessage->getSingleResult(); $category->setLastMessage($getLastMessage); } catch (\Doctrine\Orm\NoResultException $e) { $category->setLastMessage(NULL); } $getLastMessage = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.thread = :thread')->setParameter('thread', $thread)->orderBy('m.createdAt', 'DESC')->setMaxResults(1)->getQuery(); try { $getLastMessage = $getLastMessage->getSingleResult(); $thread->setLastMessage($getLastMessage); $em->persist($thread); $return = $this->redirect($this->generateUrl('forum_thread', array('id' => $thread->getId(), 'slug' => $thread->getSlug()))); } catch (\Doctrine\Orm\NoResultException $e) { $return = $this->redirect($this->generateUrl('forum_category', array('id' => $category->getId(), 'slug' => $category->getSlug()))); } $em->persist($category); $em->flush(); break; case 'lock': $thread = $em->getRepository('EtuModuleForumBundle:Thread')->find($threadId); $category = $thread->getCategory(); $checker = new PermissionsChecker($this->getUser()); if (!$checker->canLock($category)) { return $this->createAccessDeniedResponse(); } if ($thread->getState() == 200) { $thread->setState(100); } else { $thread->setState(200); } $return = $this->redirect($this->generateUrl('forum_thread', array('id' => $thread->getId(), 'slug' => $thread->getSlug()))); break; case 'move': $thread = $em->getRepository('EtuModuleForumBundle:Thread')->find($threadId); $category = $thread->getCategory(); $checker = new PermissionsChecker($this->getUser()); if (!$checker->canMove($category)) { $return = $this->createAccessDeniedResponse(); } $c = $em->getRepository('EtuModuleForumBundle:Category'); $form = $this->createFormBuilder($thread)->add('category', 'entity', array('class' => 'EtuModuleForumBundle:Category', 'query_builder' => function (EntityRepository $er) { $categoriesList = array(); $categories = $er->createQueryBuilder('c')->orderBy('c.left')->getQuery()->getResult(); foreach ($categories as $category) { $checker = new PermissionsChecker($this->getUser()); if ($checker->canRead($category)) { array_push($categoriesList, $category); } } $categories = $er->createQueryBuilder('c'); $categories->where('c.id = 0'); $i = 0; foreach ($categoriesList as $category) { $categories->orWhere('c.id = :cat' . $i); $categories->setParameter('cat' . $i, $category->getId()); $i++; } $categories->orderBy('c.left'); return $categories; }))->getForm(); $request = $this->get('request'); if ($request->getMethod() == 'POST') { $form->bind($request); if ($form->isValid()) { $category->setCountThreads($category->getCountThreads() - 1)->setCountMessages($category->getCountMessages() - $thread->getCountMessages()); $newCat = $thread->getCategory(); $newCat->setCountThreads($newCat->getCountThreads() + 1)->setCountMessages($newCat->getCountMessages() + $thread->getCountMessages()); $thread->setCategory($newCat); $modMessages = $em->createQueryBuilder()->update('EtuModuleForumBundle:Message', 'm')->set('m.category', ':newCat')->setParameter('newCat', $newCat)->where('m.thread = :thread')->setParameter('thread', $thread)->getQuery()->execute(); $em->persist($thread); $getLastMessage = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.category = :category')->setParameter('category', $category)->orderBy('m.createdAt', 'DESC')->setMaxResults(1)->getQuery(); try { $getLastMessage = $getLastMessage->getSingleResult(); $category->setLastMessage($getLastMessage); } catch (\Doctrine\Orm\NoResultException $e) { $category->setLastMessage(NULL); } $em->persist($category); $getLastMessage = $em->createQueryBuilder()->select('m')->from('EtuModuleForumBundle:Message', 'm')->where('m.category = :category')->setParameter('category', $newCat)->orderBy('m.createdAt', 'DESC')->setMaxResults(1)->getQuery(); try { $getLastMessage = $getLastMessage->getSingleResult(); $newCat->setLastMessage($getLastMessage); } catch (\Doctrine\Orm\NoResultException $e) { $newCat->setLastMessage(NULL); } $em->persist($newCat); $em->flush(); $return = $this->redirect($this->generateUrl('forum_thread', array('id' => $thread->getId(), 'slug' => $thread->getSlug()))); } } else { $return = array('parents' => $parents, 'action' => 'move', 'thread' => $thread, 'form' => $form->createView()); } } return $return; }
/** * @param UserInterface $user * @param Category $category * @return bool */ public function canDelete($user, Category $category) { $checker = new PermissionsChecker($user); return $checker->canDelete($category); }