<?PHP
// score-inhalt.php

error_reporting (5);

require_once ('phpscript/grab_globals.lib.php');
include_once ("phpscript/noAccess.php");
include_once ("phpscript/ConnectToMySql.php");

$kat       = $GLOBALS['select_category'];
$besetzung = $GLOBALS['select_besetzung'];
$select    = $GLOBALS['cmdAll'];
$search_button = $GLOBALS['cmdsuchen'];

$database_ok = 1;
$anzahl = 0;
$isFilter = FALSE;
$sqlSEL ="";
$noDS = "";

if (!empty($select)) {
	$select="all";
	$kat="";
	$besetzung="";
	$search_button="";	
}

	$link      = mysqlcon();
 	if (!$link) $database_ok = 0;

    if (!empty ($search_button)==TRUE or  ($select=="all")==TRUE) {
		if (empty($kat)==TRUE AND empty($besetzung)==TRUE) {
			$select = "all";
		}

		$isFilter = ($select=="sel");

        if ($select=="all") {
            $sqlSEL 	= "SELECT * FROM score"." GROUP BY locsample ORDER BY "."composer, title, besetzung";
        } else {

          if (!empty($kat)==TRUE AND !empty($besetzung)==TRUE) {
              $sqlSEL = "SELECT * FROM score". " WHERE (category ='". $kat . "' AND besetzung='". $besetzung. "') GROUP BY locsample ORDER BY "."composer, title, besetzung";	
          } else {
            if (!empty($kat)) {
              $sqlSEL = "SELECT * FROM score"." WHERE (category ='". $kat . "') GROUP BY locsample ORDER BY "."composer, title, besetzung";	
            } 
            if (!empty($besetzung)) {
              $sqlSEL = "SELECT * FROM score"." WHERE (besetzung='". $besetzung. "') GROUP BY locsample  ORDER BY "."composer, title, besetzung";	
            }
          }
        }
		if ($database_ok) {
          $resFilter    = mysqli_query($link, $sqlSEL);
          $count 	= mysqli_num_rows($resFilter);
          if (!$count) {
              $noDS = "Keine passenden Datensätze gefunden.";
          }
		}
    } //END OF wenn Knopf gewählt wurde!
	  
	  $isFilter=FALSE;
      if ($database_ok) {
          $catarr    = array();
          $besetzarr = array();

          $res     = mysqli_query($link, "SELECT category, COUNT(category) AS z FROM score GROUP BY category ORDER BY category");
          $i=0;
          while ($catarr[$i] =mysqli_fetch_array($res, MYSQLI_ASSOC)) {
              $i++;
          }
          $res     = mysqli_query($link, "SELECT besetzung, COUNT(besetzung) AS z FROM score GROUP BY besetzung ORDER BY besetzung");
          $i=0;
          while ($besetzarr[$i] = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
              $i++;
          }
      } //END OF wenn Datenverbindung da ist!
	 
?>

<style type="text/css">

	div {font-family:Arial, Helvetica, sans-serif; text-align:left;}
	table  {
      	table-layout: auto;
  		width: 100%;
		border-collapse:collapse;
	}
	td {
  		border: 1px solid #336699;
  		*/overflow: hidden;*/
  		white-space: wrap;
  		text-overflow: ellipsis;
		line-height:1.2em;
		/*padding: 0 0.5em 0 0.5em;*/
	}
 	td.acrobat {
      background-color: #AA0000; 
      font-style:bold;
      text-align:center;
      vertical-align:middle;
  	}
  td.acrobat a {color:#FCFCFC;}
  td.acrobat a:hover {color:#00CC00;}
	select {
		padding: 4px;
		font-family: Arial, Helvetica, sans-serif;
		line-height:1.5em;
		font-size:1em;
		color:#333;
		background-color:#FCFCFC;
		border:1px solid #DADADA;
		width:17em;
	
	}
	.box {
		width:18em; important!;
      	display: block
	}
	input.button-ok {
		background-color: #336699;
		color: #FCFCFC;
		border: 1px solid #4455AA;
		border-radius: 5px;
		line-height:2.5em;
	}	
</style>

  <?PHP 
    if ($database_ok) {
  ?>  
	<div class="box">  
    	<form action="klassische-stuecke.php?item=sel" 
     		method="post" name="frmComposer" target="_self" id="frmComposer">
    	<div style="width>: 20em; margin: 0.5em 0 0.5em 0;">
        	<div style="margin: 0.5em auto; font-size:1.2em; font-weight:bold;">
          		Filter
        	</div>
        	<div style="text-align:left; margin: 0.5em auto; font-size:1.2em;">
            	Kategorie
            </div>
            <div style="margin: 0.5em auto;">
                <select name="select_category" id="select_category">
                <?PHP
                    echo "<option value=''>beliebig</option>";
                    $i=0;
                    while ($catarr[$i]) {
                        echo "<option value=\"".$catarr[$i][category]."\">".$catarr[$i][category]." (".$catarr[$i][z].")"."</option>";
                        $i++;
                    }
                ?>
                </select>
            </div>
            <div style="text-align:left; margin: 0.5em auto; font-size:1.2em;">
                Besetzung
            </div>
	        <div style="margin: 0.5em auto;">
                <select name="select_besetzung" id="select_besetzung">
                <?PHP
                    echo "<option value=''>beliebig</option>";
                    $i=0;
                    while ($besetzarr[$i]) {
                        echo "<option value=\"".$besetzarr[$i][besetzung]."\">".$besetzarr[$i][besetzung]." (".$besetzarr[$i][z].")"."</option>";
                        $i++;
					}
                ?>
                </select>
			</div>
       		<div style="display:block; width:100%; margin: 0.5em auto;">
				<input name="cmdsuchen" type="submit" class="button-ok" style="width:100%;" 
                id="cmdsuchen"  value="ANWENDEN">
            </div>
   		  <div style="display:block; width:100%; margin: 0.5em auto;">
				<input name="cmdleeren" type="reset" class="button-ok" style="width:100%;" 
                id="cmdleeren"  value="FILTER ZURÜCKSETZEN">
        	</div>
 
    		  <div style="display:block; width:100%; margin: 0.5em auto;">
				<input name="cmdAll" type="submit" class="button-ok" style="width:100%;" 
                id="cmdAll"  value="OHNE FILTER ALLES ANZEIGEN">
        	</div>

       
		</div>
		</form>
    </div>

    <?PHP
		} else {
    		echo "<div style='width:18em; text-align:left; margin-top:1.5em;'>";
			echo "Leider gibt es z.Z. Probleme mit der Datenbank. Eine Auswahl ist im Augenblick nicht möglich.";
  			echo "</div>";
		}
	?>
    
    <!-- ab hier beginnt die AUSGABE -->
  
  	<div style="display: block; line-height:1.2em; fontsize:1em; ">
 	<?PHP
    echo "<span style='font-weight:bold;'>Hinweis:</span></br>";
    	echo "Es wird die <span style='background-color:#AA0000; color:#FCFCFC;'>Anzahl der Stimmen</span> angezeigt.</br>";
    	echo "Sind die (gew&auml;hlten) St&uuml;cke in mehreren Besetzungen verf&uuml;gbar, befinden sich diese in <span style='letter-spacing:5px; font-weight:bold;'>einem</span>Paket, das auch die ggf. gew&auml;hlte Besetzung enth&auml;lt.</br>&nbsp;</br>";

    	if (!empty($kat)) echo "Kategorie: ". $kat."<br> \n"; 
		if (!empty($besetzung)) echo "Besetzung: ".$besetzung;
	?>
		
    <div>
	<?PHP
		if ($count) {
      		echo "<table>";
	  		$komp="";
	  		$tit="";
		while ($row = mysqli_fetch_array($resFilter,MYSQLI_ASSOC)){

			echo "<tr> \n";	
			if (($komp)<>$row[composer]) {			
				echo "<td>$row[composer]</td>";
				$komp=$row[composer];
			} else {
				echo "<td>&nbsp;</td>";
			}
			echo "<td>$row[title] $row[verz]</td>";
   			echo "<td class='acrobat'><a href='sax-scores/dl.php?id=".$row[id]."&f=a'>-&nbsp;$row[stimm]&nbsp;-</a></td>";
			echo "</tr> \n";
			$z++;
		}
		echo "</table>";
		} else {
			echo "<span style='line-height:1.2em; font-size:1.2em; margin: 1em 0 1em 0;'>".$noDS."</span>";
		}
	?>
        </div>	
  	</div>

