/** * Get the count of all the articles for pagination total * * @return int */ private function countAllArticles() { if (!$this->isLoggedIn) { return false; } $perPage = 10; $offset = 0 * $perPage; $limit = $perPage; $articles = \Kanso\Kanso::getInstance()->Database->Builder()->SELECT('post.id')->FROM('posts')->FIND_ALL(); return count(\Kanso\Utility\Arr::paginate($articles, 0, 10)); }
/** * Build HTML Pagination links * * @param array $args Associative array of options (optional) */ public function pagination_links($args = null) { # Default options $options = ['base' => \Kanso\Kanso::getInstance()->Environment()['HTTP_HOST'], 'format' => '<li class="(:class)"><a href="(:link)">(:num)</a></li>', 'current' => 1, 'total' => 1, 'context' => 2, 'show_all' => false, 'prev_next' => true, 'ellipsis' => '<li>. . .</li>', 'prev_text' => '« Previous', 'next_text' => 'Next »']; # Segment the reuest URI $uri = explode("/", trim(\Kanso\Kanso::getInstance()->Environment()['PATH_INFO'], '/')); # Declare the pagination string $pagination = ''; # Replace the query and create a new one to get the post count; $queryStr = preg_replace('/limit =[^:]+:/', '', $this->queryStr); $queryStr = trim(preg_replace('/limit =[^:]+/', '', $queryStr)); $queryStr = trim($queryStr, ":"); # Load the query parser $parser = new QueryParser(); # Count the posts $posts = $parser->countQuery($queryStr); $pages = \Kanso\Utility\Arr::paginate($posts, $this->pageIndex, \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE']); # If no args were defined, Kanso will figure it out for us if (!$args || !isset($args['current']) || !isset($args['total'])) { # pages here are used as for an array so +1 $options['current'] = $this->pageIndex === 0 ? 1 : $this->pageIndex + 1; $options['total'] = count($pages); } # If options were set, overwrite the dafaults if ($args) { $options = array_merge($options, $args); } # Special case if there is only 1 page if ($options['total'] == 1 || $options['total'] == 0 || $options['total'] < 1) { return ''; } # Clean the base url $options['base'] = rtrim($options['base'], '/'); # Update the base url depending on the page type if ($this->is_search()) { $options['base'] = $options['base'] . DIRECTORY_SEPARATOR . 'search-results/?q=' . $this->searchQuery; } else { if ($this->is_archive()) { $options['base'] = $options['base'] . DIRECTORY_SEPARATOR . 'archive'; } else { if ($this->is_tag()) { $options['base'] = $options['base'] . DIRECTORY_SEPARATOR . $uri[0] . DIRECTORY_SEPARATOR . $uri[1]; } else { if ($this->is_category()) { $options['base'] = $options['base'] . DIRECTORY_SEPARATOR . $uri[0] . DIRECTORY_SEPARATOR . $uri[1]; } else { if ($this->is_author()) { $options['base'] = $options['base'] . DIRECTORY_SEPARATOR . $uri[0] . DIRECTORY_SEPARATOR . $uri[1]; } } } } } # loop always at the current minus the context, minus 1 $loopStart = $options['current'] - $options['context']; # if the loop starts before 2, reset it to 2 if ($loopStart < 2) { $loopStart = 2; } # Loop end is the context * 2 + loop start + plus 1 $loopEnd = $loopStart + $options['context'] * 2 + 1; # We should show all links if the loop ends after the total if ($loopEnd >= $options['total'] || $options['show_all'] === true) { $loopEnd = $options['total']; } # Declare variables we are going to use $frontEllipsis = $loopStart > 2 ? $options['ellipsis'] : ''; $backEllipsis = $loopEnd === $options['total'] || $options['total'] - $options['context'] === $loopEnd ? '' : $options['ellipsis']; # Variables we will need $patterns = ['/\\(:class\\)/', '/\\(:link\\)/', '/\\(:num\\)/']; $replacements = []; # If show all is true we need reset if ($options['show_all'] === true) { $frontEllipsis = ''; $backEllipsis = ''; $loopStart = 2; $loopEnd = $options['total']; } # If show previous if ($options['prev_next'] === true) { $class = $options['current'] === 1 ? 'disabled' : ''; $link = $options['current'] === 1 ? '#' : $options['base'] . DIRECTORY_SEPARATOR . 'page' . DIRECTORY_SEPARATOR . ($options['current'] - 1) . DIRECTORY_SEPARATOR; $link = $options['current'] === 2 ? $options['base'] : $link; $replacements = [$class, $link, $options['prev_text']]; $pagination .= preg_replace($patterns, $replacements, $options['format']); $replacements = []; } # Show the first page $class = $options['current'] === 1 ? 'active' : ''; $link = $options['current'] === 1 ? '#' : $options['base']; $replacements = [$class, $link, 1]; $pagination .= preg_replace($patterns, $replacements, $options['format']); $replacements = []; # Show the front ellipsis $pagination .= $frontEllipsis; # Loop over the pages # Note the loop starts after the first page and before the last page for ($i = $loopStart; $i < $loopEnd; $i++) { $class = $i === $options['current'] ? 'active' : ''; $link = $i === $options['current'] ? '#' : $options['base'] . DIRECTORY_SEPARATOR . 'page' . DIRECTORY_SEPARATOR . $i . DIRECTORY_SEPARATOR; $replacements = [$class, $link, $i]; $pagination .= preg_replace($patterns, $replacements, $options['format']); $replacements = []; } # Show the back ellipsis $pagination .= $backEllipsis; # Show the last page $class = $options['current'] === $options['total'] ? 'active' : ''; $link = $options['current'] === $options['total'] ? '#' : $options['base'] . DIRECTORY_SEPARATOR . 'page' . DIRECTORY_SEPARATOR . $options['total'] . DIRECTORY_SEPARATOR; $replacements = [$class, $link, $options['total']]; $pagination .= preg_replace($patterns, $replacements, $options['format']); $replacements = []; # If show next if ($options['prev_next'] === true) { $class = $options['current'] < $options['total'] ? '' : 'disabled'; $link = $options['current'] < $options['total'] ? $options['base'] . DIRECTORY_SEPARATOR . 'page' . DIRECTORY_SEPARATOR . ($options['current'] + 1) . DIRECTORY_SEPARATOR : '#'; $replacements = [$class, $link, $options['next_text']]; $pagination .= preg_replace($patterns, $replacements, $options['format']); } return $pagination; }
/** * Get the comments for listing in the admin panel * * @param $queries array POST data from client * @param $filter string * @return array */ public function loadAllComments($queries, $filter) { # Get the Kanso Query object $Query = \Kanso\Kanso::getInstance()->Query(); # Get the SQL builder $SQL = \Kanso\Kanso::getInstance()->Database->Builder(); $isSearch = $queries['search'] !== 'false'; $page = (int) $queries['page'] - 1; $comments = []; $sort = $queries['sortBy'] === 'newest' ? 'DESC' : 'ASC'; if ($isSearch) { $validKeys = ['ip' => 'ip_address', 'status' => 'status', 'user' => 'name', 'email' => 'email']; $searchValue = $queries['search']; $searchKey = false; if (\Kanso\Utility\Str::contains($searchValue, ':')) { $value = \Kanso\Utility\Str::getAfterFirstChar($searchValue, ':'); $key = \Kanso\Utility\Str::getBeforeFirstChar($searchValue, ':'); $key = isset($validKeys[$key]) ? $validKeys[$key] : false; if ($key) { $searchKey = $key; $searchValue = $value; } } if ($searchKey) { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE($searchKey, '=', $searchValue); } else { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('content', 'LIKE', "%{$searchValue}%"); } if ($filter === 'all') { $comments = $comments->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'approved') { $comments = $comments->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'spam') { $comments = $comments->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'pending') { $comments = $comments->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'deleted') { $comments = $comments->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL(); } } else { if ($filter === 'all') { $comments = $SQL->SELECT('*')->FROM('comments')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'approved') { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'spam') { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'pending') { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL(); } if ($filter === 'deleted') { $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL(); } } foreach ($comments as $key => $comment) { $comments[$key]['permalink'] = $Query->the_permalink($comment['post_id']); $comments[$key]['title'] = $Query->the_title($comment['post_id']); $comments[$key]['avatar'] = $Query->get_avatar($comment['email'], 100, true); } $comments = \Kanso\Utility\Arr::paginate($comments, $page, 10); return $comments; }