<?

namespace admin\factory;

class Languages
{
  public static function translation_delete( $translation_id )
  {
    global $mdb;
    return $mdb -> delete( 'pp_langs_translations', [ 'id' => $translation_id ] );
  }
  
  public static function translation_save( $translation_id, $text, $languages )
  {
    global $mdb;
    
    if ( $translation_id )
    {
      $mdb -> update( 'pp_langs_translations', [ 'text' => $text ], [ 'id' => $translation_id ] );
      if ( is_array( $languages ) and !empty( $languages ) ): foreach ( $languages as $key => $val ):
        $mdb -> update( 'pp_langs_translations', [ $key => $val ], [ 'id' => $translation_id ] );
      endforeach; endif;
      \S::htacces();
      \S::delete_dir( '../temp/' );
      return $translation_id;
    }
    else
    {
      $mdb -> insert( 'pp_langs_translations', [ 'text' => $text ] );
      if ( $translation_id = $mdb -> id() )
      {
        if ( is_array( $languages ) and !empty( $languages ) ): foreach ( $languages as $key => $val ):
          $mdb -> update( 'pp_langs_translations', [ $key => $val ], [ 'id' => $translation_id ] );
        endforeach; endif;
      }
      \S::htacces();
      \S::delete_dir( '../temp/' );
      return $translation_id;
    }
  }
  
  public static function translation_details( $translation_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_langs_translations', '*', [ 'id' => $translation_id ] );
  }
  
  public static function language_delete( $language_id )
  {
    global $mdb;

    if ( $mdb -> count( 'pp_langs' ) > 1 )
    {
      if ( $mdb -> query( 'ALTER TABLE pp_langs_translations DROP ' . $language_id )
              and
              $mdb -> delete( 'pp_langs', [ 'id' => $language_id ] )
      )
        return true;
    }
    return false;
  }

  public static function max_order()
  {
    global $mdb;
    return $mdb -> max( 'pp_langs', 'o' );
  }

  public static function language_save( $language_id, $name, $status, $start, $o )
  {
    global $mdb;

    if ( $start == 'on' )
      $mdb -> update( 'pp_langs', [
              'start' => 0
              ], [
              'id[!]' => $language_id
      ] );

    if ( $mdb -> count( 'pp_langs', [ 'id' => $language_id ] ) )
    {
      $mdb -> update( 'pp_langs',
              [
              'status' => $status == 'on' ? 1 : 0,
              'start'  => $start == 'on' ? 1 : 0,
              'name'   => $name,
              'o'      => $o
              ], [
              'id' => $language_id
      ] );

      if ( !$mdb -> count( 'pp_langs', [ 'start' => 1 ] ) )
      {
        $id_tmp = $mdb -> get( 'pp_langs', 'id', [ 'ORDER' => [ 'o' => 'ASC' ] ] );
        $mdb -> update( 'pp_langs', [ 'start' => 1 ], [ 'id' => $id_tmp ] );
      }

      \S::htacces();
      \S::delete_dir( '../temp/' );
      return $language_id;
    }
    else
    {
      if ( $mdb -> query( 'ALTER TABLE pp_langs_translations ADD ' . $language_id . ' TEXT NULL DEFAULT NULL' ) )
      {
        $mdb -> insert( 'pp_langs',
                [
                'id'     => $language_id,
                'name'   => $name,
                'status' => $status == 'on' ? 1 : 0,
                'start'  => $start == 'on' ? 1 : 0,
                'o'      => $o
        ] );

        \S::htacces();
        \S::delete_dir( '../temp/' );
        return $language_id;
      }
    }

    return faslse;
  }

  public static function language_details( $language_id )
  {
    global $mdb;
    return $mdb -> get( 'pp_langs', '*', [ 'id' => $language_id ] );
  }

  public static function languages_list()
  {
    global $mdb;
    return $mdb -> select( 'pp_langs', '*', [ 'ORDER' => [ 'o' => 'ASC' ] ] );
  }
}
?>