<?PHP

class values_ct_edit extends values_ct
{
	//related for sql table
	var $sqlgroup;
	var $client;
	var $lang;

	//for making url
	var $area;
	var $action;
	var $vid;//id beim bearbeiten, entspricht idvalues in mysqltabelle

	var $tpl_file;
	var $head_vars = array();
	var $body_vars = array();
	var $view;// use or dev (dev for more internal infos)
	var $prefix;// wenn dev , angezeigtes prefix der keys
	var $perm_edit;

	/**
	* KONSTRUKTOR - Konfiguration
	*
	*/
	function values_ct_edit($mixed = '')
	{
		global $dedi_lang, $vid;

		$this -> sqlgroup = $mixed['sqlgroup'];
		$this -> client = $mixed['client'];
		$this -> lang = $mixed['lang'];
		$this -> tpl_file = $mixed['tpl_file'];
		$this -> area = $mixed['area'];
		$this -> action = $mixed['action'];
		$this -> dedi_plugin = $mixed['dedi_plugin'];
		$this -> perm_edit = $mixed['perm_edit'];
		$this -> perm_type = ( empty($mixed['perm_type']) ) ? 'general': $mixed['perm_type'];
		$this -> extra_url_args = $mixed['extra_url_args'];

		$this -> head_vars['TABLE_PADDING'] = $mixed['table_cellpadding'];
		$this -> head_vars['TABLE_SPACING'] = $mixed['table_cellspacing'];
		$this -> head_vars['TABLE_BORDER'] =  $mixed['table_border'];

		$this -> view = (empty($mixed['view'])) ? 'use' : $mixed['view'];
		$this -> prefix = (empty($mixed['prefix'])) ? '$cfg' : $mixed['prefix'];
		//private
		$this -> head_vars['SET_ACTION'] = $dedi_lang['set_action'];
		$this -> vid = $vid;

		switch($this -> action)
		{
			case 'save_value':
				$this -> _save_entry();
		}


	}

	function start()
	{
		global $dedi_db, $db, $tpl;
		global $dedi_lang;

		$this -> _make_base_url();

		$tpl->loadTemplatefile($this -> tpl_file);

		$sql = "SELECT 		*
				FROM		". $dedi_db['values'] ."
				WHERE		group_name IN ('". $this -> sqlgroup ."')
				AND			idclient IN (". $this -> client .")
				AND			idlang IN (". $this -> lang .")
				AND			conf_visible = 1
				ORDER BY 	conf_sortindex";

		$db -> query($sql);


		// get and handle db values
		while($db -> next_record())
		{
			// Tabellenüberschriften
			if($db->f('conf_head_langstring')  ){
				$head_langstring = $db->f('conf_head_langstring');
				$this -> _make_row_head($head_langstring);
			}
			// Tabelleninhalt
			$this -> _make_row_body();
		}
		// Letzte Tabellenüberschrift
		$this -> _make_row_head($head_langstring);

	}

	function _make_row_head($head_langstring)
	{
		global $tpl, $dedi_lang;

		// ist nicht der erste Aufruf
		if(! empty($this -> head_vars['SET_HEADNAME']) ){
			$tpl->setVariable($this -> head_vars);
			$tpl->parse('TABLE');
		}
		//head template vorbereiten
		$this -> head_vars['SET_HEADNAME'] = $dedi_lang[$head_langstring];
		$this -> head_vars['LAY_DESCRIPTION'] = $dedi_lang['lay_description'];
	}

	function _make_row_body()
	{
		global $dedi_lang, $db, $tpl, $vid;

		//Werte auslesen
		$db_val['id'] = $db->f('idvalues');
		$db_val['key1'] = $db->f('key1');
		$db_val['key2'] = $db->f('key2');
		$db_val['key3'] = $db->f('key3');
		$db_val['key4'] = $db->f('key4');
		$db_val['value'] = $db->f('value');
		$db_val['conf_sortindex'] = $db->f('conf_sortindex');
		$db_val['conf_desc_langstring'] = $db->f('conf_desc_langstring');
		$db_val['conf_input_type'] = $db->f('conf_input_type');

		$row['ENTRY_DESC'] =  $this -> _format_value_desc($db_val);
		if ($this -> sqlgroup == 'cfg') {
	         	$row['ENTRY_BGCOLOR'] = '#C7D5EB';
			$row['OVERENTRY_BGCOLOR'] = '#A8BADE';
                 } else {
                 	$row['ENTRY_BGCOLOR'] = '#DBE3EF';
			$row['OVERENTRY_BGCOLOR'] = '#C7D5EB';
                 }

		// Ein Wert wird bearbeitet
		if($this -> action == 'edit' && $this -> vid == $db_val['id']){
			$row['ENTRY_VALUE'] =  $this -> _format_edit_field($db_val);
			$row['ENTRY_ACTIONS'] = $this -> _format_submit_buttons($db_val);
			$row['FORM_START'] = '<form action="main.php#val'.$db_val['id'].'" method="post">';
			$row['FORM_END'] = '</form>';
		}
		//Normale Anzeige der Werte
		else{
			$row['ENTRY_VALUE'] = $this -> _format_value($db_val);;
			$row['ENTRY_ACTIONS'] = $this -> _format_actions($db_val);
		}

		$tpl->setVariable($row);
		$tpl->parse('ENTRY');
	}

	function _save_entry()
	{
		global $value_to_save, $value_id, $perm;

		//erstmal deaktiviert
		//$perm -> check($this -> perm_edit, $this -> perm_type);

		if(is_array($value_to_save)){
			$v = '';
			$c = count($value_to_save);
			for($i=0; $i<$c; $i++)
			{
				if($i+1<$c){
					$v .= $value_to_save[$i] .',';
				}
				else{
					$v .= $value_to_save[$i];
				}
			}
			unset($value_to_save);
			$value_to_save = $v;
		}
		set_magic_quotes_gpc($value_to_save);
		$value_to_save = dedi_stripslashes($value_to_save);
		$this -> set_value(array(
								'id'    => $value_id,
								'value' => $value_to_save,
								'group' => $this -> sqlgroup
									));
	}

	function _format_edit_field($db_val)
	{
		global $sess;

		$text_field = '<input type="text" name="%s" value="%s" size="%s">';
		$textarea_field = '<textarea name="%s" cols="%s" rows="%s">%s</textarea>' . "\n";
		$hidden_field ='<input type="hidden" name="%s" value="%s">';
		$session = sprintf($hidden_field, $sess -> name, $sess -> id);
		$area = sprintf($hidden_field, 'area', $this -> area);
		if(! empty ($this -> dedi_plugin) ){
			$area .= sprintf($hidden_field, 'dedi_plugin', $this -> dedi_plugin);
			$area .= sprintf($hidden_field, 'idclient', $this -> client);

		}
		if(! empty ($this->extra_url_args)){
			$pairs = explode("&",substr($this->extra_url_args, 1));
			foreach($pairs AS $k => $v)
			{
				$thatsit = explode("=", $v);
				$area .= sprintf($hidden_field, $thatsit['0'], $thatsit['1']);
			}
		}
		$action = sprintf($hidden_field, 'action', 'save_value');
		$id = sprintf($hidden_field, 'value_id', $db_val['id']);

		$anchor = '<a name ="val' . $db_val['id'] .'">%s</a>';

		switch($db_val['conf_input_type'])
		{
			case 'txt':
				$form = sprintf($text_field, 'value_to_save', $db_val['value'], 100);
				break;
			case 'txtarea':
				$form = sprintf($textarea_field, 'value_to_save',100, 8, $db_val['value'], 100);
				break;
			case 'mutiple_groups':
				$form = $this -> _editfield_mutiple_groups($db_val['value']);
				break;
		}

		$form = sprintf($anchor, $form);
		$form .= $session . $area . $action .$id;
		return $form;

	}

	function _editfield_mutiple_groups($val)
	{
		global $DB_dedi, $dedi_db;

		$dbvals = &new DB_dedi;
		$val_array = split(',', $val);
		$sql = "SELECT idgroup, name FROM ". $dedi_db['groups'] ." WHERE idgroup NOT IN(1)";
		$dbvals -> query($sql);
		$options ='';
		while($dbvals -> next_record())
		{
			$val = $dbvals -> f('idgroup');
			$selected = (in_array($val, $val_array)) ? 'selected' : '';
			$desc = $dbvals -> f('name');
			$options .= "<option value =\"$val\" $selected >$desc</option>";
		}



		$form = '<select name="value_to_save[]" size="7" multiple>'. $options .'</select>';

		return $form;

	}

	function _format_submit_buttons($db_val)
	{
		global $dedi_lang, $cfg_dedi;

		$button_submit = '<input type ="image" onMouseOver="on(\''.$dedi_lang['set_submit'].'\');return true;"'
			.' onMouseOut="off();return true;" src="tpl/'.$cfg_dedi['skin'].'/img/but_ok.gif"'
			.' border="0" alt="'.$dedi_lang['set_submit'] .'"'
			.' title="'.$dedi_lang['set_submit'].'">';
		$button_cancel = '<a href="'.sprintf($this -> base_url, '',$db_val['id'], $db_val['id']).'"'
			.'onMouseOver="on(\'' .$dedi_lang['set_cancel'].'\');return true;" onMouseOut="off();return true;">'
			.'<img src="tpl/'.$cfg_dedi['skin'].'/img/but_cancel.gif" border="0"'
			.' alt="'.$dedi_lang['set_cancel'].'" title="'.$dedi_lang['set_cancel'].'"></a>';
		return $button_submit .'&nbsp;'. $button_cancel;
	}

	function _format_actions($db_val)
	{
		global $cfg_dedi, $dedi_lang;

		$edit_button =  '&nbsp;<a href="'. sprintf($this -> base_url, 'edit', $db_val['id'], $db_val['id'])
			.'" class="action" onMouseOver="on(\'' .$dedi_lang['set_edit'].'\');return true;" onMouseOut="off();return true;">'
			.'<img src="tpl/'.$cfg_dedi['skin'].'/img/but_edit.gif" border="0" alt="' .$dedi_lang['set_edit'].'"'
			.'title="' .$dedi_lang['set_edit'].'" width="18" height="14"></a>';
		return $edit_button;
	}

	function _make_base_url()
	{
		global $sess;

		//url erstellen zugriff später mit sprintf($url, value, value)
		$ar = 'area=' . $this -> area;
		if(! empty ($this -> dedi_plugin) ){
			$ar .= '&dedi_plugin=' . $this -> dedi_plugin;
			$ar .= '&idclient=' . $this -> client;
		}
		$ac = '&action=%s&vid=%s' ;
		$this -> base_url = $sess->url('main.php?'.$ar.$ac. $this -> extra_url_args).'#val%s';
	}

	function _format_value($db_val)
	{
		switch($db_val['conf_input_type'])
		{
			case 'txt':
				$value = htmlspecialchars($db_val['value']);
				break;
			case 'txtarea':
				$value = nl2br(htmlspecialchars($db_val['value']));
				break;
			case 'mutiple_groups':
				$value = $this -> _format_mutiple_groups($db_val['value']);
				break;
		}
		$to_return = '<a name ="val' . $db_val['id'] .'">'. $value .'</a>';
		return $to_return;
	}

	function _format_mutiple_groups($val)
	{
		global $DB_dedi, $dedi_db;

		if(empty($val)){
			return '-';
		}

		$dbvals = &new DB_dedi;
		$sql = "SELECT name FROM ". $dedi_db['groups'] ." WHERE idgroup IN($val)";

		$dbvals -> query($sql);
		$v = '';
		$c = $dbvals -> num_rows();
		$i =0;
		while($dbvals -> next_record())
		{
			if($i+1<$c){
				$v .= $dbvals -> f('name') .', ';
			}
			else{
				$v .= $dbvals -> f('name');
			}
			$i++;
		}

		return $v;
	}

	function _format_value_desc($db_val)
	{
		global $dedi_lang;

		if($this -> view == 'use'){
			return $dedi_lang[ $db_val['conf_desc_langstring'] ];
		}
		elseif($this -> view == 'dev'){
			$to_return = ( empty($db_val['key1']) ) ? '': $this -> prefix . '[\'' . $db_val['key1'] . '\']';
			$to_return .= ( empty($db_val['key2']) ) ? '': '[\'' . $db_val['key2'] . '\']';
			$to_return .= ( empty($db_val['key3']) ) ? '': '[\'' . $db_val['key3'] . '\']';
			$to_return .= ( empty($db_val['key4']) ) ? '': '[\'' . $db_val['key4'] . '\']';
			return $to_return;
		}
	}


}
?>