<?php
	FNTH_class( "SuperGrid" );

	define( "FIELD_TYPE_HTML" , 1 );
	define( "FIELD_TYPE_PRE_JS" , 2 );
	define( "FIELD_TYPE_POST_JS" , 3 );

	class BackendGrid extends FNTHSuperGrid {

		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" , "BackendGrid" );
			$this->sToolbarTitleClass = "bigttl";
		}

		protected function addSuperGridPagerFactory( $id , $parent ) {
			$retval = parent::addSuperGridPagerFactory( $id , $parent );
			$retval->setJumper( true );
			$retval->setJumperText( __("Jump_to_page") );
			return $retval;
		}

		protected function handleFilterEvent() {
			$u = $this->aG;
			foreach ($this->aP as $k => $v) {
				if ($k=="task") continue;
				if (substr( $k , -10 )=="_chk_check") continue;
				$u[$k]=$v;
			}
			unset( $u[ $this->getId()."_pagenum"] );
			FNTH_redirect( FNTH_render_url( $_SERVER["PHP_SELF"] , $u ) );
			fo_exit();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$retval =  parent::render();
			$ids = array();
			foreach ($this->oGridRows as $row) if ($row->getSelected()) $ids[] = $row->_id;
			if (!empty( $ids )) {
				FNTHPage::addUniquePageFullInit(
					sprintf( "BGInitSelection( '%s' , %s );",
						$this->getId(),
						json_encode( $ids ) ) );
			}
			return $retval;
		}

		protected function refreshGrid( $ajax = true ) {
			if ($ajax) {
				FNTH_direct_echo( "window.location.href=window.location.href" );
				exit();
			}
		}

		protected function handleStandardFilterEvent() {
			if (!isset( $this->aP["task"] )) return;
			switch ($this->aP["task"]) {
			case "filter":
				$u = $this->aG;
				foreach ($this->aP as $k => $v) {
					if ($k=="task") continue;
					$u[$k]=$v;
				}
				unset( $u[ $this->getId()."_pagenum"] );
				FNTH_redirect( FNTH_render_url( $_SERVER["PHP_SELF"] , $u ) );
				fo_exit();
			}
		}

		public function addOnEnterCallbackToWidget( $wdg ) {
			$wdg->setOnEnterCallback( sprintf( "%sAction( '%s','filter');",
				$this->getId() , $this->getId() ) );
		}
	}

?>
