<?php
# ObrEns     -  Fichero de presentacion Mambo
# Version    :  1.0 
# By         :  2005 Rodax Software
# Web        :  www.rodax-software.com
# License    :  GNU GPL Public License


###################################################################################
#INICIO
###################################################################################
# Inicializamos
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
require($mosConfig_absolute_path."/administrator/components/com_obrens/config.obrens.php");
require($mosConfig_absolute_path."/components/com_obrens/version.php");

# Establecemos idioma
if (file_exists('components/com_obrens/lang/'.$mosConfig_lang.'.php')) {
  include('components/com_obrens/lang/'.$mosConfig_lang.'.php');
} else {
  include('components/com_obrens/lang/spanish.php');
}

#PENDIENTE
$stati = array (ARTREF_IN_PLANUNG,ARTREF_IN_ENTWICKLUNG,ARTREF_ABGESCHLOSSEN);

###################################################################################
#FUNCIONES
###################################################################################
# Dibujar Footer
function RefFooter($ObrEns_version) {
  echo "<center><font face=\"Arial\" size=\"1\"><b><font color=\"#0000FF\">Área</font><font color=\"#FF0000\"><img border=\"0\" src=\"components/com_obrens/images/tynimam.jpg\" width=\"12\" height=\"11\">Ensayo</font></b> $ObrEns_version por <a href=\"http://www.rodax-software.com\" target=\"_blank\"><font face=\"Arial\" size=\"1\">Rodax Software S.L</font></a></center>";
  return;
}

# Dibujar Cabecera
function RefHeader($Itemid) {
  global $database;
  echo "<div class='componentheading'>";
  $database->setQuery("SELECT name from #__menu WHERE id='$Itemid'");
  $menuname = $database->loadResult();
  echo "$menuname";
  echo "</div>";
  return;
}

# Ver categorias
function ShowCats($Itemid, $artref_showpicture) {
	global $database;
	echo "<table width='100%' border='0' cellspacing='1' cellpadding='4'>";

	$query1="SELECT * FROM #__categories WHERE section = 'com_obrens' AND published = '1' ORDER BY ordering ASC";
	$database->setQuery($query1);
	$rows = $database->loadObjectList();

	$line=1;	
	foreach($rows as $row1) {
		$linecolor = ($line % 2) + 1;
		echo "<tr class='sectiontableentry".$linecolor."'>";
	  
	  echo "<td width='1' align=center valign='top'>";
	  //Si tiene imagen asociada la categoria la pintamos
		if (($artref_showpicture=="1") && ($row1->image <> "")) {
			echo "<a href=index.php?option=com_obrens&Itemid=$Itemid&func=viewcategory&vcatid=$row1->id>
						<img width=100 src='images/stories/$row1->image' hspace='4' border='0'/></a>";
    }
    echo "</td><td width='100%'>
				  <a href=index.php?option=com_obrens&Itemid=$Itemid&func=viewcategory&vcatid=$row1->id><b>$row1->title</b></a><br />
					$row1->description<p />";

    //Dibujamos las entradas de las que consta
    $database->setQuery("SELECT count(id) FROM #__obrens WHERE published = '1' AND catid = '".$row1->id."'");
    $count = $database->loadResult();
    echo "<a href=index.php?option=com_obrens&Itemid=$Itemid&func=viewcategory&catid=$row1->id>$row1->titulo</a>";
		if($count!=1) {
			echo "($count ".LIT_ENTRADAS.")";
    }else{
			echo "($count ".LIT_ENTRADA.")";
	  }

		echo "</td></tr>";
		$line++;
	}
	echo "</tr></table>";
  return;
}

# Ver obras por categoria
function ShowList($obrens_vercabecera, $artref_showbeschrlink, $obrens_ordenlista) {
  global $database, $Itemid;
  echo "<table width='100%' border='0' cellspacing='1' cellpadding='4' class='contentpaneopen' >";

  $cadena = "SELECT  c.*, COUNT(DISTINCT s1.id) AS num"
				  . "\nFROM #__categories AS c"
				  . "\nLEFT JOIN #__obrens AS s1 ON s1.catid = c.id"
				  . "\nWHERE c.section='com_obrens' AND c.published='1'"
				  . "\nGROUP BY c.id"
  				. "\nORDER BY c.ordering, c.title";

  $database->setQuery($cadena);
  $rows = $database->loadObjectList();
  $columnspan = 3;
  
  foreach($rows as $row1) {
    if ($row1->num > 0) {
      echo "<tr><td colspan=$columnspan class='contentheading'>";
      echo "$row1->title</td></tr>";
      echo "<tr><td colspan=$columnspan class='contentpaneopen'>";
      echo "$row1->description</td></tr>";
      
      //Consultamos las obras de la categoria ordenadas por titulo
      $cadena = "SELECT * FROM #__obrens WHERE published='1' AND catid='$row1->id' ORDER BY ordering";
			/*switch ($obrens_ordenlista) {
		  case '1': $cadena .= "titulo";
		            break;
		  case '2': $cadena .= "autor";
		            break;  
		  case '3': $cadena .= "id";  
		            break;
		  } */           
      $database->setQuery($cadena);
      $srows = $database->loadObjectList();
			$line=1;
      foreach($srows as $srow1) {
        $linecolor = ($line % 2) + 1;
        # Listen aussehen
        echo "<tr class='sectiontableentry".$linecolor."'>";
        echo "<td valign='middle' align='left' class='tituloobra'><a href='index.php?option=com_obrens&Itemid=".$Itemid."&func=fullview&refid=$srow1->id'>$srow1->titulo</a></td>";
        echo "<td valign='middle' align='left' class='autorobra'>$srow1->autor</td>";        
        echo "<td valign='middle' align='center'>";
        echo "<a href='index.php?option=com_obrens&Itemid=".$Itemid."&func=fullview&refid=$srow1->id'><img src='components/com_obrens/images/logens.png'></a></td>";
        echo "</tr>";
        $line++;
      }
    }
  }
  echo "</table><P/>";
  return;
}

# Devuelve un array dimensional que representa la tabla de voces
function darDetallesObra($refid, $ncolfil) {
  global $database;
  global $rutalocal;
  global $obrens_rutafich;
  global $obrens_nomfich;
  global $obrens_extmid;
  $contador = 0;
    
	//Inicializamos array
	$arrTabla = array();
	for ($j = 0; $j < $ncolfil + 1; $j++) {
			for ($i = 0; $i < $ncolfil + 1; $i++) {
					$arrTabla[$j][$i] = "";
  		    $arrTabla[$i][$j] = "";			
			}
	}	

	//Rellenamos cabeceras del array
	$database->setQuery( "SELECT * FROM #__obrens_cab_detalles WHERE idobra='$refid' order by id");
	$rows1 = $database->loadObjectList();
  //Obra mid
  $cadena = $obrens_rutafich . $refid . $obrens_nomfich . $refid . $obrens_extmid;  
	if (file_exists($rutalocal . $cadena)) 
		 $arrTabla[0][0] = "<a href='" . $cadena . "'>" . LIT_OBRA . $obrens_extmid . "</a>";
	//Elmentos de la tabla
  foreach($rows1 as $rows1Aux) {
		$arrTabla[0][$rows1Aux->id + 1] = $rows1Aux->descripcion;
		$arrTabla[$rows1Aux->id + 1][0] = $rows1Aux->descripcion;
	}		

  //Rellenamos contenido del array  
	for ($j = 1; $j < $ncolfil + 1; $j++) {
			for ($i = 1; $i < $ncolfil + 1; $i++) {
				  $cadena = $obrens_rutafich . $refid . $obrens_nomfich . $refid.$j.$i . $obrens_extmid;
					if (file_exists($rutalocal . $cadena)) {
  					$contador++;
						$arrTabla[$j][$i] = "<a href='" . $cadena . "'><img src='components/com_obrens/images/cuerda.png'></a><br/>";
	  		    $arrTabla[$i][$j] = "<a href='" . $cadena . "'><img src='components/com_obrens/images/cuerda.png'></a><br/>";
  		    }
			}
	}	

  $arrTablaSal[0] = $contador;
  $arrTablaSal[1] = $arrTabla;
	return $arrTablaSal;
}	

# Ver ensayo de obra
function ShowObra($refid, $Itemid) {
  global $database;
  global $rutalocal;
  global $obrens_rutafich;
  global $obrens_nomfich;
  global $obrens_extpdf;
  global $obrens_extmid;
  global $obrens_extmus;  
  

  //echo "<table width='100%' border='0' cellspacing='1' cellpadding='4'><tr><td>";

	//CABECERA
  $database->setQuery( "SELECT * FROM #__obrens WHERE id='$refid'");
  $rows = $database->loadObjectList();
  foreach($rows as $row1) {  
		echo "<table class='contentpaneopen'>";
		echo '<tr><td class="contentheading">'.$row1->titulo.'</td></tr>';
		echo '</table><br/>';

		echo "<table class='contentpaneopen'><tr>";
    //Titulo
		echo "<td width='15%'>".LIT_TITULO.":</td>";
		echo "<td class='tituloobra'>$row1->titulo</td>";
    //Autor
		echo "</tr><tr>";
		echo "<td>".LIT_AUTOR.":</td>";
		echo "<td class='autorobra'>$row1->autor</td>";
		echo "</tr>";  
	}	
	
	$cadena = '';
	echo "<tr>";
	echo "<td><br/>Partitura:</td><td><br/>";

	//Partitura pdf
	$cadena = $obrens_rutafich . $refid . $obrens_nomfich . $refid . $obrens_extpdf;
	if (file_exists($rutalocal . $cadena)) {			
		echo "<a href='" . $cadena . "'>";
		echo "<img class='buttonheading' src='components/com_obrens/images/pdf.png'/>&nbsp;";
		echo LIT_PARTITURA.$obrens_extpdf;
		echo '</a>&nbsp;&nbsp;(en formato PDF)';
		echo '</td></tr>';
		echo "<tr><td></td><td>";
	}
	
  //Partitura mus  
	$cadena = '';
	$cadena = $obrens_rutafich . $refid . $obrens_nomfich . $refid . $obrens_extmus;
	if (file_exists($rutalocal . $cadena)) {
		echo "<a href='" . $cadena . "'>";
		echo "<img class='buttonheading' src='components/com_obrens/images/mus.png'/>&nbsp;";
		echo LIT_PARTITURA.'.mus';
		echo '</a>&nbsp;&nbsp;(en formato MUS)';
		echo '</td></tr>';
		echo "<tr><td></td><td>";
	}

	// Partitura mid
	$cadena = '';
  //Hallamos el numero de columnas que vamos a tener
  $database->setQuery( "SELECT count(id) as ncol FROM #__obrens_cab_detalles WHERE idobra='$refid'");
  $row = NULL;
  $database->loadObject( $row );
	$arrTablaEnt = darDetallesObra($refid, $row->ncol);
	$arrTabla = $arrTablaEnt[1];
	$cadena = trim($arrTabla[0][0]);
	if ($cadena != '') {
		echo "<img class='buttonheading' src='components/com_obrens/images/midi.png'/>&nbsp;";
		echo $cadena;
		echo "&nbsp;&nbsp;(en formato MIDI)";
		echo '</td></tr>';
	}
	echo "</td></tr>";
	echo ('</table>');

	//DETALLES	
  //Rellenamos el array con el contenido correspondiente si tiene contenido
  if ($arrTablaEnt[0] != 0) {
		//Dibujamos la tabla detalle
		echo "<br/>";
		echo "<br/>";
		echo "<p>Ficheros MIDI por cuerdas:</p>";
		echo "<table border='0' cellspacing='0' cellpadding='4' class='tabla_cuerdas'>";		
		for ($j = 0; $j < $row->ncol + 1; $j++) {
				echo "<tr>";
				for ($i = 0; $i < $row->ncol + 1; $i++) {
					if ($j == 0) {
						echo "<th>".$arrTabla[$j][$i]."</th>";
					}
					else {
						if ($i == 0) {
								echo "<td class='nombre_cuerda'>".$arrTabla[$j][$i]."</td>";
						}
						else {		
					    if ($arrTabla[$j][$i] != "")
								 echo "<td class='celda_rellena'>".$arrTabla[$j][$i]."</td>";
							else
								 echo "<td Class='celda_vacia'>".$arrTabla[$j][$i]."</td>";
						}	 
	        }
				}		 
	
				echo "</tr>";
		}	
		echo ('</table>');
	}
  ?>
	<div class="back_button">
	<a href='javascript:history.go(-1)'>
	<?php echo _BACK; ?>
	</a>
	</div>
  <?php
  return;
}




// Kategorie anzeige
function ViewCat($Itemid, $catid, $artref_showphone, $artref_showmail, $artref_showwebsite, $artref_showstatus, $artref_showbeschrlink) {
  global $database, $stati;
  $database->setQuery( "SELECT  * FROM #__categories WHERE section = 'com_obrens' AND published = '1' AND id='$catid'" );

  $row = $database->loadObjectList();
  foreach($row as $rows1) {
    echo "$rows1->id <b>$rows1->title</b><br /><font class='small'>$rows1->description</font><hr />";
  }
  echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
  $line=1;
  $database->setQuery("SELECT * FROM #__obrens WHERE published='1' AND catid='$catid' ORDER BY ordering, titulo");
  $srows = $database->loadObjectList();
  foreach($srows as $srow1) {
    $linecolor = ($line % 2) + 1;
    echo "<tr class='sectiontableentry".$linecolor."'>
	       <td width='100%' valign='top'>";
    echo "<table width='100%' border='0' cellspacing='0' cellpadding='4'>
	       <tr>";
    if ($artref_showpicture) echo "<td width='30%' valign='top' align='right'><img src='images/stories/$srow1->picture'></td>";
    echo "<td valign='top'>";
    echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>";
    
    echo "<b>$srow1->titulo</b>";
    echo "<br /><span class='small'>$srow1->autor</span>";
    echo "</td><td align='right'>";
    echo "</td></tr></table>";
    echo "<hr /><table width='100%' border='0' cellspacing='0' cellpadding='0'>";

	echo "<tr><td><img hspace=5 vspace=5 align='left' src='images/stories/$srow1->picture'></td><td valign=top> $srow1->beschreibung</td></tr>";
    echo "</table>";
    if ($artref_showbeschrlink) echo "<p align='right'><a class='readon' href='index.php?option=com_obrens&Itemid=$Itemid&func=fullview&refid=$srow1->id'>» ".ARTREF_MEHR."</a></p>";
    echo "</td></tr></table>";
    echo "</td></tr>";
    $line++;
  }
  echo "</tr></table><P />";
  echo "<p align='right'><strong><a class='back_button' href='index.php?option=com_obrens&Itemid=$Itemid&func=cats'>« "._BACK."</a></strong></p>";
  return;
}

//Einzelne Kategorie Inhalt Auflistung
function ShowCatList($Itemid, $vcatid, $artref_showphone, $artref_showmail, $artref_showwebsite, $artref_showstatus, $artref_showbeschrlink) {
 global $mosConfig_dbprefix; 
 global $database, $stati, $vcatid, $startpage, $artref_refperside;

 //Rodax
 $art_perpage = 100;//$artref_refperside;


		
 
  echo "<table width='100%' border='0' cellspacing='1' cellpadding='4'>";
  $columnspan = $artref_showstatus + 2;
  $database->setQuery( "SELECT  c.*, COUNT(DISTINCT s1.id) AS num"
  . "\nFROM #__categories AS c"
  . "\nLEFT JOIN #__obrens AS s1 ON s1.catid = c.id"
  . "\nWHERE c.section='com_obrens' AND c.published='1'"
  . "\nGROUP BY c.id"
  . "\nORDER BY c.ordering, c.title"
  );
  $rows = $database->loadObjectList();
  foreach($rows as $row1) {

  if($row1->id == $vcatid){  
	if ($row1->num > 0) {
	
      echo "<tr><td colspan=$columnspan><b>$row1->title</b><br /><font class='small'>$row1->description</font>";

# Feststellen der Anzahl der verfügbaren Datensätze
 $database->setQuery("SELECT COUNT(id) AS amount FROM ".$mosConfig_dbprefix."artref WHERE catid = $vcatid");
 $row = NULL;
 $database->loadObject( $row );
 $count = $row->amount;
 
 # Berechnen der Gesamtseiten
 $gesamtseiten = floor($count / $art_perpage);
 $seitenrest   = $count % $art_perpage;
 if ($seitenrest>0) {
  $gesamtseiten++;
 }
 # Feststellen der aktuellen Seite
 if (isset($startpage)) {
  if ($startpage>$gesamtseiten) {
    $startpage = $gesamtseiten;
  } else if ($startpage<1) {
    $startpage = 1;
  }
 } else {
  $startpage = 1;
 }
 echo "<p>".ARTREF_EintrinListA." ".$count." ".ARTREF_EintrinListB."<br>";
 echo "</p>";
 
# Limit und Seite Vor- & Rueckfunktionen
$start = ( $startpage - 1 ) * $art_perpage;		  
	  
	  echo "</td></tr>";

      $line=1;
      // kopfteil
      echo "<tr class='sectiontableheader'><td class='sectiontableheader'>&nbsp;</td><td class='sectiontableheader'>&nbsp;</td>";
      if ($artref_showstatus)   echo "<td class='sectiontableheader'>&nbsp;</td>";
      echo "</tr>";

      #Liste befüllen
      $database->setQuery("SELECT * FROM #__obrens WHERE published='1' AND catid='$row1->id' ORDER BY ordering, titulo LIMIT $start,$art_perpage");
      $srows = $database->loadObjectList();

      foreach($srows as $srow1) {
        $linecolor = ($line % 2) + 1;

        $AREF_titulo = "$srow1->titulo";
		
        echo "<tr class='sectiontableentry".$linecolor."'>";
        echo "<td valign='top' align='center'><a href='index.php?option=com_obrens&Itemid=$Itemid&func=fullview&refid=$srow1->id'>";
		if($srow1->picture==""){
    		echo "<img border='0' width='80' src='components/com_obrens/images/nopic.jpg' alt='$AREF_titulo - ".ARTREF_BILDKLICKEN."'></a>";

		}else{
	    	echo "<img border='0' width='70' src='images/stories/$srow1->picture' alt='$AREF_titulo - ".ARTREF_BILDKLICKEN."'></a>";

		}
		echo "</td>";
        echo "<td valign='top'>";
        if ($artref_showbeschrlink) {
         echo "<a href='index.php?option=com_obrens&Itemid=$Itemid&func=fullview&refid=$srow1->id'><b>$AREF_titulo</b></a><br>$srow1->autor";
        } else {
         echo "<b>$AREF_titulo</b><br>$srow1->autor";
        }
        echo "</td>";


        $i = $srow1->status;
        if ($artref_showstatus) echo "<td valign='top'>".ARTREF_STATUS.":<br>$stati[$i]</td>";
        echo "</tr>";
        $line++;

      }

      
    }
  }
  }
  echo "</table><P />";
 
 echo "<p align=\"center\">".ARTREF_Pages.": ";
 # Ausgeben der Seite zurueck Funktion
 $seiterueck = $startpage - 1;
 if ($seiterueck>0) {
   echo "<a href=".sefRelToAbs("index.php?option=com_obrens&Itemid=$Itemid&vcatid=$vcatid&func=viewcategory&startpage=$seiterueck")."><b>«</b></a> ";
   }
 
 #Ausgeben der einzelnen Seiten
  for ($i=1; $i <= $gesamtseiten; $i++) {
    if ($i==$startpage) {
      echo "$i ";
    } else {
      echo "<a href=".sefRelToAbs("index.php?option=com_obrens&Itemid=$Itemid&vcatid=$vcatid&func=viewcategory&startpage=$i").">$i</a> ";
    }
  }

 # Ausgeben der Seite vorwärts Funktion
 $seitevor = $startpage + 1;
 if ($seitevor<=$gesamtseiten) {
   echo "<a href=".sefRelToAbs("index.php?option=com_obrens&Itemid=$Itemid&vcatid=$vcatid&func=viewcategory&startpage=$seitevor")."><b>»</b></a> ";
   }
 echo "</p>";   

  echo "<p align='right'><hr><a class='back_button' href='index.php?option=com_obrens&Itemid=$Itemid'>« "._BACK."</a></p>";
  return;
}



###################################################################################
#CUERPO
###################################################################################
# Estilo de vista
if ($obrens_verlista=="1" AND $func=="") $func = "list";
if ($obrens_verlista=="2" AND $func=="") $func = "cats";

# Cabecera
RefHeader($Itemid);

# Contenido
switch ($func) {
  case 'viewcategory':
    ShowCatList($Itemid, $catid, $artref_showphone, $artref_showmail, $artref_showwebsite, $artref_showstatus, $artref_showbeschrlink);
    break;
  case 'cats':
    ShowCats($Itemid, $artref_showpicture);
    break;
  case 'list':
    ShowList($obrens_vercabecera, $artref_showbeschrlink, $obrens_ordenlista);
    break;
  case 'fullview':
  	ShowObra($refid, $Itemid);
    break;

  default:
    $database->setQuery( "SELECT  c.*, COUNT(DISTINCT s1.id) AS num"
    . "\nFROM #__categories AS c"
    . "\nLEFT JOIN #__obrens AS s1 ON s1.catid = c.id"
    . "\nWHERE c.section='com_obrens' AND c.published='1'"
    . "\nGROUP BY c.id"
    . "\nORDER BY c.ordering, c.titulo"
    );
    $rows = $database->loadObjectList();

    foreach($rows as $row1) {
      if ($row1->num > 0) {
        echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
        echo "<tr><td width='100%'><b>$row1->title</b><br /><font class='small'>$row1->description</font><hr /></td></tr>";
        $line=1;
        $database->setQuery("SELECT * FROM #__obrens WHERE published='1' AND catid='$row1->id' ORDER BY ordering, titulo");
        $srows = $database->loadObjectList();
        foreach($srows as $srow1) {
          $linecolor = ($line % 2) + 1;
          echo "<tr class='sectiontableentry".$linecolor."'><td width='100%' valign='top'>";
          echo "<table width='100%' border='0' cellspacing='0' cellpadding='4'><tr>";
          if ($artref_showpicture) echo "<td width='30%' valign='top' align='right'><img src='images/stories/$srow1->picture'></td>";
          echo "<td valign='top'>";
          echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>";

          echo "<b>$srow1->partiturapdf</b>";
          echo "<b>$srow1->titulo</b>";

          echo "<br /><span class='small'>$srow1->autor</span>";
          echo "</td><td align='right'>";
          $i = $srow1->status;
          if ($artref_showstatus) echo "<b>$stati[$i]</b>";
          echo "</td></tr></table>";

          echo "<hr /><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
          if ($artref_showphone) echo "<tr><td width='90'><b>".LIT_PARTITURAMUS."</b></td><td>$srow1->partituramus</td></tr>";

          # Show Email
          if ($artref_showmail) {
            echo "<tr><td width='90'><b>".LIT_NCOLFIL."</b></td><td>";
            if ($srow1->ncolfil) {
              echo "<a href='mailto:$srow1->ncolfil'>$srow1->ncolfil</a>";
            } else {
              echo "-";
            }
            echo "</td></tr>";
          }
          echo "</table>";
          if ($artref_showbeschrlink) echo "<p align='right'><a class='readon' href='index.php?option=com_obrens&Itemid=$Itemid&func=fullview&refid=$srow1->id'>» ".ARTREF_MEHR."</a></p>";
          echo "</td></tr></table>";
          echo "</td></tr>";
          $line++;
        }
        echo "</tr></table><P />";
      }
    }
    break;
}

# Footer
if ($obrens_verfooter == "1")
	RefFooter($ObrEns_version);
	
?>