<?php
// +----------------------------------------------------------------------+
// | 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.                         |
// |                                                                      |
// +----------------------------------------------------------------------+
// | Incfile to manage projects ( in teh DB named as clients ) and langs. |
// | Makes the graphical Interface for Administration -> Projects.        |
// +----------------------------------------------------------------------+
// | @Author Björn Brockmann                                              |
// +----------------------------------------------------------------------+


/******************************************************************************
 1. Benötigte Funktionen und Klassen includieren
******************************************************************************/
include_once('inc/fnc.clients.php');
include_once('inc/fnc.lang.php');
include_once('inc/fnc.tpl.php');
include_once('inc/fnc.con.php');


/******************************************************************************
 2. Eventuelle Actions/ Funktionen abarbeiten
******************************************************************************/
$perm->check('area_clients');
switch($action) 
{
	case 'save_new_client':  //neues Projekt
		$perm->check(1, 'area_clients', 0);
		clients_new_client($cid, $project_name, $newdesc, $newpath, $newurl, $with_dir, $newlang, $newlangdesc, $charset);
		if(empty($errno)){
			$collapse = $cid;
		}
		else{
			$action = 'new_project';
		}
		break;
	case 'save_edited_client':  //Projekt umbenennen
		$perm->check(3, 'clients', $cid);
		$errno = clients_rename_client($cid, $newname, $newdesc);
		break;
	case 'delete_client':  //Projekt löschen
		$perm->check(5, 'clients', $cid);
		$errno = clients_delete_client($cid);
		break;
	case 'save_new_lang':  // neue Sprache anlegen
		//collapse ist idclient
		$perm->check(18, 'clients', $collapse);
		$errno = lang_new_language($collapse, $newname, $newdesc, $charset);
		break;
	case 'save_edited_lang':  // Sprache umbenennen
		$perm->check(19, 'clientlangs', $idlang);
		$errno = lang_rename_language($idlang, $newname, $newdesc, $charset);
		break;
	case 'delete_lang':  // Sprache löschen
		//collapse ist idclient
		$perm->check(21, 'clientlangs', $idlang);
		$errno = lang_delete_language($collapse, $lid);
		break;
}
/******************************************************************************
 3. Eventuelle Dateien zur Darstellung includieren
******************************************************************************/


/******************************************************************************
 4. Bildschirmausgabe aufbereiten und ausgeben
******************************************************************************/
//
// GENERAL VALUES
//
$c_conf['url'] = $sess->url('main.php?area='.$area);
$c_conf['url_extern'] = $sess->url('main.php?collapse='. $collapse);
$c_conf['image_path'] = 'tpl/'.$cfg_dedi['skin'].'/img/';
$c_conf['style_project'] = '#C7D5EB';
$c_conf['style_project_over'] = '#A8BADE';
$c_conf['style_lang'] = '#DBE3EF';
$c_conf['style_lang_over'] = '#C7D5EB';
$c_conf['html_image'] = '<img src="'. $c_conf['image_path'] .'%s" alt="%s" title="%s" width="%s" height="%s" border="0" />';
$c_conf['html_link'] = '<a href="'. $c_conf['url'] .'&collapse='. $collapse .'%s" title="%s">%s</a>';
$c_conf['html_link_extern'] = '<a href="'. $c_conf['url_extern'] .'%s" title="%s">%s</a>';
$c_conf['html_link_new_project'] = '<a href="'. $c_conf['url'] .'&action=new_project" class="action" title="'.$dedi_lang['clients_new_client'].'">'.$dedi_lang['clients_new_client'].'</a>';
$c_conf['html_link_delete'] = '<a href="'. $c_conf['url'] .'&collapse='. $collapse .'%s" onClick="return delete_confirm()" title="%s">%s</a>';
$c_conf['html_link_collapse'] = '<a href="'. $c_conf['url'] .'&collapse=%s" title="%s">%s</a>';
$c_conf['html_formname'] = 'getit';
$c_conf['html_form'] = '<form action ="main.php" name ="'. $c_conf['html_formname'] .'" method ="post">
<input type="hidden" name="area" value="'.$area.'">
<input type="hidden" name="'. $sess-> name. '" value="'.$sess -> id.'">'."\n";
$c_conf['html_field_hidden'] = '<input type="hidden" name="%s" value="%s">';
$c_conf['html_field_text'] = '<input type="text" name="%s" value="%s" style="width:%spx" maxlength="%s">';
$c_conf['html_field_submit'] = '<input type="image" src="'.$c_conf['image_path'].'but_confirm.gif" border="0" title="'.$dedi_lang['clients_submit'].'">';

//Throw out dedi-header
include('inc/inc.header.php');
//Load templatefile
$tpl->loadTemplatefile('clients.tpl');

//
// START GENERATE HEADER BLOCK
// 
if(! empty($errno)){
	$tpl_data['ERR_MSG'] = $dedi_lang['err_' . $errno];
}
if(! empty($user_msg)){
	$tpl_data['USER_MSG'] = $dedi_lang[$user_msg];
}
$tpl_data['AREA_TITLE'] = $dedi_lang['clients_clients'];
$tpl_data['SUB_NAV_RIGHT'] = ( $perm->have_perm(2, 'area_clients', 1) ) ? $c_conf['html_link_new_project']: '';
$tpl_data['TABLE_PADDING'] = $cellpadding;
$tpl_data['TABLE_SPACING'] = $cellspacing;
$tpl_data['TABLE_BORDER'] = $border;
$tpl_data['TITLE'] = $dedi_lang['clients_headline'];
$tpl_data['DESCRIPTION'] = $dedi_lang['clients_desc'];
$tpl_data['ACTIONS'] = $dedi_lang['clients_actions'];
//Look for Errors 
if(! empty($errno)){
	$tpl_error['ERR_MSG'] = $dedi_lang['err_' . $errno];
}

$tpl -> setCurrentBlock('HEADER');
$tpl->setVariable($tpl_data);
$tpl->parseCurrentBlock();
unset($tpl_data);
//
// END GENERATE HEADER BLOCK
//

function clients_get_langrow($idclient, $idlang, $client_array, $c_conf)
{
	global $dedi_lang, $perm;
	
	$tpl_data['ENTRY_NAME'] = $client_array[$idclient]['langs'][$idlang]['name'];
	$tpl_data['ENTRY_DESCRIPTION'] = $client_array[$idclient]['langs'][$idlang]['desc'];
	$tpl_data['ENTRY_BGCOLOR'] = $c_conf['style_lang'];
	$tpl_data['OVERENTRY_BGCOLOR'] = $c_conf['style_lang_over'];
	
	if($perm->have_perm(19, 'clientlangs', $idlang) ){
		$tpl_data['ENTRY_EDIT'] = clients_get_imagelink('but_edit.gif', 'action=editlang&lid='.$idlang, $dedi_lang['clients_lang_edit'], 18, 14);
	}
	
	//If there is more then one lang, you can delete them
	if($client_array['num_langs'] > 1){
		if($perm->have_perm(21, 'clientlangs', $idlang) ){
			$tpl_data['ENTRY_DELETE'] = clients_get_imagedeletelink('but_delete.gif', 'action=delete_lang&lid='.$idlang, $dedi_lang['clients_lang_delete'], 13, 13);
		}
	}
	
	return $tpl_data;
}

function clients_get_langeditrow($idclient, $idlang, $client_array, $c_conf)
{	
	global $dedi_lang,$perm;
	
	$tpl_data['FORM_START']  = $c_conf['html_form'] . sprintf($c_conf['html_field_hidden'], 'action', 'save_edited_lang');
	$tpl_data['FORM_START'] .= sprintf($c_conf['html_field_hidden'], 'idlang', $idlang);
	$tpl_data['FORM_START'] .= sprintf($c_conf['html_field_hidden'], 'collapse', $idclient);
	$tpl_data['ENTRY_TITLEFIELD'] = sprintf($c_conf['html_field_text'], 'newname', $client_array[$idclient]['langs'][$idlang]['name'], 150, 70);
	$tpl_data['LANG_DESCFIELD'] = 'Beschreibung: ';
	$tpl_data['ENTRY_DESCFIELD'] = sprintf($c_conf['html_field_text'], 'newdesc', $client_array[$idclient]['langs'][$idlang]['desc'], 350, 150);
	$tpl_data['LANG_CHARSET'] = 'Sprachcodierung: ';
	$tpl_data['ENTRY_CHARSET'] = clients_get_charset_selectbox($client_array[$idclient]['langs'][$idlang]['charset']);

	if($perm->have_perm(22, 'clientlangs', $idlang) ){
		$panel = $perm->get_right_panel('clientlangs', $idlang, array( 'formname'=> $c_conf['html_formname'] ), 'img' );
		if (!empty($panel)) {
			$tpl_data['ENTRY_RIGHTS'] =  implode("", $panel);
		}
	}
	
	$tpl_data['ENTRY_SUBMIT'] = $c_conf['html_field_submit'];
	$tpl_data['ENTRY_CANCEL'] = clients_get_imagelink('but_cancel_delete.gif', '', $dedi_lang['clients_abort'], 14, 14);
	
	$tpl_data['ENTRY_BGCOLOR'] = $c_conf['style_lang'];
	$tpl_data['OVERENTRY_BGCOLOR'] = $c_conf['style_lang'];

	return $tpl_data;
}

function clients_get_charset_selectbox($selected_charset = '')
{
	global $dedi_lang;
	
	$my_charsets = array('iso-8859-1', 'iso-8859-2', 'iso-8859-3', 'iso-8859-4',
					'iso-8859-5', 'iso-8859-6', 'iso-8859-7', 'iso-8859-8', 'iso-8859-9',
					'iso-8859-10', 'iso-8859-11', 'iso-8859-12', 'iso-8859-13', 'iso-8859-14', 
					'iso-8859-15', 'utf-8');
	
	$out = '<select name="charset" size="1">' ."\n";
	
	foreach($my_charsets AS $v)
	{
		$sel = ($v == $selected_charset) ? 'selected': '';
		$out .= sprintf('  <option value="%s"  %s>%s</option>'. "\n", $v, $sel, $v);
	}
	
	$out .= '</select>' ."\n";
	
	return $out;
}


function clients_get_langnewrow($idclient, $client_array, $c_conf)
{	
	global $dedi_lang;
	
	$tpl_data['FORM_START']  = $c_conf['html_form'] . sprintf($c_conf['html_field_hidden'], 'action', 'save_new_lang');
	$tpl_data['FORM_START'] .= sprintf($c_conf['html_field_hidden'], 'collapse', $idclient);
	$tpl_data['ENTRY_TITLEFIELD'] = sprintf($c_conf['html_field_text'], 'newname', $dedi_lang['clients_lang_new'], 150, 70);
	$tpl_data['LANG_DESCFIELD'] = $dedi_lang['clients_desc'] .': ';
	$tpl_data['ENTRY_DESCFIELD'] = sprintf($c_conf['html_field_text'], 'newdesc', '', 350, 150);
	$tpl_data['LANG_CHARSET'] = $dedi_lang['clients_charset'] .': ';
	$tpl_data['ENTRY_CHARSET'] = clients_get_charset_selectbox();
	
	$tpl_data['ENTRY_SUBMIT'] = $c_conf['html_field_submit'];
	$tpl_data['ENTRY_CANCEL'] = clients_get_imagelink('but_cancel_delete.gif', '', $dedi_lang['clients_abort'], 14, 14);
	
	$tpl_data['ENTRY_BGCOLOR'] = $c_conf['style_lang'];
	$tpl_data['OVERENTRY_BGCOLOR'] = $c_conf['style_lang'];

	return $tpl_data;
}


function clients_get_projectrow($idclient, $client_array, $collapse, $c_conf)
{
	global $dedi_lang, $perm;
	
	//make collapse/ expand 
	if($client_array[$idclient]['have_childs']){
		if($collapse == $idclient) $lin = clients_get_collapselink('but_minus.gif', $dedi_lang['clients_collapse'], 0);
		else $lin = clients_get_collapselink('but_plus.gif', $dedi_lang['clients_expand'], $idclient);
	}
	else $lin = sprintf($c_conf['html_image'], 'space.gif', '', '', 16, 11);
	
	$tpl_data['PR_ENTRY_EXPANDER'] = $lin;
	$tpl_data['PR_ENTRY_NAME'] = htmlentities($client_array[$idclient]['name']);
	$tpl_data['PR_ENTRY_DESCRIPTION'] = htmlentities($client_array[$idclient]['desc']);
	$tpl_data['PR_ENTRY_BGCOLOR'] = $c_conf['style_project'];
	$tpl_data['PR_OVERENTRY_BGCOLOR'] = $c_conf['style_project_over'];
	if($perm->have_perm(18, 'clients', $idclient) ){
		$tpl_data['PR_LANG_NEW'] = clients_get_imagelink('but_newside.gif', 'action=newlang&cid='.$idclient, $dedi_lang['clients_make_new_lang'], 14, 14);
	}
	if($perm->have_perm(3, 'clients', $idclient) ){
		$tpl_data['PR_ENTRY_EDIT'] = clients_get_imagelink('but_edit.gif', 'action=editclient&cid='.$idclient, $dedi_lang['clients_modify'], 18, 14);
	}
	if($perm->have_perm(4, 'clients', $idclient) ){
		$tpl_data['PR_ENTRY_CONF'] = clients_get_imagelinkextern('but_config.gif', 'area=clients_config&cid='.$idclient, $dedi_lang['clients_config'], 15, 15);
	}
	if($client_array['num_clients'] > 1){
		if($perm->have_perm(5, 'clients', $idclient) ){
			$tpl_data['PR_ENTRY_DELETE'] = clients_get_imagedeletelink('but_delete.gif', 'action=delete_client&cid='.$idclient, $dedi_lang['clients_delete'], 13, 13);
		}
	}
	else $tpl_data['PR_ENTRY_DELETE'] = '';
	
	return $tpl_data;
}


function clients_get_projecteditrow($idclient, $client_array, $collapse, $c_conf)
{
	global $dedi_lang, $perm;
	
	//make collapse/ expand 
	if($client_array[$idclient]['have_childs']){
		if($collapse == $idclient) $lin = clients_get_collapselink('but_minus.gif', $dedi_lang['clients_collapse'], 0);
		else $lin = clients_get_collapselink('but_plus.gif', $dedi_lang['clients_expand'], $idclient);
	}
	else $lin = sprintf($c_conf['html_image'], 'space.gif', '', '', 16, 11);
	
	$tpl_data['PR_ENTRY_EXPANDER'] = $lin;
	$tpl_data['PR_ENTRY_BGCOLOR'] = $c_conf['style_project'];
	$tpl_data['PR_OVERENTRY_BGCOLOR'] = $c_conf['style_project_over'];

	$tpl_data['PR_FORM_START']  = $c_conf['html_form'] . sprintf($c_conf['html_field_hidden'], 'action', 'save_edited_client');
	$tpl_data['PR_FORM_START'] .= sprintf($c_conf['html_field_hidden'], 'cid', $idclient);
	$tpl_data['PR_FORM_START'] .= sprintf($c_conf['html_field_hidden'], 'collapse', $collapse);
	$tpl_data['PR_ENTRY_TITLEFIELD'] = sprintf($c_conf['html_field_text'], 'newname', htmlentities($client_array[$idclient]['name']), 150, 70);
	$tpl_data['PR_ENTRY_DESCFIELD'] = sprintf($c_conf['html_field_text'], 'newdesc', htmlentities($client_array[$idclient]['desc']), 350, 150);

	if($perm->have_perm(6, 'clients', $idclient) ){
		$panel = $perm->get_right_panel('clients', $idclient, array( 'formname'=> $c_conf['html_formname'] ), 'img' );
		if (!empty($panel)) {
			$tpl_data['PR_ENTRY_RIGHTS'] = implode("", $panel);
		}
	}
	
	$tpl_data['PR_ENTRY_SUBMIT'] = $c_conf['html_field_submit'];
	$tpl_data['PR_ENTRY_CANCEL'] = clients_get_imagelink('but_cancel_delete.gif', '', $dedi_lang['clients_abort'], 14, 14);
		
	return $tpl_data;
}

function clients_get_projectnewrow($c_conf)
{	
	global $dedi_lang;
	
	global $db, $dedi_db, $cfg_client; 
	global $project_name, $newdesc, $newpath, $newurl, $with_dir, $newlang, $newlangdesc, $charset; 
	
	$sql = "SELECT MAX(idclient) AS max FROM ". $dedi_db['clients'];
	$db->query($sql);
	$db->next_record();
	$nextclient = $db->f('max') + 1;
	$project_name = ($project_name == '') ? $nextclient . '. '. $dedi_lang['clients_client']: $project_name;
	$newpath = ($newpath == '') ? strrev(strstr(strrev(substr($cfg_client['path'],0,-1)),'/'))."projekt0".$nextclient."/" : $newpath;
	$newurl = ($newurl == '') ? strrev(strstr(strrev(substr($cfg_client['htmlpath'],0,-1)),'/'))."projekt0".$nextclient."/" : $newurl;

	$tpl_data['PR_ENTRY_BGCOLOR'] = $c_conf['style_project'];
	$tpl_data['PR_OVERENTRY_BGCOLOR'] = $c_conf['style_project_over'];

	$tpl_data['PRN_FORM_START']  = $c_conf['html_form'] . sprintf($c_conf['html_field_hidden'], 'action', 'save_new_client');
	$tpl_data['PRN_FORM_START']  .= sprintf($c_conf['html_field_hidden'], 'cid', $nextclient);
	$tpl_data['PRN_ENTRY_TITLEFIELD'] = sprintf($c_conf['html_field_text'], 'project_name', htmlentities($project_name), 150, 70);

	$tpl_data['PRN_DESC'] = $dedi_lang['clients_client_desc'] .': ';
	$tpl_data['PRN_DESCFIELD'] = sprintf($c_conf['html_field_text'], 'newdesc', htmlentities($newdesc), 350, 150);
	$tpl_data['PRN_PATH'] = $dedi_lang['clients_client_path'] .': ';
	$tpl_data['PRN_PATHFIELD'] = sprintf($c_conf['html_field_text'], 'newpath', htmlentities($newpath), 350, 150);
	$tpl_data['PRN_URL'] = $dedi_lang['clients_client_url'] .': ';
	$tpl_data['PRN_URLFIELD'] = sprintf($c_conf['html_field_text'], 'newurl', htmlentities($newurl), 350, 150);
	$tpl_data['PRN_WITH_DIR'] = $dedi_lang['clients_client_directory'] .': ';
	$tpl_data['PRN_WITH_DIRFIELD'] = '<input type="checkbox" name="with_dir" value="1" checked>';
	$tpl_data['PRN_LANG'] = $dedi_lang['clients_client_start_lang'] .': ';
	$tpl_data['PRN_LANGFIELD'] = sprintf($c_conf['html_field_text'], 'newlang', htmlentities($newlang), 350, 150);
	$tpl_data['PRN_LANG_DESC'] = $dedi_lang['clients_lang_desc'] .': ';
	$tpl_data['PRN_LANG_DESCFIELD'] = sprintf($c_conf['html_field_text'], 'newlangdesc', htmlentities($newlangdesc), 350, 150);
	$tpl_data['PRN_LANG_CHARSET'] = $dedi_lang['clients_lang_charset'] .': ';
	$tpl_data['PRN_CHARSETFIELD'] = clients_get_charset_selectbox($charset);
	
	$tpl_data['PRN_ENTRY_SUBMIT'] = $c_conf['html_field_submit'];
	$tpl_data['PRN_ENTRY_CANCEL'] = clients_get_imagelink('but_cancel_delete.gif', '', 'Abbrechen', 14, 14);
		
	return $tpl_data;
}




function clients_get_collapselink($button, $desc, $idclient)
{
	global $c_conf;

	$im = sprintf($c_conf['html_image'], $button, $desc, $desc, 16, 11);
	$lin = sprintf($c_conf['html_link_collapse'] , $idclient, $desc, $im);
	
	return $lin;
}

function clients_get_imagelink($button, $urlparms, $desc, $width = 16, $height= 11)
{
	global $c_conf;

	$im = sprintf($c_conf['html_image'], $button, $desc, $desc, $width, $height);
	$lin = sprintf($c_conf['html_link'] , '&'.$urlparms, $desc, $im);
	
	return $lin;
}

function clients_get_imagelinkextern($button, $urlparms, $desc, $width = 16, $height= 11)
{
	global $c_conf;

	$im = sprintf($c_conf['html_image'], $button, $desc, $desc, $width, $height);
	$lin = sprintf($c_conf['html_link_extern'] , '&'.$urlparms, $desc, $im);
	
	return $lin;
}


function clients_get_imagedeletelink($button, $urlparms, $desc, $width = 16, $height= 11)
{
	global $c_conf;

	$im = sprintf($c_conf['html_image'], $button, $desc, $desc, $width, $height);
	$lin = sprintf($c_conf['html_link_delete'] , '&'.$urlparms, $desc, $im);
	
	return $lin;
}


//get all necessary projectdata info
$client_array = clients_get_clients();


//new project
if($action == 'new_project'){
	$perm->check(2, 'area_clients', 0);
	$tpl_data = clients_get_projectnewrow($c_conf);
	$tpl -> setCurrentBlock('PROJECTNEW');
	$tpl->setVariable($tpl_data);
	$tpl->parseCurrentBlock();
	$tpl -> setCurrentBlock('ENTRY');
	$tpl->parseCurrentBlock();

	unset($tpl_data);		
}

if(is_array($client_array['order'])){
	foreach ($client_array['order'] AS $idclient)
	{
		
		if(! $perm->have_perm(1, 'clients', $idclient)) continue;
		//new language
		if($action == 'newlang' && $idclient == $cid && $is_checked != true){
			$perm->check(18, 'clients', $cid);
			$is_checked = true;
			$collapse = $idclient;
			$client_array[$idclient]['have_childs'] = true;
			$tpl_data = clients_get_langnewrow($idclient, $client_array, $c_conf);
			$tpl->setCurrentBlock('LANGEDIT');
			$tpl->setVariable($tpl_data);
			$tpl->setCurrentBlock('LANG');
			$tpl->parseCurrentBlock();
			unset($tpl_data);
		}
		
		//echo $client_array[$idclient]['name'];echo "<br><br>";
		if($collapse == $idclient && $client_array[$idclient]['have_childs'] && is_array($client_array[$idclient]['langs']['order']) ){
	
			foreach ($client_array[$idclient]['langs']['order'] AS $idlang)
			{
				if(! $perm->have_perm(17, 'clientlangs', $idlang)) continue;
				
				//edit language
				if($action == 'editlang' && $idlang == $lid){
					$perm->check(19, 'clientlangs', $lid);
					$tpl_data = clients_get_langeditrow($idclient, $idlang, $client_array, $c_conf);
					$tpl->setCurrentBlock('LANGEDIT');
				}
				//show langrow
				else{
					$tpl_data = clients_get_langrow($idclient, $idlang, $client_array, $c_conf);
					$tpl->setCurrentBlock('LANGSHOW');
				}
				$tpl->setVariable($tpl_data);
				$tpl->setCurrentBlock('LANG');
				$tpl->parseCurrentBlock();
				unset($tpl_data);
			}
		}
	
		if($action == 'editclient' && $idclient == $cid){
			$perm->check(3, 'clients', $cid);
			$tpl_data = clients_get_projecteditrow($idclient, $client_array, $collapse, $c_conf);
			$tpl -> setCurrentBlock('PROJECTEDIT');
			$tpl->setVariable($tpl_data);
			$tpl->parseCurrentBlock();
	
			unset($tpl_data);		
		}
		else{
			$tpl_data = clients_get_projectrow($idclient, $client_array, $collapse, $c_conf);
			$tpl -> setCurrentBlock('PROJECTSHOW');
			$tpl->setVariable($tpl_data);
			$tpl->parseCurrentBlock();
			unset($tpl_data);
		}
	
		$tpl -> setCurrentBlock('ENTRY');
		$tpl->parseCurrentBlock();
	
	}
}



?>