<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/BackendGrid.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Articles.inc.php" );

	class AdminArticleConnectedsGrid extends BackendGrid {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sIdField = "article_connection_id";
			$this->sTableName = "article_connections";
			$this->setSelectable( true );
			$this->sTitle = __("articles_Connected_articles" );

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

		public function constructColumns() {
			$this->addColumn( "connected_article" , __("Connected_article","attr") , false );
			$this->addColumn( "bidirectional" , __("articles_Bidirectional_connection","attr") , false );
		}

		public function prepareDataset() {
			if (empty( $this->aData )) return;
			$article_ids = array();
			foreach ($this->aData as $row) 
				$article_ids[]=($row["source_article_id"]!=$this->aG["uid"]) ?
					$row["source_article_id"] : $row["target_article_id"];
			$query = sprintf( "select article_id,larticle_title from larticles where article_id in (%s) and site_language_id=%d",
				implode("," , $article_ids ),
				$GLOBALS["langcode"] );
			$articles = FNTH_db_get_associative( $query , "article_id" , "larticle_title" );
			$n = array();
			foreach ($this->aData as $row) {
				$row["connected_article"]=($row["source_article_id"]!=$this->aG["uid"]) ?
					$articles[ $row["source_article_id"] ] :
					$articles[ $row["target_article_id"] ];
				$n[]=$row;
			}
			$this->aData = $n;
		}

		public function handleEvents() {
			if (!isset( $this->aP["action"] )) return parent::handleEvents();

			switch ($this->aP["action"]) {
			case "delete":
				$am = new ArticlesModel();
				foreach ($this->aP["ids"] as $id) $am->deleteArticleConnection( $id );
				$this->refreshGrid();
				break;
			}
			return parent::handleEvents();
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];
			switch ($colname) {
			case "bidirectional":
				$field->setAttribute( "align" , "center" );
				$field->setValue( fo3_render_boolean_yesno( $rd[$colname] ) );
				break;
			}
			return $field;
		}
		protected function whereClause() {
			$filters = array();

			$filters[]=sprintf( "source_article_id=%d or (target_article_id=%d and bidirectional)",
				$this->aG["uid"] , $this->aG["uid"] );
	
			if (empty( $filters ))
			return " where " . implode( " and " , $filters );
		}

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

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->generateLangDefs();

			$am = new ArticlesModel();
			$larticle = $am->getLanguageArticle( $this->aG["uid"] , $GLOBALS["langcode"] );
			$this->sTitle = $larticle["larticle_title"];

			$this->setToolbarButton( "new" , __("New","attr") , "page_add" , $this->renderStandardAction( "new" , array( "source_article_id" => $this->aG["uid"] ) ) );

			return parent::render();
		}

	}

?>
