<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/BackendGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCouponType.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTristate.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSuperGridHeader.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Languages.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Coupon.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Articlecategories.inc.php" );

	class AdminAdminIpsGridHeader extends AdminSuperGridHeader {
		public $oKeywordFilter;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oKeywordFilter = new AdminTextbox( "keyword_filter" , $this );
			$this->oKeywordFilter->setTitle( __("Keyword") );
			$this->oKeywordFilter->setShowTitle( true );
		}
	}

	class AdminAdminIpsGrid extends BackendGrid {
		protected $larticle_title_filter;
		protected $article_name_filter;
		protected $bIsSelector = false;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "admin_ips";
			$this->sTitle = __("Admin_ips");
			$this->setSelectable( true );

			$this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$this->addToolbarButton( "new" , __("New","attr") , "page_add" , $this->renderStandardAction( "new" ) );
			$this->addToolbarButton( "modify" , __("Modify","attr") , "page_edit" , $this->renderStandardAction( "modify" ) , false );
			$this->addToolbarButton( "delete" , __("Delete","attr") , "page_remove",$this->renderStandardAction( "delete" ) , false );
			FNTH_include_3rdparty( "contextmenu" );
			FNTH_include_3rdparty( "tooltip" );
		}

		public function setSelector( $v ) { $this->bIsSelector = $v; }

		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return new AdminAdminIpsGridHeader( $id , $parent );
		}

		protected function whereClause() {
			$filters = array();

			/*
			// keyword filter
			$f = $this->oSuperGridHeader->oKeywordFilter->getId();
			if (!empty( $this->aG[$f] )) {
				$v = FNTH_db_addslashes( $this->aG[$f] );
				$filters[]=sprintf( "articles.article_id in (select article_id from larticles where larticle_body ilike '%%%s%%' or larticle_lead ilike '%%%s%%')" , $v , $v );
			}
			// larticle_title_filter
			$f = $this->larticle_title_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "articles.article_id in (select article_id from larticles where larticle_title ilike '%%%s%%')", $this->aG[$f] );
			*/
			if (empty( $filters )) return "";
			return " where ".implode( " and " , $filters );
		}

		public function orderClause() {
			switch ($this->sOrdering) {
			default:
			case "admin_ip_id": $retval="admin_ip_id";break;
			case "ip_address": $retval="ip_address";break;
			case "ip_description": $retval="ip_description";break;
			case "is_active": $retval="is_active";break;
			}
			if (empty($retval)) return "";
			return sprintf( " order by %s%s",
				$retval,($this->bAscending) ? "" : " desc" );
		}

		protected function fieldsClause() {
			return sprintf(
				"admin_ip_id as id,
				ip_address,
				ip_description,
				is_active" );
		}

		public function constructColumns() {
			$this->addColumn( "ip_address" , __("adminips_Admin_ip","attr") , true );
			$this->addColumn( "ip_description" , __("adminips_Ip_description","attr") , true );
			$this->addColumn( "is_active" , __("adminips_Active","attr") , true );
			// $this->addColumn( "admin" , __("Admin","attr") , false );
		}

		public function constructFilterHeader() {
			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );
			// 10I
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
		

			// ip_address			
			$field = new FNTHGridField( "ip_address" , $headerrow );
			$this->ip_address_filter = new AdminTextbox( "ip_address_filter" , $field );
			
			// ip_description			
			$field = new FNTHGridField( "ip_description" , $headerrow );
			$this->ip_description_filter = new AdminTextbox( "ip_description_filter" , $field );
			
			// is_active
			$field = new FNTHGridField( "is_active" , $headerrow );	// ures
			$this->is_active_filter = new AdminTristate( "is_active_filter" , $field );
			
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				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();
				}
			}

			// ajax events
			if (!isset( $this->aP["ajaxevent"] )) {
				parent::handleEvents();
				return;
			}
			if (!isset( $this->aP["tbl"] )) {
				parent::handleEvents();
				return;
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				parent::handleEvents();
				return;
			}
			switch ($this->aP["action"]) {
			case "delete":
				$am = new AdminIpModel();
				foreach ($this->aP["ids"] as $id) $am->deleteAdminIp( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "adminips_confirm_Delete_admin_ips" );
			fo3_add_js_langdef( "Add" );
			fo3_add_js_langdef( "Cancel" );
		}


		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "ip_address":
				$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminAdminIpsGridModify( %d );\">%s</a>",
					$rd["id"] , $rd["ip_address"] );
				$field->setValue( $val );
				break;
			case "is_active":
				$field->setValue( fo3_render_boolean_yesno($rd["is_active"]) );
				$field->setAttribute( "align" , "center" );
				break;
			/*
			case "admin":
				$prefix = $this->getId() . "_" . $rd["id"];
				$buttons = array();
				$buttons[]=$this->renderMiniButton(
					$prefix,
					"add_coupons",
					__("coupons_Add_coupons","attr"),
					"page_add",
					$this->renderStandardAction(
						"dlg_add_coupons" , array( "coupon_campaign_id" => $rd["id"] )
					)
				);
				$field->setValue( implode( " " , $buttons ) );
				$field->setAttribute( "align" , "center" );
				break;
			*/
			}
			return $field;
		}

		public function render() {
			$this->generateLangDefs();
			return parent::render();
		}

	}

?>
