#
#		ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquermfile_qryTextChildren.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "qryTextChildren"
#
@querytitle					Untergeordnete Texte und Inhalt eines Texts anzeigen
@querydescription			zeigt zu einem Text (identifiziert mit seiner Text-ID) alle untergeordneten Texte und den Inhalt (alle Fragmente dieses Texts) an
@group						ADMINTEXTFRAG
@paramnames					parTextId:textid:{{parTextIdoptions}}
@parTextIdoptions	{"labeltext":"Parent Text-ID","typedesc":"Text-ID des Texts, dessen untergeordnete Texte abgefragt werden sollen"}
@querytype	dbselect
#
# SQL selects 3 types of rows (column "Teil"):
# "03_Sub":		one row for each distinct "ident" of text fragments having parTextId as parent
# "02":			a separation row
# "01_Inhalt":	one row for each text fragment having parTExtId as "ident"
#
@sql	select distinct ident as "ID", ident as "Inhalt", parent as "ParentID", child_index as "Index", 
		       GROUP_CONCAT(frag_index) as "FragmentIndex", GROUP_CONCAT(distinct type) as "Typen", '<nicht zutreffend>' as "Daten",
		       '' as "Aktionen", '03_Sub' as "Teil" 
		  from CCTEXTFRAG
		 where parent=::parTextId:: 
		 group by ident, child_index
		UNION
		select '---'as "ID", '---' as "Inhalt", '---' as "ParentID", '---' as "Index", '---' as "FragmentIndex", '---' as "Typen", '---' as "Daten", '' as "Aktionen", '02' as "Teil" 
		UNION
		select ident as "ID", ident as "Inhalt", parent as "ParentID", child_index as "Index", 
		       frag_index as "FragmentIndex", type as "Type", data as "Daten",
		       '' as "Aktionen", '01_Inhalt' as "Teil" 
		  from CCTEXTFRAG
		 where ident=::parTextId::
		 order by "Teil" asc, "Index" asc
@decoration.debug					false
@decoration.decorate_colcell.0		$LINK:ident:identoptions
@identoptions.qexpression
	logger.debug("qryTextChildren.0/identoptions.decoration: "+aux.objTxt({ row: resobj.row, cv: cv }));
	if (resobj.row.Teil=="02") {
		cv.tdattribs = "colspan=\"9\"";
	} else {
		cv.hyperlink = { href: "/dquery?dataname=qryTextChildren&parTextId="+resobj.row.ID,
						 title: "Alle untergeordneten Text-Ids von "+resobj.row.ID+" anzeigen" };
		cv.value = resobj.row.ID;
	}
@decoration.decorate_colcell.1.hyperlink.href	/dquery?dataname=textInfo&textid=@@row.ID@@
@decoration.decorate_colcell.1.hyperlink.title	Information zu Text mit ID @@row.ID@@ anzeigen
@decoration.decorate_colcell.1.value	Inhalt
@decoration.decorate_colcell.2		$TEXTAREA:parinput:parinputoptions
@parinputoptions.rows				1
@parinputoptions.cols				15
@parinputoptions.qexpression
	logger.debug("qryTextChildren.2/parinputoptions.decoration: "+aux.objTxt({ row: resobj.row, token: token, opts: opts, sopts: sopts, copts: copts, addoptions: addoptions, cv: cv}));
	if (resobj.row.Teil=='03_Sub') {
		cv = { value: resobj.row.ParentID };
	} else if (resobj.row.Teil=='02') {
		cv.doNotGenerate = true;
	}
@decoration.decorate_colcell.3		$TEXTAREA:childindexinput:childindexinputoptions
@childindexinputoptions.rows		1
@childindexinputoptions.cols		4
@childindexinputoptions.qexpression
	if (resobj.row.Teil=='03_Sub') {
		cv = { value: resobj.row.Index };
	} else if (resobj.row.Teil=='02') {
		cv.doNotGenerate = true;
	}
@decoration.decorate_colcell.4		$TEXTAREA:fragindexinput:fragindexinputoptions
@fragindexinputoptions.rows			1
@fragindexinputoptions.cols			15
@fragindexinputoptions.qexpression
	if (resobj.row.Teil=='03_Sub') {
		cv = { value: resobj.row.FragmentIndex };
	} else if (resobj.row.Teil=='02') {
		cv.doNotGenerate = true;
	}
@decoration.decorate_colcell.5		$TEXTAREA:typeinput:typeinputoptions
@typeinputoptions.rows				1
@typeinputoptions.cols				15
@typeinputoptions.qexpression
	if (resobj.row.Teil=='03_Sub') {
		cv = { value: resobj.row.Typen };
	} else if (resobj.row.Teil=='02') {
		cv.doNotGenerate = true;
	}
@decoration.decorate_colcell.6		$TEXTAREA:datainput:datainputoptions
@datainputoptions.rows				1
@datainputoptions.cols				15
@datainputoptions.style				font-family: monospace;
@datainputoptions.qexpression
	if (resobj.row.Teil=='03_Sub') {
		cv = { value: resobj.row.Daten };
	} else if (resobj.row.Teil=='02') {
		cv.doNotGenerate = true;
	}
@decoration.decorate_colcell.7.doNotGenerate	true
@decoration.decorate_colcell.8.doNotGenerate	true
@decoration.decorate_colcell.9.doNotGenerate	true
@post_query	
	that.nd=new Date(); 
	that.result.setResultAttribute('nowdate',that.nd.getFullYear()+"-"+aux.DEC(that.nd.getMonth()+1,2));
	let acn = that.result.findColumn("Aktionen");
	let tcn = that.result.findColumn("Type");
	let dcn = that.result.findColumn("Daten");
	let crow, trow, parUpId=null, rawhtml, type;
	for (let i=0; i<that.result.getRowCount(); i++) {
		crow = that.result.getRowObject(i);
		if (crow.Teil=='01_Inhalt' && !parUpId) parUpId=crow.ParentID;
		type = that.result.getCellValue(i,tcn);
		if (acn>=0) {
			trow = that.result.getRowObject(i,"value");
			rawhtml = 
		      "<a href=\"/dquery?dataname=delTextFrag"+
		      "&ident="+trow.ID+"&child_index="+trow.Index+"&frag_index="+trow.FragmentIndex+"\""+
		      " title=\"Text Fragment "+trow.ID+"/"+trow.Index+"/"+trow.FragmentIndex+" löschen\">"+
		      "<span class=\"fas fa-trash-alt\"></span></a>";
			if (crow.Teil=="01_Inhalt") {
				rawhtml += "&nbsp;<a href=\"/dquery?dataname=updateTextFrag&ident="+trow.ID+"&child_index="+trow.Index+"&frag_index="+trow.FragmentIndex+"\""+
		      " title=\"Änderungen in Text Fragment "+trow.ID+"/"+trow.Index+"/"+trow.FragmentIndex+" speichern\">"+
		      "<span class=\"fas fa-save\"></span></a>";
			}
		    that.result.setCellValue(i,acn,"rawhtml",rawhtml);
		}
	}
	that.result.parUpId = parUpId;

@title	Untergeordnete Texte und Inhalt von [[parTextId]]
@toptext	<h3>Weitere Aktionen</h3>
		<ul>
		<li><a href="/dquery?dataname=newTextFragment&tfparent={{parTextId}}">Neuen untergeordneten Text zu {{parTextId}} anlegen</a></li>
		<li><a href="/dquery?dataname=qryTextChildren&parTextId=[[parUpId]]">Zeige übergeordnete Struktur ([[parUpId]]) an</a></li>
		<li><a href="/dquery?dataname=mergetextfrags&textident={{parTextId}}">Textfragmente von {{parTextId}} zusammenführen</a></li>
		</ul>
