<?php
 
	class str {
		
        static $_config_name = 'str';
        public $_local_cfg;
        static $instance , $i;
        public $display_element_name = true;
        public $element_name = 'Wszystkich';
        public $pages_name = 'page'; // strona LUB page
		public $pages , $ajaxpanel, $anomaly , $limit , $laps , $from , $to , $links , $position , $next , $prev , $str , $pattern , $results;
		public $orders, $order_by;
        
		public function __construct(){
		
            $this->_cfg = _cfg::$i->{self::$_config_name};
            
            $this -> limit = $this->_cfg->str_default_insite; //config::$str_default_insite;
            $this -> anomaly = $this->_cfg->str_default_anomaly; //config::$str_default_anomaly;
            $this -> links = array();
            $this -> results = 0;
            $this -> pages = array();
            
            self::$instance = $this;
            self::$i = $this;
            
		}
		
        
        public function modify( $name , $value ){
            $this -> {$name} = $value;
        }
        
		public function params(){
			
			$this -> position = core::$instance->s;
            
			if( $this -> position > $this -> results ) $position = $this -> results;
            
			$this -> laps = ceil( $this -> results / $this -> limit );
            
			if( $this -> position > $this -> laps ) $this -> position = $this -> laps;
			
			$this -> from = (int)(( $this -> position - 1 ) * $this -> limit);
			$this -> to = $this -> limit;
			
    
    			if(( $position - $this -> anomaly ) > 1) array_push( $this -> links , 1 );
    									
    			$ctrl = ( $this -> position > $this -> anomaly ) ? ( $this -> position - $this -> anomaly ) : 1;
    			
    			for( $x=$ctrl ; ($x-1)!=$this -> laps ; $x++ ){	
    				$this -> links[] = $x;
    				if( $x > ( $ctrl + $this -> anomaly ) ) break;
    			}
    			
    			if(( $this -> position + $this -> anomaly ) < $this -> laps) array_push( $this -> links , $this -> laps );

			
			if( $this -> position < $this -> laps ) $this -> next = $this -> position + 1;
			else $this -> next = false;
				
			if( $this -> position > 1 ) $this -> prev = $this -> position - 1;
			else $this -> prev = false;
			
						
			$this->anchors();
		}
		
		/**
		 * 
		 * 
		 */
		public function anchors(){
			
            $this->pages = array();
            if( !$this->pattern ) $this->autoPattern();
            
            // Prev
			if( $this -> prev ) $this->pages[] = '<a href="'. $this->parsePattern( $this -> prev ) .'" class="prev">&laquo; '.( l('Poprzednia') ).'</a>';
			
            // Pages
            foreach( $this -> links as $link ){
				if( core::$instance->s == $link ) $this->pages[$link] = '<a href="'. $this->parsePattern( $link ) .'" class="current"'.($link==1 ? (" rel=\"canonical\"") : "").'>'. $link .'</a>';
				else $this->pages[$link] = '<a href="'. $this->parsePattern( $link ) .'"'.($link==1 ? (" rel=\"canonical\"") : "").'>'. $link .'</a>';
			}
            
            // Next
			if( $this -> next ) $this->pages[] = '<a href="'. $this->parsePattern( $this->next ) .'" class="next">'.( l('Następna') ).' &raquo;</a>';
			
		}
        
        public function parsePattern( $s ){
            
            
            $string = str_replace( '{s}' , $s , str::$instance->pattern );
            if( $s == 1 ) $string = str_replace("/{$this->pages_name}/1/","",$string);
            
                return $string;
        }
        
        /**
         * 
         */
        public function autoPattern(){

            
            if( stristr( $_SERVER['REQUEST_URI'] , '.php' ) ){
                
                if(stristr($_SERVER['QUERY_STRING'],'&')){
                    
                    $params = @explode('&',$_SERVER['QUERY_STRING']);
                    foreach( $params AS $param ){
                        //printd($param);
                        if(!preg_match("#^(s\=[0-9]+)$#",$param)) $element[] = $param;
                    }
                    $params = join('&',$element);
                }
                
                $this->pattern = $_SERVER['SCRIPT_NAME'] . '?' . ($params ? ($params . '&s={s}') : ($_SERVER['QUERY_STRING'] . '&s={s}'));
            
            } else {   
                
                $string = preg_replace("#(\/?strona|page\/[0-9]+\/?)#","",str_replace("/index.html","",$_SERVER['REQUEST_URI']));
                if(!empty($_SERVER['QUERY_STRING'])){
                    
                    $string = str_replace( '?'.$_SERVER['QUERY_STRING'] , "" , $string );
                }
                
                if( substr( $string , -1 , 1 ) != '/' ) $string .= '/'; 
                
                // Mod 12.12.17
                //$this->pattern =  $string . $this->pages_name . '/{s}/' . (!empty($_SERVER['QUERY_STRING']) ? ('?'.$_SERVER['QUERY_STRING']) : '');
                $this->pattern =  $string . $this->pages_name . '/{s}/';
            }
            

            
        }
		
        
        public function orders( $array , $default_order = false ){
            $this->orders = $array;
            
            if(!empty($this->orders[core::$i->order])){
                $this->order_by = " order by " . $this->orders[core::$i->order] . ' ' . core::$i->dir;
            } else {
                if($default_order) $this->order_by = " order by " . $default_order;
                else $this->order_by = false;
            }
        }
		
	}
	

?>