コード例 #1
0
 public function newAction($boardSlug = null)
 {
     $board = $this->getBoard();
     $user = $this->getUser();
     $this->throwAccessDeniedIfLoggedOut();
     $this->throwAccessDeniedIfPermission('canCreateTopic', $user, $board);
     $request = $this->get('request');
     // List of existing topics with the same slug or title
     $existingTopics = array();
     $topic = new Topic();
     $form = $this->createForm(new CreateTopicType(), $topic);
     if ($request->getMethod() === 'POST') {
         $form->bind($request);
         $boardId = $request->request->get('board_id');
         if ($board === null && $boardId === '') {
             $form->addError(new FormError($this->get('translator')->trans('Board.selected.not.valid')));
         } else {
             if ($boardId !== null) {
                 $board = $this->get('teapotio.forum.board')->getById($boardId);
             }
         }
         if ($form->isValid() === true) {
             try {
                 $topic->setBoard($board);
                 $this->get('teapotio.forum.topic')->save($topic);
                 $message = new Message();
                 $message->setBody($form['body']->getData());
                 $message->setTopic($topic);
                 $message->setTopicBody(true);
                 $this->get('teapotio.forum.message')->save($message);
                 return $this->redirect($this->get('teapotio.forum')->forumPath('ForumListMessagesByTopic', $topic));
             } catch (DuplicateTopicException $e) {
                 $form->addError(new FormError($this->get('translator')->trans('Topic.title.is.already.in.use')));
                 $existingTopics = $e->topics;
             }
         }
     }
     $infoNotices = array();
     if ($board !== null) {
         $infoNotices[] = $this->get('translator')->trans('Create.topic.in.notice', array('%board_name%' => $board->getTitle()));
         $title = $this->generateTitle('New.topic.in.%title%', array('%title%' => $board->getTitle()));
     } else {
         $title = $this->generateTitle('New.topic');
     }
     $params = array('form' => $form->createView(), 'current_board' => $board, 'page_title' => $title, 'info_notices' => $infoNotices, 'existing_topics' => $existingTopics);
     return $this->superRender('TeapotioForumBundle:page:topic/new.html.twig', $params);
 }
コード例 #2
0
 public function listAction($boardSlug, $topicSlug)
 {
     $board = $this->getBoard();
     $topic = $this->getTopic();
     $user = $this->getUser();
     $isUserModerator = $this->get('teapotio.forum.access_permission')->isModerator($user, $board);
     if (true !== ($response = $this->isUrlValid($board, $topic, $boardSlug, $topicSlug))) {
         return $response;
     }
     $this->throwAccessDeniedIfPermission('canView', $user, $topic);
     if ($isUserModerator === false && $topic->isDeleted() === true) {
         throw $this->createNotFoundException();
     }
     $this->get('teapotio.forum.topic')->view($topic);
     // Build the form if the user is allowed
     $form = $message = null;
     if ($this->get('teapotio.forum.access_permission')->canCreateMessage($user, $board) === true) {
         $message = new Message();
         $message->setUser($user);
         $message->setBody($this->renderView('TeapotioForumBundle:component:rules.html.twig', array('prefix' => $this->get('translator')->trans('Add.a.new.message'))));
         $form = $this->createForm(new CreateMessageType(), $message, array('new_entry' => true))->createView();
         $message = $form->vars['value'];
     }
     $messagesPerPage = $this->get('teapotio.forum')->getTotalMessagesPerPage();
     $page = $this->get('request')->get('page') === null ? 1 : $this->get('request')->get('page');
     $offset = ($page - 1) * $messagesPerPage;
     $isDeleted = $isUserModerator === true ? null : false;
     $messages = $this->get('teapotio.forum.message')->getMessagesByTopic($topic, $offset, $messagesPerPage, $isDeleted);
     $stars = $this->get('teapotio.forum.message_star')->getStarsByMessages($messages);
     $userStars = $this->get('teapotio.forum.message_star')->getUserStarsByMessages($messages);
     // Load user models - it reduces the number of queries
     $userIds = array();
     foreach ($messages as $message) {
         $userIds[] = $message->getUser()->getId();
     }
     $users = $this->get('teapotio.user')->getByIds($userIds);
     $flags = new ArrayCollection();
     $flagTopic = null;
     if ($isUserModerator === true) {
         // The potential flag of the topic
         $flagTopic = $this->get('teapotio.forum.flag')->getByTopic($topic);
         // The potential flags in the list
         $flags = $this->get('teapotio.forum.flag')->getByMessages($messages, $board);
     }
     $this->get('teapotio.forum.message')->parseOutputBodies($messages);
     $title = $this->generateTitle('%title%', array('%title%' => $topic->getTitle()));
     $params = array('messages_per_page' => $messagesPerPage, 'messages' => $messages, 'messages_stars' => $stars, 'messages_user_stars' => $userStars, 'messages_users' => $users, 'flags' => $flags, 'flag_topic' => $flagTopic, 'stars' => $stars, 'current_board' => $board, 'topic' => $topic, 'message' => $message, 'form' => $form, 'page_title' => $title);
     return $this->superRender('TeapotioForumBundle:page:message/list.html.twig', $params);
 }