/** * Helper method for fetching the pagination object and pass it to K2 response object. * This is triggered by the display function. * Usually there will be no need to override this function. * * @return void */ protected function setPagination() { // Import Joomla! pagination jimport('joomla.html.pagination'); // Count rows $model = $this->getModel(); $total = $model->countRows(); $limitstart = $this->getUserState('limitstart'); $limit = $this->getUserState('limit'); if ($limitstart > $total - $limit) { $limitstart = max(0, (int) (ceil($total / $limit) - 1) * $limit); $page = (int) $limitstart / $limit; $input = JFactory::getApplication()->input; $input->set('page', $page); $input->set('limitstart', $limitstart); $model->setState('page', $page); $model->setState('limitstart', $limitstart); } // Get the pagination $pagination = new JPagination($total, $limitstart, $limit); // Set the K2 response pagination object K2Response::setPagination($pagination); }
protected function embed($id) { // Get application $application = JFactory::getApplication(); // Get user $user = JFactory::getUser(); // Get params $params = JComponentHelper::getParams('com_k2'); // Check that comments are enabled if (!$params->get('comments')) { K2Response::throwError(JText::_('K2_ALERTNOTAUTH'), 404); } // Get input $itemId = $application->input->get('itemId', 0, 'int'); $offset = $application->input->get('limitstart', 0, 'int'); if ($itemId) { // Get Item require_once JPATH_ADMINISTRATOR . '/components/com_k2/resources/items.php'; $item = K2Items::getInstance($itemId); // Check access $item->checkSiteAccess(); // Get model $model = K2Model::getInstance('Comments'); // Set itemId state $model->setState('itemId', $item->id); // If user cannot edit comments load only the published if (!$user->authorise('k2.comment.edit', 'com_k2')) { $model->setState('state', 1); } if ($id) { // Calculate the offset of the requested comment $model->setState('id', $id); $operator = $params->get('commentsOrdering') == 'ASC' ? '<' : '>'; $model->setState('id.operator', $operator); $offset = $model->countRows(); $page = $offset / (int) $params->get('commentsLimit', 10); $page = floor($page); $offset = $page * (int) $params->get('commentsLimit', 10); // Now get comments of the detected page $model->setState('id', false); $model->setState('limit', (int) $params->get('commentsLimit', 10)); $model->setState('limitstart', $offset); $model->setState('sorting', 'id.reverse'); if ($params->get('commentsOrdering') == 'ASC') { $model->setState('sorting', 'id'); } $comments = $model->getRows(); // Pagination jimport('joomla.html.pagination'); $pagination = new JPagination($model->countRows(), $offset, (int) $params->get('commentsLimit', 10)); } else { // Get comments $model->setState('id', false); $model->setState('limit', (int) $params->get('commentsLimit', 10)); $model->setState('limitstart', $offset); $model->setState('sorting', 'id.reverse'); if ($params->get('commentsOrdering') == 'ASC') { $model->setState('sorting', 'id'); } $comments = $model->getRows(); // Pagination jimport('joomla.html.pagination'); $pagination = new JPagination($model->countRows(), $offset, (int) $params->get('commentsLimit', 10)); } // Response K2Response::setRows($comments); K2Response::setPagination($pagination); } K2Response::render(); // Return return $this; }