Skip to content

Commit e7efa26

Browse files
Manual data tab to show options for multiple editors
1 parent 2b97024 commit e7efa26

File tree

8 files changed

+136
-91
lines changed

8 files changed

+136
-91
lines changed

classes/Visualizer/Module.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,21 @@ public static function is_pro_older_than( $version ) {
655655
return version_compare( VISUALIZER_PRO_VERSION, $version, '<' );
656656
}
657657

658+
/**
659+
* Should we show some specific feature on the basis of the version?
660+
*
661+
* @since 3.4.0
662+
*/
663+
public static function can_show_feature( $feature ) {
664+
switch ( $feature ) {
665+
case 'simple-editor':
666+
// if user has pro but an older version, then don't load the simple editor functionality
667+
// as the select box will not behave as expected because the pro editor's functionality will supercede.
668+
return ! Visualizer_Module::is_pro() || ! Visualizer_Module::is_pro_older_than( '1.9.2' );
669+
}
670+
return false;
671+
}
672+
658673
/**
659674
* Gets the features for the provided license type.
660675
*/

classes/Visualizer/Module/Chart.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ private function _handleDataAndSettingsPage() {
687687
wp_enqueue_script( 'visualizer-chosen' );
688688
wp_enqueue_script( 'visualizer-render' );
689689

690-
if ( ! Visualizer_Module::is_pro() ) {
690+
if ( Visualizer_Module::can_show_feature( 'simple-editor' ) ) {
691691
wp_enqueue_script( 'visualizer-editor-simple' );
692692
wp_localize_script(
693693
'visualizer-editor-simple',
@@ -843,28 +843,31 @@ public function renderFlattrScript() {
843843
*
844844
* @since 3.2.0
845845
*/
846-
private function handleCSVasString( $data ) {
846+
private function handleCSVasString( $data, $editor_type ) {
847847
$source = null;
848848

849-
// @issue https://github.com/Codeinwp/visualizer/issues/412
850-
if ( has_filter( 'visualizer_pro_handle_chart_data' ) ) {
851-
$source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
852-
} else {
853-
// data coming in from the text editor.
854-
$tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
855-
$handle = fopen( $tmpfile, 'w' );
856-
$values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
857-
if ( $values ) {
858-
foreach ( $values as $row ) {
859-
if ( empty( $row ) ) {
860-
continue;
849+
switch ( $editor_type ) {
850+
case 'text':
851+
// data coming in from the text editor.
852+
$tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
853+
$handle = fopen( $tmpfile, 'w' );
854+
$values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
855+
if ( $values ) {
856+
foreach ( $values as $row ) {
857+
if ( empty( $row ) ) {
858+
continue;
859+
}
860+
$columns = explode( ',', $row );
861+
fputcsv( $handle, $columns );
861862
}
862-
$columns = explode( ',', $row );
863-
fputcsv( $handle, $columns );
864863
}
865-
}
866-
$source = new Visualizer_Source_Csv( $tmpfile );
867-
fclose( $handle );
864+
$source = new Visualizer_Source_Csv( $tmpfile );
865+
fclose( $handle );
866+
break;
867+
case 'excel':
868+
// data coming in from the excel editor.
869+
$source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
870+
break;
868871
}
869872
return $source;
870873
}
@@ -997,6 +1000,9 @@ public function uploadData() {
9971000
// delete last error
9981001
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
9991002

1003+
// delete editor related data.
1004+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR );
1005+
10001006
$source = null;
10011007
$render = new Visualizer_Render_Page_Update();
10021008
if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
@@ -1008,9 +1014,11 @@ public function uploadData() {
10081014
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
10091015
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
10101016
} elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
1011-
$source = $this->handleCSVasString( $_POST['chart_data'] );
1017+
$source = $this->handleCSVasString( $_POST['chart_data'], $_POST['editor-type'] );
1018+
update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
10121019
} elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
10131020
$source = $this->handleTabularData();
1021+
update_post_meta( $chart_id, Visualizer_Plugin::CF_EDITOR, $_POST['editor-type'] );
10141022
} else {
10151023
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'CSV file with chart data was not uploaded for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
10161024
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );

classes/Visualizer/Plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class Visualizer_Plugin {
7575
const CF_JSON_SCHEDULE = 'visualizer-json-schedule';
7676
const CF_JSON_PAGING = 'visualizer-json-paging';
7777
const CF_JSON_HEADERS = 'visualizer-json-headers';
78+
const CF_EDITOR = 'visualizer-editor';
7879

7980
const ACTION_SAVE_FILTER_QUERY = 'visualizer-save-filter-query';
8081

classes/Visualizer/Render/Layout.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public static function _renderSimpleEditorScreen( $args ) {
318318
<?php Visualizer_Render_Layout::show( 'editor-table', null, $chart_id, 'viz-html-table', true, true ); ?>
319319
</div>
320320
<input type="hidden" name="table_data" value="yes">
321-
<button class="button button-primary" id="visualizer-save-html-table"><?php esc_html_e( 'Save &amp; Show Chart', 'visualizer' ); ?></button>
321+
<input type="hidden" name="editor-type" value="table">
322322
</form>
323323
</div>
324324

@@ -344,7 +344,6 @@ public static function _renderTextEditor( $args ) {
344344
?>
345345
<div class="viz-simple-editor-type viz-text-editor">
346346
<textarea id="edited_text"><?php echo $data; ?></textarea>
347-
<button id="viz-text-editor-button" class="button button-primary"><?php _e( 'Save &amp; Show Chart', 'visualizer' ); ?></button>
348347
</div>
349348
<?php
350349
}

classes/Visualizer/Render/Page/Data.php

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Visualizer_Render_Page_Data extends Visualizer_Render_Page {
3838
*/
3939
protected function _renderContent() {
4040
// Added by Ash/Upwork
41+
if ( Visualizer_Module::can_show_feature( 'simple-editor' ) ) {
42+
Visualizer_Render_Layout::show( 'simple-editor-screen', $this->chart->ID );
43+
}
44+
4145
if ( Visualizer_Module::is_pro() ) {
4246
do_action( 'visualizer_add_editor_etc', $this->chart->ID );
4347

@@ -48,8 +52,6 @@ protected function _renderContent() {
4852
$Visualizer_Pro->_addFilterWizard( $this->chart->ID );
4953
}
5054
}
51-
} else {
52-
Visualizer_Render_Layout::show( 'simple-editor-screen', $this->chart->ID );
5355
}
5456

5557
$this->add_additional_content();
@@ -86,6 +88,11 @@ protected function _renderSidebarContent() {
8688
if ( ! empty( $filter_config ) ) {
8789
$source_of_chart .= '_wp';
8890
}
91+
$editor_type = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_EDITOR, true );
92+
if ( $editor_type ) {
93+
$source_of_chart = 'visualizer_source_manual';
94+
}
95+
8996
$type = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
9097
$lib = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true );
9198
?>
@@ -373,34 +380,50 @@ class="dashicons dashicons-lock"></span></h2>
373380
</div>
374381
</li>
375382

376-
<?php
377-
// we will auto-open the manual data feature only when source is empty.
378-
?>
379383
<!-- manual -->
380-
<li class="viz-group <?php echo empty( $source_of_chart ) ? 'open' : ''; ?> ">
381-
<h2 class="viz-group-title viz-sub-group visualizer-editor-tab"
382-
data-current="chart"><?php _e( 'Manual Data', 'visualizer' ); ?><span
383-
class="dashicons dashicons-lock"></span></h2>
384-
<form id="editor-form" action="<?php echo $upload_link; ?>" method="post" target="thehole">
385-
<input type="hidden" id="chart-data" name="chart_data">
386-
<input type="hidden" id="chart-data-src" name="chart_data_src">
387-
</form>
388-
384+
<li class="viz-group visualizer_source_manual">
385+
<h2 class="viz-group-title viz-sub-group visualizer-editor-tab" data-current="chart"><?php _e( 'Manual Data', 'visualizer' ); ?>
386+
<span class="dashicons dashicons-lock"></span>
387+
</h2>
389388
<div class="viz-group-content edit-data-content">
390-
<div>
391-
<p class="viz-group-description"><?php echo sprintf( __( 'You can manually edit the chart data using the %s editor.', 'visualizer' ), Visualizer_Module::is_pro() ? 'spreadsheet like' : 'simple' ); ?></p>
392-
<?php if ( ! Visualizer_Module::is_pro() ) { ?>
393-
<p class="viz-group-description simple-editor-type"><input type="checkbox" id="simple-editor-type" value="textarea"><label for="simple-editor-type"><?php _e( 'Use text area editor instead', 'visualizer' ); ?></label></p>
389+
<form id="editor-form" action="<?php echo $upload_link; ?>" method="post" target="thehole">
390+
<input type="hidden" id="chart-data" name="chart_data">
391+
<input type="hidden" id="chart-data-src" name="chart_data_src">
392+
393+
<?php if ( Visualizer_Module::can_show_feature( 'simple-editor' ) ) { ?>
394+
<div>
395+
<p class="viz-group-description viz-editor-selection">
396+
<?php _e( 'Use the', 'visualizer' ); ?>
397+
<select name="editor-type" id="viz-editor-type">
398+
<?php
399+
if ( empty( $editor_type ) ) {
400+
$editor_type = Visualizer_Module::is_pro() ? 'excel' : 'text';
401+
}
402+
foreach ( apply_filters( 'visualizer_editors', array( 'text' => __( 'Text', 'visualizer' ), 'table' => __( 'Simple', 'visualizer' ) ) ) as $e_type => $e_label ) {
403+
?>
404+
<option value="<?php echo $e_type; ?>" <?php selected( $editor_type, $e_type ); ?> ><?php echo $e_label; ?></option>
405+
<?php } ?>
406+
</select>
407+
<?php _e( 'editor to manually edit the chart data.', 'visualizer' ); ?>
408+
</p>
409+
<input type="button" id="editor-undo" class="button button-secondary" style="display: none" value="<?php _e( 'Undo Changes', 'visualizer' ); ?>">
410+
<input type="button" id="editor-button" class="button button-primary "
411+
value="<?php _e( 'Edit Data', 'visualizer' ); ?>" data-current="chart"
412+
data-t-editor="<?php _e( 'Show Chart', 'visualizer' ); ?>"
413+
data-t-chart="<?php _e( 'Edit Data', 'visualizer' ); ?>"
414+
>
415+
<p class="viz-group-description viz-info-msg"><?php echo sprintf( __( 'Please make sure you click \'Show Chart\' before you save the chart.', 'visualizer' ) ); ?></p>
416+
</div>
394417
<?php } else { ?>
395-
<input type="button" id="editor-undo" class="button button-secondary" style="display: none" value="<?php _e( 'Undo Changes', 'visualizer' ); ?>">
418+
<input type="button" id="editor-undo" class="button button-secondary" style="display: none" value="<?php _e( 'Undo Changes', 'visualizer' ); ?>">
419+
<input type="button" id="editor-chart-button" class="button button-primary "
420+
value="<?php _e( 'Edit Data', 'visualizer' ); ?>" data-current="chart"
421+
data-t-editor="<?php _e( 'Show Chart', 'visualizer' ); ?>"
422+
data-t-chart="<?php _e( 'Edit Data', 'visualizer' ); ?>"
423+
>
424+
<p class="viz-group-description viz-info-msg"><?php echo sprintf( __( 'Please make sure you click \'Show Chart\' before you save the chart.', 'visualizer' ) ); ?></p>
396425
<?php } ?>
397-
<input type="button" id="editor-chart-button" class="button button-primary "
398-
value="<?php _e( 'View Editor', 'visualizer' ); ?>" data-current="chart"
399-
data-t-editor="<?php _e( 'Show Chart', 'visualizer' ); ?>"
400-
data-t-chart="<?php _e( 'View Editor', 'visualizer' ); ?>">
401-
402-
<p class="viz-group-description viz-info-msg"><?php echo sprintf( __( 'Please make sure you click \'Show Chart\' before you save the chart.', 'visualizer' ) ); ?></p>
403-
</div>
426+
</form>
404427
</div>
405428
</li>
406429
</ul>

js/frame.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,14 @@
542542

543543
function init_editor_table() {
544544
$('body').on('visualizer:db:editor:table:init', function(event, data){
545-
var $table = create_editor_table('.viz-table-editor');
545+
var $table = create_editor_table('.viz-table-editor', data.config);
546546
$('body').on('visualizer:db:editor:table:redraw', function(event, data){
547547
$table.draw();
548548
});
549549
});
550550
}
551551

552-
function create_editor_table(element) {
552+
function create_editor_table(element, config) {
553553
var settings = {
554554
paging: false,
555555
searching: false,
@@ -576,6 +576,9 @@
576576
]
577577
} );
578578
}
579+
if(config){
580+
$.extend( settings, config );
581+
}
579582

580583
var $table = $(element + ' .viz-editor-table').DataTable(settings);
581584
return $table;

js/render-google.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ var __visualizer_chart_images = [];
229229
}
230230

231231

232-
if(settings.hAxis) {
232+
if(settings.hAxis && series[0]) {
233233
format_data(id, table, series[0].type, settings.hAxis.format, 0);
234234
}
235235

js/simple-editor.js

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,76 @@
88
});
99

1010
function onReady() {
11-
$( '#editor-chart-button' ).on( 'click', function(){
12-
$('.viz-simple-editor-type').hide();
13-
if($("#simple-editor-type").is(':checked')){
14-
switch($("#simple-editor-type").val()) {
15-
case 'textarea':
16-
showText($(this));
17-
break;
18-
}
19-
} else {
20-
showTable($(this));
11+
$( '#editor-button' ).on( 'click', function(e){
12+
switch($("#viz-editor-type").val()) {
13+
case 'text':
14+
showTextEditor($(this));
15+
break;
16+
case 'table':
17+
showTableEditor($(this));
18+
break;
19+
default:
20+
$('body').trigger('visualizer:chart:edit');
2121
}
2222
});
2323
}
2424

25-
function showText(button) {
25+
function showTextEditor(button) {
2626
if( button.attr( 'data-current' ) === 'chart'){
27+
// showing the editor
2728
button.val( button.attr( 'data-t-editor' ) );
2829
button.html( button.attr( 'data-t-editor' ) );
2930
button.attr( 'data-current', 'editor' );
30-
$('p.simple-editor-type').hide();
31-
$( '.viz-text-editor' ).css("z-index", "9999").show();
32-
$( '#canvas' ).css("z-index", "-100").hide();
33-
$('.viz-simple-editor').css("z-index", "9999").show();
31+
$('p.viz-editor-selection').hide();
32+
$('.viz-text-editor').css('z-index', '9999').show();
33+
$('.viz-simple-editor').css('z-index', '9999').show();
34+
$( '#canvas' ).css('z-index', '-100').hide();
3435
}else{
36+
// showing the chart
37+
$('#chart-data').val($('#edited_text').val());
38+
$('#canvas').lock();
39+
$('#editor-form').submit();
40+
3541
button.val( button.attr( 'data-t-chart' ) );
3642
button.html( button.attr( 'data-t-chart' ) );
3743
button.attr( 'data-current', 'chart' );
38-
$( '.viz-text-editor' ).hide();
39-
$('p.simple-editor-type').show();
40-
$( '#canvas' ).css("z-index", "1").show();
44+
$('p.viz-editor-selection').show();
45+
$('.viz-text-editor').hide();
4146
$('.viz-simple-editor').hide();
47+
$( '#canvas' ).css('z-index', '1').show();
4248
}
43-
44-
$( '#viz-text-editor-button').on('click', function(e){
45-
$( '#editor-chart-button' ).attr("disabled", "disabled");
46-
$('#chart-data').val($('#edited_text').val());
47-
$('#canvas').lock();
48-
$('#editor-form').submit();
49-
$( '#editor-chart-button' ).removeAttr("disabled");
50-
$( '#editor-chart-button' ).trigger('click');
51-
});
5249
}
5350

5451
function initTable() {
5552
setTimeout(function(){
56-
$('body').trigger('visualizer:db:editor:table:init', {});
53+
$('body').trigger('visualizer:db:editor:table:init', {config: { buttons: [] } });
5754
$( '#canvas' ).unlock();
5855
}, 1000);
59-
60-
// when the data is set and the chart is updated, toggle the screen so that the chart is shown
61-
$('#table-editor-form').on( 'submit', function(e){
62-
$( '#editor-chart-button' ).trigger('click');
63-
$('#canvas').lock();
64-
});
6556
}
6657

67-
function showTable(button) {
58+
function showTableEditor(button) {
6859
if( button.attr( 'data-current' ) === 'chart'){
60+
// showing the editor
6961
button.val( button.attr( 'data-t-editor' ) );
7062
button.html( button.attr( 'data-t-editor' ) );
7163
button.attr( 'data-current', 'editor' );
64+
$('p.viz-editor-selection').hide();
7265
$( '.viz-table-editor' ).css("z-index", "9999").show();
66+
$('.viz-simple-editor').css('z-index', '9999').show();
7367
$('body').trigger('visualizer:db:editor:table:redraw', {});
7468
$( '#canvas' ).css("z-index", "-100").hide();
75-
$('p.simple-editor-type').hide();
76-
$('.viz-simple-editor').css("z-index", "9999").show();
7769
}else{
70+
$('#canvas').lock();
71+
$('#table-editor-form').submit();
72+
73+
// showing the chart
7874
button.val( button.attr( 'data-t-chart' ) );
7975
button.html( button.attr( 'data-t-chart' ) );
8076
button.attr( 'data-current', 'chart' );
81-
$( '.viz-table-editor' ).hide();
82-
$( '#canvas' ).css("z-index", "1").show();
77+
$('p.viz-editor-selection').show();
78+
$('.viz-table-editor').hide();
8379
$('.viz-simple-editor').hide();
84-
$('p.simple-editor-type').show();
80+
$( '#canvas' ).css('z-index', '1').show();
8581
}
8682
}
8783

0 commit comments

Comments
 (0)