#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_mfiles.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "mfiles"
#
@querytitle	Managed files anzeigen
@querydescription	
@group	DATAFILE
# @attributenames			searchstring:string:{{searchstringoptions}},lpath:mfilelpath:{{lpathoptions}},mfiletype:mfiletype:{{mfiletypeoptions}},mfileattribs:string:{{mfileattribsoptions}},addwhereclause:string:{{addwhereclauseoptions}}
@attributenames					lpath:mfilelpath:{{lpathoptions}},mfiletype:mfiletype:{{mfiletypeoptions}},mfileattribs:string:{{mfileattribsoptions}},addwhereclause:string:{{addwhereclauseoptions}}
@searchstringoptions.labeltext	Suchbegriff
@searchstringoptions.typedesc	Ein Teil des Namens, der ID oder der Attribut-Definitionen, nach dem Managed files ausgesucht werden sollen
@searchstringoptions.check_qexpression
								/* this is dummy code, it does not (yet) work */
								if (qryatt.is_optional) return true;
@lpathoptions			{"labeltext":"logischer Pfad","typedesc":"Logischer Verzeichnispfad, aus dem managed files angezeigt werden sollen","is_optional":"true"}
@mfiletypeoptions		{"labeltext":"Managed File Type","typedesc":"Type der Managed Files, die angezeigt werden sollen","is_optional":"true"}
@mfileattribsoptions	{"labeltext":"Attribute","typedesc":"Wert des MFILEATTRIBS-Felds für die auszuwählenden managed-File Einträge","is_optional":"true"}
@addwhereclauseoptions	{"labeltext":"Auswahlbedingungen","typedesc":"SQL-where Ausdruck zur Einschränkung der Auswahl","is_optional":"true"}
@querytype				dbselect
@toptext
	<script>
		function deleteSelection() {
			let itm, id;
			let sel = $("body table :checked");
			let ids = new Array();
			for (let ei=0; ei<sel.length; ei++) {
				itm = sel[ei];
				id = itm.getAttribute("select_id");
				if (id) {
					ids.push(id);
				}
			}
			window.location.replace("/dquery?dataname=delmfiles&cfgname=[[cfgname]]&mfileids="+ids.join(",")+"&chain_dataname=mfiles");
		}
	</script>
@pre_query	
	that.query.whereclause = "";
	that.query.wcactive = false;
	if (that.query.lpath)  {
		  that.query.lpath = that.query.lpath.trim();
		  if (that.query.lpath.substring(0,5).toUpperCase()=='WHERE') that.query.lpath = that.query.lpath.substring(5);
		  if (!that.query.wcactive) that.query.whereclause = "WHERE ";
		  that.query.whereclause += "(MFILELPATH='"+that.query.lpath+"')";
		  that.query.wcactive = true;
		  that.query.title += " auf logischem Pfad \""+that.query.lpath+"\"";
	}
	if (that.query.mfiletype) { 
		  that.query.mfiletype = that.query.mfiletype.trim();
		  if (that.query.mfiletype.substring(0,5).toUpperCase()=='WHERE') that.query.mfiletype = that.query.mfiletype.substring(5);
		  if (that.query.wcactive) { that.query.whereclause += " and "; } else { that.query.whereclause = "WHERE "; }
		  that.query.whereclause += "(MFILETYPE='"+that.query.mfiletype+"')"; 
		  that.query.wcactive = true;
		  that.query.title += " der Type "+that.query.mfiletype;
	}
	if (that.query.mfileattribs) {
		logger.debug("MFILES.UDBG.filter.mfileattribs="+that.query.mfileattribs);
		that.query.mfileattribs = that.query.mfileattribs.trim();
		  if (that.query.mfileattribs.substring(0,5).toUpperCase()=='WHERE') that.query.mfileattribs = that.query.mfileattribs.substring(5);
		  if (that.query.wcactive) { that.query.whereclause += " and "; } else { that.query.whereclause = "WHERE "; }
		  that.query.whereclause += "(MFILEATTRIBS='"+that.query.mfileattribs+"')";
		  that.query.wcactive = true;
		  that.query.title += " mit Attributen \""+that.query.mfileattribs+"\"";
	}		
	if (that.query.addwhereclause) {
		 that.query.addwhereclause = that.query.addwhereclause.trim();
		  if (that.query.addwhereclause.substring(0,5).toUpperCase()=='WHERE') that.query.addwhereclause = that.query.addwhereclause.substring(5);
		  if (that.query.wcactive) { that.query.whereclause += " and "; } else { that.query.whereclause = "WHERE "; }
		  that.query.whereclause += "(" + that.query.addwhereclause + ")";
		  that.query.wcactive = true;
		  that.query.title += " mit Bedingung\"( "+that.query.addwhereclause+")\""
	}
@sql_mysql	select	mfileid as "Managed File ID", 
					mfiletype as "Managed File Type",
					mfilename as "Ursprünglicher Dateiname", 
					mfilepath as "Speicherpfad",
					mfilelpath as "Logischer Pfad",
					mfileattribs as "Attribute (JSON-Format)",
					mfileid as "Auswahl"
			  from CCMFILES {{whereclause}}
			 order by "Managed File ID" asc
@options.notranslate	true
@post_query
	proc: {
		let escaped_whereclause = that.query.whereclause.replace(/\"/g,'&#x22');
		that.result.toptext = 	that.query.toptext + 
								"<ul>"+
								"  <li>Die Abfrage hat "+that.result.getRowCount()+" Einträge gefunden</li>\r\n"+
								"</ul>\r\n"+
								"<h3>Weitere Aktionen</h3>\r\n"+
								"<ul>"+
								" <li><a href=\"/dquery?dataname=mfilelpaths\">Logische Pfade / Verzeichnisse</a></li>\r\n"+
								" <li><table>"+
								"  <tr><form action=\"/dquery\" method=\"GET\">"+
									"<input type=\"hidden\" name=\"dataname\" value=\"delmfiles\"></input>"+
									"<td><label for_input=\"whereclause\">L&ouml;sch-Auswahl</label></td>"+
									"<td><input type=\"text\" name=\"whereclause\" size=\"132\" value=\""+escaped_whereclause+"\"></input></td>"+
									"<td><input type=\"submit\" name=\"submit\" value=\"Ausgew&auml;hlte Dateien l&ouml;schen\"></input></td>"+
									"</form></tr>"+
								"  <tr><form action=\"/dquery\" method=\"GET\">"+
									"<input type=\"hidden\" name=\"dataname\" value=\"mfiles\"></input>"+
									"<td><label for_input=\"addwhereclause\">Neue Bedingung</label></td>"+
									"<td><input type=\"text\" name=\"addwhereclause\" size=\"132\" value=\""+escaped_whereclause+"\"></input></td>"+
									"<td><input type=\"submit\" name=\"submit\" value=\"mit dieser Bedingung neu abfragen\"></input></td>"+
								"    </form></tr>"+
								" </table></li>\r\n";
		let ftyps = {};
		let cft;
		let mftcn = that.result.findColumn("Managed File Type");
		if (mftcn<0) {
			that.result.toptext += "<p>ERROR: could not find column \"Managed File Type\" in Result of managed files listing</p>";
			break proc;
		}
		for (let ri=0; ri<that.result.getRowCount(); ri++) {
			cft = that.result.getCellValue(ri,mftcn);
			if (!ftyps[cft]) {
				ftyps[cft] = { mftype: cft };
			}
			if (cft!="dmvbrfausw") {
				ftyps.more_than_dmvbrfausw = true;
			}
			if (cft!="cucccallstat" && cft!="cucccallcat" && cft!="cuccemailstat") {
				ftyps.more_than_cuccstat = true;
			}
		}
		if (ftyps.dmvbrfausw) {
			if (!ftyps.more_than_dmvbrfausw) {
				that.result.toptext += "<li><a href=\"/dquery?dataname=LOADMVBRFAUSWMULTI\">Alle noch nicht geladenen MVB-Auswertung_MeldungenClearingfaelle-Dateien laden</a></li>\r\n";
			}
		}
		if (ftyps.cucccallstat || ftyps.cucccallcat || ftyps.cuccemailstat) {
			if (!ftyps.more_than_cuccstat) {
				that.result.toptext += "<li><a href=\"/dquery?dataname=LOADCUCCSTATMULTI\">Alle noch nicht geladenen CuCC-Statistiken laden</a></li>\r\n";
			}
		}
		that.result.toptext += "<li><button onClick=\"deleteSelection();\">Auswahl löschen</button></li>\r\n";
		that.result.toptext += "</ul>\r\n";
	}
	
#
@title	Managed Files
@tableclass	excelFilterTable
@decoration.debug								false
@decoration.decorate_colcell.0.hyperlink.href	/dquery?dataname=showfile&mfileid=@@celldata@@
@decoration.decorate_colcell.0.hyperlink.title	Managed File Eigenschaften anzeigen
@decoration.decorate_colcell.0.value			@@celldata@@
@decoration.decorate_colcell.1.hyperlink.href	/dquery?dataname=mfiles&mfiletype=@@celldata@@
@decoration.decorate_colcell.1.hyperlink.title	Alle managed Files mit type @@celldata@@ anzeigen
@decoration.decorate_colcell.4.hyperlink.href	/dquery?dataname=mfiles&lpath=@@celldata@@
@decoration.decorate_colcell.4.hyperlink.title	Alle managed files unter logischem Pfad  @@celldata@@ anzeigen
@decoration.decorate_colcell.6					$SELECTBOX
