| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
die; |
| 4 |
} |
| 5 |
/*********************************Google Charts section**************************************/ |
| 6 |
|
| 7 |
function guaven_sqlcharts_google_shortcode($atts) |
| 8 |
{ |
| 9 |
wp_enqueue_script('guaven_sqlcharts_googlechart', 'https://www.gstatic.com/charts/loader.js'); |
| 10 |
wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('asset/guaven_sqlcharts.css', __FILE__),array(),GVNSQLCHARTS_VERSION); |
| 11 |
global $wpdb; |
| 12 |
$sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true)); |
| 13 |
$sql=gvn_chart_put_variables($sql,$atts['id']); |
| 14 |
$blacklister_f = gvn_chart_check_sql_query($sql); |
| 15 |
if ($blacklister_f == 1) |
| 16 |
return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries'; |
| 17 |
|
| 18 |
for($i=1;$i<20;$i++){ $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0; |
| 19 |
$sql=str_replace("{arg".$i."}",esc_sql($replacearg),$sql);} |
| 20 |
$sql_arr=explode(";",$sql); |
| 21 |
$fvs = $wpdb->get_results($sql_arr[0]); |
| 22 |
|
| 23 |
$wpdb->show_errors(); |
| 24 |
ob_start(); |
| 25 |
$wpdb->print_error(); |
| 26 |
$printerror = ob_get_clean(); |
| 27 |
|
| 28 |
if ($printerror != '' and strpos($printerror, "[]") === false) |
| 29 |
return $printerror; |
| 30 |
elseif (empty($fvs)) |
| 31 |
return 'Your SQL returnes empty date, please recheck your SQL query above'; |
| 32 |
else { |
| 33 |
ob_start(); |
| 34 |
global $sqlcharts_inserted_script; |
| 35 |
if (empty($sqlcharts_inserted_script)) |
| 36 |
$sqlcharts_inserted_script = 1;?> |
| 37 |
<script type="text/javascript">; |
| 38 |
google.charts.load('current', {'packages':[<?php |
| 39 |
$tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true); |
| 40 |
|
| 41 |
echo guaven_sqlcharts_libloads($tip_g, 'packages'); ?>]}); |
| 42 |
google.charts.setOnLoadCallback(drawChart_<?php echo $sqlcharts_inserted_script;?>); |
| 43 |
csv_data='';csv_title=''; |
| 44 |
function drawChart_<?php echo $sqlcharts_inserted_script;?>() { |
| 45 |
<?php |
| 46 |
$html_temp = ''; |
| 47 |
$csv_temp = ''; |
| 48 |
$post_g = get_post($atts['id']); |
| 49 |
$xarg_s = get_post_meta($atts['id'], 'guaven_sqlcharts_xarg_s', true); |
| 50 |
$xarg_l = get_post_meta($atts['id'], 'guaven_sqlcharts_xarg_l', true); |
| 51 |
$yarg_s = get_post_meta($atts['id'], 'guaven_sqlcharts_yarg_s', true); |
| 52 |
$yarg_l = get_post_meta($atts['id'], 'guaven_sqlcharts_yarg_l', true); |
| 53 |
|
| 54 |
$graph_width = get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true); |
| 55 |
$graph_height = get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true); |
| 56 |
|
| 57 |
if (!empty($atts["width"])) { |
| 58 |
$graph_width = intval($atts['width']); |
| 59 |
} |
| 60 |
if (!empty($atts["height"])) { |
| 61 |
$graph_height = intval($atts['height']); |
| 62 |
} |
| 63 |
|
| 64 |
foreach ($fvs as $fv) { |
| 65 |
$html_temp .= "['{$fv->$xarg_s}', {$fv->$yarg_s}, '#b87333'],"; |
| 66 |
$csv_temp .= addslashes($fv->$xarg_s) . "," . addslashes($fv->$yarg_s) . "<br>"; |
| 67 |
|
| 68 |
} |
| 69 |
?> |
| 70 |
csv_data='<?php echo $csv_temp;?>'; |
| 71 |
csv_title='<?php echo '' . $xarg_l . ',' . $yarg_l . '';?><br>'; |
| 72 |
var data = google.visualization.arrayToDataTable([ |
| 73 |
['<?php echo $xarg_l;?>', '<?php echo $yarg_l;?>', { role: 'style' }], |
| 74 |
<?php echo $html_temp;?> |
| 75 |
]); |
| 76 |
|
| 77 |
var options = { |
| 78 |
<?php echo $tip_g == '3dpie' ? "is3D: true," : '';?> |
| 79 |
chart_<?php echo $sqlcharts_inserted_script;?>: { |
| 80 |
title: '<?php echo $post_g->post_title;?>', |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
var chart_<?php |
| 85 |
echo $sqlcharts_inserted_script; |
| 86 |
?> = new google.visualization.<?php |
| 87 |
echo guaven_sqlcharts_libloads(get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true), 'charts'); |
| 88 |
?>(document.getElementById('columnchart_material_<?php echo $sqlcharts_inserted_script;?>')); |
| 89 |
|
| 90 |
|
| 91 |
var my_div = document.getElementById('chart_div_<?php echo $sqlcharts_inserted_script;?>'); |
| 92 |
google.visualization.events.addListener(chart_<?php echo $sqlcharts_inserted_script;?>, 'ready', function () { |
| 93 |
my_div.innerHTML = '<img src="' + chart_<?php echo $sqlcharts_inserted_script;?>.getImageURI() + '">'; |
| 94 |
}); |
| 95 |
|
| 96 |
chart_<?php echo $sqlcharts_inserted_script;?>.draw(data, options); |
| 97 |
} |
| 98 |
</script> |
| 99 |
|
| 100 |
<?php echo gvn_chart_top_form($atts);?> |
| 101 |
|
| 102 |
<div id="chart_div_<?php echo $sqlcharts_inserted_script;?>" style="display:none"></div> |
| 103 |
<a href="javascript://" onclick="saveaspng('chart_div_<?php echo $sqlcharts_inserted_script;?>')">Save as PNG</a> |
| 104 |
<a href="javascript://" onclick="exportcsv()">Export CSV</a> |
| 105 |
<div id="columnchart_material_<?php echo $sqlcharts_inserted_script;?>" style="width:<?php echo $graph_width > 0 ? intval($graph_width) : '500';?>px; |
| 106 |
height: <?php echo $graph_height > 0 ? intval($graph_height) : '400';?>px"></div> |
| 107 |
<?php |
| 108 |
$sqlcharts_inserted_script++; |
| 109 |
|
| 110 |
return ob_get_clean(); |
| 111 |
} |
| 112 |
} |
| 113 |
add_shortcode('gvn_schart', 'guaven_sqlcharts_google_shortcode'); |
| 114 |
|