<?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/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminArticleCategory.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/Articles.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Articlecategories.inc.php" );

	class AdminArticlesGridHeader 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 AdminArticlesGrid extends BackendGrid {
		protected $larticle_title_filter;
		protected $article_name_filter;
		protected $bIsSelector = false;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "articles,larticles";
			$this->sTitle = __("articles_Articles");
			$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 AdminArticlesGridHeader( $id , $parent );
		}

		protected function prepareDataset() {
			$retval = array();

			if (empty( $this->aData )) return;
			// larticle titles
			$article_ids = array();
			foreach ($this->aData as $row) $article_ids[]=$row["id"];
			$larticle_titles = array();
			if (!empty( $article_ids )) {
				$query = sprintf( "select article_id,larticle_title from larticles where article_id in (%s) and site_language_id=1" , implode( "," , $article_ids ) );
				$larticle_titles = FNTH_db_get_associative( $query , "article_id" , "larticle_title" );
			}
			
			foreach ($this->aData as $row) {
				$row["article_url"]=sprintf( "/cikk.php?id=%d" , $row["id"] );
				$row["larticle_title"]=(isset( $larticle_titles[$row["id"]] )) ? 
					$larticle_titles[$row["id"]] : "-";
				$retval[]=$row;
			}
			$this->aData = $retval;
		}

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

			$filters[]="articles.article_id=larticles.article_id";
			$filters[]=sprintf( "larticles.site_language_id=%d", $GLOBALS["langcode"] );

			// 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] );
			// article_name_filter filter
			$f = $this->article_name_filter->getId();
			if (!empty( $this->aG[$f] )) $filters[]=sprintf( "articles.article_name ilike '%%%s%%'", $this->aG[$f] );

			// article_category_filter
			$f = $this->article_category_filter->getId();
			if (isset( $this->aG[$f] ) && strlen( $this->aG[$f] ) ) $filters[]=sprintf( "articles.article_category_id=%d" , $this->aG[$f] );
			
			if (empty( $filters )) return "";
			return " where ".implode( " and " , $filters );
		}

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

		protected function fieldsClause() {
			return sprintf(
				"articles.article_id as id,
				articles.article_ctstamp as article_ctstamp,
				larticles.larticle_title as larticle_title,
				articles.article_name as article_name,
				articles.article_category_id as article_category_id,
				articles.article_url" );
		}

		public function constructColumns() {
			$this->addColumn( "article_ctstamp" , __("articles_Created_at","attr") , true );
			$this->addColumn( "larticle_title" , __("articles_Title","attr") , true );
			$this->addColumn( "article_name" , __("articles_Name","attr") , true );
			$this->addColumn( "article_category_id" , __("articles_Category","attr") , true );
		}

		public function constructFilterHeader() {
			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );
			// 10I
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
		
			// article_ctstamp	
			$field = new FNTHGridField( uniqid("") , $headerrow );	// ures
			
			$field = new FNTHGridField( "larticle_title" , $headerrow );
			$this->larticle_title_filter = new AdminTextbox( "larticle_title_filter" , $field );
			$this->addOnEnterCallbackToWidget( $this->larticle_title_filter );

			$field = new FNTHGridField( "article_name" , $headerrow );
			$this->article_name_filter = new AdminTextbox( "article_name_filter" , $field );
			$this->addOnEnterCallbackToWidget( $this->article_name_filter );
			
			$field = new FNTHGridField( "article_category_id" , $headerrow );
			$this->article_category_filter = new AdminArticleCategory( "article_category_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 ArticlesModel();
				foreach ($this->aP["ids"] as $id) $am->deleteArticle( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "articles_confirm_Delete_article" );
		}


		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			switch ($colname) {
			case "article_ctstamp":
				$field->setValue( fo3_format_date_to_screen( $this->aData[$rownum]["article_ctstamp"] ) );
				$field->setAttribute( "nowrap" , "nowrap" );
				break;
			case "larticle_title":
			case "article_name":
				$val = "";
				if ($this->bIsSelector) {
					$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminArticlesSelect( '%s' , %d );\">&raquo; %s</a>",
						$this->aData[$rownum]["article_url"],
						$this->aG["CKEditorFuncNum"],
						$this->aData[$rownum][$colname] );
				} else {
					$val =  sprintf( "<a class=\"green\" href=\"javascript:AdminArticlesGridModify( %d );\">%s</a>" , $this->aData[$rownum]["id"] , $this->aData[$rownum][$colname] );
				}
				$field->setValue( $val );
				break;
			case "article_category_id":
				$field->setAttribute( "align" , "center" );
				$field->setValue(
					$this->article_category_filter->getOption( $this->aData[$rownum]["article_category_id"] ) );
				break;
			}
			return $field;
		}

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

	}

?>
