PluginProbe
SQL Chart Builder / 2.3.0
SQL Chart Builder v2.3.0
3.0.2 3.0.1 trunk 1.0.2 1.0.3 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.7.1 2.3.7.2 2.3.8 3.0.0
sql-chart-builder / functions.php

functions.php in SQL Chart Builder 2.3.0, at functions.php

767 lines 25.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) {
3 die;
4 }
5 //postinstall function
6 function guaven_sqlcharts_load_defaults()
7 {
8 if (get_option("guaven_sqlcharts_already_installed_2") === false) {
9 update_option("guaven_sqlcharts_already_installed_2", "1");
10 guaven_sqlcharts_install_first_data();
11
12 }
13 }
14
15 function guaven_sqlcharts_install_first_data()
16 {
17 require_once(dirname(__FILE__) . "/initial_data.php");
18 gvn_chart_sample_nonxml_data();
19 }
20
21
22
23 function guaven_sqlcharts_my_admin_notice()
24 {
25 global $post;
26 if (!empty($post) and $post->post_type == 'gvn_schart'):
27 if (!current_user_can('manage_options')) {
28 echo '<br><br>
29 <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>';
30 die();
31 }
32 echo '<div class="updated gf-alert gf-alert-info">';
33 if (empty($_GET["post"]) and strpos($_SERVER["REQUEST_URI"], "post-new") === false):
34 $gf_message = 'Use <b>Add new</b> button above to create new sql report. And click on any existing rule names below
35 to manage them. ';
36 else:
37 $gf_message = '
38 1. Give any name to your report.<br>
39 2. Choose chart type, type sql query, enter field names, labels and then press to Publish/Update<br>
40 3. After update you will see needed shortcode below. You can use that shortcode anywhere in your website: in pages, posts, widgets etc. <br>
41 ';
42 endif;
43 _e('<div style="float:left">' . $gf_message . '</div>', 'guaven_sqlcharts');
44 echo '<div style="float: right;
45 margin-top: 0px;
46 padding-top: 0px;"><a target="_blank" style="border:0px solid #6200ee;border-radius:0px;color:white;font-weight:bold;background: #6200ee;"
47 class="button button-secondary" href="https://guaven.com/contact/solution-request/">Get Premium Support </a></div> </div>';
48 endif;
49 }
50 add_action('admin_notices', 'guaven_sqlcharts_my_admin_notice');
51
52 function guaven_sqlcharts_onboarding_notice(){
53 if((function_exists('wp_doing_ajax') and wp_doing_ajax()) or get_option('guaven_sqlcharts_onboarding_notice_dismissed') == 1)
54 return;
55
56 printf('
57 <div class="guaven-sqlcharts-notice notice notice-success is-dismissible" data-notice="onboarding_notice">
58 <p>Welcome aboard on MySQL Charts!</p>
59 </div>'
60 );
61 }
62 add_action('admin_notices', 'guaven_sqlcharts_onboarding_notice');
63
64 function guaven_sqlcharts_onboarding_notice_dismissed(){
65 check_ajax_referer('notice_dismissed', 'nonce');
66 switch ($_POST['type']){
67 case 'onboarding_notice':
68 update_option('guaven_sqlcharts_onboarding_notice_dismissed', 1);
69 break;
70 }
71 }
72 add_action('wp_ajax_guaven_sqlcharts_onboarding_notice_dismissed', 'guaven_sqlcharts_onboarding_notice_dismissed');
73
74 function guaven_sqlcharts_enqueue_chart()
75 {
76 wp_enqueue_script('guaven_sqlcharts_chartjs', plugins_url('asset/bundle.min.js', __FILE__),array('jquery'),GVNSQLCHARTS_VERSION);
77 wp_localize_script('guaven_sqlcharts_chartjs', 'guaven_sqlcharts_notice_dismissed', array(
78 'action' => 'guaven_sqlcharts_onboarding_notice_dismissed',
79 'nonce' => wp_create_nonce('notice_dismissed')
80 ));
81
82 }
83 add_action('wp_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
84 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_chart');
85
86 function guaven_sqlcharts_enqueue_main_style()
87 {
88 wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('asset/guaven_sqlcharts.css', __FILE__),array(),GVNSQLCHARTS_VERSION);
89 }
90 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style');
91
92
93 function guaven_sqlcharts_isJson($string)
94 {
95 json_decode($string);
96 return (json_last_error() == JSON_ERROR_NONE);
97 }
98
99 add_action('init', 'guaven_sqlcharts_register_post');
100 function guaven_sqlcharts_register_post()
101 {
102 //register_taxonomy('guaven_update_push_tag', 'termin');
103 register_post_type('gvn_schart', array(
104 'labels' => array(
105 'name' => __('My SQL Charts'),
106 'singular_name' => __('My SQL chart')
107 ),
108
109 'public' => true,
110 //'taxonomies' => array('guaven_update_push_tag'),
111 'supports' => array(
112 'title',
113 'postmeta'
114 ),
115 'register_meta_box_cb' => 'guaven_sqlcharts_metabox_area'
116 ));
117
118 guaven_sqlcharts_load_defaults();
119 }
120
121 add_action('admin_footer', 'guaven_sqlcharts_admin_front');
122
123
124 function guaven_sqlcharts_admin_front()
125 {
126 global $post;
127 if (!empty($post) and $post->post_type == 'gvn_schart') {
128 ?>
129 <style type="text/css">#normal-sortables{display: none}</style>
130 <?php
131 }
132 }
133
134 // metabox for editor
135 function guaven_sqlcharts_metabox_area()
136 {
137 add_meta_box('guaven_sqlcharts_metabox', 'Configure your graph chart', 'guaven_sqlcharts_metabox', 'gvn_schart', 'advanced', 'default');
138 }
139
140 function guaven_sqlcharts_metabox()
141 {
142 require_once(dirname(__FILE__) . "/admin_metabox.php");
143 }
144
145 function guaven_gutenberg_wrapper($atts){
146 if(isset($atts['sqlcharts_inserted_script'])){
147 global $sqlcharts_inserted_script;
148 $sqlcharts_inserted_script = $atts['sqlcharts_inserted_script'];
149 }
150
151 // if( isset($_GET["post_id"],$_GET["context"]) and $_GET["context"]=='edit'
152 // and strpos($_SERVER["REQUEST_URI"],'block-renderer/guaven-sqlcharts/gvn-chart-gutenberg')!==false
153 // ){
154 // $atts['chart_id']=$_GET["post_id"];
155 // }
156 $post = get_post($atts['chart_id']);
157 if( ! isset($atts['chart_id']) or !isset($post) or $post->post_type != 'gvn_schart'){
158
159 return "Invalid id";
160 }
161
162 return guaven_sqlcharts_local_shortcode(array('id' => $atts['chart_id'])); // temporary explicit value
163 }
164 function guaven_register_gutenberg_blocks()
165 {
166 wp_register_script(
167 'gvn_gutenberg_charts',
168 plugins_url( 'asset/guaven_gutenberg_charts.js', __FILE__ ),
169 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-server-side-render' ),
170 GVNSQLCHARTS_VERSION.'_'.filemtime( plugin_dir_path( __FILE__ ) . 'asset/guaven_gutenberg_charts.js' )
171 );
172 wp_localize_script('gvn_gutenberg_charts', 'guaven', array(
173 'description' => 'Add My SQL Chart to your post',
174 ));
175
176 register_block_type( 'guaven-sqlcharts/gvn-chart-gutenberg', array(
177 'editor_script' => 'gvn_gutenberg_charts',
178 'render_callback' => 'guaven_gutenberg_wrapper',
179 'attributes' => array(
180 'chart_id' => array(
181 'type' => 'string',
182 'default' => null
183 ),
184 'sqlcharts_inserted_script' => array(
185 'type' => 'number',
186 'default' => null
187 )
188 )
189 ));
190 }
191 add_action('init', 'guaven_register_gutenberg_blocks');
192
193
194
195 function guaven_sqlcharts_save_metabox_area($post_id, $post)
196 {
197 if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) {
198 return $post->ID;
199 }
200 $fields = array(
201 "guaven_sqlcharts_chartheight",
202 "guaven_sqlcharts_chartwidth",
203 "guaven_sqlcharts_graphtype",
204 "guaven_sqlcharts_xarg_s",
205 "guaven_sqlcharts_xarg_l",
206 "guaven_sqlcharts_yarg_s",
207 "guaven_sqlcharts_yarg_l",
208 "guaven_sqlcharts_tablepart",
209 "guaven_sqlcharts_variables",
210 "guaven_sqlcharts_formpartrole",
211 "guaven_sqlcharts_formpartbutton",
212 "guaven_sqlcharts_dbhost",
213 "guaven_sqlcharts_dblogin",
214 "guaven_sqlcharts_dbname",
215 "guaven_sqlcharts_colors",
216 "guaven_sqlcharts_begin_with_0_x",
217 "guaven_sqlcharts_begin_with_0_y",
218 "guaven_sqlcharts_round_y_values"
219 );
220 foreach ($fields as $key => $value) {
221 if(isset($_POST[$value]))$newval=esc_attr($_POST[$value]);
222 else $newval='';
223
224 update_post_meta($post->ID, $value, $newval);
225 }
226 if(!empty($_POST["guaven_sqlcharts_dbpass"])){
227 $encpass=guaven_sqlcharts_encrypt_decrypt('encrypt',$_POST["guaven_sqlcharts_dbpass"]);
228 update_post_meta($post->ID, 'guaven_sqlcharts_dbpass', ['encrypted',$encpass]);
229 }
230 update_post_meta($post->ID, 'guaven_sqlcharts_code', esc_attr(str_replace("'",'"',stripslashes($_POST['guaven_sqlcharts_code']))) );
231 }
232 add_action('save_post', 'guaven_sqlcharts_save_metabox_area', 1, 2);
233 // save the custom fields
234
235
236
237
238 function guaven_sqlcharts_libloads($type, $step)
239 {
240 $stty = array(
241 'bar' => array(
242 'packages' => "'corechart', 'bar'",
243 'charts' => "BarChart"
244 ),
245 'column' => array(
246 'packages' => "'corechart', 'bar'",
247 'charts' => "ColumnChart"
248 ),
249 'area' => array(
250 'packages' => "'corechart'",
251 'charts' => "AreaChart"
252 ),
253 'pie' => array(
254 'packages' => "'corechart'",
255 'charts' => "PieChart"
256 ),
257 '3dpie' => array(
258 'packages' => "'corechart'",
259 'charts' => "PieChart"
260 )
261 );
262
263 return $stty[$type][$step];
264 }
265 function gvn_chart_check_sql_query($sql)
266 {
267 $blacklister = array(
268 "delete",
269 "update",
270 "insert",
271 "drop",
272 "truncate",
273 "alter"
274 ); //add all
275 $blacklister_f = 0;
276 foreach ($blacklister as $key => $value) {
277 if (strpos($sql, $value) !== false)
278 $blacklister_f = 1;
279 }
280 return $blacklister_f;
281 }
282
283 function guaven_get_labels_and_values($id, $fvs)
284 {
285 $values = array();
286 $labels = array();
287 $xarg_s = get_post_meta($id, 'guaven_sqlcharts_xarg_s', true);
288 $xarg_l = get_post_meta($id, 'guaven_sqlcharts_xarg_l', true);
289 $yarg_s = get_post_meta($id, 'guaven_sqlcharts_yarg_s', true);
290 $yarg_l = get_post_meta($id, 'guaven_sqlcharts_yarg_l', true);
291 $chartype = array(
292 'line_l' => 'Line',
293 'pie_l' => 'Pie',
294 'donut_l' => 'Pie',
295 'bar_l' => 'Bar',
296 'horizontalbar_l' => 'Horizontal Bar',
297 'area_l' => 'Line'
298 );
299 foreach ($fvs as $key => $value) {
300 $values[] = $value->$yarg_s;
301 $labels[] = '"' . $value->$xarg_s . '"';
302 }
303 return array(
304 $labels,
305 $values,
306 explode(";", $yarg_l),
307 explode(";", $xarg_l)
308 );
309 }
310
311 function guaven_sqlcharts_print_chart_js($tip_g, $title, $labels, $values, $ylabel, $pid = null)
312 {
313 if ($tip_g == 'line_l') {
314 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'false', $pid);
315 }
316 if ($tip_g == 'area_l') {
317 guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, 'true', $pid);
318 } elseif ($tip_g == 'pie_l') {
319 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid);
320 } elseif ($tip_g == 'donut_l') {
321 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'doughnut');
322 } elseif ($tip_g == 'bar_l') {
323 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'bar', $pid);
324 } elseif ($tip_g == 'horizontalbar_l') {
325 guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, 'horizontalBar', $pid);
326 }
327 elseif($tip_g == 'custom'){
328 guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid);
329 }
330 elseif ($tip_g == 'polar_l') {
331 guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, 'polarArea');
332 }
333 }
334
335 function guaven_sqlcharts_custom($title, $labels, $values, $ylabel, $pid){
336 do_action('guaven_sqlcharts_custom',$title, $labels, $values, $ylabel, $pid);
337 }
338
339 function gvn_chart_put_variables($sql,$pid){
340 $sql_initial=$sql;
341
342
343 $default_tag_keys=['{current_user_id}','{current_user_login}','{current_user_email}','{current_user_display_name}'];
344 if(is_user_logged_in( )){
345 $currentuser=wp_get_current_user();
346 $default_tag_values=[$currentuser->ID,$currentuser->user_login,$currentuser->user_email,$currentuser->user_display_name];
347 }
348 else {
349 $default_tag_values='';
350 }
351 $sql_initial=str_replace($default_tag_keys,$default_tag_values,$sql_initial);
352
353 $variables_raw=get_post_meta($pid,'guaven_sqlcharts_variables',true);
354 $variables_arr=explode("|",$variables_raw);
355 foreach($variables_arr as $varfield){
356 $varfield_arr=explode("~",$varfield);
357 if (count($varfield_arr)<3) continue;
358 $varfield_arr=array_map("trim",$varfield_arr);
359 if (!empty($_GET[$varfield_arr[0]])) $varreplacement=$_GET[$varfield_arr[0]]; else $varreplacement=$varfield_arr[1];
360 if (!is_numeric($varreplacement) and strpos($varreplacement,'()')===false) $varreplacement='"'.$varreplacement.'"';
361
362 $sql_initial=str_replace('{'.$varfield_arr[0].'}',$varreplacement,$sql_initial);
363 }
364 return $sql_initial;
365 }
366
367 function gvn_chart_top_form($atts){
368 if (get_post_meta($atts["id"],'guaven_sqlcharts_formpartrole',true)!='' and !is_user_logged_in()) return;
369 $topform='';$dateexists=false;
370 $variables_raw=get_post_meta($atts['id'],'guaven_sqlcharts_variables',true);
371 $variables_raw=explode("|",$variables_raw);
372 foreach ($variables_raw as $vrow){
373 $vrow_arr=explode("~",$vrow);
374 $vrow_arr=array_map("trim",$vrow_arr);
375 if (empty($vrow_arr[3])) continue;
376 $gvalue=!empty($_GET[$vrow_arr[0]])?esc_attr(urldecode($_GET[$vrow_arr[0]])):'';
377 $dvalue=(strpos($vrow_arr[1],'()')===false)?esc_attr($vrow_arr[1]):'';
378 if ($vrow_arr[3]=='date') {
379 $dateexists=true;
380 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px" type="text"
381 value="'.$gvalue.'"
382 data-toggle="datepicker" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
383 ';}
384 else {
385 $topform.= $vrow_arr[2].' <input autocomplete="off" style="max-width:210px;'.($vrow_arr[3]=='number'?'width:100px;':'').'"
386 type="'.$vrow_arr[3].'"
387 value="'.$gvalue.'" name="'.$vrow_arr[0].'" placeholder="'.$dvalue.'">
388 ';
389 }
390 }
391 if (!empty($topform)) {
392 $topform='<form method="get" action="" class="guaven_sqlcharts_form">'.$topform.'
393 <input type="submit"
394 value="'.(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)!=''?esc_attr(get_post_meta($atts['id'], 'guaven_sqlcharts_formpartbutton', true)):'OK').'"></form>';
395 if ($dateexists) $topform.='<script>setTimeout(function(){jQuery(\'[data-toggle="datepicker"]\').datepicker({format: \'yyyy-mm-dd\'});
396 },300);</script>';
397 return $topform ;
398 }
399 return;
400 }
401
402
403 function guaven_sqlcharts_encrypt_decrypt($action, $string)
404 {
405 $output = false;
406 $encrypt_method = "AES-256-CBC";
407 $secret_key = 'GWSCHARTPL2022.2016.';
408 $secret_iv = 'GWSCHARTPL2016.2022';
409 $key = hash('sha256', $secret_key);
410 $iv = substr(hash('sha256', $secret_iv), 0, 16);
411 if ( $action == 'encrypt' ) {
412 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
413 $output = base64_encode($output);
414 } else if( $action == 'decrypt' ) {
415 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
416 }
417 return $output;
418 }
419
420 function guaven_sqlcharts_local_shortcode($atts)
421 {
422 $remote_host=get_post_meta($atts['id'], 'guaven_sqlcharts_dbhost', true);
423 if ($remote_host!=''){
424 $remote_db=get_post_meta($atts['id'], 'guaven_sqlcharts_dbname', true);
425 $remote_login=get_post_meta($atts['id'], 'guaven_sqlcharts_dblogin', true);
426 $remote_pass=get_post_meta($atts['id'], 'guaven_sqlcharts_dbpass', true);
427 if(is_array($remote_pass)){
428 $remote_pass=guaven_sqlcharts_encrypt_decrypt('decrypt',$remote_pass[1]);
429 }
430 $wpdb=new wpdb($remote_login,$remote_pass,$remote_db,$remote_host);
431 }
432 else {
433 global $wpdb;
434 }
435
436 $GLOBALS["guaven_sqlcharts_atts"]=$atts;
437
438 $sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true));
439 $sql=gvn_chart_put_variables($sql,$atts['id']);
440 $blacklister_f = gvn_chart_check_sql_query($sql);
441 if ($blacklister_f == 1)
442 return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries';
443 $tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true);
444
445 for($i=1;$i<20;$i++){ $replacearg=!empty($atts["arg".$i])?$atts["arg".$i]:0;
446 $sql=str_replace("{arg".$i."}",esc_sql($replacearg),$sql);}
447 $sql_split = explode(';', $sql);
448 $labels_and_values = array();
449 $post_g = get_post($atts['id']);
450
451 global $sqlcharts_inserted_script;
452 for ($i = 0; $i < count($sql_split); $i++) {
453 if (!empty($sql_split[$i])) {
454
455 $fvs = $wpdb->get_results($sql_split[$i]);
456 if (strpos($_SERVER["REQUEST_URI"],'wp-admin')!==false){
457 $wpdb->show_errors();
458 ob_start();
459 $wpdb->print_error();
460 $printerror = ob_get_clean();
461 if ($printerror != '' and strpos($printerror, "[]") === false)
462 return $printerror;
463 elseif (empty($fvs))
464 return 'Your SQL returnes empty date, please recheck your SQL query above';
465 }
466
467 ob_start();
468
469 if (empty($sqlcharts_inserted_script))
470 $sqlcharts_inserted_script = 1;
471 $labels_and_values[$i] = guaven_get_labels_and_values($atts['id'], $fvs);
472 $labels[$i] = $labels_and_values[$i][0];
473 $values[$i] = $labels_and_values[$i][1];
474 $ylabel[$i] = !empty($labels_and_values[$i][2][$i]) ? $labels_and_values[$i][2][$i] : '';
475 $xlabel[$i] = !empty($labels_and_values[$i][3][$i]) ? $labels_and_values[$i][3][$i] : '';
476 }
477 }
478
479 echo gvn_chart_top_form($atts);
480 ?>
481 <canvas
482 id="ct-chart_<?php echo $sqlcharts_inserted_script; ?>"
483 class="guaven_chart_canvas"
484 style="width: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true); ?>px !important; height: <?php echo get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true); ?>px !important;"
485 ></canvas>
486
487 <script type="text/javascript" class="gvn_charts_script" async>
488 var ctx = jQuery("#ct-chart_<?php
489 echo $sqlcharts_inserted_script;
490 ?>");
491
492 <?php
493 guaven_sqlcharts_print_chart_js($tip_g, $post_g->post_title, $labels, $values, $ylabel, $atts['id']);
494 ?>
495 </script>
496
497 <?php
498 if (!empty($atts["table"])) echo guaven_sqlcharts_tablepart($post_g->post_title, $labels, $values, $ylabel,$xlabel);
499 $sqlcharts_inserted_script++;
500 return ob_get_clean();
501 }
502
503 add_shortcode('gvn_schart_2', 'guaven_sqlcharts_local_shortcode');
504
505 function guaven_sqlcharts_colors($index, $pid = null){
506 if(!isset($pid)) {
507 global $post;
508 $pid = $post->ID;
509 }
510 $colors=get_post_meta($pid,'guaven_sqlcharts_colors',true);
511 $colors=explode(",",$colors);
512 if (!empty($colors[$index])) return $colors[$index];
513 return rand(0, 255) . ',' . rand(0, 255) . ',' . rand(0, 255);
514 }
515
516 function guaven_sqlcharts_bardata($title, $labels, $values, $ylabel, $type = 'bar', $pid = null)
517 {
518 ?>
519 var data = {
520 labels: [<?php
521 echo implode(",", $labels[0]);
522 ?>],
523 datasets: [
524 <?php
525 for ($i = 0; $i < count($values); $i++) {
526 ?>
527 {
528 <?php
529 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
530 //passing chartJS params via the shortcode
531 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
532 }
533 ?>
534 label: "<?php
535 echo esc_attr($ylabel[$i]);
536 ?>",
537 backgroundColor: [
538 <?php
539 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0, guaven_sqlcharts_colors($i, $pid));
540 ?>
541 ],
542 borderColor: [
543 <?php
544 echo guaven_sqlcharts_colorgenerator(count($values[$i]), 0, 0.2, guaven_sqlcharts_colors($i, $pid));
545 ?>
546 ],
547 borderWidth: 1,
548 data: [<?php
549 echo implode(",", $values[$i]);
550 ?>],
551 },
552 <?php
553 }
554 ?>
555 ]
556 };
557 var options={
558 responsive: true,
559 scales: {
560 xAxes: [{
561 stacked: true,
562 ticks: {
563 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
564 }
565 }],
566 yAxes: [{
567 stacked: true,
568 ticks: {
569 <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?>
570 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
571 }
572 }]
573 }
574 };
575 var myBarChart = new Chart(ctx, {
576 type: '<?php
577 echo $type;
578 ?>',
579 data: data,
580 options: options
581 });
582 <?php
583 }
584
585
586
587 function guaven_sqlcharts_linedata($title, $labels, $values, $ylabel, $type = 'false', $pid = null)
588 {
589 ?>
590 var data = {
591 labels: [<?php
592 echo implode(",", $labels[0]);
593 ?>],
594 datasets: [
595 <?php
596 for ($i = 0; $i < count($values); $i++) {
597 ?>
598 {
599 <?php
600 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
601 //passing chartJS params via the shortcode
602 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
603 }
604 ?>
605 label: "<?php
606 echo esc_attr($ylabel[$i]);
607 ?>",
608 fill: <?php echo $type=="false"?$type:($i==0?'"+1"':'"origin"');
609 ?>,
610 lineTension: 0.1,
611 backgroundColor: <?php
612 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
613 ?>
614 borderColor: <?php
615 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
616 ?>
617 pointBorderColor: <?php
618 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
619 ?>
620 pointHoverBackgroundColor: <?php
621 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
622 ?>
623 pointHoverBorderColor: <?php
624 echo guaven_sqlcharts_colorgenerator(1, 1, 0.2, guaven_sqlcharts_colors($i, $pid));
625 ?>
626 data: [<?php
627 echo implode(",", $values[$i]);
628 ?>],
629 spanGaps: false,
630 },
631 <?php
632 }
633 ?>
634 ]
635 };
636 var myLineChart = new Chart(ctx, {
637 type: 'line',
638 data: data,
639 options: {
640 responsive: true,
641 scales: {
642 xAxes: [{
643 display: true,
644 ticks: {
645 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_x', true) == 1) ? 'true':'false'; ?>
646 }
647 }],
648 yAxes: [{
649 ticks: {
650 <?php if(get_post_meta($pid, 'guaven_sqlcharts_round_y_values', true) == 1) echo 'precision: 0,'; ?>
651 beginAtZero: <?php echo (get_post_meta($pid, 'guaven_sqlcharts_begin_with_0_y', true) == 1) ? 'true':'false'; ?>
652 }
653 }]
654 }
655 }
656 });
657 <?php
658 }
659
660
661 function guaven_sqlcharts_piedata($title, $labels, $values, $ylabel, $pid, $type = 'pie')
662 {
663 ?>
664 var options={
665 responsive: true,
666 };
667 var data = {
668 labels: [ <?php echo implode(",", $labels[0]);?>],
669 datasets: [
670 <?php
671 for ($i = 0; $i < count($values); $i++) {
672 ?>
673 {
674 <?php
675 if(!empty($GLOBALS["guaven_sqlcharts_atts"]["params"])){
676 //passing chartJS params via the shortcode
677 echo esc_js($GLOBALS["guaven_sqlcharts_atts"]["params"]);
678 }
679 ?>
680 data: [<?php
681 echo implode(",", $values[$i]);
682 ?>],
683 backgroundColor: [
684 <?php
685 foreach($values[$i] as $vci=>$valuecolor){
686 echo guaven_sqlcharts_colorgenerator(1, 0, 0, guaven_sqlcharts_colors($vci, $pid));
687 }
688 ?>
689 ],
690 hoverBackgroundColor: [
691 <?php
692 foreach($values[$i] as $vci=>$valuecolor){
693 echo guaven_sqlcharts_colorgenerator(1, 0, 0.2, guaven_sqlcharts_colors($vci, $pid));
694 }
695 ?>
696 ]
697 },
698 <?php
699 }
700 ?>
701 ]
702 };
703 var myPieChart = new Chart(ctx,{
704 type: '<?php
705 echo $type;
706 ?>',
707 data: data,
708 options: options
709 });
710 <?php
711 }
712
713
714
715
716 function guaven_sqlcharts_colorgenerator($count, $indic, $darkness = 0, $initcolor = '255,0,0')
717 {
718 if (strpos($initcolor,'#')===0) {
719 $split = str_split(substr($initcolor,1), 2);
720 $r = hexdec($split[0]);
721 $g = hexdec($split[1]);
722 $b = hexdec($split[2]);
723 $ret='';
724 for ($i = 0; $i < $count; $i++) {
725 $ret .= "'rgba(" . $r . ", " . $g . ", " . $b . ",".($darkness + 0.8 - $indic * $i * 0.8 / ($count)).")',
726 ";
727 }
728 return $ret;
729 }
730 $initial_colors = array(
731 'linebg' => 'red',
732 'linebr' => 'yellow',
733 'linebc' => 'green',
734 'linehbg' => 'white',
735 'linehbc' => 'black'
736 );
737 if (!empty($initial_colors[$count]))
738 return '"' . $initial_colors[$count] . '",
739 ';
740 $ret = '';
741 for ($i = 0; $i < $count; $i++) {
742 $ret .= "'rgba(" . $initcolor . "," . ($darkness + 0.8 - $indic * $i * 0.8 / ($count)) . ")',
743 ";
744 }
745 return $ret;
746 }
747
748 function guaven_sqlcharts_tablepart($title, $labels, $values, $ylabel,$xlabel){
749 $tabledata='';
750 $fcol=[];$scol=[];
751 $tablein='<br>';
752 foreach($values as $valuerow){
753 foreach ($valuerow as $key => $value) {
754 $fcol[$key]='<td>'.str_replace('"',"",$labels[0][$key]).'</td>';
755 $scol[$key][]='<td>'.$value.'</td>';
756
757 }
758 }
759 foreach($fcol as $key=>$value){
760 $tablein.='<tr>'.$value.implode(" ",$scol[$key]).'</tr>'.PHP_EOL;
761 }
762 $tabledata.='<table><tr><th>'.$xlabel[0].'</th><th>'.implode("</th><th>",$ylabel).'</th></tr>
763 '.$tablein.'</table><br>';
764 return $tabledata;
765
766 }
767