XF 2.3 Bug with my pagination code

Lee

Well-known member
I am having an issue with my pagination code.

In my template I have:

HTML:
<xf:pagenav link="mood-tracker" page="{$page}" perpage="{$perPage}" total="{$total}" params="{$filters}"
        wrapperclass="block-outer block-outer--after" />

Route is set like this: :int<mood_id>/:page

Index Controller:

PHP:
public function actionIndex(ParameterBag $params)
    {
        $visitor = \XF::visitor();
    
        // Fetch filters from the request
        $filters = $this->filter([
            'mood' => 'str',
            'start_date' => 'str',
            'end_date' => 'str',
        ]);

        $page = $this->filterPage($params->mood_id);
        $perPage = 10; // Number of items per page
    
        // Initialize the mood repository
        $moodRepo = $this->repository('TrappedMinds\UserMoods:UserMoods');
        $finder = $moodRepo->findUserMoods($visitor->user_id);
    
        // Apply filters to the finder
        if (!empty($filters['mood'])) {
            $finder->where('mood', $filters['mood']);
        }

        if (!empty($filters['start_date'])) {
            try {
                $startDate = new \DateTime($filters['start_date']);
                $finder->where('log_date', '>=', $startDate->getTimestamp());
            } catch (\Exception $e) {
                return $this->error('Invalid start date.');
            }
        }

        if (!empty($filters['end_date'])) {
            try {
                $endDate = new \DateTime($filters['end_date']);
                // Ensure the entire day is included
                $endDate->setTime(23, 59, 59);
                $finder->where('log_date', '<=', $endDate->getTimestamp());
            } catch (\Exception $e) {
                return $this->error('Invalid end date.');
            }
        }

        $finder->limitByPage($page, $perPage);

        // Fetch filtered mood logs
        $moodLogs = $finder->fetch();
        $total = $finder->total();

        \XF::logError("Page: $page, PerPage: $perPage");

        // Prepare graph data
        $graphData = $this->prepareGraphData($moodLogs);
        $graphDataJson = json_encode($graphData);
    
        $viewParams = [
            'moodLogs' => $moodLogs,
            'filters' => $filters,
            'moods' => $this->getAvailableMoods(),
            'graphData' => $graphData,
            'graphDataJson' => $graphDataJson, // Pass JSON string to the template
            'page' => $page,
            'perPage' => $perPage,
            'total' => $total,
        ];
    
        return $this->view('TrappedMinds\UserMoods:Index', 'tm_moodtracker_index', $viewParams);
    }

For some reason my page param is never making it to the $page variable, so when I click "next" or "2" in the pagination, it always returns 1. Even though the URL is updating to say "page-2" at the end. If I manually build the query string, it works.
 
After a sleep, I have fixed this.

$params->mood_id should have been $params->page
 
Back
Top Bottom