1. 程式人生 > >【CI框架原始碼解析之】分頁類檔案Pagination.php

【CI框架原始碼解析之】分頁類檔案Pagination.php

<?php /** * ======================================= * Created by Pocket Knife Technology. * User: ZhiHua_W * Date: 2016/11/08 0041 * Time: 下午 4:14 * Project: CodeIgniter框架—原始碼分析 * Power: Analysis for Pagination.php * ======================================= */
defined('BASEPATH') OR exit('No direct script access allowed'); /** * 用於生成分頁連線 */ class CI_Pagination { //每次訪問的url地址 protected $base_url = ''; //給路徑新增一個自定義字首,字首位於偏移段的前面 protected $prefix = ''; //給路徑新增一個自定義字尾,字尾位於偏移段的後面。 protected $suffix
= ''; //這個數字表示你需要做分頁的資料的總行數。通常這個數值是你查詢資料庫得到的資料總量。 protected $total_rows = 0; //放在你當前頁碼的前面和後面的“數字”連結的數量。 //比方說值為 2 就會在每一邊放置兩個數字連結,就像此頁頂端的示例連結那樣。 protected $num_links = 2; //這個數字表示每個頁面中希望展示的數量,在上面的那個例子中,每頁顯示 10 個專案。 public $per_page = 10; //當前頁 public
$cur_page = 0; //預設分頁的 URL 中顯示的是你當前正在從哪條記錄開始分頁, //如果你希望顯示實際的頁數,將該引數設定為 TRUE 。 protected $use_page_numbers = FALSE; //首頁,左邊第一個連結顯示的文字,如果你不想顯示該連結,將其設定為 FALSE 。 protected $first_link = '‹ First'; //下一頁,下一頁連結顯示的文字,如果你不想顯示該連結,將其設定為 FALSE 。 protected $next_link = FALSE; //下一頁,下一頁連結顯示的文字,如果你不想顯示該連結,將其設定為 FALSE protected $prev_link = FALSE; //尾頁,右邊第一個連結顯示的文字,如果你不想顯示該連結,將其設定為 FALSE 。 protected $last_link = 'Last ›'; //分頁方法自動檢測你 URI 的哪一段包含頁數,如果你的情況不一樣,你可以明確指定它 protected $uri_segment = 4; //起始標籤放在所有結果的左側。 //你可以在標籤裡面寫任意的樣式等等 //不過樣式最好的還是採取分離的方式寫最好,僅在這邊新增不同的class就可以了 protected $full_tag_open = '<ul class="pagination pagination-sm">'; //結束標籤放在所有結果的右側。 protected $full_tag_close = '</ul>'; //第一個連結的起始標籤。 protected $first_tag_open = '<li>'; //第一個連結的結束標籤。 protected $first_tag_close = '</li>'; //最後一個連結的起始標籤。 protected $last_tag_open = '<li>'; //最後一個連結的結束標籤。 protected $last_tag_close = '</li>'; //首頁url protected $first_url = ''; //當前頁連結的起始標籤。 protected $cur_tag_open = '<li class="active"><a href="javascript:;">'; //當前頁連結的結束標籤。 protected $cur_tag_close = '</a></li>'; //下一頁連結的起始標籤。 protected $next_tag_open = '<li>'; //下一頁連結的結束標籤。 protected $next_tag_close = '</li>'; //上一頁連結的起始標籤。 protected $prev_tag_open = '<li>'; //上一頁連結的結束標籤。 protected $prev_tag_close = '</li>'; //數字連結的起始標籤。 protected $num_tag_open = '<li>'; //數字連結的結束標籤。 protected $num_tag_close = '</li>'; //預設情況下,分頁類假設你使用 URI 段 ,並像這樣構造你的連結: //http://example.com/index.php/test/page/20 protected $page_query_string = FALSE; protected $query_string_segment = 'per_page'; //如果你不想顯示數字連結(例如你只想顯示上一頁和下一頁連結),你可以通過下面的程式碼來阻止它顯示 protected $display_pages = TRUE; //如果你想為分頁類生成的每個連結新增額外的屬性 protected $_attributes = ''; //連線型別 protected $_link_types = array(); //預設情況下你的查詢字串引數會被忽略,將這個引數設定為 TRUE , //將會將查詢字串引數新增到 URI 分段的後面以及 URL 字尾的前面 protected $reuse_query_string = FALSE; //當該引數設定為 TRUE 時,會使用 application/config/config.php //配置檔案中定義的 $config['url_suffix'] 引數 重寫 $config['suffix'] 的值 protected $use_global_url_suffix = FALSE; //給數字增加屬性 protected $data_page_attr = 'data-ci-pagination-page'; //CI Singleton protected $CI; /** * 建構函式->處理資料 * 在使用載入此類之後,設定一些資料例如: * //配置分頁資訊 * $config['base_url'] = site_url('admin/goodstype/index'); * $config['total_rows'] = $this->goodstype_model->count_goodstype(); * $config['per_page'] = 2; * $config['uri_segment'] = 4; * * //自定義分頁連線 * $config['first_link'] = '首頁'; * $config['last_link'] = '尾頁'; * $config['prev_link'] = '上一頁'; * $config['next_link'] = '下一頁'; */ public function __construct($params = array()) { $this->CI = &get_instance(); $this->CI->load->language('pagination'); foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key) { if (($val = $this->CI->lang->line('pagination_' . $key)) !== FALSE) { $this->$key = $val; } } $this->initialize($params); log_message('info', 'Pagination Class Initialized'); } /** * 初始化 * 功能同樣是處理引數 */ public function initialize(array $params = array()) { isset($params['attributes']) OR $params['attributes'] = array(); if (is_array($params['attributes'])) { $this->_parse_attributes($params['attributes']); unset($params['attributes']); } if (isset($params['anchor_class'])) { empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class']; unset($params['anchor_class']); } foreach ($params as $key => $val) { if (property_exists($this, $key)) { $this->$key = $val; } } if ($this->CI->config->item('enable_query_strings') === TRUE) { $this->page_query_string = TRUE; } if ($this->use_global_url_suffix === TRUE) { $this->suffix = $this->CI->config->item('url_suffix'); } return $this; } /** * 建立分頁連線 * 這個就是我們需要條用到的了,這個函式最後會返回一串html程式碼, * 而我們僅將這段html程式碼在前臺顯示即可。 * CI框架的分頁類和TP框架的分頁類有這明顯的差別。 * CI僅是提供分頁顯示,並不提供其和資料庫互動的功能。 * 這也就讓我們可以對其進行100%的定製。 * 非常的小巧方便。 */ public function create_links() { //我們在初始化的時候必須要有資料總條數和每頁顯示條數 if ($this->total_rows == 0 OR $this->per_page == 0) { return ''; } //計算頁面總數 $num_pages = (int)ceil($this->total_rows / $this->per_page); //如果只有一頁,則直接然會空字串 if ($num_pages === 1) { return ''; } //檢查使用者定義的連結數 $this->num_links = (int)$this->num_links; if ($this->num_links < 0) { show_error('Your number of links must be a non-negative number.'); } //保留任何現有的查詢字串專案。 //注:與任何其他查詢字串選項無關。 if ($this->reuse_query_string === TRUE) { $get = $this->CI->input->get(); unset($get['c'], $get['m'], $get[$this->query_string_segment]); } else { $get = array(); } //處理我們的基礎網址和第一個網址 $base_url = trim($this->base_url); $first_url = $this->first_url; $query_string = ''; $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&'; if ($this->page_query_string === TRUE) { //如果自定義first_url還沒有被確定,我們會從base_url建立一個網頁,但沒有專案。 if ($first_url === '') { $first_url = $base_url; if (!empty($get)) { $first_url .= $query_string_sep . http_build_query($get); } } $base_url .= $query_string_sep . http_build_query(array_merge($get, array($this->query_string_segment => ''))); } else { //生成我們儲存的查詢字串,以在頁面號以後追加。 if (!empty($get)) { $query_string = $query_string_sep . http_build_query($get); $this->suffix .= $query_string; } if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE) { $base_url = substr($base_url, 0, $base_query_pos); } if ($first_url === '') { $first_url = $base_url . $query_string; } $base_url = rtrim($base_url, '/') . '/'; } //確定當前頁號。 $base_page = ($this->use_page_numbers) ? 1 : 0; //判斷我們是否使用查詢字串 if ($this->page_query_string === TRUE) { $this->cur_page = $this->CI->input->get($this->query_string_segment); } elseif (empty($this->cur_page)) { //如果uri_segment一個沒有被定義,預設的最後一個段的數字。 if ($this->uri_segment === 0) { $this->uri_segment = count($this->CI->uri->segment_array()); } $this->cur_page = $this->CI->uri->segment($this->uri_segment); //從該段中刪除任何指定的字首/字尾。 if ($this->prefix !== '' OR $this->suffix !== '') { $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page); } } else { $this->cur_page = (string)$this->cur_page; } if (!ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int)$this->cur_page === 0)) { $this->cur_page = $base_page; } else { //確保我們使用的是比較後的整數。 $this->cur_page = (int)$this->cur_page; } if ($this->use_page_numbers) { if ($this->cur_page > $num_pages) { $this->cur_page = $num_pages; } } elseif ($this->cur_page > $this->total_rows) { $this->cur_page = ($num_pages - 1) * $this->per_page; } $uri_page_number = $this->cur_page; //如果我們使用的是偏移量而不是頁面號,將它轉換為一個頁面號, //這樣我們就可以生成周圍的數字連結了。 if (!$this->use_page_numbers) { $this->cur_page = (int)floor(($this->cur_page / $this->per_page) + 1); } //計算開始和結束的數字。這些決定開始和結束數字連結的數量。 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; //這個變數就是最後返回的字串 $output = ''; //生成首頁連結 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + !$this->num_links)) { //為html程式碼新增設定的js屬性 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1); $output .= $this->first_tag_open . '<a href="' . $first_url . '"' . $attributes . $this->_attr_rel('start') . '>' . $this->first_link . '</a>' . $this->first_tag_close; } // 生成上一頁連結 //我個人感覺生成上一頁的這個連線沒用,我們本身已經有了和相鄰的頁面連線 //故而上一頁和下一頁在我看來沒有用處,我一般都是將此段和下一頁都注視掉 if ($this->prev_link !== FALSE && $this->cur_page !== 1) { $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page; $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1)); if ($i === $base_page) { $output .= $this->prev_tag_open . '<a href="' . $first_url . '"' . $attributes . $this->_attr_rel('prev') . '>' . $this->prev_link . '</a>' . $this->prev_tag_close; } else { $append = $this->prefix . $i . $this->suffix; $output .= $this->prev_tag_open . '<a href="' . $base_url . $append . '"' . $attributes . $this->_attr_rel('prev') . '>' . $this->prev_link . '</a>' . $this->prev_tag_close; } } //渲染頁面 //也就是將你設定的所需要新增的html標籤程式碼,屬性,都給加上 if ($this->display_pages !== FALSE) { for ($loop = $start - 1; $loop <= $end; $loop++) { $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page; $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop); if ($i >= $base_page) { if ($this->cur_page === $loop) { $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; } elseif ($i === $base_page) { $output .= $this->num_tag_open . '<a href="' . $first_url . '"' . $attributes . $this->_attr_rel('start') . '>' . $loop . '</a>' . $this->num_tag_close; } else { $append = $this->prefix . $i . $this->suffix; $output .= $this->num_tag_open . '<a href="' . $base_url . $append . '"' . $attributes . '>' . $loop . '</a>' . $this->num_tag_close; } } } } //生成下一頁連線 //不多說 if ($this->next_link !== FALSE && $this->cur_page < $num_pages) { $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page; $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1); $output .= $this->next_tag_open . '<a href="' . $base_url . $this->prefix . $i . $this->suffix . '"' . $attributes . $this->_attr_rel('next') . '>' . $this->next_link . '</a>' . $this->next_tag_close; } //生成最後一頁(尾頁)連線 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + !$this->num_links) < $num_pages) { $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page; $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages); $output .= $this->last_tag_open . '<a href="' . $base_url . $this->prefix . $i . $this->suffix . '"' . $attributes . '>' . $this->last_link . '</a>' . $this->last_tag_close; } //將生成的結果html程式碼字串進行處理 $output = preg_replace('#([^:"])//+#', '\\1/', $output); //如果存在新增封裝HTML return $this->full_tag_open . $output . $this->full_tag_close; } /** * 解析屬性 */ protected function _parse_attributes($attributes) { isset($attributes['rel']) OR $attributes['rel'] = TRUE; $this->_link_types = ($attributes['rel']) ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next') : array(); unset($attributes['rel']); $this->_attributes = ''; foreach ($attributes as $key => $value) { $this->_attributes .= ' ' . $key . '="' . $value . '"'; } } /** * 新增“關係”屬性 */ protected function _attr_rel($type) { if (isset($this->_link_types[$type])) { unset($this->_link_types[$type]); return ' rel="' . $type . '"'; } return ''; } }