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

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

include('inc/fnc.js.php');
include('inc/class.filemanager.php');
$fm = &new filemanager();

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

// Prüfe ob User diesen bereich betreten darf
if (empty($idjsfile)) 
	$perm->check(2, 'area_js');
else
	$perm->check(3, 'js_file', $idjsfile);
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;


if ($action && preg_match("/^\d/", $action) == 0) {
	eval( '$errno = js_'.$action.'();' );

	//Event
	$errlog  = ($errno) ? ', Fehler:' . $errno: '';
	fire_event('js_'.$action, array('idjsfile' => $idjsfile, 'errlog' => $errlog));
	if (empty($errno)) {
		header ('Status: 302 Moved Temporarily');
		header ('Location: '.$sess->url("main.php?area=js&idjsfile=$idjsfile&idclient=$idclient&errno=$errno"));
		exit;
	}
}

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

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

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

// Kopfbereich
$tmp['AREA_TITLE']    = $dedi_lang['area_js_edit_file'];
$tmp['ERR_MSG']       = ($errno) ? $dedi_lang["err_$errno"]: '';
$tmp['SUB_NAV_RIGHT'] = '&nbsp;';

// Tabellenformatierung
$tmp['TABLE_PADDING'] = $cellpadding;
$tmp['TABLE_SPACING'] = $cellspacing;
$tmp['TABLE_BORDER'] = $border;

// Templatedatei laden
$tpl->loadTemplatefile('js_edit_file.tpl');

$date_format = $cfg_dedi['FormatDate'] . ' ' . $cfg_dedi['FormatTime'];
// get data for editing
if (!$errno) {
	if ($idjsfile) $tmp_data = get_jscontent_data( $idjsfile, 1 );
	if ($tmp_data) {
		$jsfiledirname      = $tmp_data['dirname'];
		$jsfiledescription  = $tmp_data['description'];
		$jsfilecontent      = $tmp_data['filecontent'];
		// handle duplicate function
		if ($action == 10) {
			$idjsfile   = 0;
			$jsfilename = '';
		} else {
			$jsfilename         = $tmp_data['filename'];
			$jsfilecreated      = date($date_format, $tmp_data['created']);
			$jsfilelastmodified = date($date_format, $tmp_data['lastmodified']);
			$jsfileauthor       = $tmp_data['name'].' '.$tmp_data['surname'];
			$idjsfile           = $tmp_data['idjs'];
		}
	} else {
		$jsfilecreated      = '';
		$jsfilelastmodified = '';
		$jsfileauthor       = '';
		$idjsfile           = 0;
	}
	// Speziell für JS-Dateien wegen maskierten Zeichen notwendig
	if (get_magic_quotes_gpc() != 0) {
		$jsfilecontent = str_replace('\\', '\\\\', $jsfilecontent);
	}
	if (get_magic_quotes_gpc() == 0) {
		$jsfilecontent = str_replace('\\', '\\\\', $jsfilecontent);
	}
} else {
	// Speziell für JS-Dateien wegen maskierten Zeichen notwendig
	if (get_magic_quotes_gpc() == 0) {
		$jsfilecontent = str_replace('\\', '\\\\', $jsfilecontent);
	} else {
		remove_magic_quotes_gpc($jsfilecontent);
	}
}

/*
** start form
*/
$tmp['FORM_ACTION'] = $sess->url("main.php");
$tmp['IDJS']        = $idjsfile;
$tmp['IDCLIENT']    = $idclient;

/*
** name of the js-file
*/
$tmp['EDIT_JSFILENAME'] = $dedi_lang["js_filename"];
$tmp['EDIT_JSFILE']     = ($jsfilename && $idjsfile != 0) ? $jsfilename.'<input type="hidden" name="jsfilename" value="'.$jsfilename.'">': '<input class="field" type="text" maxlength="255" id="jsfilename" name="jsfilename" size="120" value="'.$jsfilename.'">';

/*
** description for the js-file
*/
$tmp['EDIT_JSFILEDESCNAME'] = $dedi_lang["js_description"];
$tmp['EDIT_JSFILEDESC_NAME'] = 'jsfiledescription';
$tmp['EDIT_JSFILEDESC_VALUE'] = $jsfiledescription;

/*
** textarea for rule content
*/
$tmp['EDIT_JSCODENAME'] = $dedi_lang["js_file_content"];
$tmp['EDIT_JSCODE_NAME'] = 'jsfilecontent';
$tmp['EDIT_JSCODE'] = $jsfilecontent;

/*
** button-related
*/
$tmp['BUTTON_CANCEL_URL']  = $sess->url("main.php?area=js&idclient=".$idclient);
$tmp['BUTTON_CANCEL_PICT'] = 'tpl/'.$cfg_dedi['skin'].'/img/but_cancel.gif';
$tmp['BUTTON_CANCEL_TEXT'] = $dedi_lang['js_cancel_form'];
$tmp['BUTTON_SUBMIT_PICT'] = 'tpl/'.$cfg_dedi['skin'].'/img/but_ok.gif';
$tmp['BUTTON_SUBMIT_TEXT'] = $dedi_lang['js_submit_form'];

$tpl->setVariable($tmp);
unset($tmp);

/*
** show right management
*/
if (!empty($idjsfile) && !empty($idclient) && $perm->have_perm(6, 'js_file', $idjsfile)) {
	$panel = $perm->get_right_panel('js_file', $idjsfile, array( 'formname'=>'editjsfile' ), 'text' );
	if (!empty($panel)) {
		$tpl->setCurrentBlock('JS_RIGHTS');
		$tmp['JS_RIGHTS_CONTENT'] = implode("", $panel);
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}
}

/*
** show js-managementinfo
*/
if (!empty($idjsfile)) {
	$tpl->setCurrentBlock('JS_MANAGEMENT');
	set_management_info($dedi_lang["js_created"]     , $jsfilecreated);
	set_management_info($dedi_lang["js_lastmodified"], $jsfilelastmodified);
	set_management_info($dedi_lang["js_editor"]      , $jsfileauthor);
	unset($tmp);
}

function set_management_info($topic, $value) {
	global $tmp, $tpl;

	$tmp['JS_MANAGEMENT_TOPIC']   = $topic;
	$tmp['JS_MANAGEMENT_CONTENT'] = $value;
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
}

?>