<?
namespace admin\factory;

class Settings
{
  public static function getSystemSettings( $param )
  {
    global $db;

    $query = $db -> prepare( 'SELECT value FROM pp_settings WHERE param = :param' );
    $query -> bindValue( ':param' , $param , \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $out = $row['value'];
    $query -> closeCursor();
    
    return $out;
  }
  
	public static function saveSettings()
	{
		$firm_name	= \System::saveString( \System::formGet( 'firm_name' ) );
		\admin\factory\Settings::saveParam( 'firm_name' , $firm_name );

		$street	= \System::saveString( \System::formGet( 'street' ) );
		\admin\factory\Settings::saveParam( 'street' , $street );

		$postal_code = \System::saveString( \System::formGet( 'postal_code' ) );
		\admin\factory\Settings::saveParam( 'postal_code' , $postal_code );

		$city = \System::saveString( \System::formGet( 'city' ) );
		\admin\factory\Settings::saveParam( 'city' , $city );

		$nip = \System::saveString( \System::formGet( 'nip' ) );
		\admin\factory\Settings::saveParam( 'nip' , $nip );

		$email	= \System::saveString( \System::formGet( 'email' ) );
		\admin\factory\Settings::saveParam( 'email' , $email );

		$phone	= \System::saveString( \System::formGet( 'phone' ) );
		\admin\factory\Settings::saveParam( 'phone' , $phone );

		$fax = \System::saveString( \System::formGet( 'fax' ) );
		\admin\factory\Settings::saveParam( 'fax' , $fax );

		$account = \System::saveString( \System::formGet( 'account' ) );
		\admin\factory\Settings::saveParam( 'account' , $account );

		$person	= \System::saveString( \System::formGet( 'person' ) );
		\admin\factory\Settings::saveParam( 'person' , $person );

		$admin_email = \System::saveString( \System::formGet( 'admin_email' ) );
		\admin\factory\Settings::saveParam( 'admin_email' , $admin_email );

		$email_host	= \System::saveString( \System::formGet( 'email_host' ) );
		\admin\factory\Settings::saveParam( 'email_host' , $email_host );

		$email_port	= \System::saveString( \System::formGet( 'email_port' ) );
		\admin\factory\Settings::saveParam( 'email_port' , $email_port );

		$email_login = \System::saveString( \System::formGet( 'email_login' ) );
		\admin\factory\Settings::saveParam( 'email_login' , $email_login );

		$email_password	= \System::saveString( \System::formGet( 'email_password' ) );
		\admin\factory\Settings::saveParam( 'email_password' , $email_password );

		$google_map_key	= \System::saveString( \System::formGet( 'google_map_key' ) );
		\admin\factory\Settings::saveParam( 'google_map_key' , $google_map_key );

		$facebook_url	= \System::saveString( \System::formGet( 'facebook_url' ) );
		\admin\factory\Settings::saveParam( 'facebook_url' , $facebook_url );


		$user_register = \System::saveString( \System::formGet( 'user_register' ) );
		\admin\factory\Settings::saveParam( 'user_register' , $user_register );

		$contact_form = \System::saveString( \System::formGet( 'contact_form' ) );
		\admin\factory\Settings::saveParam( 'contact_form' , $contact_form );

    $register = \System::formGetInt( 'register' );
    \admin\factory\Settings::saveParam( 'register' , $register );

    $info = \System::formGet( 'info' );
    \admin\factory\Settings::saveParam( 'info' , $info );

		\System::deleteCacheAdmin();
		\System::deleteCache();
    \System::setAlert( 'Ustawienia zostały zapisane.' );
	}

	public static function saveParam( $param, $value )
	{
		global $db;

    $query = $db -> prepare( 'SELECT id FROM pp_settings WHERE param = :param' );
    $query -> bindValue( ':param', $param, \PDO::PARAM_STR );
    $query -> execute();
    if ( $query -> rowCount() )
    {    
      $query2 = $db -> prepare( 'UPDATE pp_settings SET value = :value WHERE param = :param' );
      $query2 -> bindValue( ':value' , $value , \PDO::PARAM_STR );
      $query2 -> bindValue( ':param' , $param , \PDO::PARAM_STR );
      $query2 -> execute();
      $query2 -> closeCursor();
    }
    else
    {
      $query2 = $db -> prepare( 'INSERT INTO pp_settings ( value, param ) VALUES ( :value, :param )' );
      $query2 -> bindValue( ':value' , $value , \PDO::PARAM_STR );
      $query2 -> bindValue( ':param' , $param , \PDO::PARAM_STR );
      $query2 -> execute();
      $query2 -> closeCursor();
    }
    $query -> closeCursor();
	}
}
?>