<?php
/*
Plugin Name: Real Time Market Prices
Plugin URI: http://www.livecharts.co.uk/marketpriceswidget.php
Description: A plugin that displays real time market prices.
Author: Livecharts
Author URI: http://livecharts.co.uk
Version: 1.0
Date: 2010-09-09
*/

/*  Copyright 2009  Livecharts  (email : lee@livecharts.co.uk)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


register_activation_hook( __FILE__, 'lcwprices_plugin_activate' );
register_deactivation_hook ( __FILE__, 'lcwprices_plugin_deactivate' );
add_action ('admin_menu', 'lcwprices_add_option_page');
function lcwprices_plugin_activate(){
	//delete_option("lcwprices_options");
}
function lcwprices_plugin_deactivate(){
	//delete_option("lcwprices_options");
}

function lcwprices_add_option_page() {
	$options_page = add_options_page('Real Time Market Prices Options', 'Real Time Market Prices', 8, __FILE__, 'lcwprices_options_page');
	add_action ('admin_head-'. $options_page, 'lcwprices_head');	
	
	$options                    = array();
	$options['title']           = "Real Time Market Prices";
	$options['titleColor']      = "#999999";
	$options['titleBGColor']    = "#ffffff";
	$options['borderColor']     = "#dddddd";
	$options['backgroundColor'] = "#ffffff";
	$options['symbols']         = "1,21,25";
	add_option('lcwprices_options', $options, 'Options for the Real Time Market Prices plugin');
}
function lcwprices_head() {
	$baseUrl = lcwprices_getbaseurl();
	?>
	<link rel="stylesheet" href="<?php echo $baseUrl ?>/colorpicker/css/colorpicker.css" type="text/css" media="screen" />
	<script type="text/javascript" src="<?php echo $baseUrl ?>/colorpicker/js/jquery.js"></script>
	<script type="text/javascript" src="<?php echo $baseUrl ?>/colorpicker/js/colorpicker.js"></script>
	<?php
}

/* UTIL FUNCTIONS */
function lcwprices_slash_clean($post){
	//if ( get_magic_quotes_gpc() ) {
		return stripslashes($post);
	//}
	//return $post;
}
function lcwprices_getbaseurl() {
	$scheme            = (strpos($_SERVER['SERVER_PROTOCOL'], 'HTTPS') === false ? "http://" : "https://");
	$thisScriptPath    = str_replace("\\", "/", dirname(__FILE__));
	$documentRoot      = str_replace("\\", "/", $_SERVER['DOCUMENT_ROOT']);
	$thisScriptURLPath = str_replace($documentRoot, "", $thisScriptPath);
	$baseURL           = $scheme . $_SERVER['SERVER_NAME'] . $thisScriptURLPath;
	return $baseURL;
}
function lcwprices_get_template_content(){
	// HTTP request to livecharts
	$content = '	
	<div id="lcw-mp-container" style="width:300px; border: 0px solid red;line-height:1;">
		<div id="lcw-mp-links" style="font-family: Arial;color:#AAA;font-size: 9px;">
			<center>Powered by LiveCharts.Co.Uk <a rel="nofollow" onmouseout="this.style.color=\'#999\'" onmouseover="this.style.color=\'#0011CC\'" style="text-decoration: none; color:#999;" id="lcw-mp-linkone" href="http://www.livecharts.co.uk/share_prices/share_price.php">UK Share Prices</a> and <a rel="nofollow" onmouseout="this.style.color=\'#999\'" onmouseover="this.style.color=\'#0011CC\'" style="text-decoration: none; color:#999" id="lcw-mp-linktwo" href="http://www.livecharts.co.uk/MarketCharts/gold.php">Gold Price</a></center>
		</div>
		<script type="text/javascript">
			var lcw_mp_title           = "{TITLE}";
			var lcw_mp_titleColor      = "{TITLE_COLOR}";
			var lcw_mp_titleBGColor    = "{TITLE_BGCOLOR}";
			var lcw_mp_borderColor     = "{BORDER_COLOR}";
			var lcw_mp_backgroundColor = "{BACKGROUND_COLOR}";
			var lcw_mp_symbols         = "{SYMBOLS}"; 
		</script>
		<script type="text/javascript" src="http://www.livecharts.co.uk/widgets/market_prices/mp.js"></script>
	</div>';
	return $content;
}
function lcwprices_getsymbols() {
	// HTTP request to livecharts
	$symbols     = array();
	$symbols[0]  = "-- None --";
	$symbols[1]="EURUSD";
	$symbols[2]="GBPUSD";
	$symbols[3]="USDCHF";
	$symbols[4]="USDJPY";
	$symbols[9]="USDCAD";
	$symbols[10]="AUDUSD";
	$symbols[11]="NZDUSD";
	$symbols[12]="USDNOK";
	$symbols[13]="USDDKK";
	$symbols[14]="USDSEK";
	$symbols[29]="EURSEK";
	$symbols[60]="AUDJPY";
	$symbols[507]="USDEUR";
	$symbols[508]="USDGBP";
	$symbols[509]="EURJPY";
	$symbols[510]="EURGBP";
	$symbols[511]="EURCHF";
	$symbols[512]="JPYUSD";
	$symbols[513]="JPYEUR";
	$symbols[514]="JPYGBP";
	$symbols[515]="JPYCHF";
	$symbols[516]="GBPEUR";
	$symbols[517]="GBPJPY";
	$symbols[518]="GBPCHF";
	$symbols[519]="CHFUSD";
	$symbols[520]="CHFEUR";
	$symbols[521]="CHFJPY";
	$symbols[522]="CHFGBP";
	$symbols[21]="Dow";
	$symbols[22]="S&P";
	$symbols[345]="Futsee";
	$symbols[25]="Dax";
	$symbols[504]="Oil";
	$symbols[333]="Gold";
	$symbols[334]="Silver";
	return $symbols;	
}
function lcwprices_genoptions($options, $selected) {
	foreach ($options AS $val => $label) {
		?>
		<option <?php echo ($val == $selected ? ' selected="selected"' : '') ?> value="<?php echo $val ?>"><?php echo $label ?></option>
		<?php
	}
}
/* END UTIL FUNCTIONS */

function get_market_prices(){
	$options    = get_option('lcwprices_options');
	$widgetCode = lcwprices_get_template_content();
	
	$widgetCode = str_replace(
		array(
			"{TITLE}", 
			"{TITLE_COLOR}", 
			"{TITLE_BGCOLOR}", 
			"{BORDER_COLOR}", 
			"{BACKGROUND_COLOR}", 
			"{SYMBOLS}"
		), 
		array(
			trim(addslashes($options['title'])), 
			trim($options['titleColor']),  
			trim($options['titleBGColor']), 
			trim($options['borderColor']), 
			trim($options['backgroundColor']),
			trim($options['symbols']) 
		),
		$widgetCode
	);
	echo  $widgetCode;
}
function lcwprices_options_page(){
	if ( count($_POST) ) {
		$ops                    = array();
		$ops['title']           = lcwprices_slash_clean($_POST['widget_title']);
		$ops['titleColor']      = lcwprices_slash_clean($_POST['widget_titleColor']);
		$ops['titleBGColor']    = lcwprices_slash_clean($_POST['widget_titleBGColor']);
		$ops['borderColor']     = lcwprices_slash_clean($_POST['widget_borderColor']);
		$ops['backgroundColor'] = lcwprices_slash_clean($_POST['widget_backgroundColor']);
		$ops['symbols']         = lcwprices_slash_clean($_POST['widget_symbols']);
		update_option("lcwprices_options", $ops);
		echo '<div class="updated"><p>Plugin settings saved.</p></div>';
	}
	$arrSymbols   = lcwprices_getsymbols(); 
	$options      = get_option('lcwprices_options');
	$symbols      = explode(";", $options['symbols']);
	$row1_symbols = @explode(",", $symbols[0]);
	$row2_symbols = @explode(",", $symbols[1]);
	$row3_symbols = @explode(",", $symbols[2]);
	?>
<div class='wrap'>
<h2>Real Time Market Prices Options</h2>	
<form method="post" id="frmOptions">	
<table cellpadding="0" cellspacing="0" border="0" style="width:500px; border: 0px solid red;">
	<tr>
		<td>
			Title
		</td>
		<td>
			<input type="text" style="width:100%;" value="<?php echo htmlspecialchars($options['title']) ?>" id="widget_title" name="widget_title" />
			<input type="hidden" value="<?php echo htmlspecialchars($options['titleColor']) ?>" id="widget_titleColor" name="widget_titleColor" />
			<input type="hidden" value="<?php echo htmlspecialchars($options['titleBGColor']) ?>" id="widget_titleBGColor" name="widget_titleBGColor" />
			<input type="hidden" value="<?php echo htmlspecialchars($options['borderColor']) ?>" id="widget_borderColor" name="widget_borderColor" />
			<input type="hidden" value="<?php echo htmlspecialchars($options['backgroundColor']) ?>" id="widget_backgroundColor" name="widget_backgroundColor" />
			<input type="hidden" value="<?php echo htmlspecialchars($options['symbols']) ?>" id="widget_symbols" name="widget_symbols" />
		</td>
	</tr>
	<tr>
		<td>
			Title color
		</td>
		<td>
			<div id="cp-titleColor" class="colorpickerButton">
				<div style="background-color: <?php echo $options['titleColor'] ?>;"></div>
			</div>
			<script type="text/javascript">
			$('#cp-titleColor').ColorPicker({
				color: "<?php echo $options['titleColor'] ?>",
				onShow: function (colpkr) {
					$(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					$(colpkr).fadeOut(500);
					$("#cp-titleColor").ColorPickerSetColor(wc_titleColor);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					$('#cp-titleColor div').css('backgroundColor', "#" + hex);
					$("#widget_titleColor").val("#" + hex);
				}
			});
			</script>
		</td>
	</tr>
	<tr>
		<td>
			Title background color
		</td>
		<td>
			<div id="cp-titleBackgroundColor" class="colorpickerButton">
				<div style="background-color: <?php echo $options['titleBGColor'] ?>;"></div>
			</div>
			<script type="text/javascript">
			$('#cp-titleBackgroundColor').ColorPicker({
				color: "<?php echo $options['titleBGColor'] ?>",
				onShow: function (colpkr) {
					$(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					$(colpkr).fadeOut(500);
					$("#cp-titleBackgroundColor").ColorPickerSetColor(wc_titleBGColor);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					$('#cp-titleBackgroundColor div').css('backgroundColor', "#" + hex);
					$("#widget_titleBGColor").val("#" + hex);
				}
			});
			</script>
		</td>
	</tr>
	<tr>
		<td>
			Border color
		</td>
		<td>
			<div id="cp-borderColor" class="colorpickerButton">
				<div style="background-color: <?php echo $options['borderColor'] ?>;"></div>
			</div>
			<script type="text/javascript">
			$('#cp-borderColor').ColorPicker({
				color: "<?php echo $options['borderColor'] ?>",
				onShow: function (colpkr) {
					$(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					$(colpkr).fadeOut(500);
					$("#cp-borderColor").ColorPickerSetColor(wc_borderColor);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					$('#cp-borderColor div').css('backgroundColor', "#" + hex);
					$("#widget_borderColor").val("#" + hex);
				}
			});
			</script>
		</td>
	</tr>
	<tr>
		<td>
			Widget background color
		</td>
		<td>
			<div id="cp-backgroundColor" class="colorpickerButton">
				<div style="background-color: <?php echo $options['backgroundColor'] ?>;"></div>
			</div>
			<script type="text/javascript">
			$('#cp-backgroundColor').ColorPicker({
				color: "<?php echo $options['backgroundColor'] ?>",
				onShow: function (colpkr) {
					$(colpkr).fadeIn(500);
					return false;
				},
				onHide: function (colpkr) {
					$(colpkr).fadeOut(500);
					$("#cp-backgroundColor").ColorPickerSetColor(wc_backgroundColor);
					return false;
				},
				onChange: function (hsb, hex, rgb) {
					$('#cp-backgroundColor div').css('backgroundColor', "#" + hex);
					$("#widget_backgroundColor").val("#" + hex);
				}
			});
			</script>
		</td>
	</tr>
	
	<tr>
		<td>
			Symbols row 1
		</td>
		<td>
			<select name="row1-symbol1" id="row1-symbol1">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row1_symbols[0]);
			?>
			</select>
			<select name="row1-symbol2" id="row1-symbol2">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row1_symbols[1]);
			?>
			</select>
			<select name="row1-symbol3" id="row1-symbol3">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row1_symbols[2]);
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td>
			Symbols row 2
		</td>
		<td>
			<select name="row2-symbol1" id="row2-symbol1">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row2_symbols[0]);
			?>
			</select>
			<select name="row2-symbol2" id="row2-symbol2">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row2_symbols[1]);
			?>
			</select>
			<select name="row2-symbol3" id="row2-symbol3">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row2_symbols[2]);
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td>
			Symbols row 3
		</td>
		<td>
			<select name="row3-symbol1" id="row3-symbol1">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row3_symbols[0]);
			?>
			</select>
			<select name="row3-symbol2" id="row3-symbol2">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row3_symbols[1]);
			?>
			</select>
			<select name="row3-symbol3" id="row3-symbol3">
			<?php
			lcwprices_genoptions($arrSymbols, @(int)$row3_symbols[2]);
			?>
			</select>
		</td>
	</tr>
	
	<tr>
		<td colspan="2" align="center" style="padding-top: 10px; padding-bottom: 10px;">
			<input type="button" value="Apply" onclick="onUpdate()">
		</td>
	</tr>
</table>
</form>
<h2>Preview</h2>
<div style="border:1px solid #DDDDDD; padding: 8px; width:500px;">
	<?php get_market_prices() ?>
</div>
</div>

<script type="text/javascript">
	function onUpdate() {
		var symbols = "";
		if ( $("#row1-symbol1").val() != "0" && $("#row1-symbol2").val() != "0" && $("#row1-symbol3").val() != "0" ) {
			symbols = $("#row1-symbol1").val() + "," + $("#row1-symbol2").val() + "," + $("#row1-symbol3").val();
		}
		if ( $("#row2-symbol1").val() != "0" && $("#row2-symbol2").val() != "0" && $("#row2-symbol3").val() != "0" ) {
			symbols += ";" + $("#row2-symbol1").val() + "," + $("#row2-symbol2").val() + "," + $("#row2-symbol3").val();
		}
		if ( $("#row3-symbol1").val() != "0" && $("#row3-symbol2").val() != "0" && $("#row3-symbol3").val() != "0" ) {
			symbols += ";" + $("#row3-symbol1").val() + "," + $("#row3-symbol2").val() + "," + $("#row3-symbol3").val();
		}
		$("#widget_symbols").val(symbols);
		$("#frmOptions").submit();
	}
</script>
	<?php
}
function lcwprices_sidebar_init() {

    function lcwprices_sidebar($args) {
        extract($args);
        echo $before_widget;
        echo $before_title . $after_title;
        echo get_market_prices();
        echo $after_widget;
    }
	if ( function_exists('wp_register_sidebar_widget') ) {
		wp_register_sidebar_widget('lcw_prices', 'Real Time Market Prices', 'lcwprices_sidebar');
	}
	else if ( function_exists('register_sidebar_widget') ) {
		register_sidebar_widget('Real Time Market Prices', 'lcwprices_sidebar');	
	}
	else {
		return;
	}
    
    //wp_register_sidebar_widget('lcw_prices', 'Real Time Market Prices', 'lcwprices_sidebar');
}

add_action('plugins_loaded', 'lcwprices_sidebar_init');
?>