############################################################## 
## MOD Title: Lottery Modification
## MOD Author: Zarath < null@null.com > ( Jamie ) http://www.zarath.com
## MOD Description: Allows you to set up a lottery for your forum.
## MOD Version: 2.1.2
## 
## Installation Level: Easy
## Installation Time: 5 Minutes 
## Files To Edit: includes/constants.php
##                includes/page_header.php
##                language/lang_English/lang_main.php
##                language/lang_English/lang_admin.php
##                templates/subSilver/overall_header.tpl
## Included Files: lottery.php
##                 admin/admin_lottery.php
##                 templates/lottery_index_body.tpl
##                 templates/lottery_history_body.tpl
##                 templates/admin/lottery_config_body.tpl
############################################################## 
## Author Notes: Please check
## http://www.zarath.com if you're having any troubles before 
## posting. Posts should be on http://forums.knightsofchaos.com 
## or http://www.phpbbhacks.com
## Any major problems or bug fixes will be posted there. Also 
## make sure you're using the latest version of the mod.
##
## If you're updating from 2.x.x just upload new lottery.php
##
## You may also need to add the "duration" function to your functions.php
## file now.
############################################################## 
## MOD History:
## v1.0.0  First version.
## v1.1.0  Fixed several bugs.
## v2.0.0  Complete overhaul.
## v2.0.4  Bug fixes -- just upload lottery.php over old one.
## v2.1.1  Bug fixes in drawing. Added limitation to tickets.
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

# 
#-----[ COPY ]------------------------------------------ 
#
copy lottery.php to lottery.php
copy mod_upgrade.php to mod_upgrade.php
copy admin/admin_lottery.php to admin/admin_lottery.php
copy templates/lottery_index_body.tpl to templates/*/lottery_index_body.tpl
copy templates/lottery_history_body.tpl to templates/*/lottery_history_body.tpl
copy templates/admin/lottery_config_body.tpl to templates/*/admin/lottery_config_body.tpl

# 
#-----[ SQL ]------------------------------------------ 
#
#   Only do these SQL queries if you can not run the mod_update.php file.
#   Otherwise, please run (go to it in your browswer) that as it's easier.
#
#   If you don't use MySQL, you'll need to edit these queries accordingly
# 
#   If you have a different table prefix then change these commands accordingly. 
#   These currently use the default table prefix. 

DELETE FROM `phpbb_config` WHERE config_name='lottery_lastwon';

UPDATE `phpbb_config` set config_value = '0' where config_name = 'lottery_reset';

UPDATE `phpbb_config` set config_value = '0' where config_name = 'lottery_status';

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_items', '0');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_win_items', '');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_show_entries', '0');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_mb', '0');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_mb_amount', '1');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_history', '1');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_currency', '');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_item_mcost', '1');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_item_xcost', '500');

INSERT INTO `phpbb_config` (config_name, config_value) VALUES ('lottery_random_shop', '');

CREATE TABLE `phpbb_lottery_history`
	(
		`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, 
		`user_id` INT (20) NOT NULL, 
		`amount` INT (20) NOT NULL, 
		`currency` CHAR (32) NOT NULL, 
		`time` INT (20) NOT NULL, PRIMARY KEY(`id`), INDEX(`user_id`)
	);

# 
#-----[ OPEN ]------------------------------------------ 
# 
includes/constants.php

# 
#-----[ FIND ]------------------------------------------ 
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------ 
#
define('LOTTERY_TABLE', $table_prefix.'lottery');
define('LOTTERY_HISTORY_TABLE', $table_prefix.'lottery_history');

# 
#-----[ OPEN ]------------------------------------------------ 
# Only do these 3 steps if you haven't done it for any other of 
# my other mods! Duration function...
#

 includes/functions.php

# 
#-----[ FIND ]------------------------------------------------ 
# 
?>

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
function duration($seconds)
{
	global $lang;

	if ( $seconds > 86399 )
	{
		$days = floor($seconds / 86400);
		$seconds = ($seconds - ($days * 86400));
		$string .= ( $days > 1 ) ? $days .' ' . $lang['jobs_days'] . ', ' : $days .' ' . $lang['jobs_day'] . ', ';
	}
	if ( $seconds > 3599 )
	{
		$hours = floor($seconds / 3600);

		if ( $seconds != 0 )
		{
			$string .= ( $hours > 1 ) ? $hours .' ' . $lang['jobs_hours'] . ', ' : $hours .' ' . $lang['jobs_hour'] . ', ';
		}

		$seconds = ( $days > 0 ) ? 0 : ( $seconds - ($hours * 3600) );
	}
	if ( $seconds > 59 )
	{
		$minutes = floor($seconds / 60);
		if ( $seconds != 0 )
		{
			$string .= ( $minutes > 1) ? $minutes .' ' . $lang['jobs_minutes'] . ', ' : $minutes .' ' . $lang['jobs_minute'] . ', ';
		}

		$seconds = ( $hours > 0 ) ? 0 : ($seconds - ($minutes * 60));
	}
	if ( $seconds > 0 )
	{
		$string .= ( $seconds > 1 ) ? $seconds . ' ' . $lang['jobs_seconds'] . ', ' : $seconds . ' ' . $lang['jobs_second'] . ', ';
	}

	$string = substr($string, 0, -2);
	return $string;
}

#
#-----[ OPEN ]------------------------------------------ 
#
includes/page_header.php

# 
#-----[ FIND ]------------------------------------------ 
#
	'PAGE_TITLE' => $page_title,

#
#-----[ AFTER, ADD ]------------------------------------------ 
#
	'L_LOTTERY' => $lang['lottery']

# 
#-----[ OPEN ]------------------------------------------ 
# 
language/lang_English/lang_main.php

# 
#-----[ FIND ]------------------------------------------ 
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------ 
#
// Lottery Variables
$lang['lottery_current_history'] = 'Current History';
$lang['lottery_no_history'] = 'There is currently no history recorded!';
$lang['lottery_history_disabled'] = 'Lottery history is disabled on these forums!';
$lang['lottery_ticket_bought'] = 'Your ticket in the %s has been successfully purchased.';
$lang['lottery_tickets_bought'] = 'Your %s tickets in the %s have been successfully purchased.';
$lang['lottery_purchased_ticket'] = ' to purchase a ticket!';
$lang['lottery_purchased_tickets'] = ' to purchase %s tickets!';
$lang['lottery_purchased_ne'] = 'You do not have enough ';
$lang['lottery_too_many_tickets'] = 'You have already purchased the maximum amount of tickets allowed in this lottery! Please wait until the next draw.';
$lang['lottery_information'] = 'Information';
$lang['lottery_actions'] = 'Lottery Actions';
$lang['lottery_disabled'] = 'The lottery is currently turned off!<br /><br />Try again later.';
$lang['lottery_ID'] = 'ID';
$lang['lottery_winner'] = 'Winner';
$lang['lottery_amount_won'] = 'Amount Won';
$lang['lottery_time_won'] = 'Time Won';
$lang['lottery_total_history'] = 'There are a total of %s lottery history entries.';
$lang['lottery_history'] = 'History';
$lang['lottery_tickets_owned'] = 'Tickets Owned';
$lang['lottery_ticket_cost'] = 'Ticket Cost';
$lang['lottery_base_pool'] = 'Base Prize Pool';
$lang['lottery_current_entries'] = 'Current Entries';
$lang['lottery_total_pool'] = 'Total Prize Pool';
$lang['lottery_item_draw'] = 'Items in Draw';
$lang['lottery_time_draw'] = 'Time Until Drawn';
$lang['lottery_last_winner'] = 'Last Winner';
$lang['lottery_buy_ticket'] = 'Buy Ticket';
$lang['lottery_buy_tickets'] = 'Buy Tickets';
$lang['lottery_view_history'] = 'View Lottery History';
$lang['lottery_view_phistory'] = 'View Personal History';

// Lottery Error Variables
$lang['lottery_error_updating'] = 'Error updating %s table!';
$lang['lottery_error_deleting'] = 'Error deleting from %s table!';
$lang['lottery_error_selecting'] = 'Error getting information from %s table!';
$lang['lottery_error_inserting'] = 'Error inserting into %s table!';
$lang['lottery_error_variable'] = 'Could not read %s variable!';
$lang['lottery_invalid_command'] = 'That is not a valid command!';
$lang['lottery_no_history_type'] = 'No history type selected!';


# 
#-----[ FIND ]------------------------------------------ 
# Only do these 2 steps if you haven't done it for any other of 
# my other mods! Duration function language variables...
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------ 
#
$lang['jobs_second'] = 'Second';
$lang['jobs_seconds'] = 'Seconds';
$lang['jobs_minute'] = 'Minute';
$lang['jobs_minutes'] = 'Minutes';
$lang['jobs_hour'] = 'Hour';
$lang['jobs_hours'] = 'Hours';
$lang['jobs_day'] = 'Day';
$lang['jobs_days'] = 'Days';

# 
#-----[ OPEN ]------------------------------------------ 
# 
language/lang_English/lang_admin.php

# 
#-----[ FIND ]------------------------------------------ 
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------ 
#
// Lottery Variables
$lang['lottery_second'] = 'Second';
$lang['lottery_seconds'] = 'Seconds';
$lang['lottery_minute'] = 'Minute';
$lang['lottery_minutes'] = 'Minutes';
$lang['lottery_hour'] = 'Hour';
$lang['lottery_hours'] = 'Hours';
$lang['lottery_day'] = 'Day';
$lang['lottery_days'] = 'Days';
$lang['lottery_no_items'] = 'There are no items in the shop database!';
$lang['lottery_rand'] = 'Random';
$lang['lottery_statistics'] = 'Lottery Statistics';
$lang['lottery_edit_settings'] = 'Edit Lottery Settings';
$lang['lottery_no_one'] = 'No One';
$lang['lottery_editor'] = 'Lottery Editor';
$lang['lottery_index_explain'] = 'This section allows you to edit the lottery settings.';
$lang['lottery_no_item'] = 'No such item exists!';
$lang['lottery_invalid_action'] = 'Invalid Action!';
$lang['lottery_click_to_return'] = 'Click %sHere%s to return to Lottery Settings.';
$lang['lottery_random_items_updated'] = 'Random item settings sucessfully updated!';
$lang['lottery_item_added'] = 'Item successfully added to item pool!';
$lang['lottery_item_removed'] = 'Item successfully removed from item pool!';
$lang['lottery_updated'] = 'Lottery sucessfully updated!';
$lang['lottery_status'] = 'Lottery Status';
$lang['lottery_add_item'] = 'Add Item';
$lang['lottery_add_items'] = 'Add Item to Pool';
$lang['lottery_remove_item'] = 'Remove Item';
$lang['lottery_current_items'] = 'Current Item Pool';
$lang['lottery_update_settings'] = 'Update Settings';
$lang['lottery_max_cost'] = 'Maximum Cost';
$lang['lottery_min_cost'] = 'Minimum Cost';
$lang['lottery_all_shops'] = 'All Shops';
$lang['lottery_from_shop'] = 'From Shop';
$lang['lottery_update'] = 'Update';
$lang['lottery_currency'] = 'Currency to Use';
$lang['lottery_history'] = 'Lottery History';
$lang['lottery_item_pool'] = 'Item Pool';
$lang['lottery_full_display'] = 'Full Display';
$lang['lottery_max'] = 'max';
$lang['lottery_off'] = 'Off';
$lang['lottery_on'] = 'On';
$lang['lottery_mult_tickets'] = 'Multiple Tickets';
$lang['lottery_multiple'] = 'Multiple';
$lang['lottery_single'] = 'Single';
$lang['lottery_tickets_allowed'] = 'Tickets Allowed';
$lang['lottery_draw_periods'] = 'Lottery Draw Periods';
$lang['lottery_entry_cost'] = 'Entry Cost';
$lang['lottery_base_amount'] = 'Lottery Base Amount';
$lang['lottery_name'] = 'Lottery Name';
$lang['lottery_auto_restart'] = 'Auto Restart';
$lang['lottery_last_won'] = 'Last Won';
$lang['lottery_pool'] = 'Lottery Pool';
$lang['lottery_time_left'] = 'Time Left';
$lang['lottery_duration_left'] = 'Duration Left';
$lang['lottery_total_entries'] = 'Total Entries';
$lang['lottery_items_table'] = 'Edit Item Pool';
$lang['lottery_items_settings'] = 'Edit Item Settings';

// Lottery Error Variables
$lang['lottery_error_updating'] = 'Error updating %s table!';
$lang['lottery_error_deleting'] = 'Error deleting from %s table!';
$lang['lottery_error_selecting'] = 'Error getting information from %s table!';
$lang['lottery_error_inserting'] = 'Error inserting into %s table!';
$lang['lottery_error_variable'] = 'Could not read %s variable!';

#
#-----[ OPEN ]------------------------------------------ 
#
templates/subSilver/overall_header.tpl

# 
#-----[ FIND ]------------------------------------------ 
#
{LOTTERY}

#
#-----[ REPLACE, WITH ]------------------------------------------ 
#
{L_LOTTERY}

# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM