2017年4月28日 星期五

(php)CodeIgniter_pageLink

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MyPagination_type1
 *
 * @author xman
 *
 *
 */
class MyPagination_type1 {

    // put your code here
    protected $config = array();
    protected $CI;

    /* ---------------------------------- */
    protected $totalBlocks; // 共有幾個區塊
    protected $totalPages; // 共有幾頁
    protected $block; // 從0開始
    /* ---------------------------------- */
    protected $page;
    /* ---------------------------------- */
    protected $linkData = array();
    protected $otherLinkData = array();

    /* ====================================================================== */

    public function __construct() {
        $this->CI = &get_instance();

        if ($this->CI->config->item('enable_query_strings')) {
            throw new Exception('cant use in (enable_query_strings)');
        }
        /* ---------------------------------- */
        $this->config['page'] = NULL; // 每個命令相對的資料區塊
        $this->config['start'] = NULL; // 每個命令相對開始的資料
        /* ---------------------------------- */
        $this->config['totalRows'] = NULL;
        $this->config['perPageRows'] = NULL;
        $this->config['perPageLinks'] = 10;
        /* ---------------------------------- */
        $this->config['path'] = NULL; // url.path
        $this->config['query'] = NULL; // url.query
        $this->config['segments'] = array(); // url.query_array
        /* ---------------------------------- */
        $this->config['segment'] = NULL; // 要修改的參數位置
        /* ---------------------------------- */
        $this->config['template'] = NULL; // 樣版
        $this->config['rwd'] = false; // 為了RWD
    }

    /* ====================================================================== */

    public function initialize($config) {
        $this->_setConfig($config);

        $this->_check_1();

        $this->_check_2();
     
        echo '<pre>';
        var_export($this->config);
        echo '</pre>';
    }

    /* ====================================================================== */

    public function create_links() {
     
    }

    /* ====================================================================== */

    public function create_LinkData() {
        $this->_model_1();

        if ($this->config['rwd']) {
            // for RWD 只印出(上一頁)(下一頁)          
         
            $this->_model_2b();
        } else {
            // for 一般正常頁面
            $this->_model_2a();
        }
        /* ---------------------------------- */
        $this->_model_3();

        $linkData = $this->linkData;
        /* ---------------------------------- */

        if ($this->config['rwd']) {
            list($f2, $f1, $b1, $b2) = $this->otherLinkData;
            array_unshift($linkData, $f1);
            array_unshift($linkData, $f2);
            array_push($linkData, $b1);
            array_push($linkData, $b2);
        } else {
            list($f1, $f2, $b1, $b2) = $this->otherLinkData;
            array_unshift($linkData, $f2);
            array_unshift($linkData, $f1);
            array_push($linkData, $b1);
            array_push($linkData, $b2);
        }
        /* ---------------------------------- */
        $start = ($this->page - 1) * $this->config['perPageRows'];
     
        $result = array(
            'page' => $this->page,
            'start' => $start,
            'linkData' => $linkData,
            'totalPages' => $this->totalPages,
            'totalRows' => $this->config['totalRows']
        );
     
        return $result;
    }

    /* ====================================================================== */

    protected function _model_1() {
        $config = $this->config;

        // totalPage
        $this->totalPages = floor(($config['totalRows'] - 1) / $config['perPageRows']) + 1;
        $this->totalPages = ($this->totalPages < 1 ? 1 : $this->totalPages);

        /* ---------------------------------- */
        // check page
        $this->page = ($this->page < 1 ? 1 : $this->page);
        $this->page = ($this->page > $this->totalPages ? $this->totalPages : $this->page);

        /* ---------------------------------- */
        // totalBlock
        $this->totalBlocks = floor(($this->totalPages - 1) / $config['perPageLinks']) + 1;
        $this->totalBlocks = ($this->totalBlocks < 1 ? 1 : $this->totalBlocks);

        /* ---------------------------------- */

        // block
        $this->block = floor(($this->page - 1) / $this->config['perPageLinks']);
    }

    /* ====================================================================== */

    /**
     * 一般狀況,印出(上一頁)(下一頁)
     */
    protected function _model_2a() {
        $config = $this->config;


        for ($i = 0; $i < 4; $i++) {
            $this->otherLinkData[] = array(
                'content' => '',
                'path' => $config['path'],
                'query' => '',
                'url' => 'javascript:;',
                'show' => TRUE,
                'selected' => FALSE,
                'number' => FALSE,
                'name' => ''
            );
        }
        /* ================================== */
        // (<<)
        if ($this->block < 1) {
            // 不顯示
            $this->otherLinkData[0]['path'] = '';
            $this->otherLinkData[0]['show'] = FALSE;
        } else {
            $page = $this->page - $config['perPageLinks'];
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[0]['query'] = $query;
            $this->otherLinkData[0]['url'] = sprintf('%s/%s', $this->otherLinkData[0]['path'], $query);
        }

        $this->otherLinkData[0]['content'] = '<<';
        $this->otherLinkData[0]['name'] = 'b2';
        /* ================================== */
        // (<)
        if ($this->page < 2) {
            // 不顯示
            $this->otherLinkData[1]['path'] = '';
            $this->otherLinkData[1]['show'] = FALSE;
        } else {
            $page = $this->page - 1;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[1]['query'] = $query;
            $this->otherLinkData[1]['url'] = sprintf('%s/%s', $this->otherLinkData[1]['path'], $query);
        }

        $this->otherLinkData[1]['content'] = '<';
        $this->otherLinkData[1]['name'] = 'b1';
        /* ================================== */
        // (>)
        if ($this->page >= $this->totalPages) {
            // 不顯示
            $this->otherLinkData[2]['path'] = '';
            $this->otherLinkData[2]['show'] = FALSE;
        } else {
            $page = $this->page + 1;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[2]['query'] = $query;
            $this->otherLinkData[2]['url'] = sprintf('%s/%s', $this->otherLinkData[2]['path'], $query);
        }

        $this->otherLinkData[2]['content'] = '>';
        $this->otherLinkData[2]['name'] = 'f1';
        /* ================================== */
        // (>>)
        if ($this->block >= ($this->totalBlocks - 1)) {
            // 不顯示
            $this->otherLinkData[3]['path'] = '';
            $this->otherLinkData[3]['show'] = FALSE;
        } else {
            $page = $this->page + $config['perPageLinks'];
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[3]['query'] = $query;
            $this->otherLinkData[3]['url'] = sprintf('%s/%s', $this->otherLinkData[3]['path'], $query);
        }

        $this->otherLinkData[3]['content'] = '>>';
        $this->otherLinkData[3]['name'] = 'f2';
    }

    /* ====================================================================== */

    /**
     * (for RWD)印出(上一頁)(下一頁)
     */
    protected function _model_2b() {
        $config = $this->config;


        for ($i = 0; $i < 4; $i++) {
            $this->otherLinkData[] = array(
                'content' => '',
                'path' => $config['path'],
                'query' => '',
                'url' => 'javascript:;',
                'show' => TRUE,
                'selected' => FALSE,
                'number' => FALSE,
                'name' => ''
            );
        }
        /* ================================== */
        // (<<)回到第一頁
        if ($this->page < 2) {
            // 不顯示
            $this->otherLinkData[0]['path'] = '';
            $this->otherLinkData[0]['show'] = FALSE;
        } else {
            $page = 1;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[0]['query'] = $query;
            $this->otherLinkData[0]['url'] = sprintf('%s/%s', $this->otherLinkData[0]['path'], $query);
        }

        $this->otherLinkData[0]['content'] = '<<';
        $this->otherLinkData[0]['name'] = 'b2';
        /* ================================== */
     
     
        // (<)
        if ($this->page < 2) {
            // 不顯示
            $this->otherLinkData[1]['path'] = '';
            $this->otherLinkData[1]['show'] = FALSE;
        } else {
            $page = $this->page - 1;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[1]['query'] = $query;
            $this->otherLinkData[1]['url'] = sprintf('%s/%s', $this->otherLinkData[1]['path'], $query);
        }

        $this->otherLinkData[1]['content'] = '<';
        $this->otherLinkData[1]['name'] = 'b1';
        /* ================================== */
        // (>)
        if ($this->page >= $this->totalPages) {
            // 不顯示
            $this->otherLinkData[2]['path'] = '';
            $this->otherLinkData[2]['show'] = FALSE;
        } else {
            $page = $this->page + 1;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[2]['query'] = $query;
            $this->otherLinkData[2]['url'] = sprintf('%s/%s', $this->otherLinkData[2]['path'], $query);
        }

        $this->otherLinkData[2]['content'] = '>';
        $this->otherLinkData[2]['name'] = 'f1';
        /* ================================== */
        // (>>)最後一頁
        if ($this->page >= $this->totalPages) {
            // 不顯示
            $this->otherLinkData[3]['path'] = '';
            $this->otherLinkData[3]['show'] = FALSE;
        } else {
            $page = $this->totalPages;
            $page = $this->_checkPage($page);
            $query = $this->_changeURLSegment($page);


            $this->otherLinkData[3]['query'] = $query;
            $this->otherLinkData[3]['url'] = sprintf('%s/%s', $this->otherLinkData[3]['path'], $query);
        }

        $this->otherLinkData[3]['content'] = '>>';
        $this->otherLinkData[3]['name'] = 'f2';
    }

    /* ====================================================================== */

    /**
     * 創建(pageLink)
     */
    protected function _model_3() {
        $config = $this->config;

        for ($i = 0; $i < $config['perPageLinks']; $i++) {


            $data = array(
                'content' => '',
                'path' => $config['path'],
                'query' => '',
                'url' => 'javascript:;',
                'show' => TRUE,
                'selected' => FALSE,
                'number' => TRUE,
                'class' => 'number',
                'name' => ''
            );


            /* ================================== */
            $page = $this->block * $config['perPageLinks'] + $i + 1;

            if ($page <= $this->totalPages) {

                if ($page == $this->page) {
                    // 當前頁
                    $data['selected'] = TRUE;
                } else {
                    $data['query'] = $this->_changeURLSegment($this->_checkPage($page));
                    $data['url'] = sprintf('%s/%s', $data['path'], $data['query']);
                }

                $data['content'] = $page;
            } else {

                // 過頭了
                $data['show'] = FALSE;
            }
            $this->linkData[$i] = $data;
        }
    }

    /* ====================================================================== */

    protected function _checkPage($page) {

        $page = ($page < 1 ? 1 : $page);
        $page = ($page > $this->totalPages ? $this->totalPages : $page);

        return $page;
    }

    /* ====================================================================== */

    /**
     * 更改(page)的值
     *
     * @param type $page
     * @return type
     */
    function _changeURLSegment($page) {
        $config = $this->config;
        $value;

        $position = $config['segment'];

        /* ---------------------------------- */
        if (isset($config['page'])) {
            // 用(page)切換分頁
            $value = $page;
        } else {
            // 用(start)切換分頁
            $value = ($page - 1) * $config['perPageRows'];
        }

        /* ---------------------------------- */
        if ($position == -1 || count($config['segments']) == 0) {
            // 加在最後面
            $config['segments'][] = $value;
        } else {
            $config['segments'][$position] = $value;
        }

        return implode('/', $config['segments']);
    }

    /* ====================================================================== */

    protected function _setConfig($config) {
        foreach ($config as $k => $v) {

            if (array_key_exists($k, $this->config)) {
                if (isset($v)) {
                    $this->config[$k] = $v;
                }
            }
        }
    }

    /* ====================================================================== */

    /**
     * 做檢查
     */
    protected function _check_1() {
        $config = &$this->config;

        $error_array = array();

        // path
        if (empty($config['path'])) {
            $error_array[] = 'need set base_url';
        }

        // totoalRows
        if (!isset($config['totalRows'])) {
            $error_array[] = 'need set totalRows';
        }

        // perPageRows
        if (empty($config['perPageRows'])) {
            $error_array[] = 'need set perPageRows or != 0';
        }

        // perPageLinks
        if (empty($config['perPageLinks'])) {
            $error_array[] = 'need set perPageLinks or != 0';
        }

        // segment
        if (!isset($config['segment'])) {
            $error_array[] = 'need set segment';
        }
        /* ---------------------------------- */

        if (!isset($config['page']) && !isset($config['start'])) {
            $error_array[] = 'need set page or start';
        }

        if (count($error_array) > 0) {
            $result = implode($error_array, ' | ');
            throw new Exception($result);
        }
    }

    /* ====================================================================== */

    protected function _check_2() {
        $config = &$this->config;

        if (!isset($config['page'])) {
            $this->page = floor($config['start'] / $config['perPageRows']) + 1;
        } else {
            $this->page = $config['page'];
        }


        if (!empty($config['query']) && count($config['segments']) == 0) {
            $config['segments'] = explode('/', $config['query']);
        }

        if (empty($config['query']) && count($config['segments']) > 0) {
            $config['query'] = implode('/', $config['segments']);
        }
     
        if($config['rwd']){
            // 若是為了RWD
            $config['perPageLinks'] = 1;
        }
    }

}

?>




/////////////////////////////////////////////////////////////////////////////////////////
<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');

class Test_2 extends CI_Controller
{
 
    public function __construct () {
        parent::__construct();
    }
 
    /* ====================================================================== */
    public function index () {
        printf('<p>%s</p>', 'test_2--index');
        var_export(func_get_args());
    }
 
    /* ====================================================================== */
    public function classTest_1 () {
        $this->load->library('MyURL');
     
        $urlData = $this->myurl->parse_url(func_get_args());
     
        $data = array(
        'urlData' => $urlData
        );
     
        // printf('<a href="%s/%s">link</a>', $urlData['path'],
        // $urlData['query']);
        $this->load->view('test_2/classTest_1', $data);
    }
 
    /* ====================================================================== */
    public function classTest_2 ($arg1,$page=1) {
        $this->load->library('parseurl');
        $this->load->library('MyPagination_type1', NULL, 'pagination');
     
        $urlData = $this->parseurl->parse_url(func_get_args());
     
        printf('<p>%s => %s</p>', $urlData['path'], $urlData['query']);
        /* ---------------------------------- */
     
        $config = array();
        $config['page'] = $page;
        $config['totalRows'] = 263;
        $config['perPageRows'] = 10;
        $config['perPageLinks'] = 5;
     
        $config['path'] = $urlData['path'];
        $config['query'] = $urlData['query'];
        $config['segment'] = 1;
        $config['rwd'] = true;
     
        $this->pagination->initialize($config);
        $pagination_data = $this->pagination->create_LinkData();
     
        $data = array(
            'pagination' => $pagination_data
        );
     
        $this->load->view('test_2/pagination_t1', $data);
    }
}
?>

沒有留言:

張貼留言