XF 2.2 Get post_date of the last post of page X

Scandal

Well-known member
Hello all devs! :)
Well, I need to build a getter for XF:Thread entity which do this: for page number $page (of the thread), return the post_date of its (I mean page's) last post.

I'm trying to find an efficient way to achive this. Even with a simple query / finder. I think that we will have to use the options()->messagesPerPage, but I'm not sure.

There is also the $this->post_ids getter for XF:Thread entity, if that helps.

Any suggestions or ideas are welcome!
 
Well, I made this solution:
1. Thread getter to get all visible post_ids (since $this->post_id includes also deleted and moderated posts).
PHP:
    public function getScVisiblePostIds()
    {
        return $this->db()->fetchAllColumn("
            SELECT post_id
            FROM xf_post
            WHERE thread_id = ? AND message_state = 'visible'
            ORDER BY post_date
        ", $this->thread_id);
    }
2. And here "the core":
PHP:
        $perPage = max(1, intval($this->app->options()->messagesPerPage));
        $total = max(0, intval($thread->reply_count + 1));
        $totalPages = ceil($total / $perPage);
       if (!$check_page)
        {
            $check_page = 1;
        }

        if ($totalPages <= 1)
        {
            $last_post_date = $thread->last_post_date;
        }
        else
        {
            $chunk = array_chunk($thread->sc_visible_post_ids, $this->app->options()->messagesPerPage);
               
            for($page = 1; $page <= $totalPages; $page++)
            {
                if ($page == $check_page)
                {
                    $index = $page - 1;
                    $last_post_id = end($chunk[$index]);
                    $last_post_date = \XF::finder('XF:Post')->where('post_id', $last_post_id)->fetchOne()->post_date;
                }
            }          
        }
 
Any improvement to the script would be useful! :)
Tip: there is the column position on xf_post. If we do position + 1, we get the location of the post on the thread.

Maybe if we combine the position, messagesPerPage and thread reply_count info, we can bypass the getScVisiblePostIds query?
 
Back
Top Bottom