<?php
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
			      Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
  Last update : 2005-11-24    - Bugfix for multibyte charsets                            
  Part of     : Update to Version 01.00.02 
******************************************************************************/

/******************************************************************************
 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
******************************************************************************/

$perm->check('area_js');
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;
// if $action is named prepare a function call
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));
}

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

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

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

// Kopfbereich
$tmp['AREA_TITLE']    = (empty($idclient) && $perm->have_perm(13, 'area_js', '0')) ? $dedi_lang['area_js_import']: $dedi_lang['area_js']; // import rechte gesetzt?
$tmp['ERR_MSG']       = (empty($errno)) ? '': $dedi_lang["err_$errno"];
$tmp['SUB_NAV_RIGHT'] = '';
if ($idclient) {
	// Im Client-Bereich ... Buttons je nach Rechten
	if ($perm->have_perm(2, 'area_js')) { // edit rechte gesetzt?
		$end = ($perm->have_perm(13, 'area_js')) ? '&nbsp;|&nbsp;': '&nbsp;'; // import rechte gesetzt?
		$tmp['SUB_NAV_RIGHT'] = $tmp['SUB_NAV_RIGHT'] . make_nav_link('action', "main.php?area=js_edit_file&idclient=$client", $dedi_lang['js_file_new'], $dedi_lang['js_file_new'], $end);
	}
	if ($perm->have_perm(13, 'area_js')) { // import rechte gesetzt?
		$tmp['SUB_NAV_RIGHT'] = $tmp['SUB_NAV_RIGHT'] . make_nav_link('action', "main.php?area=js&idclient=0", $dedi_lang['js_import'], $dedi_lang['js_import']);
	}
} else {
	// Im Import-Bereich ... nur der Button "Zurück"
	$tmp['SUB_NAV_RIGHT'] = $tmp['SUB_NAV_RIGHT'] . make_nav_link('action', "main.php?area=js&idclient=$client", $dedi_lang['gen_back'], $dedi_lang['gen_back']);
}

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

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

//
// headline
//
$tmp['FILENAME'] = $dedi_lang['js_filename'];
$tmp['DESCRIPTION'] = $dedi_lang['js_description'];
$tmp['ACTIONS'] = $dedi_lang['js_action'];
$tpl->setVariable($tmp);
unset($tmp);

//
// show the js-files of a particular client, i.e. $idclient != 0
//
$tpl->setCurrentBlock('ENTRY');
if (!empty($idclient)) {
	// set client list
	$block = 'export';
	$nothing_text = $dedi_lang['js_nofile'];
  $_client = $client;
}
else {
	// set import list
	$block = 'import';
	$nothing_text = $dedi_lang['js_nofiles'];
  $_client = 0;
}
// query all js-file for the given $idclient
$db = (!empty($idclient)) ? get_jsfile_list($client): get_jsfile_list(0);
// show the list of js-files
$_found == 0;
while ($db->next_record()) {
  $_found += set_filedata($db, $block, $_client);
}
if ($_found == 0) {
  // no js-files found
  show_none( 'NODETAIL', 'contenta', $nothing_text );
}
unset($tmp);

// generate js-file upload
// Upload nur wenn wir im Projektbereich sind
if (!empty($idclient) && $perm->have_perm(9, 'area_js')) {	// upload rechte gesetzt??
	$tpl->setCurrentBlock('FILEIMPORT');
	$tmp['FILEIMPORT_ACTION'] = $sess->url("main.php");
	$tmp['FILEIMPORT_TYPE']   = 'js';
	$tmp['FILEIMPORT_FUNC']   = 'uploadfile';
	$tmp['FILEIMPORT_DIR']    = 'cms/js/';
	$tmp['FILEIMPORT_CLIENT'] = $client;

	$tmp['FILEIMPORT_BGCOLOR'] = 'content2a';
	$tmp['FILEIMPORT_TEXT'] = $dedi_lang['js_fileimport'];
	$tmp['FILEIMPORT_NAME'] = 'jsuploadfile';
	$tmp['FILEIMPORT_PICT'] = 'tpl/' . $cfg_dedi['skin'] . '/img/upl_upload.gif';
	$tmp['FILEIMPORT_HINT'] = $dedi_lang["upl_upload"];
 	$tmp['FILEIMPORT_BUTTONWIDTH']  = '15';
	$tmp['FILEIMPORT_BUTTONHEIGHT'] = '15';

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

//
// function for displaying a message if no rules or files available
//
function show_none( $block, $bgcolor, $content ) {
	global $tmp, $tpl;

	$tpl->setCurrentBlock($block);
	$tmp['BGCOLOR'] = $bgcolor;
	$tmp['CONTENT'] = $content;
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
}

function set_filedata($db, $imorexport, $idclient) {
	global $tmp, $tpl, $action, $idjsfile, $dedi_lang, $cfg_dedi, $perm, $sess;

	// variablen belegen
	$idjs = $db->f("idjs");
	// get perms for testing
	if (!empty($idclient)) {
		$test_type = 'js_file';
		$test_id   = $idjs;
    if (!$perm->have_perm(2, "js_file", $idjs)) return 0;
	} else {
		$test_type = 'area_js';
		$test_id   = '0';
	}
	// Hintergrundfarbe wählen
	$tmp['ENTRY_BGCOLOR'] = '#C7D5EB';
	$tmp['ENTRY_BGCOLOROVER'] = '#A8BADE';

	// filename und description
	$tmp['ENTRY_NAME'] = htmlentities($db->f('filename'));
	// handle delete css-file
	if ($action == 30 && $idjs == $idjsfile && $perm->have_perm(5, $test_type, $test_id)) {
		$tmp['ENTRY_NAME'] = $tmp['ENTRY_NAME'] . '&nbsp;&nbsp;' . make_image_link3 ("main.php?area=js&action=deletefile&idjsfile=".$idjs."&idupl=".$db->f('idupl')."&idclient=$idclient",  'but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['js_file_delete_confirm'], $dedi_lang['js_file_delete_cancel'], 'action', 'deletethis' );
	}
	$tmp['ENTRY_DESCRIPTION'] = htmlspecialchars($db->f('description'));

	// buttons vorbesetzen
	$tmp['ENTRY_DOWNLOAD']  = make_image_link2($url = '#', $image = 'space.gif', '', '15', '15', '', '', '', 'action', '', '', '', 'action');
	$tmp['ENTRY_DUPLICATE'] = make_image_link2($url = '#', $image = 'space.gif', '', '18', '14', '', '', '', 'action', '', '', '', 'action');
	$tmp['ENTRY_EDIT']      = make_image_link2($url = '#', $image = 'space.gif', '', '18', '14', '', '', '', 'action', '', '', '', 'action');
	$tmp['ENTRY_DELBUT']    = make_image_link2($url = '#', $image = 'space.gif', '', '13', '13', '', '', '', 'action', '', '', '', 'action');
	$tmp['ENTRY_IMEXPORT']  = make_image_link2($url = '#', $image = 'space.gif', '', '19', '11', '', '', '', 'action', '', '', '', 'action');

	// button: download js-file - wenn nicht im import
	if (!empty($idclient) && $perm->have_perm(8, $test_type, $test_id)) {
		$tmp['ENTRY_DOWNLOAD'] = make_image_link2 ("main.php?area=js&action=downloadfile&idjsfile=".$db->f('idupl')."&idclient=$idclient", 'but_download.gif', $dedi_lang['js_file_download'], '15', '15', '', '', '', 'action', '', '', '', 'action');
	}
  
	// buttons: edit, copy, 
	if ((!empty($idjs) && $perm->have_perm(3, $test_type, $test_id)) || (empty($idclient) && $perm->have_perm(3, $test_type, $test_id))) {
		// button: edit js-file
		$tmp['ENTRY_EDIT']   = make_image_link2 ("main.php?area=js_edit_file&idjsfile=".$idjs."&idclient=$idclient", 'but_edit.gif', $dedi_lang['js_file_edit'], '18', '14', '', '', '', 'action', '', '', '', 'action');
		// button: duplicate js-file -- benötigt auch Neu-Anlegen-Rechte
		if ($perm->have_perm(2, 'area_js'))
			$tmp['ENTRY_DUPLICATE']   = make_image_link2 ("main.php?area=js_edit_file&action=10&idjsfile=".$idjs."&idclient=$idclient", 'but_duplicate.gif', $dedi_lang['js_file_duplicate'], '18', '14', '', '', '', 'action', '', '', '', 'action');
	}
	// button: delete js-file
	if ($perm->have_perm(5, $test_type, $test_id)) {
		$tmp['ENTRY_DELBUT'] = make_image_link2 ("main.php?area=js&action=30&idjsfile=".$idjs."&idclient=$idclient", 'but_deleteside.gif', $dedi_lang['js_file_delete'], '13', '13', '', '', '', 'action', 'deletethis', '', '', 'action');
  }
  
	// button: export js-file
	$rights_okay = ($imorexport == 'import') ? $perm->have_perm(13, $test_type, $test_id): $perm->have_perm(14, $test_type, $test_id);
	if ($rights_okay && !empty($idjs) ) {
		$upl = ($imorexport == 'import') ? '&idupl='.$db->f('idupl'): '';
		$tmp['ENTRY_IMEXPORT']= make_image_link2 ("main.php?area=js&action=".$imorexport."&idjsfile=".$idjs.$upl."&idclient=$idclient", $imorexport.'.gif', $dedi_lang['js_'.$imorexport], '19', '11', '', '', '', 'action', '', '', '', 'action');
	}
  
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
  
  return 1;
}
?>
