<?php

namespace admin\factory;

class Scontainers
{
  public static function container_delete( $container_id )
  {
    global $mdb;
    return $mdb -> delete( 'pp_scontainers', [ 'id' => (int) $container_id ] );
  }

  public static function container_save( $container_id, $title, $text, $status, $show_title, $src, $html )
  {
    global $mdb;
    
    if ( !$container_id )
    {
      $mdb -> insert( 'pp_scontainers',
                      [
              'status'     => $status == 'on' ? 1 : 0,
              'show_title' => $show_title == 'on' ? 1 : 0,
              'src'        => $src
      ] );

      $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_scontainers_langs',
                          [
                  'container_id' => (int) $id,
                  'lang_id'      => $row['id'],
                  'title'        => $title[$i],
                  'text'         => $text[$i],
                  'html'         => $html[$i]
          ] );
          $i++;
        }
        else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
        {
          $mdb -> insert( 'pp_scontainers_langs', [
                    'container_id' => (int) $id,
                    'lang_id'      => $row['id'],
                    'title'        => $title,
                    'text'         => $text,
                    'html'         => $html
                 ] );
        }

        \S::delete_cache();

        return $id;
      }
    }
    else
    {
      $mdb -> update( 'pp_scontainers',
                      [
              'status'     => $status == 'on' ? 1 : 0,
              'show_title' => $show_title == 'on' ? 1 : 0,
              'src'        => $src
              ],
                      [
              'id' => (int) $container_id
      ] );

      $mdb -> delete( 'pp_scontainers_langs',
                      [ 'container_id' => (int) $container_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_scontainers_langs',
                        [
                'container_id' => (int) $container_id,
                'lang_id'      => $row['id'],
                'title'        => $title[$i],
                'text'         => $text[$i],
                'html'         => $html[$i]
        ] );
        $i++;
      }
      else if ( is_array( $results ) and count( $results ) == 1 ) foreach ( $results as $row )
      {
        $mdb -> insert( 'pp_scontainers_langs',
                        [
                'container_id' => (int) $container_id,
                'lang_id'      => $row['id'],
                'title'        => $title,
                'text'         => $text,
                'html'         => $html
        ] );
      }

      \S::delete_cache();

      return $container_id;
    }
  }

  public static function container_details( $container_id )
  {
    global $mdb;
    
    $container = $mdb -> get( 'pp_scontainers', '*', [ 'id' => (int) $container_id ] );

    $results = $mdb -> select( 'pp_scontainers_langs', '*', [ 'container_id' => (int) $container_id ] );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $container['languages'][$row['lang_id']] = $row;

    return $container;
  }
}