Skip to content

Commit a73b3de

Browse files
Merge pull request #537 from contactashish13/issue-510
For every chart show the last updated date and any error that exists
2 parents 3d4323b + b43aa65 commit a73b3de

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,8 @@ public function uploadData() {
934934
exit;
935935
}
936936

937+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploading data for chart %d with POST = %s and GET = %s', $chart_id, print_r( $_POST, true ), print_r( $_GET, true ) ), 'debug', __FILE__, __LINE__ );
938+
937939
if ( ! isset( $_POST['vz-import-time'] ) ) {
938940
apply_filters( 'visualizer_pro_remove_schedule', $chart_id );
939941
}
@@ -954,6 +956,9 @@ public function uploadData() {
954956
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
955957
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
956958

959+
// delete last error
960+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
961+
957962
$source = null;
958963
$render = new Visualizer_Render_Page_Update();
959964
if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
@@ -969,8 +974,13 @@ public function uploadData() {
969974
} elseif ( isset( $_POST['table_data'] ) && 'yes' === $_POST['table_data'] ) {
970975
$source = $this->handleTabularData();
971976
} else {
977+
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__ );
972978
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
979+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' ) );
973980
}
981+
982+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Uploaded data for chart %d with source %s', $chart_id, print_r( $source, true ) ), 'debug', __FILE__, __LINE__ );
983+
974984
if ( $source ) {
975985
if ( $source->fetch() ) {
976986
$content = $source->getData();
@@ -981,6 +991,7 @@ public function uploadData() {
981991
// if we populate the data even if it is empty, the chart will show "Table has no columns".
982992
if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
983993
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ), 'warn', __FILE__, __LINE__ );
994+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ) );
984995
$populate = false;
985996
}
986997
}
@@ -992,6 +1003,8 @@ public function uploadData() {
9921003
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
9931004
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
9941005

1006+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Updated post for chart %d', $chart_id ), 'debug', __FILE__, __LINE__ );
1007+
9951008
Visualizer_Module_Utility::set_defaults( $chart, null );
9961009

9971010
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
@@ -1001,11 +1014,16 @@ public function uploadData() {
10011014
$render->series = json_encode( $source->getSeries() );
10021015
$render->settings = json_encode( $settings );
10031016
} else {
1004-
$render->message = $source->get_error();
1005-
if ( empty( $render->message ) ) {
1006-
$render->message = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
1017+
$error = $source->get_error();
1018+
if ( empty( $error ) ) {
1019+
$error = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
10071020
}
1021+
$render->message = $error;
1022+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( '%s for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1023+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, $error );
10081024
}
1025+
} else {
1026+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unknown internal error for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );
10091027
}
10101028
$render->render();
10111029
if ( ! $can_die ) {

classes/Visualizer/Module/Setup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
249249
);
250250

251251
$chart = get_post( $chart_id );
252+
} else {
253+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Error while updating chart: %s', $error ) );
252254
}
253255

254256
return $chart;

classes/Visualizer/Plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Visualizer_Plugin {
4040
const CF_DEFAULT_DATA = 'visualizer-default-data';
4141
const CF_SETTINGS = 'visualizer-settings';
4242
const CF_CHART_LIBRARY = 'visualizer-chart-library';
43+
const CF_ERROR = 'visualizer-error';
4344

4445
const CF_SOURCE_FILTER = 'visualizer-source-filter';
4546
const CF_FILTER_CONFIG = 'visualizer-filter-config';

classes/Visualizer/Render/Library.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ private function _renderChartBox( $placeholder_id, $chart_id ) {
220220
),
221221
admin_url( 'admin-ajax.php' )
222222
);
223+
224+
$chart_status = array( 'date' => get_the_modified_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $chart_id ), 'error' => get_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, true ), 'icon' => 'dashicons-yes-alt', 'title' => '' );
225+
if ( ! empty( $chart_status['error'] ) ) {
226+
$chart_status['icon'] = 'error dashicons-dismiss';
227+
$chart_status['title'] = __( 'Click to view the error', 'visualizer' );
228+
}
229+
223230
echo '<div class="visualizer-chart"><div class="visualizer-chart-title">', esc_html( $title ), '</div>';
224231
echo '<div id="', $placeholder_id, '" class="visualizer-chart-canvas">';
225232
echo '<img src="', VISUALIZER_ABSURL, 'images/ajax-loader.gif" class="loader">';
@@ -232,6 +239,7 @@ private function _renderChartBox( $placeholder_id, $chart_id ) {
232239
echo '<span class="visualizer-chart-shortcode" title="', esc_attr__( 'Click to select', 'visualizer' ), '">';
233240
echo '&nbsp;[visualizer id=&quot;', $chart_id, '&quot;]&nbsp;';
234241
echo '</span>';
242+
echo '<hr><div class="visualizer-chart-status"><span class="visualizer-date" title="' . __( 'Last Updated', 'visualizer' ) . '">' . $chart_status['date'] . '</span><span class="visualizer-error"><i class="dashicons ' . $chart_status['icon'] . '" data-viz-error="' . esc_attr( str_replace( '"', "'", $chart_status['error'] ) ) . '" title="' . esc_attr( $chart_status['title'] ) . '"></i></span></div>';
235243
echo '</div>';
236244
echo '</div>';
237245
}

css/library.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,21 @@ div#visualizer-types ul, div#visualizer-types form p {
311311
vertical-align: middle;
312312
margin: 0;
313313
}
314+
315+
.visualizer-chart-status {
316+
padding: 3px 0;
317+
color: #aaa;
318+
text-shadow: 0 1px 0 #fff;
319+
}
320+
321+
.visualizer-chart-status span.visualizer-error {
322+
float: right;
323+
}
324+
325+
.visualizer-error i {
326+
color: green;
327+
}
328+
.visualizer-error i.error {
329+
color: red;
330+
cursor: pointer;
331+
}

index.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ function visualizer_launch() {
9191
define( 'VISUALIZER_DB_QUERY_DOC_URL', 'https://docs.themeisle.com/article/970-visualizer-sample-queries-to-generate-charts' );
9292
define( 'VISUALIZER_MAIN_DOC', 'https://docs.themeisle.com/category/657-visualizer' );
9393

94+
// to redirect all themeisle_log_event to error log.
95+
define( 'VISUALIZER_LOCAL_DEBUG', false );
96+
9497
// instantiate the plugin
9598
$plugin = Visualizer_Plugin::instance();
9699

@@ -153,3 +156,17 @@ function visualizer_register_parrot( $plugins ) {
153156
spl_autoload_register( 'visualizer_autoloader' );
154157
// launch the plugin
155158
visualizer_launch();
159+
160+
161+
if ( VISUALIZER_LOCAL_DEBUG ) {
162+
add_action( 'themeisle_log_event', 'visualizer_themeisle_log_event', 10, 5 );
163+
164+
/**
165+
* Redirect themeisle_log_event to error log.
166+
*/
167+
function visualizer_themeisle_log_event( $name, $msg, $type, $file, $line ) {
168+
if ( $name === Visualizer_Plugin::NAME ) {
169+
error_log( sprintf( '%s (%s:%d): %s', $type, $file, $line, $msg ) );
170+
}
171+
}
172+
}

js/library.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* global visualizer */
2+
/* global alert */
3+
24
(function (wpmv) {
35
var vm, vmv;
46

@@ -129,5 +131,9 @@
129131
$('.visualizer-chart-canvas').adjust();
130132
}, 100);
131133
});
134+
135+
$('.visualizer-error i.error').on('click', function(){
136+
alert( $(this).attr('data-viz-error') );
137+
});
132138
});
133139
})(jQuery, visualizer.media.view, visualizer.urls);

0 commit comments

Comments
 (0)