Browse Source

iframe -> XMLHttpRequest, mostra i criteri di ricerca in uso

pezcurrel 4 years ago
parent
commit
e0b182c8d7
3 changed files with 118 additions and 59 deletions
  1. 110 32
      web/admin/instances.php
  2. 6 21
      web/admin/instancesh.php
  3. 2 6
      web/admin/theme.css

+ 110 - 32
web/admin/instances.php

@@ -137,9 +137,9 @@ $cols=array(
 	'TagID'=>array('name'=>'[Tags]','type'=>'join','join'=>array('InstTags ON InstTags.InstID=Instances.ID')),
 	'Tags.Name'=>array('name'=>'Tags','type'=>'join','joini'=>array('InstTags ON InstTags.InstID=Instances.ID','Tags ON Tags.ID=InstTags.TagID'),'subtype'=>'text'),
 
-	'Statuses'=>array('name'=>'Stati ultima settimana','type'=>'joini','join'=>array('InstActivity ON InstActivity.InstID=Instances.ID'),'subtype'=>'int','select'=>'MAX(Week) AS maxweek'),
+	/*'Statuses'=>array('name'=>'Stati ultima settimana','type'=>'joini','join'=>array('InstActivity ON InstActivity.InstID=Instances.ID'),'subtype'=>'int','select'=>'MAX(Week) AS maxweek'),
 	'Logins'=>array('name'=>'Logins ultima settimana','type'=>'joini','join'=>array('InstActivity ON InstActivity.InstID=Instances.ID'),'subtype'=>'int','select'=>'MAX(Week) AS maxweek'),
-	'Registrations'=>array('name'=>'Registrazioni ultima settimana','type'=>'joini','join'=>array('InstActivity ON InstActivity.InstID=Instances.ID'),'subtype'=>'int','select'=>'MAX(Week) AS maxweek'),
+	'Registrations'=>array('name'=>'Registrazioni ultima settimana','type'=>'joini','join'=>array('InstActivity ON InstActivity.InstID=Instances.ID'),'subtype'=>'int','select'=>'MAX(Week) AS maxweek'),*/
 );
 
 $types=array('bool','int','time','text','join','joini');
@@ -188,25 +188,39 @@ foreach ($cols as $key=>$val) {
 [condsel-6] => =
 [valuesel-6] => 20
 [closepar-6] => null
+[andor-6] => AND
 */
 
+$i=0;
+$jsaddrows='';
+$jssetrows='';
+$cp=count($_POST)/6; // /6 perché ogni "blocco" contiene 6 righe
 $sels=array();
 $joins=array();
 $where='';
 foreach ($_POST as $key=>$val) {
-	if (preg_match('/^openpar-\d+/',$key)===1 && preg_match('/^\(+$/',$val)===1)
-		$where.=$val;
+	if (preg_match('/^openpar-\d+/',$key)===1) {
+		$i++;
+		if (preg_match('/^\(+$/',$val)===1)
+			$where.=$val;
+		$jsaddrows.='addrow('.$i.',false);'.N;
+		$jssetrows.='setrow('.$i.','.json_encode($val);
+	}
 	if (preg_match('/^fieldsel-\d+/',$key)===1 && preg_match('/^(.+)§(.+)§(.+)$/',$val,$buf)===1) {
-		if (array_key_exists($buf[1],$cols) && in_array($buf[2],$types)) {
-			$where.=$buf[1];
-			if ($buf[2]=='join' || $buf[2]=='joini') {
-				foreach ($cols[$buf[1]]['join'] as $join) {
+		$col=$buf[1];
+		$type=$buf[2];
+		$subtype=$buf[3];
+		if (array_key_exists($col,$cols) && in_array($type,$types)) {
+			$where.=$col;
+			if ($type=='join' || $type=='joini') {
+				foreach ($cols[$col]['join'] as $join) {
 					if (!in_array($join,$joins))
 						$joins[]=$join;
 				}
-				if ($buf[2]=='joini' && array_key_exists('select',$cols[$buf[1]]) && !in_array($cols[$buf[1]]['select'],$sels))
-					$sels[]=$cols[$buf[1]]['select'];
+				if ($type=='joini' && array_key_exists('select',$cols[$col]) && !in_array($cols[$col]['select'],$sels))
+					$sels[]=$cols[$col]['select'];
 			}
+			$jssetrows.=','.json_encode($val);
 		} else {
 			muoribene($dbg.'<br>Dati POST corrotti.<br>Puoi <a href="instances.php">riprovare</a> o <a href="index.php">tornare al login</a>.',true);
 		}
@@ -214,19 +228,35 @@ foreach ($_POST as $key=>$val) {
 	if (preg_match('/^condsel-\d+/',$key)===1) {
 		$where.=' '.$val.' ';
 		$cond=$val;
+		$jssetrows.=','.json_encode($val);
 	}
-	if (preg_match('/^valuesel-\d+/',$key)===1)
+	if (preg_match('/^valuesel-\d+/',$key)===1) {
 		$where.=$val;
+		$jssetrows.=','.json_encode($val);
+		$jssetrows.=',""';
+	}
 	if (preg_match('/^valueinp-\d+/',$key)===1) {
 		if (preg_match('/^(LIKE|NOT LIKE)$/',$cond)===1)
 			$where.='\'%'.myesc($link,$val).'%\'';
 		else
 			$where.='\''.myesc($link,$val).'\'';
+		$jssetrows.=',""';
+		$jssetrows.=','.json_encode($val);
 	}
-	if (preg_match('/^closepar-\d+/',$key)===1 && preg_match('/^\)+$/',$val)===1)
-		$where.=$val;
-	if (preg_match('/^andor-\d+/',$key)===1 && preg_match('/^(AND|OR)$/',$val)===1)
-		$where.=' '.$val.' ';
+	if (preg_match('/^closepar-\d+/',$key)===1) {
+		if (preg_match('/^\)+$/',$val)===1)
+			$where.=$val;
+		$jssetrows.=','.json_encode($val);
+	}
+	if (preg_match('/^andor-\d+/',$key)===1 && preg_match('/^(AND|OR)$/',$val)===1) {
+		if ($i<$cp)
+			$where.=' '.$val.' ';
+		$jssetrows.=','.json_encode($val).');'.N;
+	}
+}
+if ($jsaddrows!='') {
+	$jsaddrows.='document.getElementById("filbut").value="Rimuovi tutti i criteri di ricerca";'.N;
+	$jsaddrows.='synctai();'.N;
 }
 
 if (count($sels)>0)
@@ -242,10 +272,10 @@ else
 
 $query='SELECT *, Instances.ID AS IID'.$sels.' FROM Instances '.$joins.' '.$where.' GROUP BY IID ORDER BY Instances.URI ASC LIMIT 50';
 $dbg.='QUERONA: '.$query.'<br>'.N;
+$tini=microtime(true);
 $res=mysqli_query($link,$query)
 	or muoribene($dbg.'<br>'.$query.': '.mysqli_error($link),true);
-
-
+$dbg.='Durata esecuzione query: '.round(microtime(true)-$tini,4).' sec.<br>'.N;
 
 if (mysqli_num_rows($res)<1) {
 	$out='<p>Nessuna istanza da mostrare.</p>'.N;
@@ -291,7 +321,7 @@ if (mysqli_num_rows($res)<1) {
 		if (mysqli_num_rows($sres)>0) {
 			$attr.='<div class="colsectcont">Stati: '.$tot['tstatuses'].'<br>Accessi: '.$tot['tlogins'].'<br>Registrazioni: '.$tot['tregs'].'</div>'.N;
 			while ($srow=mysqli_fetch_assoc($sres)) {
-				$attr.='<div class="colsectcontb">'.strftime('%e %b %Y',$srow['Week']).'</div>'.N;
+				$attr.='<div class="colsectcontb">'.strftime('%e %b %Y',$srow['Week']).' ('.$srow['Week'].')</div>'.N;
 				($tot['tstatuses']==0) ? $width=0 : $width=str_replace(',','.',100/$tot['tstatuses']*$srow['Statuses']);
 				$attr.='<div class="percstatuses" style="width:'.$width.'%;">'.$srow['Statuses'].'&nbsp;stati</div>'.N;
 				($tot['tlogins']==0) ? $width=0 : $width=str_replace(',','.',100/$tot['tlogins']*$srow['Logins']);
@@ -491,7 +521,7 @@ function inpdisif(index) {
 		if (sel.style.display!='none') sel.disabled=false;
 	}
 }
-function popusels(index) {
+function popusels(index,valselval) {
 	console.log('Index: '+index);
 	var key=document.getElementById('fieldsel-'+index).value;
 	var type=key.replace(/^.*§(.*)§.*$/,'$1');
@@ -559,12 +589,26 @@ function popusels(index) {
 		valinp.disabled=true;
 		condsel.setAttribute('onchange','inpdisif('+index+');');
 		truncsel('valuesel-'+index);
-		document.getElementById('helper').setAttribute('src','instancesh.php?key='+key+'&parsel=valuesel-'+index);
+		let xhr=new XMLHttpRequest();
+		xhr.open('GET','instancesh.php?key='+key);
+		xhr.responseType='json';
+		xhr.send();
+		xhr.onload=function() {
+			let jarr=xhr.response;
+			for (i=0; i<jarr.length; i++)
+				addselopt(valsel,jarr[i][0],jarr[i][1]);
+			console.log('valsel pronto!');
+			if (valselval!==false)
+				selind('valuesel-'+index,valselval);
+		};
+		xhr.onerror=function() {
+			alert('Request failed');
+		};
 	} else {
 		alert('Attenziò: non so che farmene del tipo "'+type+'": diglielo, al programmatore.');
 	}
 }
-function addrow(index) {
+function addrow(index,refresh) {
 	var table=document.getElementById('planciafil');
 	var newrow=table.insertRow(index);
 	var newcell0=newrow.insertCell(0);
@@ -583,8 +627,10 @@ function addrow(index) {
 	newcell5.innerHTML='<select class="andor"><option value="AND">E</option><option value="OR">O</option></select>';
 	newcell6.innerHTML='<img class="plus" src="imgs/plus.svg">';
 	newcell7.innerHTML='<img class="minus" src="imgs/minus.svg">';
-	synctai();
-	popusels(index);
+	if (refresh) {
+		synctai();
+		popusels(index,false);
+	}
 }
 function synctai() {
 	var i=0;
@@ -597,7 +643,7 @@ function synctai() {
 	nodes=table.querySelectorAll('.plus');
 	for (i=0; i<cnodes; i++)
 		// +2 perché la devi aggiungere dopo la corrente
-		nodes[i].setAttribute('onclick','addrow('+(i+2)+');');
+		nodes[i].setAttribute('onclick','addrow('+(i+2)+',true);');
 // da qui in poi faccio a scendere per non avere mai id e nomi duplicati
 	cnodes--;
 	nodes=table.querySelectorAll('.openpar');
@@ -609,7 +655,7 @@ function synctai() {
 	for (i=cnodes; i>-1; i--) {
 		nodes[i].setAttribute('id','fieldsel-'+(i+1));
 		nodes[i].setAttribute('name','fieldsel-'+(i+1));
-		nodes[i].setAttribute('onchange','popusels('+(i+1)+');');
+		nodes[i].setAttribute('onchange','popusels('+(i+1)+',false);');
 	}
 	nodes=table.querySelectorAll('.condsel');
 	for (i=cnodes; i>-1; i--) {
@@ -640,6 +686,28 @@ function synctai() {
 	if (cnodes>=0)
 		nodes[cnodes].disabled=true;
 }
+function selind(id,val) {
+	var el=document.getElementById(id), i=0, found=false;
+	while (!found && i<el.length) {
+		if (el[i].value==val) {
+			el.selectedIndex=i;
+			found=true;
+		}
+		i++;
+	}
+	if (!found)
+		console.log(val+' non è stato trovato in '+id);
+}
+function setrow(index,openparv,fieldselv,condselv,valueselv,valueinpv,closeparv,andorv) {
+	selind('openpar-'+index,openparv);
+	selind('fieldsel-'+index,fieldselv);
+	popusels(index,valueselv);
+	selind('condsel-'+index,condselv);
+//	selind('valuesel-'+index,valueselv);
+	document.getElementById('valueinp-'+index).value=valueinpv;
+	selind('closepar-'+index,closeparv);
+	selind('andor-'+index,andorv);
+}
 function remrow(index) {
 	var table=document.getElementById('planciafil');
 	table.deleteRow(index);
@@ -657,19 +725,22 @@ function manfil() {
 	var table=document.getElementById('planciafil');
 	var crows=table.rows.length;
 	if (crows<2) {
-		addrow(1);
+		addrow(1,true);
 		filbut.value='Rimuovi tutti i criteri di ricerca';
 	} else {
 		confirma('Confermi di voler rimuovere tutti i criteri di ricerca?','truncfil();filbut.value=\'Aggiungi criteri di ricerca\'');
 	}
 }
 function ckf() {
+	var table=document.getElementById('planciafil');
+	var crows=table.rows.length;
+	document.getElementById('andor-'+(crows-1)).disabled=false;
 	document.getElementById('f').submit();
 }
 //-->
 </script>
 </head>
-<body>
+<body onload="console.log('body bu!');">
 
 <nav>
 <div id="hmenu">
@@ -688,8 +759,6 @@ function ckf() {
 </div>
 </div>
 
-<iframe name="helper" id="helper"></iframe>
-
 <div id="footer">
 <form action="edinst.php" name="addinst" method="post">
 <table><tr><td>Aggiungi un’istanza:</td><td><input type="text" name="URI" maxlength="512"></td><td><input type="button" value="Vai" onClick="ckaif();"></td></tr></table>
@@ -700,9 +769,6 @@ function ckf() {
 <?php echo($dbg); ?>
 </div>
 
-<div id="fullscreen">
-<div id="middlerow">
-
 <form method="post" id="f">
 <div id="plancia">
 <table id="planciafil" class="planciatab">
@@ -717,6 +783,18 @@ function ckf() {
 </div>
 </form>
 
+<script language="JavaScript">
+<!--
+<?php
+echo($jsaddrows);
+echo($jssetrows);
+?>
+//-->
+</script>
+
+<div id="fullscreen">
+<div id="middlerow">
+
 <?php echo($out); ?>
 
 </div>

+ 6 - 21
web/admin/instancesh.php

@@ -13,32 +13,17 @@ $cols=array(
 	'TagID'=>array('select'=>'SELECT Tags.ID AS Tid, CONCAT(Name,\' (\',COUNT(Tags.ID),\')\') AS Txt FROM InstTags LEFT JOIN Tags ON Tags.ID=TagID GROUP BY (Tags.ID) ORDER BY Name ASC','optcol'=>'Tid','txtcol'=>'Txt'),
 );
 
-if (array_key_exists('key',$_GET) && array_key_exists('parsel',$_GET) && array_key_exists($_GET['key'],$cols)) {
+if (array_key_exists('key',$_GET) && array_key_exists($_GET['key'],$cols)) {
 	require('include/myconn.php');
 	$res=mysqli_query($link,$cols[$_GET['key']]['select'])
 		or muoribene(mysqli_error($link),true);
 	mysqli_close($link);
-	$jsout='e=window.parent.document.getElementById(\''.$_GET['parsel'].'\');'.N;
-	while ($row=mysqli_fetch_assoc($res)) {
-		$jsout.='o=new Option('.json_encode($row[$cols[$_GET['key']]['txtcol']]).','.$row[$cols[$_GET['key']]['optcol']].');e.add(o);'.N;
-	}
+	$buf=array();
+	while ($row=mysqli_fetch_assoc($res))
+		$buf[]=array($row[$cols[$_GET['key']]['optcol']],$row[$cols[$_GET['key']]['txtcol']]);
+	echo(json_encode($buf));
 }
 
-$out='Ciao!';
+exit(0);
 
 ?>
-<!DOCTYPE HTML>
-<html lang="it">
-<head>
-<title>Mastodon Startpage Admin - Istanze - Helper</title>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-<script language="JavaScript">
-<!--
-<?php echo($jsout); ?>
-//-->
-</script>
-</head>
-<body>
-<?php echo($out); ?>
-</body>
-</html>

+ 2 - 6
web/admin/theme.css

@@ -412,16 +412,12 @@ input {
 	min-width: 320px;
 }
 
-#helper, #debug {
+#debug {
 	position: fixed;
-	right: 0;
+	left: 0;
 	top: 32px;
 	width: 320px;
 	height: 240px;
-}
-
-#debug {
-	left: 0;
 	font-size: 8pt;
 }