/** * TODO: replace usage of this function with awpcp_pagination() */ function _create_pager($item_count, $offset, $results, $tpname) { $permastruc = get_option('permalink_structure'); if (isset($permastruc) && !empty($permastruc)) { $awpcpoffset_set = "?offset="; } else { if (is_admin()) { $awpcpoffset_set = "?offset="; } else { $awpcpoffset_set = "&offset="; } } mt_srand(create_awpcp_random_seed()); $radius = 5; global $accepted_results_per_page; $accepted_results_per_page = awpcp_pagination_options($results); // TODO: remove all fields that belongs to the Edit Ad form (including extra fields and others?) $params = array_merge($_GET, $_POST); unset($params['page_id'], $params['offset'], $params['results']); unset($params['PHPSESSID'], $params['aeaction'], $params['category_id']); unset($params['cat_ID'], $params['action'], $params['aeaction']); unset($params['category_name'], $params['category_parent_id']); unset($params['createeditadcategory'], $params['deletemultiplecategories']); unset($params['movedeleteads'], $params['moveadstocategory']); unset($params['category_to_delete'], $params['tpname']); unset($params['category_icon'], $params['sortby'], $params['adid']); unset($params['picid'], $params['adkey'], $params['editemail']); unset($params['awpcp_ads_to_action'], $params['post_type']); $cid = intval(awpcp_request_param('category_id')); $cid = empty($cid) ? get_query_var('cid') : $cid; if ($cid > 0) { $params['category_id'] = intval($cid); } $myrand = mt_rand(1000, 2000); $form = "<form id=\"pagerform{$myrand}\" name=\"pagerform{$myrand}\" action=\"\" method=\"get\">\n"; $form .= "<table>\n"; $form .= "<tr>\n"; $form .= "\t<td>\n"; $totalrows = $item_count; $total_pages = ceil($totalrows / $results); $dotsbefore = false; $dotsafter = false; $current_page = 0; $myreturn = ''; for ($i = 1; $i <= $total_pages; $i++) { if (($i - 1) * $results <= $offset && $offset < $i * $results) { $myreturn .= "{$i} "; $current_page = $i; } elseif (($i - 1 + $radius) * $results < $offset) { if (!$dotsbefore) { $myreturn .= "..."; $dotsbefore = true; } } elseif (($i - 1 - $radius) * $results > $offset) { if (!$dotsafter) { $myreturn .= "..."; $dotsafter = true; } } else { $href_params = array_merge($params, array('offset' => ($i - 1) * $results, 'results' => $results)); $href = add_query_arg(urlencode_deep($href_params), $tpname); $myreturn .= sprintf('<a href="%s">%d</a> ', esc_url($href), esc_attr($i)); } } if ($offset != 0) { //Subtract 2, page is 1-based index, results is 0-based, must compensate for 2 pages here if (($current_page - 2) * $results < $results) { $href_params = array_merge($params, array('offset' => 0, 'results' => $results)); $href = add_query_arg(urlencode_deep($href_params), $tpname); } else { $href_params = array_merge($params, array('offset' => ($current_page - 2) * $results, 'results' => $results)); $href = add_query_arg(urlencode_deep($href_params), $tpname); } $prev = sprintf('<a href="%s">«</a> ', esc_url($href)); } else { $prev = ''; } if ($offset != ($total_pages - 1) * $results) { $href_params = array_merge($params, array('offset' => $current_page * $results, 'results' => $results)); $href = add_query_arg(urlencode_deep($href_params), $tpname); $next = sprintf('<a href="%s">»</a> ', esc_url($href)); } else { $next = ''; } if (isset($_REQUEST['page_id']) && !empty($_REQUEST['page_id'])) { $form .= "\t\t<input type=\"hidden\" name=\"page_id\" value='" . esc_attr($_REQUEST['page_id']) . "' />\n"; } $form = $form . $prev . $myreturn . $next; $form .= "\t</td>\n"; if (count($accepted_results_per_page) > 1) { $form .= "\t<td>\n"; $form .= "\t\t<input type=\"hidden\" name=\"offset\" value=\"{$offset}\" />\n"; $flat_params = awpcp_flatten_array($params); while (list($k, $v) = each($flat_params)) { if (is_array($v)) { $v = count($v) > 0 ? reset($v) : ''; } $form .= "\t\t<input type=\"hidden\" name=\"" . esc_attr($k) . "\" value=\"" . esc_attr($v) . "\" />\n"; } $form .= "\t\t<select name=\"results\" onchange=\"document.pagerform{$myrand}.submit()\">\n"; $form .= vector2options($accepted_results_per_page, $results); $form .= "\t\t</select>\n"; $form .= "\t</td>\n"; } $form .= "</tr>\n"; $form .= "</table>\n"; $form .= "</form>\n"; return $form; }
/** * @since 3.0 * @param array $params * @param string $url * @return String HTML */ function awpcp_pagination($config, $url) { $blacklist = array('page_id', 'offset', 'results', 'PHPSESSID', 'aeaction', 'cat_ID', 'action', 'aeaction', 'category_name', 'category_parent_id', 'createeditadcategory', 'deletemultiplecategories', 'movedeleteads', 'moveadstocategory', 'category_to_delete', 'tpname', 'category_icon', 'sortby', 'adid', 'picid', 'adkey', 'editemail', 'awpcp_ads_to_action', 'post_type'); $params = array_merge($_GET, $_POST); foreach ($blacklist as $param) { unset($params[$param]); } extract(shortcode_atts(array('offset' => 0, 'results' => 10, 'total' => 10), $config)); $pages = ceil($total / $results); $page = floor($offset / $results) + 1; $items = array(); $radius = 5; if ($page - $radius > 2) { $items[] = awpcp_render_pagination_item('««', 1, $results, $params, $url); } if ($page - $radius > 1) { $items[] = awpcp_render_pagination_item('«', $page - $radius - 1, $results, $params, $url); } for ($i = 1; $i <= $pages; $i++) { if ($page == $i) { $items[] = sprintf('%d', $i); } else { if ($i < $page - $radius) { // pass } else { if ($i > $page + $radius) { // pass } else { $items[] = awpcp_render_pagination_item($i, $i, $results, $params, $url); } } } } if ($page < $pages - $radius) { $items[] = awpcp_render_pagination_item('»', $page + $radius + 1, $results, $params, $url); } if ($page + $radius < $pages - 1) { $items[] = awpcp_render_pagination_item('»»', $pages, $results, $params, $url); } $pagination = implode('', $items); $options = awpcp_pagination_options($results); ob_start(); include AWPCP_DIR . '/frontend/templates/listings-pagination.tpl.php'; $html = ob_get_contents(); ob_end_clean(); return $html; }