コード例 #1
0
 /**
  * @param \Coyote\Forum $forum
  * @param \Coyote\Topic $topic
  * @param string $slug
  * @param Request $request
  * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function index($forum, $topic, $slug, Request $request)
 {
     if ($topic->forum_id !== $forum->id) {
         return redirect(route('forum.topic', [$forum->path, $topic->id, $topic->path]));
     }
     $userId = auth()->id();
     $sessionId = $request->session()->getId();
     // pobranie daty i godziny ostatniego razu gdy uzytkownik przeczytal ten watek
     $topicMarkTime = $this->topic->markTime($topic->id, $userId, $sessionId);
     // pobranie daty i godziny ostatniego razy gdy uzytkownik przeczytal to forum
     $forumMarkTime = $this->forum->markTime($forum->id, $userId, $sessionId);
     if ($request->get('view') === 'unread') {
         if ($topicMarkTime < $topic->last_post_created_at && $forumMarkTime < $topic->last_post_created_at) {
             $markTime = max($topicMarkTime, $forumMarkTime);
             if ($markTime) {
                 $postId = $this->post->getFirstUnreadPostId($topic->id, $markTime);
                 if ($postId && $postId !== $topic->first_post_id) {
                     $url = route('forum.topic', [$forum->path, $topic->id, $topic->path]);
                     return response($url) . '?p=' . $postId . '#id' . $postId;
                 }
             }
         }
     }
     // current page...
     $page = $request->page;
     if ($request->has('p')) {
         $page = $this->post->getPage($request->get('p'), $topic->id);
     }
     // magic happens here. get posts for given topic
     $posts = $this->post->takeForTopic($topic->id, $topic->first_post_id, $userId, $page);
     $paginate = new LengthAwarePaginator($posts, $topic->replies, 10, $page, ['path' => ' ']);
     $parser = ['post' => app()->make('Parser\\Post'), 'comment' => app()->make('Parser\\Comment'), 'sig' => app()->make('Parser\\Sig')];
     $markTime = null;
     foreach ($posts as &$post) {
         // parse post or get it from cache
         $post->text = $parser['post']->parse($post->text);
         if ((auth()->guest() || auth()->check() && auth()->user()->allow_sig) && $post->sig) {
             $post->sig = $parser['sig']->parse($post->sig);
         }
         $markTime = $post->created_at;
     }
     if ($topicMarkTime < $markTime && $forumMarkTime < $markTime) {
         $this->topic->markAsRead($topic->id, $forum->id, $markTime, $userId, $sessionId);
         $isUnread = true;
         if ($forumMarkTime < $markTime) {
             $isUnread = $this->topic->isUnread($forum->id, $forumMarkTime, $userId, $sessionId);
         }
         if (!$isUnread) {
             $this->forum->markAsRead($forum->id, $userId, $sessionId);
         }
     }
     $this->breadcrumb($forum);
     $this->breadcrumb->push($topic->subject, route('forum.topic', [$forum->path, $topic->id, $topic->path]));
     // create forum list for current user (according to user's privileges)
     $this->pushForumCriteria();
     $forumList = $this->forum->forumList();
     $viewers = app('Session\\Viewers')->render($request->getRequestUri());
     // let's cache tags. we don't need to run this query every time
     $tags = Cache::remember('forum:tags', 60 * 24, function () {
         return $this->forum->getTagClouds();
     });
     return parent::view('forum.topic')->with(compact('viewers', 'posts', 'forum', 'topic', 'paginate', 'forumList', 'tags'));
 }