<?php
namespace factory;

class Settings {
  
  public function getSettings()
  {
    global $db;
    
    $query = $db -> prepare( 'SELECT * FROM pro_settings' );
    $query -> execute();
    if ( $query -> rowCount() ) while ( $row = $query -> fetch() )
      $settings[ $row['param'] ] = $row['value'];
    $query -> closeCursor();
    return $settings;    
  }

  public function saveSettings( $param, $value )
  {
    global $db;
    
    $query = $db -> prepare( 'UPDATE pro_settings SET value = :value WHERE param = :param' );
    $query -> bindValue( ':value', $value, \PDO::PARAM_STR );
    $query -> bindValue( ':param', $param, \PDO::PARAM_STR );
    $query -> execute();
    if ( !$query -> rowCount() )
    {
      $query2 = $db -> prepare( 'INSERT INTO pro_settings ( param, value ) VALUES ( :param, :value )' );
      $query2 -> bindValue( ':value', $value, \PDO::PARAM_STR );
      $query2 -> bindValue( ':param', $param, \PDO::PARAM_STR );
      $query2 -> execute();
      $query2 -> closeCursor();
    }
    $query -> closeCursor();
    
    \S::deleteSessionVar( 'settings' );
  }
}
?>
