<?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/AdminSuperGridHeader.inc.php" );

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

	class AdminArticlecategoriesGridHeader extends AdminSuperGridHeader {

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
		}
	}

	class AdminArticlecategoriesGrid extends BackendGrid {
		protected function prepareDataset() {
			$d = array();
			$acm = new ArticleCategoriesModel();
			foreach ($this->aData as $row) {
				$ldefs = $acm->getArticleCategoryLanguageDefsFromId( $row["id"] );
				foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
					if (!isset( $ldefs[$langcode])) continue;
					$fname = sprintf( "acname_%s" , $langiso );
					$row[$fname]=$ldefs[$langcode]["article_category_title"];
				}
				$d[]=$row;
			}
			$this->aData = $d;
		}

		protected function constructTable() {
			parent::constructTable();
			foreach ($this->aData as $rownum => $datarow) {
				$id = $this->oGridRows[$rownum]->getParentalId( 1 );
				FNTHPage::addUniquePageFullInit(
					sprintf( "AdminArticlecategoriesGridRowInit( '%s',%d,%d,aJSLangDefs );\n",
						$id,
						$rownum,
						$datarow["id"] )
				);
			}
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "article_categories";
			$this->sTitle = __("articlecategories_Categories");
			$this->setSelectable( true );
			$this->iPagesize = 20000;

			// $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" );
		}

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

		protected function whereClause() {
			return "";
		}

		protected function orderClause() {
			return "order by article_category_meta_l";
		}

		protected function fieldsClause() {
			return "article_category_id as id,article_category_name,article_category_level";
		}

		public function constructColumns() {
			$this->addColumn( "article_category_name" , __("articlecategories_Name","attr") , true );
			foreach ($GLOBALS["languages"] as $langiso => $langcode ) {
				$this->addColumn( "acname_".$langiso , $langiso , false );
			}
		}

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

			$field = new FNTHGridField( "article_category_name" , $headerrow );
			// $this->language_filter = new AdminTextbox( "language_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"] )) {
				return parent::handleEvents();
			}
			if (!isset( $this->aP["tbl"] )) {
				return parent::handleEvents();
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				return parent::handleEvents();
			}
			switch ($this->aP["action"]) {
			case "delete":
				$lm = new ArticlecategoriesModel();
				foreach ($this->aP["ids"] as $id) $lm->deleteArticleCategory( $id );
				$this->refreshGrid();
				break;
			}
			fo_exit();
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			switch ($colname) {
			case "article_category_name":
				$padnum = 1 + ($this->aData[$rownum]["article_category_level"]-1)*2;
				$padding = str_repeat( "&raquo;" , $padnum ) . " ";
				
				$val =  sprintf( "%s<a class=\"green\" href=\"javascript:AdminArticlecategoriesGridModify( %d );\">%s</a>" ,
					$padding,
					$this->aData[$rownum]["id"],
					$this->aData[$rownum]["article_category_name"]
				);
				$field->setValue( $val );
				break;
			default:
			}
			return $field;
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "articlecategories_New_article_category" );
			fo3_add_js_langdef( "articlecategories_Modify_article_category" );
			fo3_add_js_langdef( "articlecategories_Delete_article_category" );
			fo3_add_js_langdef( "articlecategories_confirm_Delete_article_category" );
		}

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