<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Pagination for module MODULE
 *
 * @author CIEXT skeleton
 */
class NAME_pagination
{

    protected $ci = null;
    protected $config = [
        'first_url'        => '',
        'first_link'       => '<<',
        'last_link'        => '>>',
        'use_page_numbers' => true,
        'num_links'        => 3,
        'next_tag_open'    => '<div class="pag_link">',
        'next_tag_close'   => '</div>',
        'prev_tag_open'    => '<div class="pag_link">',
        'prev_tag_close'   => '</div>',
        'num_tag_open'     => '<div class="pag_link">',
        'num_tag_close'    => '</div>',
        'cur_tag_open'     => '<strong>',
        'cur_tag_close'    => '</strong>',
        // dummy
        'total_rows'       => 20,
        'per_page'         => 1,
    ];

    public function __construct($config = ['total_rows' => 20, 'per_page' => 1])
    {
        $this->ci = & get_instance();
        $this->initialize($config);
    }

    public function initialize($config)
    {
        $this->config = array_merge($this->config, $config);
        if (empty($this->config['first_url'])) {
            $this->config['first_url'] = '/' . preg_replace('/\/page\/\d*/', '', $this->ci->uri->uri_string());
        }
        if (empty($this->config['base_url'])) {
            $this->config['base_url'] = $this->config['first_url'] . '/page';
        }
        if (empty($this->config['uri_segment'])) {
            $this->config['uri_segment'] = count($this->ci->uri->segment_array());
        }
        if ($this->ci->input->get()) {
            $this->config['suffix'] = '?' . http_build_query($this->ci->input->get(), '', "&");
            $this->config['first_url'] .= $this->config['suffix'];
        }

        return $this->ci->load->library('pagination')->initialize($this->config);
    }

    public function create_links()
    {
        return $this->ci->load->library('pagination')->create_links();
    }

}
