<?php

class Getter
{
    public $mode, $content, $option, $quality, $count;
    
    function __construct( $postID ) {
        
        $this->ID = $postID;
        if( !$_POST['auto_options'] ) return 0;
        else $this->option = addslashes( $_POST['auto_options'] );
        
        switch( $_POST['spin_mode'] ) {
            case 'replace':
                $this->mode = 'replace';
                $this->content = '';
                if( $_POST['quality'] ) {
                    $this->quality = addslashes( $_POST['quality'] );
                } else {
                    $this->quality = '1';
                }
                break;
            case 'identify':
                if( $_POST['spin_select'] ) {
                    $this->mode = 'identify';
                    $this->quality = '1';
                    //$this->content = str_replace( "%u201C", '"', str_replace( "%u201D", '"', $_POST['spin_select'] ) );
                    //$this->content = str_replace( "&nbsp;", '', strip_tags( urldecode( $this->content ) ) );
                    $this->content = $_POST['spin_select'];
                } else { return 0; }
                break;
            default:
                return 0;
        }
        
        if( $this->option == 3 ) {
            if( $_POST['spin_count'] AND $_POST['datepick'] AND $_POST['spin_publish'] ) {
                $this->count    = stripslashes( $_POST['spin_count'] );
                $this->datepick = stripslashes( $_POST['datepick'] );
                $this->publish  = stripslashes( $_POST['spin_publish'] );
            } else { return 0; }
        }
        
        if( $_POST['spin_title'] == 1 ) {
            $this->spin_title = 1;
        } else {
            $this->spin_title = 0;
        }
        
        if( $_POST['excluded'] ) { 
            $this->excluded = stripslashes( $_POST['excluded'] ); 
        } else {
            $this->excluded = '';
        }
        
        $this->insert();
    }
    
    function insert() {
    
        global $wpdb;
        $myTable = $wpdb->prefix . "wpspin_base";
        
        if( $this->option == 3 ) {
            if( $wpdb->get_var( "SELECT COUNT(*) FROM `$myTable` WHERE `id`='".$this->ID."'" ) ) {
                $sql = "UPDATE `$myTable` SET 
                `id`='".$this->ID."',`mode`='".$this->mode."',`auto_options`='3',`quality`='".$this->quality."',`spin_title`='".addslashes($this->spin_title)."',`time_count`='1',`time_type`='5',`spin_count`='".$this->count."',`spin_publish`='".$this->publish."',`excluded`='".$this->excluded."',`alarm`='".$this->datepick."',`content`='".addslashes($this->content)."' 
                WHERE `id`='".$this->ID."'";
            } else {
                $sql = "INSERT INTO `$myTable` 
                (`id`, `mode`, `auto_options`, `quality`, `spin_title`, `time_count`, `time_type`, `spin_count`, `spin_publish`, `excluded`, `alarm`, `content`,`status`) 
                VALUES 
                ('".$this->ID."', '".$this->mode."', '3','".$this->quality."','".addslashes($this->spin_title)."','1','5','".$this->count."','".$this->publish."','".$this->excluded."','".$this->datepick."', '".addslashes($this->content)."','1');";
            }
        } else {
            if( $wpdb->get_var( "SELECT COUNT(*) FROM `$myTable` WHERE `id`='".$this->ID."'" ) ) {
                $sql = "UPDATE $myTable SET 
                `id`='".$this->ID."',`mode`='".$this->mode."',`auto_options`='".$this->option."',`quality`='".$this->quality."',`spin_title`='".addslashes($this->spin_title)."',`excluded`='".$this->excluded."',`content`='".addslashes($this->content)."' 
                WHERE `id`='".$this->ID."'";
            } else {
                $sql = "INSERT INTO `$myTable` 
                (`id`, `mode`, `auto_options`, `quality`, `spin_title`, `excluded`, `content`,`status`) 
                VALUES 
                ('".$this->ID."', '".$this->mode."', '".$this->option."','".$this->quality."','".addslashes($this->spin_title)."','".$this->excluded."', '".addslashes($this->content)."','1')";
            }
        }
        //echo $sql; exit;
        
        if( !$wpdb->query($sql) ) return 0;
        
        return 1;
        
    }
}

?>