<?php
namespace admin\factory;
class Globelus
{
  public static function save_to_db( $table, $column_id, $row_id, $column, $operation, $value, $values = '' )
  {
    global $mdb;

    if ( $column == 'date_of_birth' and $value != '' )
      $value = date( 'Y-m-d', strtotime( $value ) );

    if ( $operation == 'insert_update' )
    {
      if ( $mdb -> count( $table, [ $column_id => $row_id ] ) )
      {
        if ( $mdb -> update( $table, [
                        $column => $value === '' ? null : $value
                      ], [
                        $column_id => $row_id
                      ] ) )
          return $row_id;
      }
      else
      {
        if ( $values )
        {
          $values_s1 = explode( '|', $values );
          foreach ( $values_s1 as $value_s1 )
          {
            $values_s2 = explode( ';', $value_s1 );
            $columns[ $values_s2[0] ] = $values_s2[1];
          }
        }

        if ( $mdb -> insert( $table, [
                array_merge( [
                  $column     => $value,
                  $column_id  => $row_id
                ], $columns )
              ] ) )
          return $mdb -> id();
      }
    }

    if ( $operation == 'update_insert' )
    {
      if ( $mdb -> count( $table, [ $column_id => $row_id ] ) )
        return $mdb -> update( $table, [
                        $column => $value
                      ], [
                        $column_id => $row_id
                      ] );
      else
        return $mdb -> insert( $table, [
                $column     => $value,
                $column_id  => $row_id
              ] );
    }

    if ( $operation == 'update' )
    {
      if ( $column == 'cv_access_date' or $column == 'id_voivodeship' )
      {
        if  ( $value == '' )
          $value = null;
      }

      if ( $value == null and $column == 'id_position' )
      {
        return $mdb -> delete( $table, [ $column_id => $row_id ] );
      }
      else
      {
        if ( $mdb -> update( $table, [
                        $column => $value
                      ], [
                        $column_id => $row_id
                      ] ) )
        {
          if ( $column == 'cv_access_date' ) {
            if ( $value != '' ) {
              $mdb -> update( $table, [
                        'cv_access' => 1
                      ], [
                        $column_id => $row_id
                      ] );
            } else {
              $mdb -> update( $table, [
                        'cv_access' => 0
                      ], [
                        $column_id => $row_id
                      ] );
            }
          }
        }
        return $row_id;
      }
    }

    if ( $operation == 'insert_delete' )
    {
      if ( !$value )
      {
        if ( $values )
        {
          $values_s1 = explode( '|', $values );
          foreach ( $values_s1 as $value_s1 )
          {
            $values_s2 = explode( ';', $value_s1 );
            $columns[ $values_s2[0] ] = $values_s2[1];
          }
        }

        if ( is_array( $columns ) )
          return $mdb -> delete( $table, $columns );
      }
      else
      {
        if ( $values )
        {
          $values_s1 = explode( '|', $values );
          foreach ( $values_s1 as $value_s1 )
          {
            $values_s2 = explode( ';', $value_s1 );
            $columns[ $values_s2[0] ] = $values_s2[1];
          }
        }

        if ( is_array( $columns ) )
          return $mdb -> insert( $table, $columns );
      }
    }

    return false;
  }
}
