Skip to content

Commit ddc4fba

Browse files
authored
Merge pull request #196 from Codeinwp/v220
Added custom number format for pie chart. Added frontend actions buttons ( Print, Export to CSV, Export to Excel, Copy)
2 parents 497fa25 + c40f531 commit ddc4fba

31 files changed

+1123
-575
lines changed

classes/Visualizer/Module.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,177 @@ protected function _addShortcode( $tag, $method ) {
143143
return $this;
144144
}
145145

146+
/**
147+
* Extracts the data for a chart and prepares it for the given type.
148+
*
149+
* @access protected
150+
* @param int $chart_id The chart id.
151+
* @param string $type The exported type.
152+
*/
153+
protected function _getDataAs( $chart_id, $type ) {
154+
$final = null;
155+
$success = false;
156+
if ( $chart_id ) {
157+
$chart = get_post( $chart_id );
158+
$success = $chart && $chart->post_type == Visualizer_Plugin::CPT_VISUALIZER;
159+
}
160+
if ( $success ) {
161+
$settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
162+
$rows = array();
163+
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
164+
$data = unserialize( $chart->post_content );
165+
if ( ! empty( $series ) ) {
166+
$row = array();
167+
foreach ( $series as $array ) {
168+
$row[] = $array['label'];
169+
}
170+
$rows[] = $row;
171+
$row = array();
172+
foreach ( $series as $array ) {
173+
$row[] = $array['type'];
174+
}
175+
$rows[] = $row;
176+
}
177+
if ( ! empty( $data ) ) {
178+
foreach ( $data as $array ) {
179+
// ignore strings
180+
if ( ! is_array( $array ) ) {
181+
continue;
182+
}
183+
// if this is an array of arrays...
184+
if ( is_array( $array[0] ) ) {
185+
foreach ( $array as $arr ) {
186+
$rows[] = $arr;
187+
}
188+
} else {
189+
// just an array
190+
$rows[] = $array;
191+
}
192+
}
193+
}
194+
195+
$filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id;
196+
197+
switch ( $type ) {
198+
case 'csv':
199+
$final = $this->_getCSV( $rows, $filename );
200+
break;
201+
case 'xls':
202+
$final = $this->_getExcel( $rows, $filename );
203+
break;
204+
case 'print':
205+
$final = $this->_getHTML( $rows );
206+
break;
207+
}
208+
}
209+
return $final;
210+
}
211+
212+
/**
213+
* Prepares a CSV.
214+
*
215+
* @access private
216+
* @param array $rows The array of data.
217+
* @param string $filename The name of the file to use.
218+
*/
219+
private function _getCSV( $rows, $filename ) {
220+
$filename .= '.csv';
221+
222+
$fp = tmpfile();
223+
// support for MS Excel
224+
fprintf( $fp, $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ) );
225+
foreach ( $rows as $row ) {
226+
fputcsv( $fp, $row );
227+
}
228+
rewind( $fp );
229+
$csv = '';
230+
while ( ( $array = fgetcsv( $fp ) ) !== false ) {
231+
if ( strlen( $csv ) > 0 ) {
232+
$csv .= PHP_EOL;
233+
}
234+
$csv .= implode( ',', $array );
235+
}
236+
fclose( $fp );
237+
238+
return array(
239+
'csv' => $csv,
240+
'name' => $filename,
241+
);
242+
}
243+
244+
/**
245+
* Prepares an Excel file.
246+
*
247+
* @access private
248+
* @param array $rows The array of data.
249+
* @param string $filename The name of the file to use.
250+
*/
251+
private function _getExcel( $rows, $filename ) {
252+
$chart = $filename;
253+
$filename .= '.xlsx';
254+
255+
$doc = new PHPExcel();
256+
$doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
257+
$doc->getActiveSheet()->setTitle( $chart );
258+
$doc = apply_filters( 'visualizer_excel_doc', $doc );
259+
$writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' );
260+
ob_start();
261+
$writer->save( 'php://output' );
262+
$xlsData = ob_get_contents();
263+
ob_end_clean();
264+
return array(
265+
'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
266+
'name' => $filename,
267+
);
268+
}
269+
270+
/**
271+
* Prepares an HTML table.
272+
*
273+
* @access private
274+
* @param array $rows The array of data.
275+
*/
276+
private function _getHTML( $rows ) {
277+
$css = '
278+
table.visualizer-print {
279+
border-collapse: collapse;
280+
}
281+
table.visualizer-print, table.visualizer-print th, table.visualizer-print td {
282+
border: 1px solid #000;
283+
}
284+
';
285+
$html = '';
286+
$html .= '
287+
<html>
288+
<head>
289+
<style>
290+
' . apply_filters( 'visualizer_print_css', $css ) . '
291+
</style>
292+
</head>
293+
<body>';
294+
295+
$table = '<table class="visualizer-print">';
296+
$index = 0;
297+
foreach ( $rows as $row ) {
298+
$table .= '<tr>';
299+
foreach ( $row as $col ) {
300+
if ( $index === 0 ) {
301+
$table .= '<th>' . $col . '</th>';
302+
} else {
303+
$table .= '<td>' . $col . '</td>';
304+
}
305+
}
306+
$table .= '</tr>';
307+
$index++;
308+
}
309+
$table .= '</table>';
310+
311+
$html .= apply_filters( 'visualizer_print_table', $table ) . '
312+
</body>
313+
</html>';
314+
return array(
315+
'csv' => $html,
316+
);
317+
}
318+
146319
}

classes/Visualizer/Module/Admin.php

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,20 @@ public function enqueueLibraryScripts( $suffix ) {
246246
wp_enqueue_style( 'visualizer-library', VISUALIZER_ABSURL . 'css/library.css', array(), Visualizer_Plugin::VERSION );
247247
$this->_addFilter( 'media_upload_tabs', 'setupVisualizerTab' );
248248
wp_enqueue_media();
249-
wp_enqueue_script( 'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
250-
'jquery',
251-
'media-views',
252-
), Visualizer_Plugin::VERSION, true );
249+
wp_enqueue_script(
250+
'visualizer-library', VISUALIZER_ABSURL . 'js/library.js', array(
251+
'jquery',
252+
'media-views',
253+
), Visualizer_Plugin::VERSION, true
254+
);
253255
wp_enqueue_script( 'google-jsapi-new', '//www.gstatic.com/charts/loader.js', array(), null, true );
254256
wp_enqueue_script( 'google-jsapi-old', '//www.google.com/jsapi', array( 'google-jsapi-new' ), null, true );
255-
wp_enqueue_script( 'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
256-
'google-jsapi-old',
257-
'visualizer-library',
258-
), Visualizer_Plugin::VERSION, true );
257+
wp_enqueue_script(
258+
'visualizer-render', VISUALIZER_ABSURL . 'js/render.js', array(
259+
'google-jsapi-old',
260+
'visualizer-library',
261+
), Visualizer_Plugin::VERSION, true
262+
);
259263
}
260264
}
261265

@@ -298,12 +302,14 @@ public function registerAdminMenu() {
298302
*/
299303
public function renderLibraryPage() {
300304
// get current page
301-
$page = filter_input( INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
302-
'options' => array(
303-
'min_range' => 1,
304-
'default' => 1,
305-
),
306-
) );
305+
$page = filter_input(
306+
INPUT_GET, 'vpage', FILTER_VALIDATE_INT, array(
307+
'options' => array(
308+
'min_range' => 1,
309+
'default' => 1,
310+
),
311+
)
312+
);
307313
// the initial query arguments to fetch charts
308314
$query_args = array(
309315
'post_type' => Visualizer_Plugin::CPT_VISUALIZER,
@@ -358,32 +364,40 @@ public function renderLibraryPage() {
358364
}
359365
// enqueue charts array
360366
$ajaxurl = admin_url( 'admin-ajax.php' );
361-
wp_localize_script( 'visualizer-library', 'visualizer', array(
362-
'charts' => $charts,
363-
'urls' => array(
364-
'base' => add_query_arg( 'vpage', false ),
365-
'create' => add_query_arg( array(
366-
'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
367-
'library' => 'yes',
368-
), $ajaxurl ),
369-
'edit' => add_query_arg( array(
370-
'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
371-
'library' => 'yes',
372-
), $ajaxurl ),
373-
),
374-
) );
367+
wp_localize_script(
368+
'visualizer-library', 'visualizer', array(
369+
'charts' => $charts,
370+
'urls' => array(
371+
'base' => add_query_arg( 'vpage', false ),
372+
'create' => add_query_arg(
373+
array(
374+
'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
375+
'library' => 'yes',
376+
), $ajaxurl
377+
),
378+
'edit' => add_query_arg(
379+
array(
380+
'action' => Visualizer_Plugin::ACTION_EDIT_CHART,
381+
'library' => 'yes',
382+
), $ajaxurl
383+
),
384+
),
385+
)
386+
);
375387
// render library page
376388
$render = new Visualizer_Render_Library();
377389
$render->charts = $charts;
378390
$render->type = $filter;
379391
$render->types = self::_getChartTypesLocalized();
380-
$render->pagination = paginate_links( array(
381-
'base' => add_query_arg( 'vpage', '%#%' ),
382-
'format' => '',
383-
'current' => $page,
384-
'total' => $query->max_num_pages,
385-
'type' => 'array',
386-
) );
392+
$render->pagination = paginate_links(
393+
array(
394+
'base' => add_query_arg( 'vpage', '%#%' ),
395+
'format' => '',
396+
'current' => $page,
397+
'total' => $query->max_num_pages,
398+
'type' => 'array',
399+
)
400+
);
387401
$render->render();
388402
}
389403

0 commit comments

Comments
 (0)