<?php
// File: $Id: inc.mod_edit.php,v 1.29.4.1 2004/09/27 15:24:25 roland Exp $
// +----------------------------------------------------------------------+
// | 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>                       |
// +----------------------------------------------------------------------+

/**
 * 1. Benötigte Funktionen und Klassen includieren
 */
include_once('inc/fnc.mod.php');
/**
 * 2. Eventuelle Actions/ Funktionen abarbeiten
 */
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;
if (is_numeric($idmod)) $perm->check(3, 'mod', $idmod);
else $perm->check(3, 'area_mod', 0);
$sql = "SELECT DISTINCT cat FROM ". $dedi_db['mod'];
$cat_arr = $rep->_fetch_query($sql);
/**
 * 3. Eventuelle Dateien zur Darstellung includieren
 */
include('inc/inc.header.php');
/**
 * 4. Bildschirmausgabe aufbereiten und ausgeben
 */
if (is_numeric($idmod)) {
    $modul = $rep->mod_data($idmod, $idclient);
}
if (is_array($s_modul)) {
    $modul = ( is_array($modul) ) ? array_merge($modul, $s_modul) : $s_modul;
    if ($sess->is_registered('s_modul')) $sess->unregister('s_modul');
}
if (is_array($modul)) {
    $modname = $modul['name'];
	$modverbose = $modul['verbose'];
    $modversion = $modul['version'];
    $modcat = $modul['cat'];
    $description = $modul['description'];
    $input =  $modul['input'];
    $output = $modul['output'];
    $source = $modul['source_id'];
    $repository_id =  $modul['repository_id'];
    $sql_install = dedi_stripslashes($rep->encode_sql($modul['install_sql']));
    $sql_uninstall = dedi_stripslashes($rep->encode_sql($modul['uninstall_sql']));
    $sql_update = dedi_stripslashes($rep->encode_sql($modul['update_sql']));
}
if (!is_numeric($idmod) && is_array($modul)) $errno = ($errno) ? $errno : '0400';
if (($err_i = $rep->mod_test($input, $idmod))) $error = sprintf($dedi_lang['err_0416'], 'Input', $err_i);
if (($err_o = $rep->mod_test($output, $idmod))) $error = ($error != '') ? $error . ';  ' . sprintf($dedi_lang['err_0416'], 'Output', $err_o) : sprintf($dedi_lang['err_0416'], 'Output', $err_o);

// Modul dublizieren
if($action == 'duplicate') {
    $idmod_for_form = '';
    if ($idclient >= 1) $modverbose = $dedi_lang['tpl_copy_of'] . $modname; 
    $source_for_form = $idmod;
} elseif($mod_no_wedding) {
    $idmod_for_form = '';
    $source_for_form = 0;
    $error .= $dedi_lang['err_0425'];
} else {
    $idmod_for_form = $idmod;
    $source_for_form = $source;
    if ($idmod) $noeeditname = 'readonly';
}
if  ($source_for_form > 0) {
	if ($idclient >= 1) $noeeditversion = 'readonly';
	$parent  = $rep->mod_data($source_for_form, 'all');
	if ($idclient >= 1) $modname = htmlspecialchars($parent['name']); else $modname = $dedi_lang['tpl_copy_of'] . $modname;
	$modname_version = (!empty($parent['version'])) ? ' (' . htmlspecialchars($parent['version']) . ')' : '';
	if (empty($modverbose)) $modverbose = htmlspecialchars($modname);
    $wedding = sprintf($dedi_lang['mod_no_wedding'], htmlspecialchars($modname), htmlspecialchars($modname_version));
}
 
// Menue
echo "<center>\n";
echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"98%\">\n";
echo "  <tr>\n";
// todo: check perm for mod->edit_sql
echo "    <td>&nbsp;";
if ($editsql != '1') 
	if ($idmod >= 1) if ($action == 'duplicate') echo $dedi_lang['area_mod_duplicate'];else echo $dedi_lang['area_mod_edit'];
    else echo $dedi_lang['area_mod_new'];
else echo $dedi_lang['area_mod_edit_sql'];
    echo "&nbsp;";
    if ($errno) echo "&nbsp;<span class=\"errormsg\">" . $dedi_lang["err_$errno"] . "</span>&nbsp;";
    if ($error) echo "&nbsp;<span class=\"errormsg\">" . $error . "</span>&nbsp;";
    echo "</td>\n";
    $tmp['BACK'] = "<a class=\"action\" href=\"" . $sess->url("main.php?area=mod&idclient=$idclient") . "\" onMouseOver=\"on('" . $dedi_lang['gen_back'] . "');return true;\" onMouseOut=\"off();return true;\">" . $dedi_lang['gen_back'] . "</a>&nbsp;";
    if ($editsql != '1' && $idmod >= 1 && empty($source_for_form)) {
        $tmp['NEW_SQL'] = "|&nbsp;<a class=\"action\" href=\"" . $sess->url("main.php?area=mod_edit&idmod=$idmod&idclient=$idclient&editsql=1") . "\" onMouseOver=\"on('" . $dedi_lang['mod_new_sql'] . "');return true;\" onMouseOut=\"off();return true;\">" . $dedi_lang['mod_new_sql'] . "</a>&nbsp;";
    } elseif ($idmod >= 1 && empty($source_for_form)) $tmp['NEW_SQL'] = "|&nbsp;<a class=action href=\"" . $sess->url("main.php?area=mod_edit&idmod=$idmod&idclient=$idclient&editsql=0") . "\" onMouseOver=\"on('" . $dedi_lang['mod_edit'] . "');return true;\" onMouseOut=\"off();return true;\">" . $dedi_lang['mod_edit'] . "</a>&nbsp;";

    echo "<td align=\"right\">&nbsp;" . $tmp['BACK'] . $tmp['NEW_SQL'] . "&nbsp;</td>";
    echo "  </tr>\n";
    echo "  <tr>\n";
    echo "    <td colspan=\"2\"><br>\n";
    // Formular zum bearbeiten der Module
    echo "    <form name=\"newmodule\" method=\"post\" action=\"" . $sess->url("main.php") . "\">\n";
    echo "    <input type=\"hidden\" name=\"area\" value=\"mod\">\n";
    echo "    <input type=\"hidden\" name=\"action\" value=\"save\">\n";
    echo "    <input type=\"hidden\" name=\"oldversion\" value=\"" . htmlspecialchars($modversion) . "\">\n";
    echo "    <input type=\"hidden\" name=\"idmod\" value=\"$idmod_for_form\">\n";
    echo "    <input type=\"hidden\" name=\"source\" value=\"$source_for_form\">\n";
    echo "    <input type=\"hidden\" name=\"idclient\" value=\"" . (string)$idclient . "\">\n";
    echo "    <input type=\"hidden\" name=\"repository_id\" value=\"" . (string)$repository_id . "\">\n";
    echo "    <input type=\"hidden\" name=\"mod_no_wedding\" value=\"" . $mod_no_wedding . "\">\n";
    if ($editsql != '1') {
        echo "    <input type=\"hidden\" name=\"install_sql\" value=\"" . htmlspecialchars($sql_install) . "\">\n";
        echo "    <input type=\"hidden\" name=\"uninstall_sql\" value=\"" . htmlspecialchars($sql_uninstall) . "\">\n";
        echo "    <input type=\"hidden\" name=\"update_sql\" value=\"" . htmlspecialchars($sql_update) . "\">\n";
    } else {
        $noeeditversion = $noeditfield = 'readonly';
        echo "    <input type=\"hidden\" name=\"input\" value=\"" . htmlspecialchars($input) . "\">\n";
        echo "    <input type=\"hidden\" name=\"output\" value=\"" . htmlspecialchars($output) . "\">\n";
        echo "    <input type=\"hidden\" name=\"modcat\" value=\"" . htmlspecialchars($modcat) . "\">\n";
    }
    if ($noeeditversion) {
        $noeeditname = $noeeditversion;
        echo "    <input type=\"hidden\" name=\"modversion\" value=\"" . htmlspecialchars($modversion) . "\">\n";
        echo "    <input type=\"hidden\" name=\"modverbose\" value=\"" . htmlspecialchars($modverbose) . "\">\n";
    }
    if ($noeeditname) {
        echo "    <input type=\"hidden\" name=\"modname\" value=\"" . htmlspecialchars($modname) . "\">\n";
    }
	if ($idclient == 0 && ($perm->have_perm(7, 'mod', $idmod) || $perm->have_perm(8, 'mod', $idmod)) && $source_for_form > 0) {
		echo "    <input type=\"hidden\" name=\"mod_no_wedding\" value=\"1\">\n";
	}
    echo "	 <table cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\">\n";
    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;"; if (!empty($source_for_form)) echo $dedi_lang["mod_parent"]; else echo $dedi_lang["mod_modulename"]; echo "&nbsp;</td>\n";
    if (!$noeeditname) echo "	     <td class=\"content\"><input type=\"text\" name=\"modname\" value=\"" . $modname . "\" size=\"90\" style=\"width:640\"></td>\n";
    else {
        echo "          <td class=\"content\" style=\"width:640\"><table cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\"><tr><td align=\"left\" style=\"width:600\">" . $modname . $modname_version . "</td><td align=\"right\" style=\"width:40\">";
        // Modul vom Original trennen
        if ($idclient >= 1 && ($perm->have_perm(7, 'mod', $idmod) || $perm->have_perm(8, 'mod', $idmod)) && $source_for_form > 0) echo "<a class=\"action\" href=\"" . $sess->url("main.php?area=mod_edit&idmod=" . $idmod . "&idclient=" . $idclient . "&mod_no_wedding=1&action=" . $action) . "\" onMouseOver=\"on('" . $dedi_lang['gen_parent'] . "');return true;\" onMouseOut=\"off();return true;\"><img src=\"tpl/" . $cfg_dedi['skin'] . "/img/but_resetform.gif\" border=\"0\" alt=\"" . $wedding . "\" title=\"" . $wedding . "\" width=\"18\" height=\"18\"></a>";
        echo "</td></tr></table></td>\n";
    }
    echo "      </tr>\n";
	if ($idclient >= 1 && $idmod >= 1) {
		echo "	   <tr valign=\"top\">\n";
		echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["mod_verbosename"] . "&nbsp;</td>\n";
		if ($editsql != '1') echo "	     <td class=\"content\"><input type=\"text\" name=\"modverbose\" value=\"" . htmlspecialchars($modverbose) . "\" size=\"90\" style=\"width:640\"></td>\n";
		else echo "	     <td class=\"content\">" . htmlspecialchars($modverbose) . "</td>\n";
		echo "      </tr>\n";
	}
	
    // rechte management
    // change JB: Kein Panel ohne Gruppen
    if (!empty($idmod) && $perm->have_perm(6, 'mod', $idmod) && $editsql != '1') {
        $panel = $perm->get_right_panel('mod', $idmod, array('formname' => 'newmodule'), 'text');
		if (!empty($panel)) {
        echo "      <tr>\n";
        echo "        <td class=\"head\">&nbsp;" . $dedi_lang['gen_rights'] . "&nbsp;</td>\n";
        echo "        <td class=\"content\">&nbsp;";
	        echo implode("", $panel);
        echo "      </tr>\n";
		}
    } 

    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["mod_version"] . "&nbsp;</td>\n";
    if (!$noeeditversion) echo "	     <td class=\"content\"><input $noeeditversion type=\"text\" name=\"modversion\" value=\"" . htmlspecialchars($modversion) . "\" size=\"90\" maxlength=\"60\" style=\"width:150\"></td>\n";
    else echo "	     <td class=\"content\">" . htmlspecialchars($modversion) . "</td>\n";
    echo "      </tr>\n";
    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["mod_cat"] . "&nbsp;</td>\n";
    echo "	     <td class=\"content\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td class=\"content\" align=\"left\" nowrap width=\"100%\">";
    if (!$noeditfield) echo "          <input type=\"text\" name=\"modcat\" value=\"" . htmlspecialchars($modcat) . "\" maxlength=\"60\" size=\"90\" style=\"width:150\"></td>\n";
    else echo htmlspecialchars($modcat) . "</td>\n";
    if (!$noeditfield) {
        echo "<td class=\"content\" align=\"right\" nowrap>".$mod_lang['gen_select'].":&nbsp;<select onchange=\"document.newmodule.modcat.value=(this.value);\" name=\"catchange\" maxlength=\"60\" size=\"1\" style=\"width:150\">\n";
        echo "<option value=\"\"></option>\n";
        foreach($cat_arr as $cat_art) {
            if (empty($cat_art['cat'])) continue;
            $cat_value = $cat_art['cat'];
            if($cat_value == '') continue;
            if($cat_value == htmlspecialchars($modcat)) $cat_selected = ' selected '; else $cat_selected = '';
            echo "<option value=\"".htmlspecialchars($cat_value)."\"$cat_selected>".htmlspecialchars($cat_value)."</option>\n";
        }
        echo "</select></td>\n";
    }
    echo "</tr></table></td>\n";
    echo "      </tr>\n";
    echo "      <tr valign=\"top\">\n";
    echo "        <td class=\"head\">&nbsp;" . $dedi_lang["mod_description"] . "&nbsp;</td>\n";
    echo "	     <td class=\"content\"><textarea $noeditfield name=\"description\" rows=\"3\" cols=\"52\" style=\"width:640\">" . htmlspecialchars($description) . "</textarea></td>\n";
    echo "	   </tr>\n";
    if ($editsql == '1') {
        echo "      <tr valign=\"top\">\n";
        echo "        <td class=\"head\">&nbsp;" . $dedi_lang["mod_sql_install"] . "&nbsp;</td>\n";
        echo "        <td class=\"content\"><textarea name=\"install_sql\" rows=\"14\" cols=\"52\" style=\"width:640\" wrap=\"off\">" . htmlspecialchars($sql_install) . "</textarea></td>\n";
        echo "	   </tr>\n";
        echo "      <tr valign=\"top\">\n";
        echo "        <td class=\"head\">&nbsp;" . $dedi_lang["mod_sql_uninstall"] . "&nbsp;</td>\n";
        echo "        <td class=\"content\"><textarea name=\"uninstall_sql\" rows=\"14\" cols=\"52\" style=\"width:640\" wrap=\"off\">" . htmlspecialchars($sql_uninstall) . "</textarea></td>\n";
        echo "	   </tr>\n";
        echo "      <tr valign=\"top\">\n";
        echo "        <td class=\"head\">&nbsp;" . $dedi_lang["mod_sql_update"] . "&nbsp;</td>\n";
        echo "        <td class=\"content\"><textarea name=\"update_sql\" rows=\"14\" cols=\"52\" style=\"width:640\" wrap=\"off\">" . htmlspecialchars($sql_update) . "</textarea></td>\n";
        echo "	   </tr>\n"; 
        // rechte import und export werden benötigt
        if ($idclient >= 1 && ($perm->have_perm(7, 'mod', $idmod) || $perm->have_perm(8, 'mod', $idmod))) {
            // Sql erneut installieren
            echo "  <tr>\n";
            echo "    <td class=\"head\" valign=\"middle\">&nbsp;" . $dedi_lang['gen_expand'] . "</td>\n";
            echo '    <td class="content"><input type="checkbox" name="mod_rebuild_sql" value="1" id="touchme"><label for="touchme">' . $dedi_lang['mod_rebuild_sql'] . '</label><img src="tpl/' . $cfg_dedi['skin'] . '/img/space.gif" border="0" width="18" height="18"></td>';
            echo "  </tr>\n";
        } 
    } else {
        echo "      <tr valign=\"top\">\n";
        echo "	     <td class=\"" . (($err_i) ? 'head_error' : 'head') . "\">&nbsp;" . $dedi_lang["mod_input"] . "&nbsp;</td>\n";
        echo "        <td class=\"content\"><textarea name=\"input\" rows=\"14\" cols=\"52\" style=\"width:640\" wrap=\"off\">" . htmlspecialchars($input) . "</textarea></td>\n";
        echo "	   </tr>\n";
        echo "      <tr valign=\"top\">\n";
        echo "        <td class=\"" . (($err_o) ? 'head_error' : 'head') . "\">&nbsp;" . $dedi_lang["mod_output"] . "&nbsp;</td>\n";
        echo "        <td class=\"content\"><textarea name=\"output\" rows=\"14\" cols=\"52\" style=\"width:640\" wrap=\"off\">" . htmlspecialchars($output) . "</textarea></td>\n";
        echo "	   </tr>\n";
        if (!empty($idmod) && $error) {
            // Modul trotz Fehler speichern
            echo "  <tr>\n";
            echo "    <td class=\"head\" valign=\"middle\">&nbsp;" . $dedi_lang['gen_expand'] . "</td>\n";
            echo '    <td class="content"><input type="checkbox" name="s_overide" value="1" id="s_overide"><label for="s_overide">' . sprintf($dedi_lang['mod_s_overide'], (($err_i) ? (($err_o) ? 'Input/Output' : 'Input') : 'Output')) . '</label><img src="tpl/' . $cfg_dedi['skin'] . '/img/space.gif" border="0" width="18" height="18"></td>';
            echo "  </tr>\n";
        } 
    } 
    echo "      <tr>\n";
    echo "        <td>&nbsp;</td>\n";
    echo "        <td><a href=\"" . $sess->url("main.php?area=mod&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');

?>