Skip to content

Commit 18136bb

Browse files
Merge pull request #562 from contactashish13/issue-203-pro
Ability to import from external databases
2 parents edb013e + 550f27e commit 18136bb

File tree

8 files changed

+120
-47
lines changed

8 files changed

+120
-47
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public function renderChartPages() {
549549
/**
550550
* Load code editor assets.
551551
*/
552-
private function loadCodeEditorAssets() {
552+
private function loadCodeEditorAssets( $chart_id ) {
553553
global $wp_version;
554554

555555
$wp_scripts = wp_scripts();
@@ -565,7 +565,7 @@ private function loadCodeEditorAssets() {
565565
return;
566566
}
567567

568-
$table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping();
568+
$table_col_mapping = Visualizer_Source_Query_Params::get_all_db_tables_column_mapping( $chart_id );
569569

570570
if ( version_compare( $wp_version, '4.9.0', '<' ) ) {
571571
// code mirror assets.
@@ -681,7 +681,7 @@ private function _handleDataAndSettingsPage() {
681681
);
682682
}
683683

684-
$table_col_mapping = $this->loadCodeEditorAssets();
684+
$table_col_mapping = $this->loadCodeEditorAssets( $this->_chart->ID );
685685

686686
wp_localize_script(
687687
'visualizer-render',
@@ -743,11 +743,11 @@ private function _handleDataAndSettingsPage() {
743743
$render->button = esc_attr__( 'Insert Chart', 'visualizer' );
744744
}
745745

746-
do_action( 'visualizer_enqueue_scripts_and_styles', $data );
746+
do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
747747

748748
if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
749749
global $Visualizer_Pro;
750-
$Visualizer_Pro->_enqueueScriptsAndStyles( $data );
750+
$Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
751751
}
752752

753753
$this->_addAction( 'admin_head', 'renderFlattrScript' );
@@ -962,6 +962,7 @@ public function uploadData() {
962962
// delete "import from db" specific parameters.
963963
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY );
964964
delete_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE );
965+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS );
965966
}
966967

967968
// delete json related data.
@@ -1166,11 +1167,11 @@ private function _handleDataPage() {
11661167
)
11671168
);
11681169

1169-
do_action( 'visualizer_enqueue_scripts_and_styles', $data );
1170+
do_action( 'visualizer_enqueue_scripts_and_styles', $data, $this->_chart->ID );
11701171

11711172
if ( Visualizer_Module::is_pro() && Visualizer_Module::is_pro_older_than( '1.9.0' ) ) {
11721173
global $Visualizer_Pro;
1173-
$Visualizer_Pro->_enqueueScriptsAndStyles( $data );
1174+
$Visualizer_Pro->_enqueueScriptsAndStyles( $data, $this->_chart->ID );
11741175
}
11751176

11761177
// Added by Ash/Upwork
@@ -1187,11 +1188,12 @@ public function getQueryData() {
11871188
check_ajax_referer( Visualizer_Plugin::ACTION_FETCH_DB_DATA . Visualizer_Plugin::VERSION, 'security' );
11881189

11891190
$params = wp_parse_args( $_POST['params'] );
1190-
$source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1191+
$chart_id = filter_var( $params['chart_id'], FILTER_VALIDATE_INT );
1192+
1193+
$source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
11911194
$html = $source->fetch( true );
1192-
$error = '';
1193-
if ( empty( $html ) ) {
1194-
$error = $source->get_error();
1195+
$error = $source->get_error();
1196+
if ( ! empty( $error ) ) {
11951197
wp_send_json_error( array( 'msg' => $error ) );
11961198
}
11971199
wp_send_json_success( array( 'table' => $html ) );
@@ -1228,14 +1230,14 @@ public function saveQuery() {
12281230
)
12291231
);
12301232

1231-
if ( ! $hours ) {
1233+
if ( ! is_int( $hours ) ) {
12321234
$hours = -1;
12331235
}
12341236

12351237
$render = new Visualizer_Render_Page_Update();
12361238
if ( $chart_id ) {
12371239
$params = wp_parse_args( $_POST['params'] );
1238-
$source = new Visualizer_Source_Query( stripslashes( $params['query'] ) );
1240+
$source = new Visualizer_Source_Query( stripslashes( $params['query'] ), $chart_id, $params );
12391241
$source->fetch( false );
12401242
$error = $source->get_error();
12411243
if ( empty( $error ) ) {
@@ -1244,6 +1246,12 @@ public function saveQuery() {
12441246
update_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
12451247
update_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, $hours );
12461248
update_post_meta( $chart_id, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
1249+
if ( isset( $params['db_type'] ) && $params['db_type'] !== Visualizer_Plugin::WP_DB_NAME ) {
1250+
$remote_db_params = $params;
1251+
unset( $remote_db_params['query'] );
1252+
unset( $remote_db_params['chart_id'] );
1253+
update_post_meta( $chart_id, Visualizer_Plugin::CF_REMOTE_DB_PARAMS, $remote_db_params );
1254+
}
12471255

12481256
$schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
12491257
$schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;

classes/Visualizer/Module/Setup.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
258258
}
259259

260260
$params = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_QUERY, true );
261-
$source = new Visualizer_Source_Query( $params );
261+
$source = new Visualizer_Source_Query( $params, $chart_id );
262262
$source->fetch( false );
263263
$load_series = true;
264264
break;
@@ -298,6 +298,7 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
298298
);
299299

300300
$chart = get_post( $chart_id );
301+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
301302
} else {
302303
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Error while updating chart: %s', $error ) );
303304
}

classes/Visualizer/Plugin.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Visualizer_Plugin {
4141
const CF_SETTINGS = 'visualizer-settings';
4242
const CF_CHART_LIBRARY = 'visualizer-chart-library';
4343
const CF_ERROR = 'visualizer-error';
44+
const CF_REMOTE_DB_PARAMS = 'visualizer-remote-db-params';
4445

4546
const CF_SOURCE_FILTER = 'visualizer-source-filter';
4647
const CF_FILTER_CONFIG = 'visualizer-filter-config';
@@ -94,6 +95,12 @@ class Visualizer_Plugin {
9495
// Added by Ash/Upwork
9596
const PRO_TEASER_URL = 'https://themeisle.com/plugins/visualizer-charts-and-graphs/upgrade/#pricing';
9697
const PRO_TEASER_TITLE = 'Check PRO version ';
98+
99+
/**
100+
* Name of the option for WordPress DB.
101+
*/
102+
const WP_DB_NAME = 'WordPress DB';
103+
97104
// Added by Ash/Upwork
98105
/**
99106
* Singletone instance of the plugin.

classes/Visualizer/Render/Layout.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static function _renderDbQuery( $args ) {
6060
<div class="visualizer-db-query-form">
6161
<div>
6262
<form id='db-query-form'>
63+
<input type="hidden" name="chart_id" value="<?php echo $args[2]; ?>">
64+
<?php do_action( 'visualizer_db_query_add_layout', $args ); ?>
6365
<textarea name='query' class='visualizer-db-query' placeholder="<?php _e( 'Your query goes here', 'visualizer' ); ?>"><?php echo $query; ?></textarea>
6466
</form>
6567
<div class='db-wizard-error'></div>
@@ -72,6 +74,7 @@ public static function _renderDbQuery( $args ) {
7274
<ul>
7375
<li><?php echo sprintf( __( 'For examples of queries and links to resources that you can use with this feature, please click %1$shere%2$s', 'visualizer' ), '<a href="' . VISUALIZER_DB_QUERY_DOC_URL . '" target="_blank">', '</a>' ); ?></li>
7476
<li><?php echo sprintf( __( 'Use %1$sControl+Space%2$s for autocompleting keywords or table names.', 'visualizer' ), '<span class="visualizer-emboss">', '</span>' ); ?></li>
77+
<?php do_action( 'visualizer_db_query_add_hints', $args ); ?>
7578
</ul>
7679
</div>
7780
<div class='db-wizard-results'></div>

classes/Visualizer/Render/Page/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private function add_additional_content() {
611611
if ( 'visualizer_source_query' === $source ) {
612612
$query = get_post_meta( $this->chart->ID, Visualizer_Plugin::CF_DB_QUERY, true );
613613
}
614-
Visualizer_Render_Layout::show( 'db-query', $query );
614+
Visualizer_Render_Layout::show( 'db-query', $query, $this->chart->ID );
615615
Visualizer_Render_Layout::show( 'json-screen', $this->chart->ID );
616616
}
617617

classes/Visualizer/Source/Query.php

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,34 @@ class Visualizer_Source_Query extends Visualizer_Source {
3535
*/
3636
protected $_query;
3737

38+
/**
39+
* The chart id.
40+
*
41+
* @access protected
42+
* @var int
43+
*/
44+
protected $_chart_id;
45+
46+
/**
47+
* Any additional parameters (e.g. for connecting to a remote db).
48+
*
49+
* @access protected
50+
* @var array
51+
*/
52+
protected $_params;
53+
3854
/**
3955
* Constructor.
4056
*
4157
* @access public
4258
* @param string $query The query.
59+
* @param int $chart_id The chart id.
60+
* @param array $params Any additional parameters (e.g. for connecting to a remote db).
4361
*/
44-
public function __construct( $query = null ) {
62+
public function __construct( $query = null, $chart_id = null, $params = null ) {
4563
$this->_query = $query;
64+
$this->_chart_id = $chart_id;
65+
$this->_params = $params;
4666
}
4767

4868
/**
@@ -67,48 +87,67 @@ public function fetch( $as_html = false, $results_as_numeric_array = false, $raw
6787

6888
// impose a limit if no limit clause is provided.
6989
if ( strpos( strtolower( $this->_query ), ' limit ' ) === false ) {
70-
$this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000 );
90+
$this->_query .= ' LIMIT ' . apply_filters( 'visualizer_sql_query_limit', 1000, $this->_chart_id );
7191
}
7292

73-
global $wpdb;
74-
$wpdb->hide_errors();
75-
// @codingStandardsIgnoreStart
76-
$rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
77-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ );
78-
// @codingStandardsIgnoreEnd
79-
$wpdb->show_errors();
80-
81-
if ( $raw_results ) {
82-
return $rows;
93+
$results = array();
94+
$headers = array();
95+
96+
// short circuit results for remote dbs.
97+
if ( false !== ( $remote_results = apply_filters( 'visualizer_db_query_execute', false, $this->_query, $as_html, $results_as_numeric_array, $raw_results, $this->_chart_id, $this->_params ) ) ) {
98+
$error = $remote_results['error'];
99+
if ( empty( $error ) ) {
100+
$results = $remote_results['results'];
101+
$headers = $remote_results['headers'];
102+
}
103+
104+
$this->_error = $error;
105+
106+
if ( $raw_results ) {
107+
return $results;
108+
}
83109
}
84110

85-
if ( $rows ) {
86-
$results = array();
87-
$headers = array();
111+
if ( ! ( $results && $headers ) ) {
112+
global $wpdb;
113+
$wpdb->hide_errors();
114+
// @codingStandardsIgnoreStart
115+
$rows = $wpdb->get_results( $this->_query, $results_as_numeric_array ? ARRAY_N : ARRAY_A );
116+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Firing query %s to get results %s with error %s', $this->_query, print_r( $rows, true ), print_r( $wpdb->last_error, true ) ), 'debug', __FILE__, __LINE__ );
117+
// @codingStandardsIgnoreEnd
118+
$wpdb->show_errors();
119+
120+
if ( $raw_results ) {
121+
return $rows;
122+
}
123+
88124
if ( $rows ) {
89-
$row_num = 0;
90-
foreach ( $rows as $row ) {
91-
$result = array();
92-
$col_num = 0;
93-
foreach ( $row as $k => $v ) {
94-
$result[] = $v;
95-
if ( 0 === $row_num ) {
96-
$headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k );
125+
$results = array();
126+
$headers = array();
127+
if ( $rows ) {
128+
$row_num = 0;
129+
foreach ( $rows as $row ) {
130+
$result = array();
131+
$col_num = 0;
132+
foreach ( $row as $k => $v ) {
133+
$result[] = $v;
134+
if ( 0 === $row_num ) {
135+
$headers[] = array( 'type' => $this->get_col_type( $col_num++ ), 'label' => $k );
136+
}
97137
}
138+
$results[] = $result;
139+
$row_num++;
98140
}
99-
$results[] = $result;
100-
$row_num++;
101141
}
102-
}
103142

104-
if ( $as_html ) {
105-
return $this->html( $headers, $results );
143+
$this->_error = $wpdb->last_error;
106144
}
107-
return $this->object( $headers, $results );
108145
}
109146

110-
$this->_error = $wpdb->last_error;
111-
return null;
147+
if ( $as_html ) {
148+
return $this->html( $headers, $results );
149+
}
150+
return $this->object( $headers, $results );
112151
}
113152

114153
/**

classes/Visualizer/Source/Query/Params.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,17 @@ public static function get_db_tables() {
337337
* @access public
338338
* @return array
339339
*/
340-
public static function get_all_db_tables_column_mapping() {
340+
public static function get_all_db_tables_column_mapping( $chart_id, $use_filter = true ) {
341341
$mapping = array();
342342
$tables = self::get_db_tables();
343343
foreach ( $tables as $table ) {
344344
$cols = self::get_db_table_columns( $table, true );
345345
$names = wp_list_pluck( $cols, 'name' );
346346
$mapping[ $table ] = $names;
347347
}
348+
if ( $use_filter ) {
349+
return apply_filters( 'visualizer_db_tables_column_mapping', $mapping, $chart_id );
350+
}
348351
return $mapping;
349352
}
350353

js/frame.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@
278278
$('body').on('visualizer:db:query:update', function(event, data){
279279
cm.save();
280280
});
281+
282+
// clear the editor.
283+
$('body').on('visualizer:db:query:setvalue', function(event, data){
284+
cm.setValue(data.value);
285+
cm.clearHistory();
286+
cm.refresh();
287+
});
288+
289+
// set an option at runtime?
290+
$('body').on('visualizer:db:query:changeoption', function(event, data){
291+
cm.setOption(data.name, data.value);
292+
});
281293
}
282294

283295
function init_filter_import() {

0 commit comments

Comments
 (0)