Skip to content

Commit caad32d

Browse files
Merge pull request #401 from contactashish13/issue-388
Add simple editor
2 parents 802ec9f + 397b05d commit caad32d

File tree

8 files changed

+448
-70
lines changed

8 files changed

+448
-70
lines changed

classes/Visualizer/Module.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
6666

6767
$this->_addFilter( Visualizer_Plugin::FILTER_UNDO_REVISIONS, 'undoRevisions', 10, 2 );
6868
$this->_addFilter( Visualizer_Plugin::FILTER_HANDLE_REVISIONS, 'handleExistingRevisions', 10, 2 );
69+
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, 'getDataAs', 10, 3 );
6970

7071
}
7172

@@ -147,6 +148,15 @@ protected function _addShortcode( $tag, $method ) {
147148
return $this;
148149
}
149150

151+
/**
152+
* A wrapper around the actual function _getDataAs. This function is invoked as a filter.
153+
*
154+
* @since 3.2.0
155+
*/
156+
public function getDataAs( $final, $chart_id, $type ) {
157+
return $this->_getDataAs( $chart_id, $type );
158+
}
159+
150160
/**
151161
* Extracts the data for a chart and prepares it for the given type.
152162
*
@@ -223,9 +233,10 @@ public function _getDataAs( $chart_id, $type ) {
223233
private function _getCSV( $rows, $filename ) {
224234
$filename .= '.csv';
225235

236+
$bom = chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF );
226237
$fp = tmpfile();
227238
// support for MS Excel
228-
fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
239+
fprintf( $fp, $bom );
229240
foreach ( $rows as $row ) {
230241
fputcsv( $fp, $row );
231242
}
@@ -243,6 +254,7 @@ private function _getCSV( $rows, $filename ) {
243254
return array(
244255
'csv' => $csv,
245256
'name' => $filename,
257+
'string' => str_replace( $bom, '', $csv ),
246258
);
247259
}
248260

classes/Visualizer/Module/Chart.php

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function getJsonData() {
162162
wp_send_json_error();
163163
}
164164

165-
$data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table' );
165+
$data = Visualizer_Render_Layout::show( 'editor-table', $data, $chart_id, 'viz-json-table', false, false );
166166
wp_send_json_success( array( 'table' => $data, 'root' => $params['root'], 'url' => $params['url'], 'paging' => $source->getPaginationElements() ) );
167167
}
168168

@@ -482,11 +482,14 @@ public function renderChartPages() {
482482
Visualizer_Plugin::VERSION,
483483
true
484484
);
485+
wp_register_script( 'visualizer-editor-simple', VISUALIZER_ABSURL . 'js/simple-editor.js', array( 'jquery' ), Visualizer_Plugin::VERSION, true );
486+
485487
// added by Ash/Upwork
486488
if ( VISUALIZER_PRO ) {
487489
global $Visualizer_Pro;
488490
$Visualizer_Pro->_addScriptsAndStyles();
489491
}
492+
490493
// dispatch pages
491494
$this->_chart = get_post( $chart_id );
492495
$tab = isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'visualizer';
@@ -624,6 +627,23 @@ private function _handleDataAndSettingsPage() {
624627
wp_enqueue_script( 'visualizer-chosen' );
625628
wp_enqueue_script( 'visualizer-render' );
626629

630+
if ( ! VISUALIZER_PRO ) {
631+
wp_enqueue_script( 'visualizer-editor-simple' );
632+
wp_localize_script(
633+
'visualizer-editor-simple',
634+
'visualizer1',
635+
array(
636+
'ajax' => array(
637+
'url' => admin_url( 'admin-ajax.php' ),
638+
'nonces' => array(
639+
),
640+
'actions' => array(
641+
),
642+
),
643+
)
644+
);
645+
}
646+
627647
$table_col_mapping = $this->loadCodeEditorAssets();
628648

629649
wp_localize_script(
@@ -741,7 +761,105 @@ private function _handleTypesPage() {
741761
public function renderFlattrScript() {
742762
echo '';
743763
}
744-
// changed by Ash/Upwork
764+
765+
/**
766+
* Processes the CSV that is sent in the request as a string.
767+
*
768+
* @since 3.2.0
769+
*/
770+
private function handleCSVasString( $data ) {
771+
$source = null;
772+
if ( VISUALIZER_PRO ) {
773+
$source = apply_filters( 'visualizer_pro_handle_chart_data', $data, '' );
774+
} else {
775+
// data coming in from the text editor.
776+
$tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
777+
$handle = fopen( $tmpfile, 'w' );
778+
$values = preg_split( '/[\n\r]+/', stripslashes( trim( $data ) ) );
779+
if ( $values ) {
780+
foreach ( $values as $row ) {
781+
if ( empty( $row ) ) {
782+
continue;
783+
}
784+
$columns = explode( ',', $row );
785+
fputcsv( $handle, $columns );
786+
}
787+
}
788+
$source = new Visualizer_Source_Csv( $tmpfile );
789+
fclose( $handle );
790+
}
791+
return $source;
792+
}
793+
794+
/**
795+
* Parses the data uploaded as an HTML table.
796+
*
797+
* @since 3.2.0
798+
*
799+
* @access private
800+
*/
801+
private function handleTabularData() {
802+
$csv = array();
803+
// the datatable mentions the headers twice, so lets remove the duplicates.
804+
$headers = array_unique( array_filter( $_POST['header'] ) );
805+
$types = $_POST['type'];
806+
807+
// capture all the indexes that correspond to excluded columns.
808+
$exclude = array();
809+
$index = 0;
810+
foreach ( $types as $type ) {
811+
if ( empty( $type ) ) {
812+
$exclude[] = $index;
813+
}
814+
$index++;
815+
}
816+
817+
// when N headers are being renamed, the number of headers increases by N
818+
// because of the way datatable duplicates header information
819+
// so unset the headers that have been renamed.
820+
if ( count( $headers ) !== count( $types ) ) {
821+
$to = count( $headers );
822+
for ( $i = count( $types ); $i < $to; $i++ ) {
823+
unset( $headers[ $i + 1 ] );
824+
}
825+
}
826+
827+
$columns = array();
828+
for ( $i = 0; $i < count( $headers ); $i++ ) {
829+
if ( ! isset( $_POST[ 'data' . $i ] ) ) {
830+
continue;
831+
}
832+
$columns[ $i ] = $_POST[ 'data' . $i ];
833+
}
834+
835+
$csv[] = $headers;
836+
$csv[] = $types;
837+
for ( $j = 0; $j < count( $columns[0] ); $j++ ) {
838+
$row = array();
839+
for ( $i = 0; $i < count( $headers ); $i++ ) {
840+
$row[] = $columns[ $i ][ $j ];
841+
}
842+
$csv[] = $row;
843+
}
844+
845+
$tmpfile = tempnam( get_temp_dir(), Visualizer_Plugin::NAME );
846+
$handle = fopen( $tmpfile, 'w' );
847+
848+
if ( $csv ) {
849+
$index = 0;
850+
foreach ( $csv as $row ) {
851+
// remove all the cells corresponding to the excluded headers.
852+
foreach ( $exclude as $j ) {
853+
unset( $row[ $j ] );
854+
}
855+
fputcsv( $handle, $row );
856+
}
857+
}
858+
$source = new Visualizer_Source_Csv( $tmpfile );
859+
fclose( $handle );
860+
return $source;
861+
}
862+
745863
/**
746864
* Parses uploaded CSV file and saves new data for the chart.
747865
*
@@ -805,7 +923,9 @@ public function uploadData() {
805923
} elseif ( isset( $_FILES['local_data'] ) && $_FILES['local_data']['error'] == 0 ) {
806924
$source = new Visualizer_Source_Csv( $_FILES['local_data']['tmp_name'] );
807925
} elseif ( isset( $_POST['chart_data'] ) && strlen( $_POST['chart_data'] ) > 0 ) {
808-
$source = apply_filters( 'visualizer_pro_handle_chart_data', $_POST['chart_data'], '', $chart_id, $_POST );
926+
$source = $this->handleCSVasString( $_POST['chart_data'] );
927+
} elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
928+
$source = $this->handleTabularData();
809929
} else {
810930
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please, try again.', 'visualizer' );
811931
}

classes/Visualizer/Plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Visualizer_Plugin {
8181
const FILTER_GET_CHART_SETTINGS = 'visualizer-get-chart-settings';
8282
const FILTER_UNDO_REVISIONS = 'visualizer-undo-revisions';
8383
const FILTER_HANDLE_REVISIONS = 'visualizer-handle-revisions';
84+
const FILTER_GET_CHART_DATA_AS = 'visualizer-get-chart-data-as';
8485

8586
const CF_DB_SCHEDULE = 'visualizer-db-schedule';
8687
const CF_DB_QUERY = 'visualizer-db-query';

0 commit comments

Comments
 (0)