<?
namespace admin\controls;

class Languages
{
  public static function language_delete()
  {
    global $user;

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

    if ( \admin\factory\Languages::language_delete( \S::get( 'id' ) ) )
      \S::alert( 'Język został usunięty.' );
    header( 'Location: /admin/languages/view_list/' );
    exit;
  }

  public static function language_save()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania języka wystąpił błąd. Proszę spróbować ponownie.' ];

    $values = \S::json_to_array( \S::get( 'values' ) );
    
    if ( \admin\factory\Languages::language_save(
                    $values['id'], $values['name'], $values['status'], $values['start'], $values['o'], $values['domain'], $values['main_domain']
                  ) )
      $response = [ 'status' => 'ok', 'msg' => 'Język został zapisany.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }

  public static function language_edit()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    return \admin\view\Languages::language_edit(
                    \admin\factory\Languages::language_details(
                            \S::get( 'id' )
                    ), \admin\factory\Languages::max_order()
    );
  }

  public static function view_list()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    return \admin\view\Languages::languages_list();
  }

  public static function translation_delete()
  {
    global $user;

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

    if ( \admin\factory\Languages::translation_delete( \S::get( 'id' ) ) )
      \S::alert( 'Tłumaczenie zostało usunięte.' );
    header( 'Location: /admin/languages/translation_list/' );
    exit;
  }
  
  public static function translation_save()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );
    
    $response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania tłumaczenia wystąpił błąd. Proszę spróbować ponownie.' ];

    $values = \S::json_to_array( \S::get( 'values' ) );
    
    $languages_list = \admin\factory\Languages::languages_list();
    if ( is_array( $languages_list ) and !empty( $languages_list ) ) foreach ( $languages_list as $language )
    {
      \S::delete_session( 'lang-' . $language['id'] );
      $languages[ $language['id'] ] = $values[ $language['id'] ];
    }

    if ( $id = \admin\factory\Languages::translation_save( $values['id'], $values['text'], $languages ) )
      $response = [ 'status' => 'ok', 'msg' => 'Tłumaczenie zostało zapisane.', 'id' => $id ];

    echo json_encode( $response );
    exit;
  }
  
  public static function translation_edit()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration', $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    return \admin\view\Languages::translation_edit(
                    \admin\factory\Languages::translation_details( \S::get( 'id' ) ),
                    \admin\factory\Languages::languages_list()
            );
  }

  public static function translation_list()
  {
    global $user;
    
    if ( !\admin\factory\Users::check_privileges( 'language_administration',
                    $user['id'] ) )
      return \S::alert( 'Nie masz uprawnień' );

    return \admin\view\Languages::translations_list();
  }

}
?>