| @@ -142,5 +142,178 @@ | ||
| 142 | 142 | add_shortcode( $tag, array( $this, $method ) ); |
| 143 | 143 | return $this; |
| 144 | 144 | } |
| 145 | 145 | |
| 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 | + | |
| 146 | 319 | } |