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

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

if (empty($idcssfile)) 
	$perm->check(2, 'area_css', '0');
else
	$perm->check(3, 'css_file', $idcssfile);
	
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;

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

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

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

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

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

// Kopfbereich
$tmp['AREA_TITLE']    = empty($idcssfile) ? $dedi_lang['area_css_new_file'] : $dedi_lang['area_css_edit_file'];
$tmp['ERR_MSG']       = empty($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('css_edit_file.tpl');

// get data for editing
$date_format = $cfg_dedi['FormatDate'] . ' ' . $cfg_dedi['FormatTime'];

if (empty($errno)) {
	if (!empty($idcssfile) || !empty($idcssfilecopy)) {
		$idfile = (!empty($idcssfile)) ? $idcssfile: $idcssfilecopy;
		$tmp_data = $fm->get_file((int) $idfile, (int) $client);
	}
	if (!empty($tmp_data)) {
		$filedirname      = $tmp_data['dirname'];
		$filedescription  = $tmp_data['description'];
	    if (empty($idcssfilecopy)) {
	  		$filename         = $tmp_data['filename'];
	  		$filecreated      = date($date_format, $tmp_data['created']);
	  		$filelastmodified = date($date_format, $tmp_data['lastmodified']);
	  		$fileauthor       = $tmp_data['vorname'] . ' ' . $tmp_data['nachname'];
	    }
	} else {
		$filedirname      = 'cms/css/';
		$filecreated      = '';
		$filelastmodified = '';
		$fileauthor       = '';
		$idcssfile        = 0;
		$idcssfilecopy    = 0;
	}
} else {
	if (!empty($idcssfilecopy)) {
		$idfile = $idcssfilecopy;
		$tmp_data = $fm->get_file((int) $idfile, (int) $client);
		if (!empty($tmp_data)) {
			$filedirname      = $tmp_data['dirname'];
			$filedescription  = $tmp_data['description'];
		}
	} else {
		// get managementinfo
		$idfile = $idcssfile;
		$tmp_data = $fm->get_file((int) $idfile, (int) $client);
		if (!empty($tmp_data)) {
	  		$filecreated      = date($date_format, $tmp_data['created']);
	  		$filelastmodified = date($date_format, $tmp_data['lastmodified']);
	  		$fileauthor       = $tmp_data['vorname'] . ' ' . $tmp_data['nachname'];
		} else {
			$filecreated      = '';
			$filelastmodified = '';
			$fileauthor       = '';
		}
	}
}

/*
** set start of form
*/
$tmp['FORM_ACTION']   = $sess->url('main.php');
$tmp['CSS_FILE_ID']   = $idcssfile;
$tmp['CSS_EXPAND_ID'] = $idexpand;
$tmp['CSS_CLIENT_ID'] = (!empty($idclient)) ? $idclient: $client;
$tmp['CSS_FILE_DIR']  = $filedirname;
$tmp['CSS_COPY_ID']   = $idcssfilecopy;
/*
** name of css-file
*/
$tmp['CSS_FILE_NAME'] = $dedi_lang['css_filename'];
$tmp['CSS_FILE']      = ($idcssfile) ? $filename.'<input type="hidden" id="filename" name="filename" value="'.$filename.'">': '<input class="desc" type="text" maxlength="255" id="filename" name="filename" size="120" value="'.$filename.'">';

/*
** description for css-file
*/
$tmp['CSS_FILE_DESC_NAME'] = $dedi_lang['css_description'];
$tmp['CSS_FILE_DESC']      = $filedescription;

/*
** Cancel, Submit- and Reset-Button
*/
$tmp['BUT_RESET']       = 'tpl/'.$cfg_dedi['skin'].'/img/but_resetform.gif';
$tmp['BUT_RESET_TEXT']  = $dedi_lang['css_reset_form'];

$tmp['BUT_CANCEL']      = 'tpl/'.$cfg_dedi['skin'].'/img/but_cancel.gif';
$tmp['BUT_CANCEL_URL']  = $sess->url("main.php?area=css&idclient=$idclient");
$tmp['BUT_CANCEL_TEXT'] = $dedi_lang['css_cancel_form'];

$tmp['BUT_SUBMIT']      = 'tpl/'.$cfg_dedi['skin'].'/img/but_ok.gif';
$tmp['BUT_SUBMIT_TEXT'] = $dedi_lang['css_submit_form'];

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

/*
** SELECT-Listen
*/

/*
** multi-selection-list for css-rules
** use only related or free css-rules
*/
$tpl->setCurrentBlock('CSSRULES');
$tmp_dbresult = false;
$tmp_rules = "";
// check if user may import css-rules into file or generally in the area if the file is a new one
if ($perm->have_perm(29, 'css_file', $idcssfile) || (empty($idcssfile) && $perm->have_perm(29, 'area_css', '0'))) {
	// css-rules for import
	$db = get_all_cssrules_of_file( 0, 0, 0, 1);
	while ($db->next_record()) {
		$cssname = $db->f('type').$db->f('name').",";
		if ( strpos($tmp_rules, $cssname) === false ) {
			set_option_tag(-$db->f('idcss'), $db->f('type').$db->f('name'), '');
			$tmp_dbresult = true;
		}
		}
  unset($tmp);
  if ($tmp_dbresult) {
    $tpl->setCurrentBlock('CSSSELECT');
    $tmp['CSS_RULES_SELECT_NAME'] = $dedi_lang['css_rule_list_importable'];
    $tmp['CSS_RULES_SELECT'] = "<select multiple class=\"desc\" id=\"idcss[]\" name=\"idcss[]\" size=\"8\">";
    $tmp['CSS_RULES_SELECT_END'] = ($tmp_dbresult) ? '</select>': '&nbsp;'.$dedi_lang['css_rule_listempty'];
  	$tpl->setVariable($tmp);
  	$tpl->parseCurrentBlock();
  	unset($tmp);
	}
}

if (!empty($idcssfile) || !empty($idcssfilecopy)) {
	// used css-rules
	$tpl->setCurrentBlock('CSSRULES1');
	$tmp_dbresult = 0;
	if ($idfile) {
		$db = get_all_cssrules_of_file( $client, $idfile, 0, 2);
		while ($db->next_record()) {
			set_option_tag($db->f('idcss'), $db->f('type').$db->f('name'), " selected", '1');
			$tmp_dbresult = true;
		}
	}
	// select-tag must be written, if any option is present
	unset($tmp);
  if ($tmp_dbresult) {
    $tpl->setCurrentBlock('CSSSELECT1');
    $tmp['CSS_RULES_SELECT_NAME1'] = $dedi_lang['css_rule_list_used'];
    $tmp['CSS_RULES_SELECT1'] = "<select multiple class=\"desc\" id=\"idcss[]\" name=\"idcss[]\" size=\"8\">";
    $tmp['CSS_RULES_SELECT_END1'] = ($tmp_dbresult) ? '</select><br>&nbsp;'.$dedi_lang['css_rule_listhint']: '&nbsp;'.$dedi_lang['css_rule_listempty'];
  	$tpl->setVariable($tmp);
  	$tpl->parseCurrentBlock();
	unset($tmp);
  }
}


/*
** rechte management
*/
if (!empty($idcssfile) && !empty($idclient) && $perm->have_perm(6, 'css_file', $idcssfile)) {
	$panel = $perm->get_right_panel('css_file', $idcssfile, array( 'formname'=>'editcssfile' ), 'text' );
	if (!empty($panel)) {
		$tpl->setCurrentBlock('CSS_RIGHTS');
		$tmp['CSS_RIGHTS_CONTENT'] = implode("", $panel);
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}
}

/*
** show css-managementinfo
*/
if (!empty($idcssfile)) {
	$tpl->setCurrentBlock('CSS_MANAGEMENT');
	set_management_info($dedi_lang['css_created']     , $filecreated);
	set_management_info($dedi_lang['css_lastmodified'], $filelastmodified);
	set_management_info($dedi_lang['css_editor']      , $fileauthor);
	unset($tmp);
}


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

	$tmp['CSS_MANAGEMENT_TOPIC']   = $topic;
	$tmp['CSS_MANAGEMENT_CONTENT'] = $value;
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
}
//
// set_option_tag($value, $rule, $selected)
//
// creates an option tag in the template
//
function set_option_tag($value, $rule, $selected, $pos = '') {
	global $tmp, $tpl;

	$tmp['CSSRULE_VALUE'.$pos]    = $value;
	$tmp['CSSRULE'.$pos]          = $rule;
	$tmp['CSSRULE_SELECTED'.$pos] = $selected;
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
}

?>