Skip to content

Commit e424c8d

Browse files
Merge pull request #594 from contactashish13/issue-579
Manual data tab to show options for multiple editors
2 parents e3ce3a0 + 39e1141 commit e424c8d

File tree

10 files changed

+193
-92
lines changed

10 files changed

+193
-92
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( 'View Editor', 'visualizer' ); ?>" data-current="chart"
421+
data-t-editor="<?php _e( 'Show Chart', 'visualizer' ); ?>"
422+
data-t-chart="<?php _e( 'View Editor', '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>

cypress.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"plan2": 0
1515
},
1616
"query": "SELECT post_type, count(*) from wp_posts GROUP by post_type",
17-
"wait": 10000
17+
"wait": 10000,
18+
"editors": {
19+
"free": 2,
20+
"selected" : "table"
21+
}
1822
},
1923
"videoUploadOnPasses": false,
2024
"projectId": "yhx2jj"

cypress/integration/free-sources.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,56 @@ describe('Test Free - sources', function() {
160160
});
161161
});
162162

163+
it('Manual Data', function() {
164+
cy.visit(Cypress.env('urls').library ).then(() => {
165+
const id = Cypress.$('div.visualizer-chart div.visualizer-chart-canvas').first().attr('id');
166+
first_chart_content = Cypress.$('#' + id).html();
167+
});
168+
169+
cy.get('.visualizer-chart-action.visualizer-chart-edit').first().click({force:true});
170+
171+
cy.wait( Cypress.env('wait') );
172+
173+
cy.get('iframe')
174+
.then(function ($iframe) {
175+
const $body = $iframe.contents().find('body');
176+
177+
cy.wrap($body).find('.viz-simple-editor-type.viz-table-editor').should('not.be.visible');
178+
cy.wrap($body).find('#viz-editor-type').should('not.be.visible');
179+
180+
cy.wrap($body).find('.viz-group-title.visualizer-editor-tab').click().then( () => {
181+
// check how many editors.
182+
cy.wrap($body).find('#viz-editor-type option').should('have.length', Cypress.env('editors').free);
183+
184+
// select an editor, save chart and then check if the chart is reloaded with it
185+
cy.wrap($body).find('#viz-editor-type').select(Cypress.env('editors').selected);
186+
cy.wrap($body).find('#editor-button').click();
187+
188+
cy.wrap($body).find('.viz-simple-editor-type.viz-table-editor').should('be.visible');
189+
190+
cy.wrap($body).find('#editor-button').click().then( () => {
191+
cy.wrap($body).find('.viz-simple-editor-type.viz-table-editor').should('not.be.visible');
192+
cy.wrap($body).find('#settings-button').click({force:true});
193+
});
194+
});
195+
});
196+
197+
cy.wait( Cypress.env('wait') );
198+
199+
// reload the same chart, it should open in the edit tab with the correct editor selected
200+
cy.get('.visualizer-chart-action.visualizer-chart-edit').first().click({force:true});
201+
202+
cy.wait( Cypress.env('wait') );
203+
204+
cy.get('iframe')
205+
.then(function ($iframe) {
206+
const $body = $iframe.contents().find('body');
207+
208+
cy.wrap($body).find('.viz-simple-editor-type.viz-table-editor').should('not.be.visible');
209+
210+
cy.wrap($body).find('#viz-editor-type').should('be.visible');
211+
cy.wrap($body).find('#viz-editor-type').invoke('val').should('contain', Cypress.env('editors').selected);
212+
});
213+
});
214+
163215
})

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
@@ -252,7 +252,7 @@ var __visualizer_chart_images = [];
252252
format_data(id, table, 'number', settings.format, 1);
253253
}
254254

255-
if(settings.hAxis) {
255+
if(settings.hAxis && series[0]) {
256256
format_data(id, table, series[0].type, settings.hAxis.format, 0);
257257
}
258258

0 commit comments

Comments
 (0)