<?php

class Tac_StopHumanSpam_Install
{
	public static function installCode()
	{
		$db = XenForo_Application::get('db');

		$db->query("
			CREATE TABLE IF NOT EXISTS `sf_stophumanspam_log` (
			  `log_id` int(11) NOT NULL AUTO_INCREMENT,
			  `log_date` int(11) NOT NULL,
			  `user_id` int(11) NOT NULL,
			  `username` varchar(50) NOT NULL,
			  `error_msg` text NOT NULL,
			  `content` text NOT NULL,
			  `location` text NOT NULL,
			  PRIMARY KEY (`log_id`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8;
		");

		return true;	
	}

	
	
	
	public static function uninstallCode()
	{
		$db = XenForo_Application::get('db');		
		// drop tables

		$db->query("
			DROP TABLE IF EXISTS
				`sf_stophumanspam_log`
		");

		return true;
	}

	
	public static function addColumnIfNotExist($db, $table, $field, $attr)
	{
		if ($db->fetchRow('SHOW columns FROM `'.$table.'` WHERE Field = ?', $field))
		{
			return false;
		}	
		return $db->query("ALTER TABLE `".$table."` ADD `".$field."` ".$attr);
	}
	
	
	public static function tableHasRows($db, $tableName, $rowName = "node_id")
	{
		$stmt  = $db->query('SELECT COUNT('.$rowName.') AS c FROM '.$tableName.' LIMIT 1');
		$row = $stmt->fetch();
		$num_rows = $row['c'];
		if($num_rows > 0){return true;}
		return false;	
	}
	

	
	

}
