<?php

// security check
if( ! defined( 'WP_PLUGIN_DIR' ) )
{
	die( 'There is nothing to see here.' );
}

$counterize_plugin_dir = WP_PLUGIN_DIR . '/' . plugin_basename( dirname( __FILE__ ) );
if( counterize_is_ssl() )
{
	$counterize_plugin_url = str_replace( 'http://', 'https://', WP_PLUGIN_URL ) . '/' . plugin_basename( dirname( __FILE__ ) );
}
else
{
	$counterize_plugin_url = WP_PLUGIN_URL . '/' . plugin_basename( dirname( __FILE__ ) );
}

// Small info on DashBoard-page
function counterize_dashboard_callback()
{
	//what was the two following lines supposed to do?...
	//$admin = dirname( $_SERVER['SCRIPT_FILENAME'] );
	//$admin = substr( $admin, strrpos( $admin, '/' ) + 1 );
	$count = counterize_getamount();
	$unique = counterize_getuniqueamount();
	$todaycount = counterize_gethitstoday();
	$online = counterize_get_online_users();
	$todayunique = counterize_getuniquehitstoday();

	?>

		<p>
			<?php _e( 'Total: ', COUNTERIZE_TD ); ?><strong><?php echo $count; ?></strong> <?php _e( 'hits and', COUNTERIZE_TD )?> <strong><?php echo $unique; ?></strong> <?php _e( 'unique.' ); ?>
		</p>
		<p>
			<?php _e( 'Today: ', COUNTERIZE_TD ); ?><strong><?php echo $todaycount; ?></strong> <?php _e( 'hits and', COUNTERIZE_TD ); ?> <strong><?php echo $todayunique; ?></strong> <?php _e( 'unique.' ); ?>
		</p>
		<p>
			<?php _e( 'Currently: ', COUNTERIZE_TD ); ?><strong><?php echo $online; ?></strong> <?php _e( 'users online.', COUNTERIZE_TD ); ?>
		</p>

		<a href="admin.php?page=counterize_dashboard"><?php _e( 'Detailed view', COUNTERIZE_TD ); ?> &raquo;</a>
	<?php
}

//Content of the Counterize dashboard page
function counterize_display_dashboard_page_callback()
{
	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . '</h2>
	';

	//Show charts
	counterize_showStats( true );

	if( current_user_can( 'manage_options' ) )
	{

		$updateText = '';
		if( isset( $_GET['killmass'] ) && check_admin_referer( 'action_killmass' ) )
		{
			if( $_GET['killmass']=='yes' )
			{
				if( isset( $_POST['counterize_killemall'] ) )
				{
					foreach( $_POST['counterize_killemall'] as $key => $val )
					{
						$val = intval( $val );
						counterize_killEntry( $val );
						$updateText .= __( 'Entry: ' . $val . ' removed<br />', COUNTERIZE_TD );
					}
					counterize_updateText( $updateText );
					unset( $updateText );
				}
			}
		}

		// For the zap-an-entry-option
		if( isset( $_GET['kill'] ) && check_admin_referer( 'action_uri_kill' ) )
		{
			$val = intval( $_GET['kill'] );
			counterize_killEntry( $val );
			counterize_updateText( __( 'Deleting entry ', COUNTERIZE_TD ) . $val );
		}

		//Show latest entries
		counterize_show_history( 'counterize_dashboard', false );
	}

	//Print the footer
	counterize_pagefooter();
}


function counterize_display_dashboard_traffic_page_callback()
{
	$width = 250;
	$amount2 = 0;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Traffic', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_traffic( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_pages_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Popular pages', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_pages( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_ip_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'IP addresses', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_ip( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_countries_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Countries', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_countries( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_referers_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Referers', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_referers( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_browsers_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Browsers', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_browsers( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_os_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Operating systems', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_os( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_keywords_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'Keywords', COUNTERIZE_TD ) . '</h2>
	';

	counterize_show_data_keywords( true, $width, $amount2, true );

	//Print the footer
	counterize_pagefooter();
}

function counterize_display_dashboard_history_page_callback()
{
	// Amount to pass as option to the graphs...
	$amount2 = counterize_get_option( 'amount2' );
	if( $amount2 == '' || $amount2 == ' ' || ! is_numeric( $amount2 ) )
	{
		$amount2 = 10;
	}
	$width = 250;

	echo '
	<div id="icon-plugins" class="icon32"></div>
	<h2>' . __( 'Counterize', COUNTERIZE_TD ) . ' - ' . __( 'Dashboard', COUNTERIZE_TD ) . ' - ' . __( 'History', COUNTERIZE_TD ) . '</h2>
	';

	if( current_user_can( 'manage_options' ) )
	{

		$updateText = '';
		if( isset( $_GET['killmass'] ) && check_admin_referer( 'action_killmass' ) )
		{
			if( $_GET['killmass']=='yes' )
			{
				if( isset( $_POST['counterize_killemall'] ) )
				{
					foreach( $_POST['counterize_killemall'] as $key => $val )
					{
						$val = intval( $val );
						counterize_killEntry( $val );
						$updateText .= __( 'Entry: ' . $val . ' removed<br />', COUNTERIZE_TD );
					}
					counterize_updateText( $updateText );
					unset( $updateText );
				}
			}
		}

		// For the zap-an-entry-option
		if( isset( $_GET['kill'] ) && check_admin_referer( 'action_uri_kill' ) )
		{
			$val = intval( $_GET['kill'] );
			counterize_killEntry( $val );
			counterize_updateText( __( 'Deleting entry ', COUNTERIZE_TD ) . $val );
		}

		//Show latest entries
		counterize_show_history( 'counterize_dashboard_history',  true );
	}

	//Print the footer
	counterize_pagefooter();
}

?>