<?php
class Modelmodulesocnetauth extends Model 
{
	public function createTables() 
	{
		$sql = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "socnetauth_preregistration` (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				`soc_id` varchar(100) NOT NULL,
				`type` varchar(100) NOT NULL,
				`firstname` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
				`lastname` varchar(200) NOT NULL,
				`email` varchar(200) NOT NULL,
				`profile` varchar(200) NOT NULL,
				`precode` varchar(100) NOT NULL,
				`cdate` datetime NOT NULL,
				`city` varchar(300) NOT NULL,
				PRIMARY KEY (`id`)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
		
		$this->db->query($sql);
		
		
		$sql = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "socnetauth_records` (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				`state` varchar(100) NOT NULL,
				`redirect` varchar(300) NOT NULL,
				`cdate` datetime NOT NULL,
				PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		
		$this->db->query($sql);
		
		
		$query = $this->db->query("SELECT * FROM information_schema.COLUMNS
								   WHERE TABLE_NAME = '" . DB_PREFIX . "customer'");
								   
		$column_hash = array();
		
		foreach($query->rows as $row )
		{
			$column_hash[ $row['COLUMN_NAME'] ] = 1;
		}
		
		if( !isset( $column_hash['socnetauth_facebook_id'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_facebook_id` VARCHAR( 100 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_facebook_profile'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_facebook_profile` VARCHAR( 200 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_vkontakte_id'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_vkontakte_id` VARCHAR( 100 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_vkontakte_profile'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_vkontakte_profile` VARCHAR( 200 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_twitter_id'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_twitter_id` VARCHAR( 100 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_twitter_profile'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_twitter_profile` VARCHAR( 200 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_odnoklassniki_id'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_odnoklassniki_id` VARCHAR( 100 ) NOT NULL";
			$this->db->query($sql);
		}
		
		if( !isset( $column_hash['socnetauth_odnoklassniki_profile'] ) )
		{
			$sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `socnetauth_odnoklassniki_profile` VARCHAR( 100 ) NOT NULL";
			$this->db->query($sql);
		}
	}
	
	public function dropTables()
	{
		$this->db->query("DROP TABLE " . DB_PREFIX . "socnetauth_preregistration");
		$this->db->query("DROP TABLE " . DB_PREFIX . "socnetauth_records");
	}
	
}
?>