<?php
// File: $Id: inc.plug_edit.php,v 1.19.6.2 2004/09/27 14:24:39 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.plug.php');
/**
 * 2. Eventuelle Actions/ Funktionen abarbeiten
 */
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;
if (is_numeric($idplug)) $perm->check(3, 'plug', $idplug);
else $perm->check(3, 'area_plug', 0);
$sql = "SELECT DISTINCT cat FROM ". $dedi_db['plug'];
$cat_arr = $rep->_fetch_query($sql);
/**
 * 3. Eventuelle Dateien zur Darstellung includieren
 */
include('inc/inc.header.php');
/**
 * 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 ($editsql != '1') if ($idplug >= 1) echo $dedi_lang['area_plug_edit'];
    else (($name != '' || $import) ? print($dedi_lang['area_plug_new_import']) : print($dedi_lang['area_plug_new_create']));
    else echo $dedi_lang['area_plug_edit_sql'];
    echo "&nbsp;";
    if (is_numeric($idplug)) {
        $plugin = $rep->plug_data($idplug, $idclient);
        if (is_array($plugin)) {
            $plugname      = $plugin['name'];
            $plugversion   = $plugin['version'];
            $plugcat       = $plugin['cat'];
            $description   = $plugin['description'];
            $plugroot      = $plugin['root_name'];
            $plugindex     = $plugin['index_file'];
            $repository_id = $plugin['repository_id'];
            $sql_install   = $rep->plug_execute($idplug, 'this', 'get', 'install');
            $sql_uninstall = $rep->plug_execute($idplug, 'this', 'get', 'uninstall');
            $sql_update    = $rep->plug_execute($idplug, 'this', 'get', 'update');
            if ($idclient >= 1) $noeeditversion = 'readonly';
        } else $errno = '1600';
    } elseif ($name != '') {
        $filecontent = $rep->_file($cfg_dedi['dedi_path'] . 'plugins/' . $name . '/' . $name . '.dediplug');
        if(is_array($xml   = $rep->dedi_plug($filecontent))) {
            $plugname      = $xml['name'];
            $plugversion   = $xml['version'];
            $plugcat       = $xml['cat'];
            $repository_id = $xml['repository_id'];
            $description   = $xml['description'];
            $plugroot      = $xml['root_name'];
            $plugindex     = $xml['index_file'];
            $noeeditversion = 'readonly';
        } else $errno = '1613';
    } else {
        $plugroot = '';
        $newplugin = true;
        $errno = '1620';
    }
    // remove '-1' for no existing File
    $sql_install   = ($sql_install != '-1')   ? $sql_install : '';
    $sql_uninstall = ($sql_uninstall != '-1') ? $sql_uninstall : '';
    $sql_update    = ($sql_update != '-1')    ? $sql_update : '';
    if ($errno) echo "&nbsp;<span class=\"errormsg\">" . $dedi_lang["err_$errno"] . "</span>&nbsp;";
    echo "</td>\n";
    $tmp['BACK'] = "<a class=\"action\" href=\"" . $sess->url("main.php?area=plug&idclient=$client") . "\" onmouseover=\"on('" . $dedi_lang['gen_back'] . "');return true;\" onmouseout=\"off();return true;\">" . $dedi_lang['gen_back'] . "</a>&nbsp;";
    if ($editsql != '1' && $idplug >= 1 && $idclient == 0) {
        $tmp['NEW_SQL'] = "|&nbsp;<a class=\"action\" href=\"" . $sess->url("main.php?area=plug_edit&idplug=$idplug&idclient=$idclient&editsql=1") . "\" onmouseover=\"on('" . $dedi_lang['plug_new_sql'] . "');return true;\" onmouseout=\"off();return true;\">" . $dedi_lang['plug_new_sql'] . "</a>&nbsp;";
    } elseif($idplug >= 1 && $idclient == 0 && $editsql) $tmp['NEW_SQL'] = "|&nbsp;<a class=action href=\"" . $sess->url("main.php?area=plug_edit&idplug=$idplug&idclient=$idclient&editsql=0") . "\" onmouseover=\"on('" . $dedi_lang['plug_edit'] . "');return true;\" onmouseout=\"off();return true;\">" . $dedi_lang['plug_edit'] . "</a>&nbsp;";
    elseif(!$import && empty($idplug)) $tmp['NEW_SQL'] = "|&nbsp;<a class=\"action\" href=\"" . $sess->url("main.php?area=plug_edit&idplug=&idclient=$idclient&import=1") . "\" onmouseover=\"on('" . $dedi_lang['plug_import_path'] . "');return true;\" onmouseout=\"off();return true;\">" . $dedi_lang['plug_import_path'] . "</a>&nbsp;";
    #else $tmp['BACK'] = "<a class=\"action\" href=\"" . $sess->url("main.php?area=plug_edit&idplug=&idclient=$idclient") . "\" onmouseover=\"on('" . $dedi_lang['gen_back'] . "');return true;\" onmouseout=\"off();return true;\">" . $dedi_lang['gen_back'] . "</a>&nbsp;";
    $idclient = !$idclient ? '0' : $idclient;
    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 Reload
    echo "    <form name=\"reload\" method=\"post\" action=\"" . $sess->url("main.php?area=plug_edit&idclient=$idclient&newplugin=$newplugin") . "\">\n";
    echo "    <input type=\"hidden\" name=\"name\" value=\"$name\">\n";
    echo "    <input type=\"hidden\" name=\"import\" value=\"$import\">\n";
    echo "    </form>\n";
    // Formular zum bearbeiten der Plugins
    echo "    <form name=\"newplugin\" method=\"post\" action=\"" . $sess->url("main.php") . "\">\n";
    echo "    <input type=\"hidden\" name=\"area\" value=\"plug\">\n";
    echo "    <input type=\"hidden\" name=\"action\" value=\"save\">\n";
    echo "    <input type=\"hidden\" name=\"idplug\" value=\"$idplug\">\n";
    echo "    <input type=\"hidden\" name=\"repid\" value=\"$repository_id\">\n";
    echo "    <input type=\"hidden\" name=\"idclient\" value=\"$idclient\">\n";
    if ($editsql != '1' && !$newplugin) {
        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";
    } elseif (!$newplugin) {
        $noeeditversion = $noeditfield = 'readonly';
        echo "    <input type=\"hidden\" name=\"plugcat\" value=\"" . htmlspecialchars($plugcat) . "\">\n";
    }
    if ($noeeditversion) {
        echo "    <input type=\"hidden\" name=\"plugname\" value=\"" . htmlspecialchars($plugname) . "\">\n";
        echo "    <input type=\"hidden\" name=\"plugversion\" value=\"" . htmlspecialchars($plugversion) . "\">\n";
        echo "    <input type=\"hidden\" name=\"root_name\" value=\"" . htmlspecialchars($plugroot) . "\">\n";
        echo "    <input type=\"hidden\" name=\"index_file\" value=\"" . htmlspecialchars($plugindex) . "\">\n";
    }
    if ($editsql == '1' | $idclient >= 1) $noeditfield2 = 'readonly';
    echo "	 <table cellpadding=\"$cellpadding\" cellspacing=\"$cellspacing\" border=\"$border\">\n";
    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["plug_pluginname"] . "&nbsp;</td>\n";
    if (!$noeeditversion) echo "	     <td class=\"content\"><input $noeditfield type=\"text\" name=\"plugname\" value=\"" . htmlspecialchars($plugname) . "\" size=\"90\" style=\"width:640\"></td>\n";
    else echo "	     <td class=\"content\">" . htmlspecialchars($plugname) . "</td>\n";
    echo "      </tr>\n";

    //rechte bearbeiten
    if (!empty($idplug) && $perm->have_perm(6, 'area_plug', 0)){
    	$panel = $perm->get_right_panel('area_plug', $idplug, array('formname' => 'newplugin'), 'text', true);
		if (!empty($panel)) {
		    echo "	   <tr valign=\"top\">\n";
		    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang['gen_rights'] . "&nbsp;</td>\n";
		    echo "	     <td class=\"content\">".implode("", $panel)."</td>\n";
		    echo "      </tr>\n";
		}
    }    
    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["plug_version"] . "&nbsp;</td>\n";
    if (!$noeeditversion) echo "	     <td class=\"content\"><input $noeditfield type=\"text\" name=\"plugversion\" value=\"" . htmlspecialchars($plugversion) . "\" size=\"90\" maxlength=\"60\" style=\"width:150\"></td>\n";
    else echo "	     <td class=\"content\">" . htmlspecialchars($plugversion) . "</td>\n";
    echo "      </tr>\n";
    echo "	   <tr valign=\"top\">\n";
    echo "	     <td class=\"head\" width=\"110\" nowrap>&nbsp;" . $dedi_lang["plug_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=\"plugcat\" value=\"" . htmlspecialchars($plugcat) . "\" maxlength=\"60\" size=\"90\" style=\"width:150\"></td>\n";
    else echo htmlspecialchars($plugcat) . "</td>\n";
    if (!$noeditfield) {
        echo "<td class=\"content\" align=\"right\" nowrap>".$mod_lang['gen_select'].":&nbsp;<select onchange=\"document.newplugin.plugcat.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($plugcat)) $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["plug_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["plug_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["plug_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["plug_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";
    } else {
        echo "      <tr valign=\"top\">\n";
        echo "	     <td class=\"head\">&nbsp;" . $dedi_lang["plug_root_name"] . "&nbsp;</td>\n";
        if (!$newplugin) {
            if (!$noeeditversion) echo "        <td class=\"content\"><input $noeditfield2 type=\"text\" name=\"root_name\" value=\"" . htmlspecialchars($plugroot) . "\" maxlength=\"60\" size=\"90\" style=\"width:640\"></td>\n";
            else echo "        <td class=\"content\">plugins/" . htmlspecialchars($plugroot) . "</td>\n";
        } elseif($import) {
            $newliste = $rep->get_new_plugin_list();
            $oldliste = $rep->get_plugin_list();
            foreach($oldliste as $old) {
                $js_old .= "olds[\"" . htmlspecialchars($old['root_name']) . "\"] = " . intval($old['base']) . ";\n";
            } 
            echo "        <td class=\"content\"><select onchange=\"reloadthis(this.value);\" name=\"root_name\" style=\"width:640\">\n";
            echo "         <option value=\"" . htmlspecialchars($plugroot) . "\">" . htmlspecialchars($plugroot) . "</option>\n";
            foreach($newliste as $new) {
                if($new['root_name'] != $plugroot) {
                    $js_var .= "values[\"" . htmlspecialchars($new['root_name']) . "\"] = " . intval($new['xml']) . ";\n";
                    echo "         <option value=\"" . htmlspecialchars($new['root_name']) . "\">" . htmlspecialchars($new['root_name']) . "</option>\n";
                } 
            } 
            echo "        </select></td>\n";
            echo "<script language=\"JavaScript\">\n
              <!--\n
              function reloadthis(name) {\n
              var values = new Array();\n
              var olds   = new Array();\n";
            echo $js_old;
            echo $js_var;
            echo "   if ( values[name] == 1 ) {\n
                    document.reload.name.value=name;
                    document.reload.submit();\n
                 }
              }\n
              //-->\n
              </script>";
        } else {
            echo "        <td class=\"content\"><input type=\"text\" name=\"root_name\" value=\"\" maxlength=\"60\" size=\"90\" style=\"width:640\"></td>\n";
        } 
        echo "	   </tr>\n";
        echo "      <tr valign=\"top\">\n";
        echo "        <td class=\"head\">&nbsp;" . $dedi_lang["plug_index_file"] . "&nbsp;</td>\n";
        if (!$noeeditversion) echo "        <td class=\"content\"><input $noeditfield2 type=\"text\" name=\"index_file\" value=\"" . htmlspecialchars($plugindex) . "\" maxlength=\"60\" size=\"90\" style=\"width:640\"></td>\n";
    else {echo "        <td class=\"content\">"; if ($plugindex != '') echo "plugins/" . htmlspecialchars($plugroot) . '/' . htmlspecialchars($plugindex); echo "</td>\n";}
        echo "	   </tr>\n";
    } 
    echo "      <tr>\n";
    echo "        <td>&nbsp;</td>\n";
    echo "        <td><a href=\"" . $sess->url("main.php?area=plug&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');
    ?>
