<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSuperGridPager.inc.php" );
	
	class FrontendSuperGrid extends FNTHSuperGrid {
		
		protected function addSuperGridPagerFactory( $id , $parent ) {
			$pager = new FrontendSuperGridPager( $id , $parent );
			if (!$this->oSuperGridPager) $this->oSuperGridPager = $pager;
			$pager->setPageSize( $this->iPagesize );
			return $pager;
		}	

		protected function prepareUriArray( $input ) {
			$output = $input;
			unset( $output["lang"] );
			return $output;
		}
		
		public function constructOrderingHeader() {
			$headerrow = $this->addRowFactory( "orderingheader" , $this , "header" );
			$headerrow->setAttribute( "class" , "ord" );
			$page = FNTH_strip_url_params( $_SERVER["REQUEST_URI"] );

			foreach ($this->aColumns as $key => $column) {
				$field = $this->addFieldFactory( $key , $headerrow );
				if (isset( $column["align"] )) $field->setCSSAttribute( "text-align" , $column["align"] );
				if (isset( $column["width"] )) $field->setAttribute( "width" , $column["width"] );

				if (!$column["ordering"]) {
					$field->setValue( $column["title"] );
					continue;
				}
				$val=$column["title"];
				
				$u = $this->prepareUriArray( $this->aG );
				$u[$this->_ordering]=$key;
				if ($this->sOrdering==$key) {
					$up = $this->bAscending;
					$down = !$this->bAscending;
				} else {
					$up = false;
					$down = false;
				}

				// up image
				$u[$this->_orderingdir]="asc";
				$url = FNTH_render_url( $page , $u , true );
				$alt=sprintf( "%s: %s (%s)",__("Sorting","attr"),$column["title"],__("ascending","attr") );
				$img=($up) ? "sel_up" : "up";
				$val.=sprintf( "<a href=\"%s\"><img alt=\"%s\" src=\"%s/images/%s.png\" /></a>" , $url , $alt , $GLOBALS["site_www"] , $img );

				// down image	
				$u[$this->_orderingdir]="desc";
				$url = FNTH_render_url( $page , $u , true );
				$alt=sprintf( "%s: %s (%s)",__("Sorting","attr"),$column["title"],__("descending","attr") );
				$img=($down) ? "sel_down" : "down";
				$val.=sprintf( "<a href=\"%s\"><img alt=\"%s\" src=\"%s/images/%s.png\" /></a>" , $url , $alt , $GLOBALS["site_www"] , $img );

				$field->setValue( $val );
			}
		}


		public function __construct( $id , $parent = null ) {
			$this->addMediaDirectory( $GLOBALS["site_root"] , $GLOBALS["site_www"] , "design/rp/","widgets/");
			parent::__construct( $id , $parent );
			$this->setParam("language",$GLOBALS["language"]);
			$this->setAttribute( "class" , "FrontendSuperGrid" );
			$this->iPagesize = 15;
		}
	}
?>
