Current File : /home/n742ef5/royalanteam.com/mls/helpers/TableHelper.php
<?php
class TableHelper
{
    function create_table_if_not_exists($full_table_name, $columns)
    {
        global $wpdb;
        // $full_table_name = $table_name;
        // Check if the table already exists
        if ($wpdb->get_var("SHOW TABLES LIKE '{$full_table_name}'") !== $full_table_name) {
            // Define the charset collate
            $charset_collate = $wpdb->get_charset_collate();

            // Build the SQL for creating the table
            $sql = "CREATE TABLE {$full_table_name} (";
            foreach ($columns as $column) {
                $sql .= "{$column['name']} {$column['type']}";
                if (isset($column['collation']))
                    $sql .= " COLLATE {$column['collation']}";
                if (isset($column['attributes']))
                    $sql .= " {$column['attributes']}";
                if (!$column['null'])
                    $sql .= " NOT NULL";
                if (isset($column['default']))
                    $sql .= " DEFAULT {$column['default']}";
                if (isset($column['extra']))
                    $sql .= " {$column['extra']}";
                $sql .= ", ";
            }
            $sql .= "PRIMARY KEY (id)";
            if ($full_table_name === 'properties') {
                $sql .= ", KEY ListingKey (ListingKey)";
            }
            $sql .= ") $charset_collate;";

            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);

            echo "Table '{$full_table_name}' has been created. \n";
        } else {
            echo "Table '{$full_table_name}' already exists. \n";
        }
    }




    function propertyTableCheck() {
        // Define the columns for the properties table
        $property_columns = [
            ['name' => 'id', 'type' => 'INT', 'null' => false, 'extra' => 'AUTO_INCREMENT'],
            ['name' => 'ListingKey', 'type' => 'VARCHAR(255)', 'null' => true],
            ['name' => 'PropertyType', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'PropertySubType', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'BedroomsTotal', 'type' => 'INT', 'null' => true],
            ['name' => 'BathroomsTotalInteger', 'type' => 'INT', 'null' => true],
            ['name' => 'City', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'StateOrProvince', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'PostalCode', 'type' => 'VARCHAR(20)', 'null' => true],
            ['name' => 'BuildingAreaTotal', 'type' => 'DECIMAL(10,2)', 'null' => true],
            ['name' => 'LotSizeArea', 'type' => 'DECIMAL(15,2)', 'null' => true],
            ['name' => 'LotFeatures', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'PoolFeatures', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'ParkingTotal', 'type' => 'INT', 'null' => true],
            ['name' => 'ExteriorFeatures', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'FoundationDetails', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'Cooling', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'HeatType', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'FireplaceFeatures', 'type' => 'VARCHAR(1024)', 'null' => true],
            ['name' => 'ModificationTimestamp', 'type' => 'TIMESTAMP', 'null' => true],
            ['name' => 'PhotosCount', 'type' => 'INT', 'null' => true],
            ['name' => 'PhotosChangeTimestamp', 'type' => 'TIMESTAMP', 'null' => true],
            ['name' => 'PublicRemarks', 'type' => 'TEXT', 'null' => true],
            ['name' => 'PriceChangeTimestamp', 'type' => 'TIMESTAMP', 'null' => true],
            ['name' => 'propertyArea', 'type' => 'VARCHAR(100)', 'null' => true],
            ['name' => 'propertyStatus', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'ListAgentFullName', 'type' => 'VARCHAR(100)', 'null' => true],
            ['name' => 'ListOfficeName', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'MlsStatus', 'type' => 'VARCHAR(20)', 'null' => true],
            ['name' => 'ListPrice', 'type' => 'VARCHAR(100)', 'null' => true],
            ['name' => 'SubdivisionName', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'CloseDate', 'type' => 'DATE', 'null' => true],
            ['name' => 'ClosePrice', 'type' => 'DECIMAL(10,2)', 'null' => true],
            ['name' => 'OriginalListPrice', 'type' => 'DECIMAL(10,2)', 'null' => true],
            ['name' => 'TaxAnnualAmount', 'type' => 'DECIMAL(10,2)', 'null' => true],
            ['name' => 'TaxYear', 'type' => 'INT', 'null' => true],
            ['name' => 'LotSizeSource', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'ListingContractDate', 'type' => 'DATE', 'null' => true],
            ['name' => 'Longitude', 'type' => 'DECIMAL(10,8)', 'null' => true],
            ['name' => 'Latitude', 'type' => 'DECIMAL(10,8)', 'null' => true],
            ['name' => 'Media', 'type' => 'JSON', 'null' => true],
            ['name' => 'otherjson', 'type' => 'JSON', 'null' => true],
            ['name' => 'agentData', 'type' => 'JSON', 'null' => true],
            ['name' => 'flag', 'type' => 'BOOLEAN', 'null' => true, 'default' => 0],
            ['name' => 'post_id', 'type' => 'INT', 'null' => true, 'default' => null], 
            ['name' => 'CountyOrParish', 'type' => 'VARCHAR(50)', 'null' => true, 'default' => null], 
            ['name' => 'created_at', 'type' => 'TIMESTAMP', 'null' => true, 'default' => 'CURRENT_TIMESTAMP'],
            ['name' => 'updated_at', 'type' => 'TIMESTAMP', 'null' => false, 'default' => 'CURRENT_TIMESTAMP', 'extra' => 'ON UPDATE CURRENT_TIMESTAMP'],
        ];
        
        self::create_table_if_not_exists('properties', $property_columns);
        self::check_properties_removed_property_table();
    
        self::add_indexes_to_property_table();
    }
    
    function check_properties_removed_property_table(){
        $cron_log_columns = [
            ['name' => 'id', 'type' => 'INT', 'null' => false, 'extra' => 'AUTO_INCREMENT'],
            ['name' => 'ModificationTimestamp', 'type' => 'TIMESTAMP', 'null' => true],
            ['name' => 'MlsStatus', 'type' => 'VARCHAR(20)', 'null' => true],
            ['name' => 'ListingKey', 'type' => 'VARCHAR(100)', 'null' => true],
            ['name' => 'post_id', 'type' => 'INT', 'null' => true],
            ['name' => 'City', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'SubdivisionName', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'PropertyType', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'PropertySubType', 'type' => 'VARCHAR(50)', 'null' => true],
            ['name' => 'deleted_at', 'type' => 'DATETIME', 'null' => true,'default' => 'CURRENT_TIMESTAMP']
        ];
        self::create_table_if_not_exists('temp_deleted_listings', $cron_log_columns);
    }

    function add_indexes_to_property_table()
    {
        global $wpdb;
    
        
        $indexes = [
            'ListingKey' => 'index_listing_id',
            'post_id' => 'index_post_id',
            'flag' => 'index_flag', 
        ];
    
        
        foreach ($indexes as $column => $index_name) {
            $sql = "SHOW INDEX FROM properties WHERE Key_name = '$index_name'";
    
            
            $index_exists = $wpdb->get_var($sql);
    
            if (!$index_exists) {
                
                $wpdb->query("ALTER TABLE properties ADD INDEX `$index_name` (`$column`)");
                echo "Index added for $column \n";
            }
        }
    }
    

    function cronLogTableCheck()
    {

        $cron_log_columns = [
            ['name' => 'id', 'type' => 'INT', 'null' => false, 'extra' => 'AUTO_INCREMENT'],
            ['name' => 'cron_start_time', 'type' => 'DATETIME', 'null' => true],
            ['name' => 'cron_end_time', 'type' => 'DATETIME', 'null' => true],
            ['name' => 'property_download_start_time', 'type' => 'DATETIME', 'null' => true],
            ['name' => 'property_download_end_time', 'type' => 'DATETIME', 'null' => true],
            ['name' => 'records_downloaded', 'type' => 'INT', 'null' => true],
            ['name' => 'records_updated', 'type' => 'INT', 'null' => true],
            ['name' => 'Class_name', 'type' => 'VARCHAR(255)', 'collation' => 'utf8mb4_general_ci', 'null' => true],
            ['name' => 'success', 'type' => 'TINYINT', 'null' => true],
            ['name' => 'query_url', 'type' => 'VARCHAR(1024)', 'collation' => 'utf8mb4_general_ci', 'null' => true],
            ['name' => 'mls_count', 'type' => 'INT', 'null' => true],
        ];
        self::create_table_if_not_exists('cron_log', $cron_log_columns);
    }
    function logerrorCheck() {
        $errorlog_columns = [
            ['name' => 'id', 'type' => 'INT', 'null' => false, 'extra' => 'AUTO_INCREMENT'],
            ['name' => 'message', 'type' => 'TEXT', 'null' => false],
            ['name' => 'file', 'type' => 'VARCHAR(255)', 'null' => true],
            ['name' => 'line', 'type' => 'INT', 'null' => true],
            ['name' => 'code', 'type' => 'INT', 'null' => true],
            ['name' => 'created_at', 'type' => 'DATETIME', 'null' => true],
        ];
    
        self::create_table_if_not_exists('error_log', $errorlog_columns);
    }    
}