<?
namespace admin\controls;

class Settings
{
  public static function settings_save()
  {
    $values = json_decode( \S::get( 'values' ), true );
    $settings = \admin\factory\Settings::settings_details( true );

    $response = \admin\factory\Settings::settings_save(
            $values['firm_name'], $values['firm_adress'], $values['additional_info'], $values['contact_form'], $values['contact_email'], $values['email_host'],
            $values['email_port'], $values['email_login'], $values['email_password'], $values['google_maps'], $values['facebook_link'], $values['statistic_code'], $values['htaccess'],
            $values['robots'], $values['shop_bank_account_info'], $values['update'], $values['boot_animation'], $settings['newsletter_header'], $settings['newsletter_footer'], $values['hotpay_api']
          );

    \admin\factory\Settings::settings_update( 'devel', $values['devel'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'ssl', $values['ssl'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'htaccess_cache', $values['htaccess_cache'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'free_delivery', $values['free_delivery'] );
    \admin\factory\Settings::settings_update( 'przelewy24_sandbox', $values['przelewy24_sandbox'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'przelewy24_merchant_id', $values['przelewy24_merchant_id'] );
    \admin\factory\Settings::settings_update( 'przelewy24_crc_key', $values['przelewy24_crc_key'] );
    \admin\factory\Settings::settings_update( 'update_key', $values['update_key'] );
    \admin\factory\Settings::settings_update( 'tpay_id', $values['tpay_id'] );
    \admin\factory\Settings::settings_update( 'tpay_sandbox', $values['tpay_sandbox'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'tpay_security_code', $values['tpay_security_code'] );
    \admin\factory\Settings::settings_update( 'piksel', $values['piksel'] );
    \admin\factory\Settings::settings_update( 'generate_webp', $values['generate_webp'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'lazy_loading', $values['lazy_loading'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'ekomi_survey', $values['ekomi_survey'] );
    \admin\factory\Settings::settings_update( 'google_tag_manager_id', $values['google_tag_manager_id'] );
    \admin\factory\Settings::settings_update( 'infinitescroll', $values['infinitescroll'] == 'on' ? 1 : 0 );
    // own_gtm_js
    \admin\factory\Settings::settings_update( 'own_gtm_js', $values['own_gtm_js'] );
    // own_gtm_html
    \admin\factory\Settings::settings_update( 'own_gtm_html', $values['own_gtm_html'] );

    foreach ( $values[ 'warehouse_message_zero'] as $key => $val )
      \admin\factory\Settings::settings_update( 'warehouse_message_zero_' . $key, $val );

    foreach ( $values[ 'warehouse_message_nonzero'] as $key => $val )
      \admin\factory\Settings::settings_update( 'warehouse_message_nonzero_' . $key, $val );

    \S::delete_dir( '../temp/' );
    \S::htacces();

    echo json_encode( $response );
    exit;
  }

  public static function view()
  {
    return \Tpl::view( 'settings/settings', [
              'languages' =>  \admin\factory\Languages::languages_list(),
              'settings' => \admin\factory\Settings::settings_details()
            ] );
  }
}
?>