public function save() { if (!$this->isValid()) { return false; } return DB::transaction(function () { $this->topic->update(['poll_title' => $this->params['title'], 'poll_start' => Carbon::now(), 'poll_length' => ($this->params['length_days'] ?? 0) * 3600, 'poll_max_options' => $this->params['max_options'], 'poll_vote_change' => $this->params['vote_change'] ?? false]); $this->topic->pollVotes()->delete(); $this->topic->pollOptions()->delete(); for ($i = 0; $i < count($this->params['options']); $i++) { PollOption::create(['topic_id' => $this->topic->topic_id, 'poll_option_id' => $i, 'poll_option_text' => $this->params['options'][$i]]); } return true; }); }
public function save() { if (!$this->isValid()) { return false; } return DB::transaction(function () { $this->topic->update(['poll_last_vote' => Carbon::now()]); $this->topic->pollVotes()->where('vote_user_id', $this->params['user_id'])->delete(); foreach (array_unique($this->params['option_ids']) as $optionId) { $this->topic->pollVotes()->create(['poll_option_id' => $optionId, 'vote_user_id' => $this->params['user_id'], 'vote_user_ip' => $this->params['ip']]); } PollOption::updateTotals(['topic_id' => $this->topic->topic_id]); return true; }); }
public function show($id) { $postStartId = Request::input('start'); $postEndId = get_int(Request::input('end')); $nthPost = get_int(Request::input('n')); $skipLayout = Request::input('skip_layout') === '1'; $jumpTo = null; $topic = Topic::with(['forum.cover', 'pollOptions.votes'])->findOrFail($id); priv_check('ForumView', $topic->forum)->ensureCan(); $posts = $topic->posts(); if ($postStartId === 'unread') { $postStartId = Post::lastUnreadByUser($topic, Auth::user()); } else { $postStartId = get_int($postStartId); } if ($nthPost !== null) { $post = $topic->nthPost($nthPost); if ($post) { $postStartId = $post->post_id; } } if (!$skipLayout) { foreach ([$postStartId, $postEndId, 0] as $jumpPoint) { if ($jumpPoint === null) { continue; } $jumpTo = $jumpPoint; break; } } if ($postStartId !== null && !$skipLayout) { // move starting post up by ten to avoid hitting // page autoloader right after loading the page. $postPosition = $topic->postPosition($postStartId); $post = $topic->nthPost($postPosition - 10); $postStartId = $post->post_id; } if ($postStartId !== null) { $posts = $posts->where('post_id', '>=', $postStartId); } elseif ($postEndId !== null) { $posts = $posts->where('post_id', '<=', $postEndId)->orderBy('post_id', 'desc'); } $posts = $posts->take(20)->with('topic')->with('user.rank')->with('user.country')->with('user.supports')->get()->sortBy('post_id'); if ($posts->count() === 0) { abort($skipLayout ? 204 : 404); } $postsPosition = $topic->postsPosition($posts); $pollSummary = PollOption::summary($topic, Auth::user()); Event::fire(new TopicWasViewed($topic, $posts->last(), Auth::user())); $template = $skipLayout ? '_posts' : 'show'; $cover = json_item($topic->cover()->firstOrNew([]), new TopicCoverTransformer()); $isWatching = TopicWatch::check($topic, Auth::user()); return view("forum.topics.{$template}", compact('cover', 'isWatching', 'jumpTo', 'pollSummary', 'posts', 'postsPosition', 'topic')); }