<?
namespace admin\factory;

class Languages
{
  public static function available_domains()
  {
    global $mdb;
    return $mdb -> query( 'SELECT domain FROM pp_langs WHERE status = 1 AND domain IS NOT NULL GROUP BY domain' ) -> fetchAll( \PDO::FETCH_ASSOC );
  }
  
  public static function default_domain()
  {
    global $mdb;
    $results = $mdb -> query( 'SELECT domain FROM pp_langs WHERE status = 1 AND domain IS NOT NULL AND main_domain = 1' ) -> fetchAll();
    return $default_domain = $results[0][0];
  }
  
  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_cache();
      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_cache();
      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, $domain, $main_domain )
  {
    global $mdb;

    if ( $start == 'on' and $status == 'on' and !\S::get_domain( $domain ) )
      $mdb -> update( 'pp_langs', [
                'start' => 0
              ], [
                'id[!]' => $language_id
              ] );

    if ( $start == 'on' and $status == 'on' and \S::get_domain( $domain ) )
      $mdb -> update( 'pp_langs', [
                'start' => 0
              ], [
                'AND' => [ 'id[!]' => $language_id, 'domain' => \S::get_domain( $domain ) ]
              ] );
    
    if ( $main_domain == 'on' and $domain and $status == 'on' )
      $mdb -> update( 'pp_langs', [
                'main_domain' => 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,
              'domain'      => \S::get_domain( $domain ) ? \S::get_domain( $domain ) : null,
              'main_domain' => $main_domain == 'on' and \S::get_domain( $domain ) ? 1 : 0,
            ], [
            'id' => $language_id
      ] );
    }
    else
    {      
      if ( $mdb -> query( 'ALTER TABLE pp_langs_translations ADD ' . strtolower( $language_id ) . ' TEXT NULL DEFAULT NULL' ) )
      {
        $mdb -> insert( 'pp_langs', [
                'id'          => strtolower( $language_id ),
                'name'        => $name,
                'status'      => $status == 'on' ? 1 : 0,
                'start'       => $start == 'on' ? 1 : 0,
                'o'           => $o,
                'domain'      => \S::get_domain( $domain ) ? \S::get_domain( $domain ) : null,
                'main_domain' => $main_domain == 'on' && \S::get_domain( $domain ) ? 1 : 0,
              ] );
      }
    }

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

    $domains = $mdb -> select( 'pp_langs', 'domain', [ 'domain[!]' => null, 'GROUP' => 'domain'] );
    if ( is_array( $domains ) and !empty( $domains ) ) 
    {
      $mdb -> update( 'pp_langs', [ 'start' => 0 ], [ 'domain' => null ] );
      foreach ( $domains as $domain )
      {
        if ( !$mdb -> count( 'pp_langs', [ 'AND' => [ 'status' => 1, 'start' => 1, 'domain' => $domain ] ] ) )
        {
          if ( $id_tmp = $mdb -> get( 'pp_langs', 'id', [ 'AND' => [ 'status' => 1, 'domain' => $domain ], 'ORDER' => [ 'o' => 'ASC' ] ] ) )
            $mdb -> update( 'pp_langs', [ 'start' => 1 ], [ 'id' => $id_tmp ] );
        }
      }
    }

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

    \S::htacces();
    \S::delete_cache();
    return $language_id;
  }

  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' ] ] );
  }
}
?>