Example #1
0
 public function render()
 {
     // Try get active page
     $http_query = $_GET;
     if (isset($_GET['page'])) {
         $this->_num_active = (int) $_GET['page'];
         unset($http_query['page']);
         if (empty($http_query)) {
             $link = $this->_link . '/?page=';
         } else {
             $link = $this->_link . '/?' . http_build_query($http_query) . '&page=';
         }
     } else {
         if (class_exists('O2System', FALSE)) {
             $segments = \O2System::URI()->segments;
             if ($key = array_search('page', $segments)) {
                 if (isset($segments[$key + 1])) {
                     $this->_num_active = $segments[$key + 1];
                 }
                 $link = str_replace(\O2System::$config['URI']['suffix'], '/page/', $this->_link);
             } else {
                 $link = $this->_link . '/?' . http_build_query($http_query) . '&page=';
             }
         } else {
             $link = $this->_link . '/?' . http_build_query($http_query) . '&page=';
         }
     }
     $link = str_replace('?&', '?', $link);
     $this->_num_active = $this->_num_active == 0 ? 1 : $this->_num_active;
     $num_previous = $this->_num_active - 1;
     $num_previous = $num_previous == 0 ? 1 : $num_previous;
     if ($num_previous > 1) {
         $this->add_item(new Link(new Tag('span', '«', ['aria-hidden' => TRUE]), $link . $num_previous));
     } else {
         $this->add_item(new Link(new Tag('span', '«', ['aria-hidden' => TRUE]), '#'), Lists::ITEM_DISABLED);
     }
     $total_pages = $this->_num_active + $this->_num_display;
     $total_pages = $total_pages > $this->_num_pages ? $this->_num_pages : $total_pages;
     $pages = range($this->_num_active, $total_pages);
     if (count($pages) > $this->_num_display) {
         if (end($pages) < $this->_num_pages or end($pages) == $this->_num_pages) {
             array_pop($pages);
         }
     } elseif (count($pages) < $this->_num_display) {
         $start_page = $this->_num_active - ($this->_num_display - count($pages));
         $pages = range($start_page, $total_pages);
     }
     foreach ($pages as $page) {
         if ($page <= $this->_num_pages and $page > 0) {
             if ($page == $this->_num_active) {
                 $this->add_item(new Link($page, $link . $page), Lists::ITEM_ACTIVE);
             } else {
                 $this->add_item(new Link($page, $link . $page));
             }
         }
     }
     $num_next = $this->_num_active + 1;
     if ($num_next < $this->_num_pages) {
         $this->add_item(new Link(new Tag('span', '&raquo;', ['aria-hidden' => TRUE]), $link . $num_next));
     } else {
         $this->add_item(new Link(new Tag('span', '&raquo;', ['aria-hidden' => TRUE]), '#'), Lists::ITEM_DISABLED);
     }
     if (!empty($this->_items)) {
         return (new Tag('nav', parent::render()))->render();
     }
     return '';
 }