<?PHP
//  Last update : 2005-11-24     - Bugfix for multibyte charsets
//  Part of     : Update to Version 01.00.02
// +----------------------------------------------------------------------+
// | Der Dirigent 1.0                                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 the dedi-group <dedi-group@der-dirigent.de>  |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License                 |
// |                                                                      |
// | This program is subject to the GPL license, that is bundled with     |
// | this package in the file LICENSE.TXT.                                |
// | If you did not receive a copy of the GNU General Public License      |
// | along with this program write to the Free Software Foundation, Inc., |
// | 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA               |
// |                                                                      |
// | This program is distributed in the hope that it will be useful,      |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
// | GNU General Public License for more details.                         |
// |                                                                      |
// +----------------------------------------------------------------------+
// | Authors: Roland Braband <stam@der-dirigent.de>,                      |
// |          Björn Brockmann <björn@der-dirigent.de>                     |
// |          Paul Eppner <paul@twomoons.de>                              |
// |          Sven Bräutigam <sven777b@der-dirigent.de>                   |
// +----------------------------------------------------------------------+

/**
 * 1. Benötigte Funktionen und Klassen includieren
 */

include('inc/fnc.tpl.php');
include('inc/fnc.mipforms.php');

/**
 * 2. Eventuelle Actions/ Funktionen abarbeiten
 */

// $idcat vorhanden, prüfen, ob Recht zum bearbeiten gegeben ist
if(is_numeric($idcat))$perm->check(3, 'cat', $idcat);

// Keine idcat, prüfen, ob recht auf neue Seite anlegen vorhanden ist
// jb ... 23.04.04 ... parentid des parent für rechte prüfung als id übergeben
//                     ermöglicht die prüfung ob in einer kategorie durch den user
//                     weitere kategorien angelegt werden dürfen.
//                     Rechtegruppe auf Untergruppe cat von area_con gesetzt.
else $perm->check(2, 'cat', $parent);

// Ordnerkonfiguration speichern
switch($action) {
	case 'save':  // Template bearbeiten
		con_config_folder_save($idcat, $idcatside, $idtpl, $view, $idtplconf, $description, $name, $parent, $area, $idlay);
		break;
	case 'change':  // Layout oder Modul wechseln
		$cconfig = tpl_change($idlay);
		break;
}

/**
 * 3. Eventuelle Dateien zur Darstellung includieren
 */

// getrennte Header für Backend und Frontendbearbeitung
if (empty($view)) include('inc/inc.header.php');
else {
	echo "<html>\n";
	echo "<head>\n";
	echo "  <title>der-dirigent ".$cfg_dedi['version']."</title>\n";
	echo "  <link rel=\"stylesheet\" type=\"text/css\" href=\"tpl/".$cfg_dedi['skin']."/css/styles.css\">\n";
	echo "  <script src=\"tpl/".$cfg_dedi['skin']."/js/standard.js\" type=\"text/javascript\"></script>\n";
	echo "</head>\n";
	echo "<body marginheight=\"0\" marginwidth=\"0\" leftmargin=\"0\" topmargin=\"0\" onload=\"off();return true;\">\n";
}

/**
 * 4. Bildschirmausgabe aufbereiten und ausgeben
 */

echo "<center>\n";
echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"98%\">\n";
echo "  <tr>\n";
echo "    <td>&nbsp;";
if(! $view) echo $dedi_lang['area_con_configcat']."&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";

// getrennte Formularaufrufe für Backend und Frontendbearbeitung
if (empty($view)) echo "  <form name=\"editform\" method=\"post\" action=\"".$sess->url('main.php?area=con_configcat')."\">\n";
else echo "  <form name=\"editform\" method=\"post\" action=\"".$sess->url('main.php?area=con_configcat&view='.$view)."\">\n";
echo "    <input type=hidden name=\"action\" value=\"save\">\n";
echo "    <input type=hidden name=\"idtplconf\" value=\"$idtplconf\">\n";
echo "    <input type=hidden name=\"idcat\" value=\"$idcat\">\n";
echo "    <input type=hidden name=\"parent\" value=\"$parent\">\n";
echo "  <table cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\">\n";

// Ordnername
if (!$action && $idcat) {
	$sql = "SELECT name, description FROM ".$dedi_db['cat_lang']." WHERE idcat='$idcat' AND idlang='$lang'";
	$db->query($sql);
	$db->next_record();
	$name = $db->f('name');
	$description = $db->f('description');
} else {
	remove_magic_quotes_gpc($name);
	remove_magic_quotes_gpc($description);
}
echo "    <tr>\n";
echo "      <td class=\"head\" width=\"110\" nowrap>&nbsp;".$dedi_lang['con_configcat_folder']."&nbsp;</td>\n";
echo "      <td class=\"content\"><input type=\"text\" name=\"name\" value=\"".htmlspecialchars($name)."\" size=\"90\" maxlength=\"255\" style=\"width: 640px;\"></td>\n";
echo "    </tr>\n";

// rechte management
if (!empty($idcat) && $perm->have_perm(6, 'cat', $idcat)) {
	$panel = $perm->get_right_panel('cat', $idcat, array( 'formname'=>'editform' ), 'text' );
	if (!empty($panel)) {
		echo "      <tr>\n";
		echo "        <td class=\"head\">&nbsp;</td>\n";
		echo "        <td class=\"content\">";
		echo implode("", $panel);
		echo "      </tr>\n";
	}
}

// Ordnerbeschreibung
echo "    <tr>\n";
echo "      <td class=\"head\" width=\"110\" valign=\"top\" nowrap>&nbsp;".$dedi_lang['con_configcat_description']."&nbsp;</td>\n";
echo "      <td class=\"content\"><textarea name=\"description\" rows=\"3\" cols=\"52\" style=\"width:640\">".htmlspecialchars($description)."</textarea></td>\n";
echo "    </tr>\n";

// Darf Templatekonfiguration betreten
$have_config_tpl_perm = (is_numeric($idcat)) ? $perm->have_perm(11, 'cat', $idcat) : $perm -> have_perm(11, 'area_con', 0);
echo "    <tr>\n";
echo "      <td class=\"head\" width=\"110\" nowrap>&nbsp;".$dedi_lang['con_template']."&nbsp;</td>\n";
if ($have_config_tpl_perm) echo "      <td class=\"content\" width=\"640\" nowrap><select name=\"idtpl\" size=\"1\" onchange=\"document.editform.action.value='changetpl';document.editform.submit();\">\n";
else echo "      <td class=\"content\" width=\"640\" nowrap><select name=\"idtpl\" size=\"1\">\n";

// konfiguriertes Template und Layout suchen
if ($idtplconf != '0' && !$idtpl && !$configtpl) {
	$sql = "SELECT B.idlay, B.idtpl FROM ".$dedi_db['tpl_conf']." A LEFT JOIN ".$dedi_db['tpl']." B USING(idtpl) WHERE idtplconf='$idtplconf'";
	$db->query($sql);
	$db->next_record();
	$idlay = $db->f('idlay');
	$idtpl = $db->f('idtpl');
	$configtpl = $idtpl;
} else {
	$sql = "SELECT idlay, idtpl FROM ".$dedi_db['tpl']." WHERE idtpl='$idtpl'";
	$db->query($sql);
	$db->next_record();
	$idlay = $db->f('idlay');
}
if (!$idtpl) echo "        <option value=\"0\" selected>".$dedi_lang['form_nothing']."</option>";

// Templates Auflisten
$sql = "SELECT idtpl, name FROM $dedi_db[tpl] WHERE idclient='$client' ORDER BY name";
$db->query($sql);
while ($db->next_record()) {
	if ($db->f('idtpl') == $idtpl) echo "<option value=\"".$db->f('idtpl')."\" selected>".$db->f('name')."</option>";
	else if ($perm -> have_perm(1, 'tpl', $db->f('idtpl'))) {
		echo "<option value=\"".$db->f('idtpl')."\">".$db->f('name')."</option>";
	}
}
echo "      </select></td>\n";
echo "    </tr>\n";
echo "    <input type=hidden name=\"configtpl\" VALUE=\"$configtpl\">";
echo "         <tr valign=\"top\">\n";
echo "      <td>&nbsp;</td>\n";
echo "      <td>&nbsp;</td>\n";
echo "    </tr>\n";
if ($have_config_tpl_perm && $idtpl) {
	echo "    <input type=hidden name=\"idlay\" VALUE=\"$idlay\">\n";

	// Module auflisten
	$list = browse_layout_for_containers($idlay);

	// Einstellungen suchen
	if ($configtpl == $idtpl) {
                $sql = "SELECT A.config, A.view, A.edit, B.container, C.name, C.input, C.idmod, C.version, C.verbose, C.cat, C.source_id, C.idmod
                        FROM $dedi_db[container_conf] A
                        LEFT JOIN $dedi_db[container] B USING(idcontainer)
                        LEFT JOIN $dedi_db[mod] C USING(idmod)
                        WHERE A.idtplconf='$idtplconf'";
	} else {
                $sql = "SELECT A.config, A.view, A.edit, B.container, C.name, C.input, C.idmod, C.version, C.verbose, C.cat, C.source_id, C.idmod
                        FROM $dedi_db[container_conf] A
                        LEFT JOIN $dedi_db[container] B USING(idcontainer)
                        LEFT JOIN $dedi_db[mod] C USING(idmod)
                        WHERE A.idtplconf='0' AND B.idtpl='$idtpl'";
        }
	$db->query($sql);
	while ($db->next_record()) {
                $container[$db->f('container')] = array ( $db->f('config'),      // value 0
		                                          $db->f('view'),        // value 1
		                                          $db->f('edit'),        // value 2
		                                          htmlspecialchars($db->f('name')),        // value 3
		                                          $db->f('input'),       // value 4
		                                          htmlspecialchars($db->f('idmod')),       // value 5
		                                          htmlspecialchars($db->f('version')),     // value 6
                                                          htmlspecialchars($db->f('verbose')),     // value 7
                                                          htmlspecialchars($db->f('cat')),         // value 8
                                                          htmlspecialchars($db->f('source_id')),   // value 9
                                                          htmlspecialchars($db->f('idmod')));      // value 10
        }
	if (is_array($container)) {
		ksort($container);
		foreach ($container as $key => $value) {
			if (is_array($list['id'])) {
				if (in_array($key, $list['id'])) {
					$input = $value['4'];
					// Containername
					$modname = ( (($value['7'] != '') ? $value['7'] : $value['3']) . ((empty($value['6'])) ? '' : ' (' . $value['6'] . ')') );
					$modtitel = ( ' ++ ' .$dedi_lang['gen_description'] . ' ++ &#10;' . (($value['8'] != '') ? $dedi_lang['gen_cat'] . ': ' . $value['8'] . ' &#10;' : '') .
                                                    (($value['7'] != '') ? $dedi_lang['gen_verbosename'] . ': ' . $value['7'] . ' &#10;' : '') .
                                                    (empty($value['9']) ? $dedi_lang['gen_name'] : $dedi_lang['gen_original']) . ': ' . $value['3'] . ' &#10;' .
                                                    (($value['6'] != '') ? $dedi_lang['gen_version'] . ': ' . $value['6'] . ' &#10;' : '') . 'IdMod: ' . $value['10'] );
					$modcursor = strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') ? 'hand' : 'pointer';
                                        echo "    <input type=\"hidden\" name=\"c$key\" value=\"".$value['5']."\">";
					echo "    <tr valign=\"top\">\n";
                                        printf ("        <td class=\"head\" rowspan=\"2\">&nbsp;%s&nbsp;</td>\n", (!empty($list[$key]['title'])) ? $list[$key]['title']:"$key. ".$dedi_lang['tpl_container']."");
					echo "      <td class=\"head\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"640\">\n";
					echo "        <tr>";
					echo "          <td class=\"head\"><img style=\"cursor:$modcursor\" src=\"tpl/" . $cfg_dedi['skin'] . "/img/about.gif\" border=\"0\" alt=\"" . $modtitel .
                                                "\" title=\"" . $modtitel . "\" width=\"16\" height=\"16\">&nbsp;".$modname."&nbsp;</td>";
					echo "          <td class=\"head\" align=\"right\">&nbsp;";
					echo "        <select name=\"cview$key\" size=\"1\">\n";
					printf ("          <option value=\"0\"%s>". $dedi_lang['gen_mod_active'] ."</option>\n", ($value['1'] == '0' || !$value['1']) ? ' selected':'');
					printf ("          <option value=\"-1\"%s>". $dedi_lang['gen_mod_deactive'] ."</option>\n", ($value['1'] == '-1') ? ' selected':'');
					echo "        </select>&nbsp;";
					echo "        <select name=\"cedit$key\" size=\"1\">\n";
					printf ("          <option value=\"0\"%s>". $dedi_lang['gen_mod_edit_allow'] ."</option>\n", ($value['2'] == '0' || !$value['2']) ? ' selected':'');
					printf ("          <option value=\"-1\"%s>". $dedi_lang['gen_mod_edit_disallow'] ."</option>\n", ($value['2'] == '-1') ? ' selected':'');
					echo "        </select>\n";
					echo "        </td></tr>";
					echo "      </table></td>\n";
					echo "    </tr>\n";
					echo "    <tr valign=\"top\">\n";
					echo "      <td class=\"content\" colspan=\"2\">";

					// Developer-Modul
					if (strpos($value['6'], 'dev') != false && $value['6'] != '') $input = '<p align="center" class="errormsg">'.$dedi_lang['tpl_devmessage']."</p>\n".$input;

					// Modulkonfiguration einlesen
					if ($cconfig) $tmp1 = preg_split("/&/", $cconfig[$key]);
					else $tmp1 = preg_split("/&/", $value['0']);
					$varstring = array();
					foreach ($tmp1 as $key1=>$value1) {
						$tmp2 = explode('=', $value1);
						foreach ($tmp2 as $key2=>$value2) $varstring["$tmp2[0]"]=$tmp2[1];
					}
					foreach ($varstring as $key3=>$value3) $dedi_mod['value'][$key3] = dedi_stripslashes(urldecode($value3));
					foreach ($value as $key4=>$value4) $dedi_mod['info'][$key4] = dedi_stripslashes(urldecode($value4));
					$input = str_replace("MOD_VAR", "C".$key."MOD_VAR" , $input);
					eval(' ?>'.$input);
					unset($dedi_mod['value'], $dedi_mod['info']);
					echo "</td>\n";
					echo "    </tr>\n";
				}
			}
		}
	}
}

// Abbrechen und Bestätigungsbutton
echo "    <tr>\n";
echo "      <td>&nbsp;</td>\n";

// Backend oder Frontend?
if (empty($view)) {
	echo "      <td><a href=".$sess->url('main.php?area=con')."><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";
} else {
	echo "      <td><a href=".$sess->url($cfg_client['htmlpath'].$cfg_client['contentfile']."?lang=$lang&idcat=$idcat&idcatside=$idcatside&view=$view")."><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');
?>