Skip to content

Commit 2f7d7e7

Browse files
For every chart show the last updated date and any error that exists
1 parent bded578 commit 2f7d7e7

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

classes/Visualizer/Module/Chart.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,9 @@ public function uploadData() {
955955
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_ROOT );
956956
delete_post_meta( $chart_id, Visualizer_Plugin::CF_JSON_PAGING );
957957

958+
// delete last error
959+
delete_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR );
960+
958961
$source = null;
959962
$render = new Visualizer_Render_Page_Update();
960963
if ( isset( $_POST['remote_data'] ) && filter_var( $_POST['remote_data'], FILTER_VALIDATE_URL ) ) {
@@ -972,6 +975,7 @@ public function uploadData() {
972975
} else {
973976
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__ );
974977
$render->message = esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' );
978+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, esc_html__( 'CSV file with chart data was not uploaded. Please try again.', 'visualizer' ) );
975979
}
976980

977981
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__ );
@@ -986,6 +990,7 @@ public function uploadData() {
986990
// if we populate the data even if it is empty, the chart will show "Table has no columns".
987991
if ( array_key_exists( 'source', $json ) && ! empty( $json['source'] ) && ( ! array_key_exists( 'data', $json ) || empty( $json['data'] ) ) ) {
988992
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__ );
993+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, sprintf( 'Not populating chart data as source exists (%s) but data is empty!', $json['source'] ) );
989994
$populate = false;
990995
}
991996
}
@@ -1010,9 +1015,11 @@ public function uploadData() {
10101015
} else {
10111016
$error = $source->get_error();
10121017
if ( empty( $error ) ) {
1013-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'CSV file is broken or invalid (%s) for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1014-
$render->message = sprintf( esc_html__( 'CSV file is broken or invalid (%s). Please try again.', 'visualizer' ), $error );
1018+
$error = esc_html__( 'CSV file is broken or invalid. Please try again.', 'visualizer' );
10151019
}
1020+
$render->message = $error;
1021+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( '%s for chart %d.', $error, $chart_id ), 'error', __FILE__, __LINE__ );
1022+
update_post_meta( $chart_id, Visualizer_Plugin::CF_ERROR, $error );
10161023
}
10171024
} else {
10181025
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unknown internal error for chart %d.', $chart_id ), 'error', __FILE__, __LINE__ );

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', true );
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,9 @@
129129
$('.visualizer-chart-canvas').adjust();
130130
}, 100);
131131
});
132+
133+
$('.visualizer-error i.error').on('click', function(){
134+
alert( $(this).attr('data-viz-error') );
135+
});
132136
});
133137
})(jQuery, visualizer.media.view, visualizer.urls);

0 commit comments

Comments
 (0)