<?php
    require_once('../../../server/api-adodb/config.php');
    require_once(LIB_PATH.DS.'loggedin.php');
    ob_start();

    if(!empty($_POST['table'])){
        $tableName = $_POST['table'];
    }else{
        die("Please select a table!");
    }


    $table = new Table($tableName);



    if(isPostEmpty()){
        $result = "Please fill in values.";
        echo $result;
    }else{
        try{

            $id = $table->save($_POST);
            $result = 1;

            if($table->isPhotoGallery() && empty($_POST['id'])){
                echo json_encode(array('success' => true));
                die;
            }

            echo $id !== true ? 'Created!' : 'Updated!';

        } catch (Exception $e){
            $result = $e->getMessage();
            if($table->isPhotoGallery() && empty($_POST['id']) ){
                echo json_encode(array('error' => $result));
                die;
            }
            echo $e->getMessage();
        }


        /*
        $id = $table->save($_POST);

        if(is_numeric($id) || $id === true){
        $session->message("Your entry was saved!");
        $result = 1;
        echo $id !== true ? 'Created!' : 'Updated!';
        }elseif($id == 'file_upload_failed'){
        echo $id;
        $result = 'File Upload Failed! Try changing the file name.';
        echo 'File upload failed!';
        }elseif($id == 'invalid_mime_type'){
        $result = 'Your file is an invalid format';
        echo 'Invalid Mime Type!';
        }elseif($id == 'invalid_max_image_height'){

        $result = $session->printMessage();
        echo 'Invalid Max Image Height!';
        }elseif($id == 'invalid_max_image_width'){
        $result = $session->printMessage();
        echo 'Invalid Max Image WIDTH!';
        }elseif($id == 'invalid_image_proportions'){
        $result = 'Your image has invalid proportions';
        echo 'Invalid Image Proportions!';
        }else{

        $result = 'Nothing is saved. Perhaps you did not change anything?';
        echo'Nothing changed!';
        }
        */
    }
?>

<script language="javascript" type="text/javascript">window.top.window.stopUpload("<?php echo $result; ?>","<?php echo $tableName; ?>");</script>
