<?php

namespace admin\factory;

class Banners
{
  public static function banner_delete( $banner_id )
  {
    global $mdb;
    
    $result = $mdb -> delete( 'pp_banners', [ 'id' => (int) $banner_id ] );
    \S::delete_cache();
    
    return $result;
  }

  public static function banner_save( $banner_id, $name, $status, $date_start, $date_end, $home_page, $src, $url, $html, $text )
  {
    global $mdb;

    if ( !$banner_id )
    {
      $mdb -> insert( 'pp_banners', [
                      'name'       => $name,
                      'status'     => $status == 'on' ? 1 : 0,
                      'date_start' => $date_start != '' ? $date_start : null,
                      'date_end'   => $date_end != '' ? $date_end : null,
                      'home_page'  => $home_page == 'on' ? 1 : 0
              ] );

      $id = $mdb -> id();

      if ( $id )
      {
        $i = 0;

        $results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
        if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_banners_langs', [
                  'id_banner' => (int)$id,
                  'id_lang'   => $row['id'],
                  'src'       => $src[ $i ],
                  'url'       => $url[ $i ],
                  'html'      => $html[ $i ],
                  'text'      => $text[ $i ]
          ] );
          $i++;
        }    
        else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_banners_langs', [
                  'id_banner' => (int)$id,
                  'id_lang'   => $row['id'],
                  'src'       => $src,
                  'url'       => $url,
                  'html'      => $html,
                  'text'      => $text
          ] );
        }

        \S::delete_cache();

        return $id;
      }
    }
    else
    {
      $mdb -> update( 'pp_banners',
                      [
              'name'       => $name,
              'status'     => $status == 'on' ? 1 : 0,
              'date_start' => $date_start != '' ? $date_start : null,
              'date_end'   => $date_end != '' ? $date_end : null,
              'home_page'  => $home_page == 'on' ? 1 : 0
              ], [
              'id' => (int) $banner_id
      ] );

      $mdb -> delete( 'pp_banners_langs', [ 'id_banner' => (int)$banner_id ] );

      $i = 0;

      $results = $mdb -> select( 'pp_langs', [ 'id' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
      if ( is_array( $results ) and count( $results ) > 1 ) foreach ( $results as $row )
      {
        $mdb -> insert( 'pp_banners_langs', [
                  'id_banner' => (int)$banner_id,
                  'id_lang'   => $row['id'],
                  'src'       => $src[ $i ],
                  'url'       => $url[ $i ],
                  'html'      => $html[ $i ],
                  'text'      => $text[ $i ]
        ] );
        $i++;
      }
      else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
      {
        $mdb -> insert( 'pp_banners_langs', [
                'id_banner' => (int)$banner_id,
                'id_lang'   => $row['id'],
                'src'       => $src,
                'url'       => $url,
                'html'      => $html,
                'text'      => $text
        ] );
      }

      \S::delete_cache();
      return $banner_id;
    }
    return false;
  }

  public static function banner_details( $id_banner )
  {
    global $mdb;
    
    $banner = $mdb -> get( 'pp_banners', '*', [ 'id' => (int)$id_banner ] );

    $results = $mdb -> select( 'pp_banners_langs', '*', [ 'id_banner' => (int)$id_banner ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $banner['languages'][$row['id_lang']] = $row;

    return $banner;
  }

}
?>
