<?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/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 AdminCouponCampaignsGridHeader 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 AdminCouponCampaignsGrid extends BackendGrid {
		protected $larticle_title_filter;
		protected $article_name_filter;
		protected $bIsSelector = false;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "coupon_campaigns";
			$this->sTitle = __("coupons_Campaigns");
			$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( "export" , __("Export","attr") , "attachment" , $this->renderStandardAction( "export" ) , 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 AdminCouponCampaignsGridHeader( $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 "cc_name": $retval="cc_name";break;
			case "cc_avail_from": $retval="cc_avail_from";break;
			case "cc_avail_until": $retval="cc_avail_until";break;
			}
			if (empty($retval)) return "";
			return sprintf( " order by %s%s",
				$retval,($this->bAscending) ? "" : " desc" );
		}

		protected function fieldsClause() {
			return sprintf(
				"coupon_campaign_id as id,
				cc_name,
				cc_avail_from,
				cc_avail_until,
				cc_coupon_type,
				cc_number_of_coupons,
				cc_freedays" );
		}

		public function constructColumns() {
			$this->addColumn( "cc_name" , __("coupons_Campaign_name","attr") , true );
			$this->addColumn( "cc_avail_from" , __("coupons_From","attr") , true );
			$this->addColumn( "cc_avail_until" , __("coupons_Until","attr") , true );
			$this->addColumn( "cc_coupon_type" , __("coupons_Type","attr") , true );
			$this->addColumn( "cc_info" , __("coupons_Info","attr") , false );
			$this->addColumn( "cc_number_of_coupons" , __("coupons_Number_of_coupons","attr") , true );
			$this->addColumn( "admin" , __("Admin","attr") , false );
		}

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

			// cc_name			
			$field = new FNTHGridField( "cc_name" , $headerrow );
			$this->cc_name_filter = new AdminTextbox( "cc_name_filter" , $field );
			
			// cc_avail_from	
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
			// cc_avail_until
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
			// cc_coupon_type	
			$field = new FNTHGridField( "cc_coupon_type" , $headerrow );
			$this->cc_coupon_type_filter = new AdminCouponType( "cc_coupon_type_filter" , $field );
			
			// cc_info	
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
			
			// admin
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				switch ($this->aP["task"]) {
				case "add_coupons":
					$cm = new CouponModel();
					$cm->addCouponsToCampaign(
						$this->aP["cc_id"],
						$this->aP["num"] );
					FNTH_refresh();
					break;
				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 CouponModel();
				foreach ($this->aP["ids"] as $id) $am->deleteCouponCampaign( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "coupons_confirm_Delete_coupon" );
			fo3_add_js_langdef( "coupons_Add_coupons" );
			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 "cc_avail_from":
			case "cc_avail_until":
				$field->setAttribute( "align" , "center" );
				if (!isset( $rd[$colname] )) $field->setValue( "---" );
				break;
			case "cc_coupon_type":
				$field->setAttribute( "align" , "center" );
				$field->setValue( __("coupons_".$rd[$colname]) );
				break;
			case "cc_number_of_coupons":
				$field->setAttribute( "align" , "right" );
				break;
			case "cc_info":
				switch ($rd["cc_coupon_type"]) {
				case "freedays":
					$field->setAttribute( "align" , "center" );
					$field->setValue( sprintf( "+%d" , $rd["cc_freedays"] ) );
					break;
				default:
					$field->setAttribute( "align" , "center" );
					$field->setValue( "---" );
				}
				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();
		}

	}

?>
