public function handleActions($topic_id, $action)
    {
        $action = Container::get('hooks')->fire('model.topic.handle_actions_start', $action, $topic_id);

        // If action=new, we redirect to the first new post (if any)
        if ($action == 'new') {
            if (!User::get()->is_guest) {
                // We need to check if this topic has been viewed recently by the user
                $tracked_topics = Track::getTrackedTopics();
                $last_viewed = isset($tracked_topics['topics'][$topic_id]) ?
                    $tracked_topics['topics'][$topic_id] : User::get()->last_visit;

                $first_new_post_id = DB::forTable('posts')
                                        ->where('topic_id', $topic_id)
                                        ->where_gt('posted', $last_viewed)
                                        ->min('id');

                $first_new_post_id = Container::get('hooks')
                    ->fire('model.topic.handle_actions_first_new', $first_new_post_id);

                if ($first_new_post_id) {
                    return Router::redirect(
                        Router::pathFor('viewPost', ['pid' => $first_new_post_id]).'#p'.$first_new_post_id
                    );
                }
            }

            // If there is no new post, we go to the last post
            $action = 'last';
        }

        // If action=last, we redirect to the last post
        if ($action == 'last') {
            $last_post_id = DB::forTable('posts')
                                ->where('topic_id', $topic_id)
                                ->max('id');

            $last_post_id = Container::get('hooks')->fire('model.topic.handle_actions_last_post', $last_post_id);

            if ($last_post_id) {
                return Router::redirect(Router::pathFor('viewPost', ['pid' => $last_post_id]).'#p'.$last_post_id);
            }
        }

        Container::get('hooks')->fire('model.topic.handle_actions', $action, $topic_id);
    }
public function handleActions($topic_id, $action)
    {
        $action = Container::get('hooks')->fire('model.topic.handle_actions_start', $action, $topic_id);

        // If action=new, we redirect to the first new post (if any)
        if ($action == 'new') {
            if (!User::get()->is_guest) {
                // We need to check if this topic has been viewed recently by the user
                $tracked_topics = Track::getTrackedTopics();
                $last_viewed = isset($tracked_topics['topics'][$topic_id]) ?
                    $tracked_topics['topics'][$topic_id] : User::get()->last_visit;

                $first_new_post_id = DB::forTable('posts')
                                        ->where('topic_id', $topic_id)
                                        ->where_gt('posted', $last_viewed)
                                        ->min('id');

                $first_new_post_id = Container::get('hooks')
                    ->fire('model.topic.handle_actions_first_new', $first_new_post_id);

                if ($first_new_post_id) {
                    return Router::redirect(
                        Router::pathFor('viewPost', ['pid' => $first_new_post_id]).'#p'.$first_new_post_id
                    );
                }
            }

            // If there is no new post, we go to the last post
            $action = 'last';
        }

        // If action=last, we redirect to the last post
        if ($action == 'last') {
            $last_post_id = DB::forTable('posts')
                                ->where('topic_id', $topic_id)
                                ->max('id');

            $last_post_id = Container::get('hooks')->fire('model.topic.handle_actions_last_post', $last_post_id);

            if ($last_post_id) {
                return Router::redirect(Router::pathFor('viewPost', ['pid' => $last_post_id]).'#p'.$last_post_id);
            }
        }

        Container::get('hooks')->fire('model.topic.handle_actions', $action, $topic_id);
    }
public function handleActions ( $ topic_id , $ action ) { $ action = Container :: get ( 'hooks' ) -> fire ( 'model.topic.handle_actions_start' , $ action , $ topic_id ) ; // If action=new, we redirect to the first new post (if any) if ( $ action == 'new' ) { if ( ! User :: get ( ) -> is_guest ) { // We need to check if this topic has been viewed recently by the user $ tracked_topics = Track :: getTrackedTopics ( ) ; $ last_viewed = isset ( $ tracked_topics [ 'topics' ] [ $ topic_id ] ) ? $ tracked_topics [ 'topics' ] [ $ topic_id ] : User :: get ( ) -> last_visit ; $ first_new_post_id = DB :: forTable ( 'posts' ) -> where ( 'topic_id' , $ topic_id ) -> where_gt ( 'posted' , $ last_viewed ) -> min ( 'id' ) ; $ first_new_post_id = Container :: get ( 'hooks' ) -> fire ( 'model.topic.handle_actions_first_new' , $ first_new_post_id ) ; if ( $ first_new_post_id ) { return Router :: redirect ( Router :: pathFor ( 'viewPost' , [ 'pid' => $ first_new_post_id ] ) . '#p' . $ first_new_post_id ) ; } } // If there is no new post, we go to the last post $ action = 'last' ; } // If action=last, we redirect to the last post if ( $ action == 'last' ) { $ last_post_id = DB :: forTable ( 'posts' ) -> where ( 'topic_id' , $ topic_id ) -> max ( 'id' ) ; $ last_post_id = Container :: get ( 'hooks' ) -> fire ( 'model.topic.handle_actions_last_post' , $ last_post_id ) ; if ( $ last_post_id ) { return Router :: redirect ( Router :: pathFor ( 'viewPost' , [ 'pid' => $ last_post_id ] ) . '#p' . $ last_post_id ) ; } } Container :: get ( 'hooks' ) -> fire ( 'model.topic.handle_actions' , $ action , $ topic_id ) ; }
Redirect to new posts or last post
@param $topic_id
@param $action
@return mixed
Redirect to new posts or last post