PluginProbe
SQL Chart Builder / 1.0.3
SQL Chart Builder v1.0.3
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 1.0.3, at functions.php

443 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //postinstall function
4 function guaven_sqlcharts_load_defaults()
5 {
6 if (get_option("guaven_sqlcharts_already_installed") === false) {
7 update_option("guaven_sqlcharts_already_installed", "1");
8 guaven_sqlcharts_install_first_data();
9 }
10 }
11
12
13
14
15 function guaven_sqlcharts_my_admin_notice()
16 {
17 global $post;
18 if (!empty($post) and $post->post_type == 'gvn_schart'):
19 if (!current_user_can('manage_options')) {
20 echo '<br><br>
21 <div class="updated gf-alert gf-alert-danger">Only administrators can manage this page</div>';
22 die();
23 }
24 echo '<div class="updated gf-alert gf-alert-info">';
25 if (empty($_GET["post"]) and strpos($_SERVER["REQUEST_URI"], "post-new") === false):
26 $gf_message = 'Use <b>Add new</b> button above to create new sql report. And click on any existing rule names below
27 to manage them. ';
28 else:
29 $gf_message = '
30 1. Give any name to your report.<br>
31 2. Choose chart type, type sql query, enter field names, labels and then press to Publish/Update<br>
32 3. After update you will see needed shortcode below. You can use that shortcode anywhere in your website: in pages, posts, widgets etc. <br>
33 ';
34 ?>
35 <?php
36 endif;
37 _e('<div style="float:left">' . $gf_message . '</div>', 'guaven_sqlcharts');
38 echo '<div style="float: right;
39 margin-top: 0px;
40 padding-top: 0px;"><a class="button button-secondary" href="http://guaven.com/contact">Contact us for custom reports</a></div> </div>';
41 endif;
42 }
43 add_action('admin_notices', 'guaven_sqlcharts_my_admin_notice');
44
45
46
47
48
49
50 function guaven_sqlcharts_enqueue_main_style()
51 {
52 wp_enqueue_style('guaven_sqlcharts_main_style', plugins_url('guaven_sqlcharts.css', __FILE__));
53 }
54
55 add_action('admin_enqueue_scripts', 'guaven_sqlcharts_enqueue_main_style');
56
57 function guaven_sqlcharts_isJson($string)
58 {
59 json_decode($string);
60 return (json_last_error() == JSON_ERROR_NONE);
61 }
62
63 add_action('init', 'guaven_sqlcharts_register_post');
64 function guaven_sqlcharts_register_post()
65 {
66 //register_taxonomy('guaven_update_push_tag', 'termin');
67 register_post_type('gvn_schart', array(
68 'labels' => array(
69 'name' => __('My SQL Charts'),
70 'singular_name' => __('My SQL chart')
71 ),
72
73 'public' => true,
74 //'taxonomies' => array('guaven_update_push_tag'),
75 'supports' => array(
76 'title',
77 'postmeta'
78 ),
79 'register_meta_box_cb' => 'guaven_sqlcharts_metabox_area'
80 ));
81
82 guaven_sqlcharts_load_defaults();
83 }
84
85 add_action('admin_footer', 'guaven_sqlcharts_admin_front');
86
87
88 function guaven_sqlcharts_admin_front()
89 {
90 global $post;
91 if (!empty($post) and $post->post_type == 'gvn_schart') {
92 ?>
93 <style type="text/css">#normal-sortables{display: none}</style>
94 <?php
95 }
96 }
97
98 // metabox for editor
99 function guaven_sqlcharts_metabox_area()
100 {
101 add_meta_box('gvn_schart_metabox', 'Configure your graph chart', 'gvn_schart_metabox', 'gvn_schart', 'advanced', 'default');
102 }
103
104 function gvn_schart_metabox()
105 {
106 global $post;
107 wp_nonce_field('meta_box_nonce_action', 'meta_box_nonce_field');
108
109
110 ?>
111 <h4>Choose your graph</h4>
112 <p>
113 <select name="guaven_sqlcharts_graphtype">
114 <option value="pie" <?php
115 $guaven_sqlcharts_graphtype = get_post_meta($post->ID, 'guaven_sqlcharts_graphtype', true);
116 echo ($guaven_sqlcharts_graphtype == 'pie' ? 'selected' : '');
117 ?>>Pie Chart</option>
118
119 <option value="3dpie" <?php
120 echo ($guaven_sqlcharts_graphtype == '3dpie' ? 'selected' : '');
121 ?>>3D Pie Chart</option>
122
123 <option value="column" <?php
124 echo ($guaven_sqlcharts_graphtype == 'column' ? 'selected' : '');
125 ?>>Column Chart</option>
126
127 <option value="bar" <?php
128 echo ($guaven_sqlcharts_graphtype == 'bar' ? 'selected' : '');
129 ?>>Bar Chart</option>
130
131 <option value="area" <?php
132 echo ($guaven_sqlcharts_graphtype == 'area' ? 'selected' : '');
133 ?>>Area Chart</option>
134
135
136 </select>
137 </p>
138 <hr>
139
140
141
142
143 <h4>Your SQL code </h4>
144 <span>(Use double " " quotes instead of single ' ' ones)</span>
145 <p>
146 <textarea name="guaven_sqlcharts_code" id="guaven_sqlcharts_code" style="width:100%" rows="6"><?php
147 echo (get_post_meta($post->ID, 'guaven_sqlcharts_code', true) != '' ? get_post_meta($post->ID, 'guaven_sqlcharts_code', true) : '');
148 ?>
149 </textarea>
150 </p>
151 <p style="text-align: right;">Need help with custom SQL reports? <a href="http://guaven.com/contact">Contact us.</a></p>
152 <hr>
153
154
155
156
157 <h4>Arguments</h4>
158 <p>
159 Label for Y axis: <br><input type="text" name="guaven_sqlcharts_xarg_l" id="guaven_sqlcharts_xarg_l" value="<?php
160 echo get_post_meta($post->ID, 'guaven_sqlcharts_xarg_l', true);
161 ?>"></p>
162 <p>
163 SQL field name of Y axis (Write corresponding SQL field name here, which has a number value. f.e. post_count):
164 <br>
165 <input type="text" name="guaven_sqlcharts_xarg_s" id="guaven_sqlcharts_xarg_s" value="<?php
166 echo get_post_meta($post->ID, 'guaven_sqlcharts_xarg_s', true);
167 ?>">
168 </p>
169
170 <p>
171 Label for X axis: <br><input type="text" name="guaven_sqlcharts_yarg_l" id="guaven_sqlcharts_yarg_l" value="<?php
172 echo get_post_meta($post->ID, 'guaven_sqlcharts_yarg_l', true);
173 ?>"></p><p>
174 SQL field name of X axis (Write corresponding SQL field name here, which usually has a string value. f.e. display_name):
175 <br>
176 <input type="text" name="guaven_sqlcharts_yarg_s" id="guaven_sqlcharts_yarg_s" value="<?php
177 echo get_post_meta($post->ID, 'guaven_sqlcharts_yarg_s', true);
178 ?>">
179 </p>
180
181 <hr>
182
183
184 <h4>Width and height (with px. don't type px itself, just numbers)</h4>
185 <p>
186
187 <input type="number" name="guaven_sqlcharts_chartwidth" id="guaven_sqlcharts_chartwidth" value="<?php
188 echo get_post_meta($post->ID, 'guaven_sqlcharts_chartwidth', true);
189 ?>">
190
191 <input type="number" name="guaven_sqlcharts_chartheight" id="guaven_sqlcharts_chartheight" value="<?php
192 echo get_post_meta($post->ID, 'guaven_sqlcharts_chartheight', true);
193 ?>">
194 </p>
195
196 <hr>
197
198
199
200
201 <?php
202 if (get_post_meta($post->ID, 'guaven_sqlcharts_graphtype', true) != '') {
203 ?>
204 <div class="gf-alert-success gf-alert">
205 <h4>
206 Text shortcode: [gvn_schart id="<?php
207 echo $post->ID;
208 ?>"]
209 <br>
210 <br>
211 PHP shorcode: &lt;?php echo do_shortcode(' [gvn_schart id="<?php
212 echo $post->ID;
213 ?>"]'); ?&gt;
214 </h4>
215 <p>Note: Shortcodes supports width and height attributes. F.e. [gvn_schart id="1" width="500" height="400"]. If no attributes, default width, height (which you enter in this page above) will be used.
216 </div>
217 <h4>Demo</h4>
218 <?php
219 echo do_shortcode(' [gvn_schart id="' . $post->ID . '"]');
220
221 }
222
223 }
224
225
226
227 function gvn_schart_save_metabox_area($post_id, $post)
228 {
229
230 if (!isset($_POST['meta_box_nonce_field']) or !wp_verify_nonce($_POST['meta_box_nonce_field'], 'meta_box_nonce_action')) {
231 return $post->ID;
232 }
233 $fields = array(
234 "guaven_sqlcharts_chartheight",
235 "guaven_sqlcharts_chartwidth",
236 "guaven_sqlcharts_graphtype",
237 "guaven_sqlcharts_code",
238 "guaven_sqlcharts_xarg_s",
239 "guaven_sqlcharts_xarg_l",
240 "guaven_sqlcharts_yarg_s",
241 "guaven_sqlcharts_yarg_l"
242 );
243 foreach ($fields as $key => $value) {
244 update_post_meta($post->ID, $value, esc_attr($_POST[$value]));
245 }
246 }
247 add_action('save_post', 'gvn_schart_save_metabox_area', 1, 2);
248 // save the custom fields
249
250
251
252
253 function gvn_schart_libloads($type, $step)
254 {
255 $stty = array(
256 'bar' => array(
257 'packages' => "'corechart', 'bar'",
258 'charts' => "BarChart"
259 ),
260 'column' => array(
261 'packages' => "'corechart', 'bar'",
262 'charts' => "ColumnChart"
263 ),
264 'area' => array(
265 'packages' => "'corechart'",
266 'charts' => "AreaChart"
267 ),
268 'pie' => array(
269 'packages' => "'corechart'",
270 'charts' => "PieChart"
271 ),
272 '3dpie' => array(
273 'packages' => "'corechart'",
274 'charts' => "PieChart"
275 )
276 );
277
278 return $stty[$type][$step];
279 }
280
281
282
283
284
285
286 function gvn_schart_shortcode($atts)
287 {
288
289
290 global $wpdb;
291 $sql = html_entity_decode(get_post_meta($atts['id'], 'guaven_sqlcharts_code', true));
292
293
294 $blacklister = array(
295 "delete",
296 "update",
297 "insert",
298 "drop",
299 "truncate"
300 ); //add all
301 $blacklister_f = 0;
302 foreach ($blacklister as $key => $value) {
303 if (strpos($sql, $value) !== false)
304 $blacklister_f = 1;
305 }
306
307 if ($blacklister_f == 1)
308 return 'You given SQL code contains forbidden commands. Remember that you should only use SELECT queries';
309
310 $fvs = $wpdb->get_results($sql);
311
312 $wpdb->show_errors();
313 ob_start();
314 $wpdb->print_error();
315 $printerror = ob_get_clean();
316
317 if ($printerror != '' and strpos($printerror, "[]") === false)
318 return $printerror;
319 elseif (empty($fvs))
320 return 'Your SQL returnes empty date, please recheck your SQL query above';
321 else {
322 ob_start();
323 ?>
324 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
325
326 <script type="text/javascript">;
327 google.charts.load('current', {'packages':[<?php
328 $tip_g = get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true);
329
330 echo gvn_schart_libloads($tip_g, 'packages');
331 ?>]});
332 google.charts.setOnLoadCallback(drawChart);
333 function drawChart() {
334 <?php
335 $html_temp = '';
336
337 $post_g = get_post($atts['id']);
338 $xarg_s = get_post_meta($atts['id'], 'guaven_sqlcharts_xarg_s', true);
339 $xarg_l = get_post_meta($atts['id'], 'guaven_sqlcharts_xarg_l', true);
340 $yarg_s = get_post_meta($atts['id'], 'guaven_sqlcharts_yarg_s', true);
341 $yarg_l = get_post_meta($atts['id'], 'guaven_sqlcharts_yarg_l', true);
342
343 $graph_width = get_post_meta($atts['id'], 'guaven_sqlcharts_chartwidth', true);
344 $graph_height = get_post_meta($atts['id'], 'guaven_sqlcharts_chartheight', true);
345
346 if (!empty($atts["width"])) {
347 $graph_width = intval($atts['width']);
348 }
349 if (!empty($atts["height"])) {
350 $graph_height = intval($atts['height']);
351 }
352
353 foreach ($fvs as $fv) {
354 $html_temp .= "['{$fv->$yarg_s}', {$fv->$xarg_s}, '#b87333'],";
355
356 }
357
358 ?>
359 var data = google.visualization.arrayToDataTable([
360 ['<?php
361 echo $yarg_l;
362 ?>', '<?php
363 echo $xarg_l;
364 ?>', { role: 'style' }],
365 <?php
366 echo $html_temp;
367 ?>
368 ]);
369
370 var options = {
371 <?php
372 echo $tip_g == '3dpie' ? "is3D: true," : '';
373 ?>
374 chart: {
375 title: '<?php
376 echo $post_g->post_title;
377 ?>',
378 }
379 };
380
381 var chart = new google.visualization.<?php
382 echo gvn_schart_libloads(get_post_meta($atts['id'], 'guaven_sqlcharts_graphtype', true), 'charts');
383 ?>(document.getElementById('columnchart_material'));
384 chart.draw(data, options);
385 }
386
387 </script>
388
389 <div id="columnchart_material" style="width:<?php
390 echo $graph_width > 0 ? intval($graph_width) : '500';
391 ?>px;
392 height: <?php
393 echo $graph_height > 0 ? intval($graph_height) : '400';
394 ?>px"></div>
395 <?php
396 return ob_get_clean();
397 }
398 }
399 add_shortcode('gvn_schart', 'gvn_schart_shortcode');
400
401
402
403
404 function gvn_schart_wp_tags()
405 {
406 $tags = get_tags(array(
407 "order" => "DESC",
408 "orderby" => "count",
409 "number" => 10
410 ));
411 $html = '';
412 $itag = 0;
413 $html60 = '';
414 foreach ($tags as $tag) {
415 $itag++;
416 $html_temp .= "['{$tag->name}', {$tag->count}, '#b87333'],";
417 }
418 ?>
419 var data = google.visualization.arrayToDataTable([
420 ['Element', 'Density', { role: 'style' }],
421 <?php
422 echo $html_temp;
423 ?>
424 ]);
425
426 var options = {
427 chart: {
428 title: 'Company Performance',
429 subtitle: 'Sales, Expenses, and Profit: 2014-2017',
430 }
431 };
432 <?php
433 return $html_temp;
434 }
435
436
437 function guaven_sqlcharts_install_first_data()
438 {
439
440 require_once(dirname(__FILE__) . "/initial_data.php");
441 gvn_chart_sample_nonxml_data();
442 }
443