<?php
class DataEdit
{
	private $_table;
	private $_key;
	private $_val;
	private $_field_id;
	private $_field_name;
	private $_field_type;
	private $_field_action;
	private $_quantity;
	private $_select;
	private $_password;
	private $_pass;
	private $_form_id;
	private $_quantity_hidden;
	private $_field_value_hidden;
	private $_field_key_hidden;
	private $_upload_quantity;
	private $_field_upload;
	private $_cancel_button;
	private $_param;
	private $_field_tab;

	function DataEdit( $table , $key = '' , $val = '' , $adres = 'index.php' )
	{
		$this -> _table           = $table;
		$this -> _adres           = $adres;
		$this -> _quantity        = 0;
		$this -> _quantity_hidden = 0;
		if ( $key && $val )
		{
			$this -> _key = $key;
			$this -> _val = $val;
		}
	}

	function addPositionHidden( $key , $val )
  {
		$this -> _field_value_hidden[ $this -> _quantity_hidden ] = $val;
		$this -> _field_key_hidden[ $this -> _quantity_hidden ] 	= $key;
		$this -> _quantity_hidden ++;
	}

	function addCancelButton( $nazwa , $key = '' , $val = '' )
  {
		$this -> _cancel_button['nazwa'] 	= $nazwa;
		$this -> _cancel_button['key']    = $key;
		$this -> _cancel_button['val']    = $val;
	}

	function setFormId( $id )
  {
		$this -> _form_id = $id;
	}

	function addPosition( $id , $nazwa , $typ , $action = '' , $tab = '' , $empty = false )
	{
		global $db;

		$this -> _field_id[$this -> _quantity]      = $id;
		$this -> _field_name[$this -> _quantity]    = $nazwa;
		$this -> _field_type[$this -> _quantity]    = $typ;
		$this -> _field_action[$this -> _quantity] 	= $action;
		if ( is_array( $tab ) )
      $this -> _field_tab[$this -> _quantity] = $tab;
		else
		{
			if ( $tab )
			{
				$query = $db -> prepare($tab);
				$query -> execute();
				if ( $empty )
          $tabs[''] = '';
				if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
          $tabs[$res[0]] = $res[1];
				$this -> _field_tab[$this -> _quantity] = $tabs;
			}
		}
		$this -> _quantity ++;
	}

	function addHiddenParam( $id )
  {
		$this -> _param = $id;
	}

	function setSubmitButton( $button )
  {
		$this -> _button_submit = $button;
	}

	function addPositionPassword( $nazwa = '' , $action1 = '' , $action2 = '' )
  {
		$this -> _password = true;
		if ( isset( $nazwa ) )
    {
			$nazwa1 = 'password';
			$nazwa2 = 'password_repeat';
		}
    else
			$nazwa2 = $nazwa . '_repeat';
		$this -> _pass['nazwa1'] 	= $nazwa1;
		$this -> _pass['nazwa2']	= $nazwa2;
		$this -> _pass['action1'] = $action1;
		$this -> _pass['action2'] = $action2;
	}

	function setUploadFolder( $id , $folder )
  {
		$this -> _upload_quantity ++;
		$this -> _field_upload[$this -> _upload_quantity]['id'] 		= $id;
		$this -> _field_upload[$this -> _upload_quantity]['folder'] = $folder;
	}

	function draw()
  {
		global $sys , $db , $lang;

    $hidden_tab       = '';
    $hidden_param_tab = '';
    $out              = '';

		$sys -> setSessionVar( 'db_edit_table'      , $this -> _table );
		$sys -> setSessionVar( 'db_edit_key'        , $this -> _key );
		$sys -> setSessionVar( 'db_edit_val'        , $this -> _val );
		$sys -> setSessionVar( 'db_edit_pols'       , $this -> _field_id );
		$sys -> setSessionVar( 'db_edit_pass'       , $this -> _pass );
		$sys -> setSessionVar( 'db_edit_pols_hidden', $this -> _field_key_hidden );
		$sys -> setSessionVar( 'db_edit_pols_type'  , $this -> _field_type );

		if ( !$this -> _form_id )
			$this -> _form_id = 'formularz';

		$flaga = false;
		if ( is_array( $this -> _field_id ) ) foreach ( $this -> _field_id as $p )
    {
			if ( !$this -> _select )
				$this -> _select = $p;
			else
				$this -> _select .= ', ' . $p;
		}
		if ( $this -> _param )
			$this -> _select .= ", " . $this -> _param;

		if ( $this -> _key )
			$flaga = true;

		if ( $flaga )
    {
			$query = $db -> prepare( 'SELECT ' . $this -> _select . ' FROM ' . $this -> _table . ' WHERE ' . $this -> _key . '="' . $this -> _val . '"' );
			$query->execute();
		}

		$tpl = new \Savant3;

		$tpl -> _form_adress  = $this -> _adres;
		$tpl -> _form_id      = $this -> _form_id;

		if ( is_array( $this -> _field_key_hidden ) )
		{
			for ( $j = 0; $j < $this -> _quantity_hidden; $j++ )
      {
				$hidden['name'] 	= $this -> _field_key_hidden[$j];
				$hidden['value'] 	= $this -> _field_value_hidden[$j];
				$hidden_tab[]     = $hidden;
			}
		}
		$tpl -> _hidden_tab = $hidden_tab;
		$tpl -> _form_key   = $this -> _key;
		$tpl -> _form_val   = $this -> _val;

		if ( $flaga )
		{
			if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
			{
				if ( $this -> _param )
				{
					$hidden_param['name']   = $this -> _param;
					$hidden_param['value']	= $res[$this -> _param];
					$hidden_param_tab[]     = $hidden_param;
				}
			}
		}
		$tpl -> _hidden_param_tab = $hidden_param_tab;

		for ( $i = 0; $i < $this -> _quantity; $i++ )
		{
			$table_edit['tr_id'] 	= $this -> _field_id[$i];
			$table_edit['name']		= $this -> _field_name[$i];

			if ( $this -> _field_type[$i] == "label" )
			{

				$table_edit['type']         = 'label';
				$table_edit['input_id']     = $this -> _field_id[$i];
				$table_edit['input_name']   = $this -> _field_id[$i];
				$table_edit['input_action']	= $this -> _field_action[$i];

				if ( $flaga )
				{
					$query -> execute();
					if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						$table_edit['input_value']	= $res[$i];
				}
				else
					$table_edit['input_value']	= '';

				if ( isset( $this -> _filed_tab[$i] ) && is_array( $this -> _field_tab[$i] ) ) foreach ( $this -> _field_tab[$i] as $key => $val )
				{
					if ( $flaga )
					{
						if ( $key == $res[$i] )
							$table_edit['text'] = $val;
					}
				}
				else
					$table_edit['text'] = $res[$i];
			}
			else if ( $this -> _field_type[$i] == 'text' )
			{
				$table_edit['type']         = 'text';
				$table_edit['input_id']     = $this -> _field_id[$i];
				$table_edit['input_name']   = $this -> _field_id[$i];
				$table_edit['input_action']	= $this -> _field_action[$i];
				if ( $flaga )
        {
					$query -> execute();
					if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						$table_edit['input_value']	= $res[$i];
				}
        else
					$table_edit['input_value']	= '';

			}
      else if ( $this -> _field_type[$i] == 'radio' )
			{
				$table_edit['type']         = 'radio';
				$table_edit['input_id']     = $this -> _field_id[$i];
				$table_edit['input_name']   = $this -> _field_id[$i];
				$table_edit['input_action']	= $this -> _field_action[$i];
				$value = '';
				if ( is_array( $this -> _field_tab[$i] ) ) foreach ( $this -> _field_tab[$i] as $key => $val )
				{
					$value[$key]                = $lang -> getTrans( $val );
					$table_edit['input_value'] 	= $value;
					if ( $flaga )
					{
						$query -> execute();
						if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						{
							if ( $key == $res[$i] )
								$table_edit['var'] = $res[$i];
						}
					}
				}
				else
					$table_edit['input_value'] = '';
			}
			else if ( $this -> _field_type[$i] == 'textarea' )
			{

				$table_edit['type']       = 'textarea';
				$table_edit['input_id']		= $this -> _field_id[$i];
				$table_edit['input_name']	= $this -> _field_id[$i];
				if ( $flaga )
				{
					$query -> execute();
					if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						$table_edit['input_value']	= $res[$i];
				}
				else
					$table_edit['input_value']	= '';

			}
			else if ( $this -> _field_type[$i] == 'file' )
			{

				$table_edit['type']       = 'file';
				$table_edit['input_id']		= $this -> _field_id[$i];
				$table_edit['input_name']	= 'image';
				if ( $flaga )
				{
					$query -> execute();
					if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						$table_edit['input_value']	= $res[$i];
				}
				else
					$table_edit['input_value'] = '';

			}
			else if ( $this -> _field_type[$i] == 'combo' )
			{
				$table_edit['type']         = 'combo';
				$table_edit['input_id']     = $this -> _field_id[$i];
				$table_edit['input_name']   = $this -> _field_id[$i];
				$table_edit['input_action']	= $this -> _field_action[$i];

				$value = '';
				if ( is_array( $this -> _field_tab[$i] ) ) foreach ( $this -> _field_tab[$i] as $key => $val )
				{
					$value[$key]                = $lang -> getTrans( $val );
					$table_edit['input_value'] 	= $value;
					if ( $flaga )
					{
						$query -> execute();
						if ( $query -> rowCount() ) while ( $res = $query -> fetch() )
						{
							if ( $key == $res[$i] )
								$table_edit['var'] = $res[$i];
						}
					}
				}
				else
					$table_edit['input_value'] = '';
			}
			$table_edit_tab[]		= $table_edit;
		}
		if ( $this -> _password )
		{
			$tpl -> _password 		= $this -> _password;
			$tpl -> _pass_name1		= $this -> _pass['nazwa1'];
			$tpl -> _pass_action	= $this -> _pass['action1'];
			$tpl -> _pass_name2		= $this -> _pass['nazwa2'];
			$tpl -> _pass_action2	= $this -> _pass['action2'];
		}
		if ( is_array( $this -> _field_upload ) )
		{
			$tpl -> _file_upload = true;
			for ( $j=1; $j<=$this -> _upload_quantity; $j++ )
			{
				$upload['name']		= $this -> _field_upload[$j]['id'] . '_folder';
				$upload['value']	= $this -> _field_upload[$j]['folder'];
				$upload_tab[]		= $upload;
			}
			$tpl -> _upload_tab = $upload_tab;
		}
		if ( $this -> _cancel_button )
		{
			$tpl -> _cancel_button 	= true;
			$tpl -> _cancel_adress	= $this -> _adres;
			$tpl -> _cancel_name    = $this -> _cancel_button['nazwa'];
			$tpl -> _cancel_key     = $this -> _cancel_button['key'];
			$tpl -> _cancel_val     = $this -> _cancel_button['val'];
		}
		$tpl -> _table_edit_tab	= $table_edit_tab;
		$out .= $tpl -> fetch( 'templates/data-edit.php' );
		return $out;
	}
}
?>
