<?
namespace admin\factory;
class Settings
{
  public static function settings_update( $param, $value )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_settings', [ 'param' => $param ] ) )
      return $mdb -> update( 'pp_settings', [ 'value' => $value ], [ 'param' => $param ] );
    else
      return $mdb -> insert( 'pp_settings', [ 'param' => $param, 'value' => $value ] );
  }

  public static function settings_save(
    $firm_name, $firm_adress, $additional_info, $contact_form, $contact_email, $email_host, $email_port, $email_login, $email_password, $google_maps,
    $facebook_link, $statistic_code, $htaccess, $robots, $newsletter_header, $newsletter_footer_1, $newsletter_footer_2, $google_map_key, $google_search_console, $update, $devel,
    $news_limit, $visit_counter, $calendar, $tags, $ssl, $mysql_debug, $htaccess_cache, $visits, $links_structure, $link_version, $widget_phone, $update_key )
  {
    global $mdb;

    $mdb -> query( 'TRUNCATE pp_settings' );

    $mdb -> insert( 'pp_settings', [
            [
              'param' => 'firm_name',
              'value' => $firm_name,
            ], [
              'param' => 'firm_adress',
              'value' => $firm_adress
            ], [
              'param' => 'additional_info',
              'value' => $additional_info
            ], [
              'param' => 'contact_form',
              'value' => $contact_form
            ], [
              'param' => 'contact_email',
              'value' => $contact_email
            ], [
              'param' => 'email_host',
              'value' => $email_host
            ], [
              'param' => 'email_port',
              'value' => $email_port
            ], [
              'param' => 'email_login',
              'value' => $email_login
            ], [
              'param' => 'email_password',
              'value' => $email_password
            ], [
              'param' => 'google_maps',
              'value' => $google_maps == 'on' ? 1 : 0
            ], [
              "param" => 'facebook_link',
              'value' => $facebook_link
            ], [
              'param' => 'statistic_code',
              'value' => $statistic_code
            ], [
              'param' => 'htaccess',
              'value' => $htaccess
            ], [
              'param' => 'robots',
              'value' => $robots
            ], [
              'param' => 'newsletter_header',
              'value' => $newsletter_header
            ], [
              'param' => 'newsletter_footer_1',
              'value' => $newsletter_footer_1
            ], [
              'param' => 'newsletter_footer_2',
              'value' => $newsletter_footer_2
            ], [
              'param' => 'google_map_key',
              'value' => $google_map_key
            ], [
              'param' => 'google_search_console',
              'value' => $google_search_console
            ], [
              'param' => 'update',
              'value' => $update == 'on' ? 1 : 0
            ], [
              'param' => 'devel',
              'value' => $devel == 'on' ? 1 : 0
            ], [
              'param' => 'news_limit',
              'value' => $news_limit
            ], [
              'param' => 'visit_counter',
              'value' => $visit_counter == 'on' ? 1 : 0
            ], [
              'param' => 'calendar',
              'value' => $calendar == 'on' ? 1 : 0
            ], [
              'param' => 'tags',
              'value' => $tags == 'on' ? 1 : 0
            ], [
              'param' => 'ssl',
              'value' => $ssl == 'on' ? 1 : 0
            ], [
              'param' => 'mysql_debug',
              'value' => $mysql_debug == 'on' ? 1 : 0
            ], [
              'param' => 'htaccess_cache',
              'value' => $htaccess_cache == 'on' ? 1 : 0
            ], [
              'param' => 'visits',
              'value' => $visits
            ], [
              'param' => 'links_structure',
              'value' => $links_structure
            ], [
              'param' => 'link_version',
              'value' => $link_version
            ], [
              'param' => 'widget_phone',
              'value' => $widget_phone == 'on' ? 1 : 0
            ], [
              'param' => 'update_key',
              'value' => $update_key
            ]
          ]
    );

    \S::set_message( 'Ustawienia zostały zapisane' );
    \S::delete_cache();
    \S::htacces();

    return true;
  }

  public static function settings_details()
  {
    global $mdb;

    $results = $mdb -> select( 'pp_settings', '*', [ 'ORDER' => [ 'id' => 'ASC' ] ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $settings[$row['param']] = $row['value'];

    return $settings;
  }

}
?>