<?php

/**
 * @package Helix Framework
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2016 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */
//no direct accees
defined('_JEXEC') or die('resticted aceess');

function pagination_list_render($list) {
    // Calculate to display range of pages
    $currentPage = 1;
    $range = 1;
    $step = 5;
    foreach ($list['pages'] as $k => $page) {
        if (!$page['active']) {
            $currentPage = $k;
        }
    }
    if ($currentPage >= $step) {
        if ($currentPage % $step == 0) {
            $range = ceil($currentPage / $step) + 1;
        } else {
            $range = ceil($currentPage / $step);
        }
    }

    $html = '<div class="pagination-wraper">';
    $html .= '<ul class="pagination">';
    $html .= $list['start']['data'];
    $html .= $list['previous']['data'];

    foreach ($list['pages'] as $k => $page) {
        if (in_array($k, range($range * $step - ($step + 1), $range * $step))) {
            if (($k % $step == 0 || $k == $range * $step - ($step + 1)) && $k != $currentPage && $k != $range * $step - $step) {
                $page['data'] = preg_replace('#(<a.*?>).*?(</a>)#', '$1...$2', $page['data']);
            }
        }

        $html .= $page['data'];
    }

    $html .= $list['next']['data'];
    $html .= $list['end']['data'];

    $html .= '</ul>';
    $html .= '</div>';

    return $html;
}

function pagination_item_active(&$item) {
    $class = '';

    // Check for "Prev" item
    if ($item->text == JText::_('JPREV')) {
        $display = '<i class="fa fa-angle-left"></i>';
    }

    // Check for "Next" item
    if ($item->text == JText::_('JNEXT')) {
        $display = '<i class="fa fa-angle-right"></i>';
    }

    // If the display object isn't set already, just render the item with its text
    if (!isset($display)) {
        $display = $item->text;
        $class = ' class="hidden-xs"';
    }

    return '<li' . $class . '><a title="' . $item->text . '" href="' . $item->link . '" class="pagenav">' . $display . '</a></li>';
}

function pagination_item_inactive(&$item) {

    // Check for "Prev" item
    if ($item->text == JText::_('JPREV')) {
        return '<li class="disabled"><a><i class="fa fa-angle-left"></i></a></li>';
    }

    // Check for "Next" item
    if ($item->text == JText::_('JNEXT')) {
        return '<li class="disabled"><a><i class="fa fa-angle-right"></i></a></li>';
    }

    // Check if the item is the active page
    if (isset($item->active) && ($item->active)) {
        return '<li class="active hidden-xs"><a>' . $item->text . '</a></li>';
    }

    // Doesn't match any other condition, render a normal item
    return '<li class="disabled hidden-xs"><a>' . $item->text . '</a></li>';
}
