<?
namespace admin\controls;

class Settings
{
  public static function settings_save()
  {
    global $user;

    if ( !\admin\factory\Users::check_privileges( 'settings_administration', $user['id'] ) )
      return \S::alert('Nie masz uprawnień');

    $settings = \admin\factory\Settings::settings_details();
    $values   = \S::json_to_array( \S::get( 'values' ) );

    \admin\factory\Settings::settings_save(
              $values['firm_name'],
              $values['firm_adress'],
              $values['additional_info'],
              $values['contact_form'] == 'on' ? 1 : 0,
              $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'],
              $settings['newsletter_header'],
              $settings['newsletter_footer_1'],
              $settings['newsletter_footer_2'],
              $values['google_map_key'],
              $values['google_search_console'],
              $values['update'],
              $values['devel'],
              $values['news_limit'],
              $values['visit_counter'],
              $values['calendar'],
              $values['tags'],
              $values['ssl'],
              $values['mysql_debug'],
              $values['htaccess_cache'],
              $settings['visits'],
              $values['links_structure'],
              $values['link_version'],
              $values['widget_phone'],
              $values['update_key']
        );

    \admin\factory\Settings::settings_update( 'image_px', $values['image_px'] );
    \admin\factory\Settings::settings_update( 'newsletter_cron', $values['newsletter_cron'] );
    \admin\factory\Settings::settings_update( 'lazy_loading', $values['lazy_loading'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'generate_webp', $values['generate_webp'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'contact_form_captcha', $values['contact_form_captcha'] == 'on' ? 1 : 0 );
    \admin\factory\Settings::settings_update( 'url_version', $values['url_version'] );

    \S::alert( 'Ustawienia zostały zapisane.' );

    echo json_encode( [ 'status' => 'ok', 'msg' => 'Ustawienia zostały zapisane.' ] );
    exit;
  }

  public static function view()
  {
    global $user;

    if ( !\admin\factory\Users::check_privileges( 'settings_administration', $user['id']))
      return \S::alert('Nie masz uprawnień');

    return \admin\view\Settings::view(
              \admin\factory\Settings::settings_details()
            );
   }
}
?>