Skip to content

Commit d4eed90

Browse files
Export data? #41
1 parent f71ea6b commit d4eed90

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
6161
$this->_addAjaxAction( Visualizer_Plugin::ACTION_CLONE_CHART, 'cloneChart' );
6262

6363
// Added by Ash/Upwork
64+
$this->_addAjaxAction( Visualizer_Plugin::ACTION_EXPORT_DATA, 'exportData' );
6465
if( defined( 'Visualizer_Pro' ) ){
6566
global $Visualizer_Pro;
6667
list($action, $name, $class) = $Visualizer_Pro->_getAjaxAction($this);
@@ -597,4 +598,74 @@ public function cloneChart() {
597598
exit;
598599
}
599600

601+
/**
602+
* Exports the chart data
603+
*
604+
* @since 1.0.0
605+
*
606+
* @access public
607+
*/
608+
public function exportData() {
609+
$chart_id = $success = false;
610+
$capable = current_user_can( 'edit_posts' );
611+
if ( $capable ) {
612+
$chart_id = filter_input( INPUT_GET, 'chart', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) );
613+
if ( $chart_id ) {
614+
$chart = get_post( $chart_id );
615+
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
616+
}
617+
}
618+
619+
if ( $success ) {
620+
$settings = get_post_meta($chart_id, Visualizer_Plugin::CF_SETTINGS, true);
621+
$filename = $settings["title"];
622+
if (empty($filename)) {
623+
$filename = "export.csv";
624+
} else {
625+
$filename .= ".csv";
626+
}
627+
$rows = array();
628+
$series = get_post_meta($chart_id, Visualizer_Plugin::CF_SERIES, true);
629+
$data = unserialize($chart->post_content);
630+
if (!empty($series)) {
631+
$row = array();
632+
foreach ($series as $array) {
633+
$row[] = $array["label"];
634+
}
635+
$rows[] = $row;
636+
637+
$row = array();
638+
foreach ($series as $array) {
639+
$row[] = $array["type"];
640+
}
641+
$rows[] = $row;
642+
}
643+
644+
if (!empty($data)) {
645+
foreach ($data as $array) {
646+
$rows[] = $array;
647+
}
648+
}
649+
650+
$fp = tmpfile();
651+
foreach ($rows as $row) {
652+
fputcsv($fp, $row);
653+
}
654+
rewind($fp);
655+
656+
$csv = "";
657+
while (($array = fgetcsv($fp)) !== FALSE) {
658+
if (strlen($csv) > 0) $csv .= PHP_EOL;
659+
$csv .= implode(",", $array);
660+
}
661+
fclose($fp);
662+
663+
echo wp_send_json_success(array(
664+
"csv" => $csv,
665+
"name" => $filename
666+
));
667+
}
668+
669+
exit;
670+
}
600671
}

classes/Visualizer/Plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Visualizer_Plugin {
4949
const ACTION_CLONE_CHART = 'visualizer-clone-chart';
5050
const ACTION_DELETE_CHART = 'visualizer-delete-chart';
5151
const ACTION_UPLOAD_DATA = 'visualizer-upload-data';
52+
// Added by Ash/Upwork
53+
const ACTION_EXPORT_DATA = 'visualizer-export-data';
5254

5355
// custom filters
5456
const FILTER_CHART_WRAPPER_CLASS = 'visualizer-chart-wrapper-class';

classes/Visualizer/Render/Page/Data.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,16 @@ protected function _renderSidebarContent() {
108108
<a href="<?php echo Visualizer_Plugin::PRO_TEASER_URL;?>" title="<?php echo Visualizer_Plugin::PRO_TEASER_TITLE;?>" class="check-pro-btn" target="_new">
109109
<input type="button" class="button preview preview-btn" id="existing-chart-free" value="<?php esc_attr_e( 'Check PRO Version ', Visualizer_Plugin::NAME );?>">
110110
</a>
111-
112-
113111
<?php
114112
}
115113

114+
$export_link = add_query_arg( array(
115+
'action' => Visualizer_Plugin::ACTION_EXPORT_DATA,
116+
'chart' => $this->chart->ID,
117+
), admin_url( 'admin-ajax.php' ) );
118+
?>
119+
<input type="button" class="button" id="export-data" value="<?php esc_attr_e( 'Export Data', Visualizer_Plugin::NAME );?>" data-url="<?php echo $export_link;?>">
120+
<?php
116121
echo'<input type="button" name="advanced_button" class="advanced-settings-btn preview-btn" value="'. __( 'Advanced', Visualizer_Plugin::NAME ).' &raquo;">';
117122
// Added by Ash/Upwork
118123

js/frame.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@
5555
$(this).parent().find('.section-description:first').toggle();
5656
return false;
5757
});
58+
59+
$("#export-data").on("click", function() {
60+
$.ajax({
61+
url: $(this).attr("data-url"),
62+
method: "get",
63+
success: function( data, textStatus, jqXHR ){
64+
var a = document.createElement("a");
65+
document.body.appendChild(a);
66+
a.style = "display: none";
67+
var blob = new Blob([data.data.csv], {type: "application/csv"}),
68+
url = window.URL.createObjectURL(blob);
69+
a.href = url;
70+
a.download = data.data.name;
71+
a.click();
72+
window.URL.revokeObjectURL(url);
73+
}
74+
});
75+
});
76+
5877
});
5978
})(jQuery);
6079

0 commit comments

Comments
 (0)