<?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.css.php');
include('inc/class.filemanager.php');
$fm = &new filemanager();

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

$perm->check('area_css');
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 = css_'.$action.'();' );

	// Event
	$errlog  = ($errno != '1112' && $errno != '1113' && $errno != '1117' && $errno != '1116' && $errno) ? ', Fehler:' . $errno: '';
	$errrule = (preg_match('/(file)/i', $action) > 0) ? '': ', Rule: '.$idcss;
	fire_event('css_'.$action, array('idcss' => $idcssfile, 'errrule' => $errrule, 'errlog' => $errlog));
}

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

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

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

// Kopfbereich
$tmp['AREA_TITLE'] = (!$idclient && $perm->have_perm('29', 'area_css')) ? $dedi_lang['area_css_import']: $dedi_lang['area_css'];
$tmp['ERR_MSG']    = ($errno) ? $dedi_lang["err_$errno"]: '';
if ($css_warnings != '') {
	$tmp['CSS_WARN'] = preg_replace ( '/\-ele\-([^:]*):/i', "\\1", trim($css_warnings));
	$tmp['CSS_WARN'] = '<br>&nbsp;<span class="errormsg">'.str_replace ( ',', '<br />', $tmp['CSS_WARN']).'</span>&nbsp;';
}
$tmp['SUB_NAV_RIGHT'] = '';
if (!empty($idclient)) {
	if ($perm->have_perm('2', 'area_css')) {
		$tmp['SUB_NAV_RIGHT'] = $tmp['SUB_NAV_RIGHT'] . make_nav_link('action', "main.php?area=css_edit_file&idclient=$idclient", $dedi_lang['css_file_new'], $dedi_lang['css_file_new']);
	}
} else {
	$tmp['SUB_NAV_RIGHT'] = $tmp['SUB_NAV_RIGHT'] . make_nav_link('action', "main.php?area=css&idclient=$client&idexpand=$idexpand&idcssfile=$idcssfile", $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['css_filename'];
$tmp['DESCRIPTION'] = $dedi_lang['css_description'];
$tmp['ACTIONS']     = $dedi_lang['css_action'];
$tpl->setVariable($tmp);
unset($tmp);

//
// show the css-rules of a particular css-file, i.e. $idclient != 0
//
if ($idclient != 0) {
	// get css-files from db
	$tpl->setCurrentBlock('ENTRY');
	
	$db = $fm->get_file_list_by_filetype( $client, 'css', 'A.filename', 'ASC', '', 4 );
	// get_cssfile_list($client);
	
	$int_max = $db->affected_rows();
	for($i = 0; $i < $int_max; $i++) {
		$db->next_record();
		$_idcssfile = $db->f('idupl');
		if ($perm->have_perm('1', 'css_file', $_idcssfile)) {
			// Hintergrundfarbe wählen
			$tmp['ENTRY_BGCOLOR'] = '#C7D5EB';
			$tmp['ENTRY_BGCOLOROVER'] = '#A8BADE';

			// filename und description
			// jb - 04.09.2004 - added debug info: idcssfile of css-file
			$tmp['ENTRY_NAME'] = htmlentities($db->f('filename')) . (($cfg_dedi['debug_active']) ? ' (' . $_idcssfile . ')': '') ;
			// handle delete css-file
			if ($action == 50 && $_idcssfile == $idcssfile && $perm->have_perm('5', 'area_css')) {
				$tmp['ENTRY_NAME'] = $tmp['ENTRY_NAME'] . '&nbsp;&nbsp;' . make_image_link3 ("main.php?area=css&action=deletefile&idcssfile=".$_idcssfile."&idclient=$idclient",  'but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['css_file_delete_confirm'], $dedi_lang['css_file_delete_cancel'], 'action', 'deletethis' );
			}
			$tmp['ENTRY_DESCRIPTION'] = htmlspecialchars($db->f('description'));

			// buttons and actions
			$tmp['ENTRY_SHOWDETAIL'] = make_image_link2 ( '#', 'space.gif', '', 16, 11, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_NEW']		 = make_image_link2 ( '#', 'space.gif', '', 14, 14, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_EDIT']		 = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_DELBUT']	 = make_image_link2 ( '#', 'space.gif', '', 13, 13, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_DOWNLOAD']   = make_image_link2 ( '#', 'space.gif', '', 15, 15, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_DUPLICATE']  = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
			$tmp['ENTRY_IMEXPORT']   = make_image_link2 ( '#', 'space.gif', '', 19, 11, '', '', '', 'action', '', '', '', 'action' );

			// button: expand css-file
			if ($_idcssfile == $idexpand)
				$tmp['ENTRY_SHOWDETAIL'] = make_image_link2 ("main.php?area=css&idclient=$idclient", 'but_minus.gif', $dedi_lang['css_hiderules'], '16', '11', '', '', '', 'action', 'showthis');
			else
				$tmp['ENTRY_SHOWDETAIL'] = make_image_link2 ("main.php?area=css&idexpand=".$_idcssfile."&idclient=$idclient", 'but_plus.gif', $dedi_lang['css_showrules'], '16', '11', '', '', '', 'action', 'showthis');

			// button: new css-rule
			if ($perm->have_perm('18', 'css_file', $_idcssfile)) 
				$tmp['ENTRY_NEW'] = make_image_link2 ("main.php?area=css_edit&idcssfile=".$_idcssfile."&idcss=0&idclient=$idclient", 'but_newside.gif', $dedi_lang['css_new'], '14', '14', '', '', '', 'action', '', '', '', 'action');

			// button: download css-file
			if ($perm->have_perm('8', 'css_file', $_idcssfile)) {
				$tmp['ENTRY_DOWNLOAD'] = make_image_link2 ("main.php?area=css&action=downloadfile&idcssfile=".$_idcssfile."&idclient=$idclient&idexpand=$idexpand", 'but_download.gif', $dedi_lang['css_file_download'], '15', '15', '', '', '', 'action', '', '', '', 'action');
			}
			if ($perm->have_perm('3', 'css_file', $_idcssfile)) {
			// button: edit css-file
				$tmp['ENTRY_EDIT'] = make_image_link2 ("main.php?area=css_edit_file&idcssfile=".$_idcssfile."&idclient=$idclient&idexpand=$idexpand", 'but_edit.gif', $dedi_lang['css_file_edit'], '18', '14', '', '', '', 'action', '', '', '', 'action');
				// button: duplicate css-file, benötigt zusätzlich create rechte
			if ($perm->have_perm('2', 'area_css')) {
				$tmp['ENTRY_DUPLICATE'] = make_image_link2 ("main.php?area=css_edit_file&idcssfilecopy=".$_idcssfile."&idclient=$idclient&idexpand=$idexpand", 'but_duplicate.gif', $dedi_lang['css_file_duplicate'], '18', '14', '', '', '', 'action', '', '', '', 'action');
				}
			}
			if ($perm->have_perm('5', 'css_file', $_idcssfile)) {
			// button: delete css-file
				$tmp['ENTRY_DELBUT'] = make_image_link2 ("main.php?area=css&action=50&idexpand=$idexpand&idcssfile=".$_idcssfile."&idclient=$idclient", 'but_deleteside.gif', $dedi_lang['css_file_delete'], '13', '13', '', '', '', 'action', 'deletethis', '', '', 'action');
			}
			// button: import css-rules
			if ($perm->have_perm('29', 'css_file', $_idcssfile)) {
				$tmp['ENTRY_IMEXPORT'] = make_image_link2 ("main.php?area=css&idcssfile=".$_idcssfile."&idclient=0&idexpand=$idexpand", 'import.gif', $dedi_lang['css_import'], '19', '11', '', '', '', 'action', '', '', '', 'action');
			}

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

			// change the current block, to show all further entries below the details
			// we do not support viewing more than one css-file details
			if ($_idcssfile == $idexpand) $tpl->setCurrentBlock('PASTENTRY');
		}
	}

	// no css-files to display
	if ($int_max < 1) show_none( 'NODETAIL', 'content2a', $dedi_lang['css_nofile'] );
	else {
		//
		// show css-rules, if a css-file is in expanded view
		//
		if ($idexpand && $idexpand != -1 && $perm->have_perm('17', 'css_file', $idexpand)) {
			$tpl->setCurrentBlock('DETAIL');
			$db = get_all_cssrules_of_file( $client, $idexpand, 0, 2, $cfg_client['css_sort_original'] );
			$int_max = $db->affected_rows();
			for($i = 0; $i < $int_max; $i++) {
				$db->next_record();
				set_rule_data($db, 'export');
				$tpl->setVariable($tmp);
				$tpl->parseCurrentBlock();
				unset($tmp);
			}
			// no css-rules in the css-file to display
			if ($int_max < 1) show_none( 'NODETAIL', 'contenta', $dedi_lang['css_norules'] );
		}
	}
	unset($tmp);
}
//
// show the css-rules the user may import into a css-file, i.e. $idclient == 0
//
else {
 	if ($perm->have_perm('29', 'css_file', $idcssfile)) {
		// Zeige den Importberecih, wenn der User die Berechtigung zum Inport hat
	$tpl->setCurrentBlock('DETAIL');
	$db = get_all_cssrules_of_file( $idclient, 0, 0, 1 );
	$int_max = $db->affected_rows();
	for($i = 0; $i < $int_max; $i++) {
		$db->next_record();
		set_rule_data($db, 'import');
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}

	// no css-rules to display for import
	if ($db->affected_rows() == '0') show_none( 'NODETAIL', 'contenta', $dedi_lang['css_norulesforimport'] );
	unset($tmp);
	}
}


if ($perm->have_perm('9', 'area_css')) {
	// Zeige den File-Upload wenn der User die Berechtigung zum Upload hat
	$tpl->setCurrentBlock('FILEIMPORT');
	$tmp['FILEIMPORT_ACTION'] = $sess->url("main.php");
	$tmp['FILEIMPORT_TYPE']   = 'css';
	$tmp['FILEIMPORT_FUNC']   = 'uploadfile';
	$tmp['FILEIMPORT_DIR']    = 'cms/css/';
	$tmp['FILEIMPORT_CLIENT'] = $client;

	$tmp['FILEIMPORT_BGCOLOR'] = 'content2a';
	$tmp['FILEIMPORT_TEXT'] = $dedi_lang['css_fileimport'];
	$tmp['FILEIMPORT_NAME'] = 'cssuploadfile';
	$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);
}

//
// display error message if a rule has warnings
//
if ($ruleserror) show_none( 'RULESERROR', 'content3a', $dedi_lang['err_1118'] );
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 for displaying rule-records
//
function set_rule_data($db, $imorexport) {
	global $tmp, $tpl, $ruleserror, $action, $idcss, $idcssfile, $idexpand, $idclient, $dedi_lang, $cfg_dedi, $perm, $sess;

	if ($perm->have_perm('17', 'css_file', $idexpand)) {
		// Zeige alle CSS-regeln, wenn der User die Berechtigung zum Anzeigen von CSS-Regeln hat
		// background-color of css-rules
		$tmp['DETAIL_BGCOLOR'] = ($db->f("status") == 1) ? '#DBE3EF':'#EFE3DB';
		$tmp['DETAIL_BGCOLOROVER'] = ($db->f("status") == 1) ? '#C7D5EB':'#EFE3DB';
		if (!$db->f('status')) $ruleserror = '1';

		// buttons
		$tmp['DETAIL_EDIT']		 = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
		$tmp['DETAIL_DELBUT']	 = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
		$tmp['DETAIL_DUPLICATE'] = make_image_link2 ( '#', 'space.gif', '', 13, 13, '', '', '', 'action', '', '', '', 'action' );
		$tmp['DETAIL_EXIMPORT']	 = make_image_link2 ( '#', 'space.gif', '', 19, 11, '', '', '', 'action', '', '', '', 'action' );

		// css-rule name and description
		// jb - 29.08.2004 - added details as tooltip for comfort
		// jb - 04.09.2004 - added debug info: idcss of rule
		$tmp['DETAIL_NAME'] = '<a class="action" href="javascript:void(0)" title="' . htmlspecialchars ($db->f('styles'), ENT_QUOTES) . '">' . htmlentities($db->f('type')) . htmlentities($db->f('name')) . (($cfg_dedi['debug_active']) ? ' (' . $db->f('idcss') . ')': '') . '</a>';
		// handle delete css-rule
		if ($action == 30 && $db->f('idcss') == $idcss && $perm->have_perm('21', 'css_file', $idexpand)) {
			$tmp['DETAIL_NAME'] = $tmp['DETAIL_NAME'] . '&nbsp;&nbsp;' . make_image_link3 ("main.php?area=css&action=deleterule&idcssfile=$idcssfile&idexpand=$idexpand&idcss=".$db->f('idcss')."&idclient=$idclient", 'but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['css_delete_confirm'], $dedi_lang['css_delete_cancel'], 'action', 'deletethis' );
		}
		$tmp['DETAIL_DESCRIPTION'] = htmlspecialchars($db->f('description'));

		// buttons
		if ($perm->have_perm('19', 'css_file', $idexpand)) {
			$url = ($imorexport == 'export') ? "&idexpand=$idexpand&idcssfile=$idexpand" : "&idcssfile=$idcssfile";
			// button: edit css-rule
			$tmp['DETAIL_EDIT']      = make_image_link2 ("main.php?area=css_edit".$url."&idcss=".$db->f('idcss')."&idclient=$idclient", 'but_edit.gif', $dedi_lang['css_edit'], '18', '14', '', '', '', 'action', '', '', '', 'action');
			// button: duplicate css-rule, benötigt zusätzlich create rechte
			if ($perm->have_perm('18', 'area_css')) {
				$tmp['DETAIL_DUPLICATE'] = make_image_link2 ("main.php?area=css_edit&action=10".$url."&idcss=".$db->f('idcss')."&idclient=$idclient", 'but_duplicate.gif', $dedi_lang['css_duplicate'], '18', '14', '', '', '', 'action', '', '', '', 'action');
			}
		}
		if ($perm->have_perm('21', 'css_file', $idexpand)) {
			// button: delete css-rule
			$tmp['DETAIL_DELBUT']    = make_image_link2 ("main.php?area=css&action=30".$url."&idcss=".$db->f("idcss")."&idclient=$idclient", 'but_deleteside.gif', $dedi_lang['css_delete'], '13', '13', '', '', '', 'action', 'deletethis', '', '', 'action');
		}
		// button: export or import css-rule
		if (($perm->have_perm('29', 'css_file', $idcssfile) && $imorexport == 'import' && $idclient == 0) || ($perm->have_perm('30', 'css_file', $idexpand) && $imorexport == 'export' && $idclient != 0)) {
			$url = "&idexpand=$idcssfile&idcssfile=$idcssfile";
			$tmp['DETAIL_EXIMPORT'] = make_image_link2 ("main.php?area=css&action=$imorexport".$url."&idcss=".$db->f('idcss')."&idclient=$idclient", "$imorexport.gif", $dedi_lang['css_'.$imorexport], '19', '11', '', '', '', 'action', '', '', '', 'action');
		}
	}
}
?>
