<?
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
			      Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
******************************************************************************/

include('inc/fnc.lay.php');

if(is_numeric($idlay)) $perm->check(3, 'lay', $idlay);
else $perm->check(3, 'area_lay', 0);

include('inc/inc.header.php');


echo "<center>\n";
echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"98%\">\n";
echo "  <tr>\n";
echo "    <td>&nbsp;".$dedi_lang['area_lay_edit']."&nbsp;";
if ($errno) echo "&nbsp;<span class=\"errormsg\">".$dedi_lang["err_$errno"]."</span>&nbsp;";
echo "</td>\n";
echo "  </tr>\n";
echo "  <tr>\n";
echo "    <td colspan=\"2\"><br>\n";

// Layout aus der Datenbank suchen
if ($idlay) {
	$sql = "SELECT * FROM $dedi_db[lay] WHERE idlay='$idlay'";
	$db->query($sql);
	$db->next_record();
	$layname = htmlspecialchars($db->f('name'));
	$description = htmlspecialchars($db->f('description'));
	$code = htmlspecialchars($db->f('code'));
}
else{
	$code = $cfg_client['default_layout'];
}

// Layout dublizieren
if($action == 'duplicate'){
	$idlay_for_form = '';
	$layname = $dedi_lang['lay_copy_of'] . $layname;
}
else{
	$idlay_for_form = $idlay;
}

// Benutzen CSS-Dateien in Array schreiben
$sql = "SELECT B.idupl FROM $dedi_db[lay_upl] A LEFT JOIN $dedi_db[upl] B USING(idupl) LEFT JOIN $dedi_db[filetype] C ON B.idfiletype=C.idfiletype WHERE idlay='$idlay' AND C.filetype='css'";
$db->query($sql);
while ($db->next_record()) {
	$used_files['css'][] = $db->f('idupl');
}

// Benutzen JS-Dateien in Array schreiben
$sql = "SELECT B.idupl FROM $dedi_db[lay_upl] A LEFT JOIN $dedi_db[upl] B USING(idupl) LEFT JOIN $dedi_db[filetype] C ON B.idfiletype=C.idfiletype WHERE idlay='$idlay' AND C.filetype='js'";
$db->query($sql);
while ($db->next_record()) {
	$used_files['js'][] = $db->f('idupl');
}

// Formular zum bearbeiten des Layouts
echo "    <form name=\"newlayout\" method=\"post\" action=\"".$sess->url("main.php?area=lay&action=save&idlay=$idlay_for_form&idclient=$idclient")."\">\n";
echo "    <table cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\">\n";
echo "      <tr valign=\"top\">\n";
echo "        <td class=\"head\" width=\"110\" nowrap>&nbsp;".$dedi_lang['lay_layoutname']."&nbsp;</td>\n";
echo "        <td class=\"content\"><input type=\"text\" name=\"layname\" value=\"$layname\" size=\"90\" style=\"width:640\"></td>\n";
echo "      </tr>\n";
//
// rechte management
// change JB: Kein Panel ohne Gruppen
if (!empty($idlay) && !empty($idclient) && $action != 'duplicate' && $perm->have_perm(6, 'lay', $idlay)) {
	$panel = $perm->get_right_panel('lay', $idlay, array( 'formname'=>'newlayout' ), 'text' );
	if (!empty($panel)) {
	echo "      <tr>\n";
	echo "        <td class=\"head\">&nbsp;</td>\n";
	echo "        <td class=\"content\">";
		echo implode("", $panel);
	echo "      </tr>\n";
	}
}
echo "      <tr valign=\"top\">\n";
echo "        <td class=\"head\">&nbsp;".$dedi_lang['lay_description']."&nbsp;</td>\n";
echo "        <td class=\"content\"><textarea name=\"description\" rows=\"3\" cols=\"52\" style=\"width:640\">$description</textarea></td>\n";
echo "      </tr>\n";
echo "      <tr valign=\"top\">\n";
echo "        <td class=\"head\" rowspan=\"2\">&nbsp;".$dedi_lang['lay_cmshead']."&nbsp;</td>\n";
echo "        <td class=\"content\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"640\">\n";
echo "          <tr>\n";
echo "            <td width=\"320\" class=\"content\">&nbsp;".$dedi_lang['lay_css']."&nbsp;</td>\n";
echo "            <td width=\"320\" class=\"content\">&nbsp;".$dedi_lang['lay_js']."&nbsp;</td>\n";
echo "          </tr>\n";
echo "        </table></td>\n";
echo "      </tr>\n";
echo "      <tr valign=\"top\">\n";
echo "        <td class=\"content\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"640\">\n";
echo "          <tr>\n";

// Stylesheet-Dateien suchen
$sql = "SELECT A.idupl, A.filename, A.description FROM $dedi_db[upl] A left join $dedi_db[filetype] B on A.idfiletype=B.idfiletype WHERE A.idclient='$client' AND B.filetype='css' ORDER BY A.filename";
$db->query($sql);
echo "            <td width=\"320\" class=\"content\"><select name=\"css[]\" multiple size=\"5\" style=\"width: 320px;\">\n";
if ($db->affected_rows()) {
	if (is_array($used_files['css'])) echo "              <option value=\"0\">&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
	else echo "              <option value=\"0\" selected>&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
	while ($db->next_record()) {
		if (is_array($used_files['css'])) {
			if (in_array($db->f('idupl'),$used_files['css'])) printf ("              <option value=\"".$db->f('idupl')."\" selected>&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
			else printf ("              <option value=\"".$db->f('idupl')."\">&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
                 } else printf ("              <option value=\"".$db->f('idupl')."\">&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
	}
} else echo "              <option value=\"0\" selected>&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
echo "            </select></td>\n";

// Javascript-Dateien suchen
$sql = "SELECT A.idupl, A.filename, A.description FROM $dedi_db[upl] A left join $dedi_db[filetype] B on A.idfiletype=B.idfiletype WHERE A.idclient='$client' AND B.filetype='js' ORDER BY A.filename";
$db->query($sql);
echo "            <td width=\"320\" class=\"content\"><select name=\"js[]\" multiple size=\"5\" style=\"width: 320px;\">\n";
if ($db->affected_rows()) {
	if (is_array($used_files['js'])) echo "              <option value=\"0\">&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
	else echo "              <option value=\"0\" selected>&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
	while ($db->next_record()) {
		if (is_array($used_files['js'])) {
			if (in_array($db->f('idupl'),$used_files['js'])) printf ("              <option value=\"".$db->f('idupl')."\" selected>&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
			else printf ("              <option value=\"".$db->f('idupl')."\">&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
                 } else printf ("              <option value=\"".$db->f('idupl')."\">&nbsp;".$db->f('filename')."%s&nbsp;</option>\n", ($db->f('description')) ? ' - '.$db->f('description') : '');
	}
} else echo "              <option value=\"0\" selected>&nbsp;".$dedi_lang['lay_nofile']."&nbsp;</option>\n";
echo "            </select></td>\n";
echo "          </tr>\n";
echo "        </table></td>\n";
echo "      </tr>\n";
// Quellcode
echo "      <tr valign=\"top\">\n";
echo "        <td class=\"head\">&nbsp;".$dedi_lang['lay_code']."</td>\n";
echo "        <td class=\"content\"><textarea name=\"code\" rows=\"26\" cols=\"52\" style=\"width:640\" wrap=\"off\">$code</textarea></td>\n";
echo "      </tr>\n";
// Buttons
echo "      <tr>\n";
echo "        <td>&nbsp;</td>\n";
echo "        <td><a href=\"".$sess->url("main.php?area=lay&idclient=$idclient")."\"><img src=\"tpl/".$cfg_dedi['skin']."/img/but_cancel.gif\" border=\"0\"></a>&nbsp;<input type=\"image\" src=\"tpl/".$cfg_dedi['skin']."/img/but_ok.gif\" border=\"0\"></td>\n";
echo "      </tr>\n";
echo "    </table>\n";
echo "    </form>\n";
include('tpl/'.$cfg_dedi['skin'].'/footer.php');
?>