| @@ -142,201 +142,5 @@ | ||
| 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 public | |
| 150 | - * @param int $chart_id The chart id. | |
| 151 | - * @param string $type The exported type. | |
| 152 | - */ | |
| 153 | - public 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 | - // PHPExcel does not like sheet names longer than 31 characters. | |
| 253 | - $chart = substr( $filename, 0, 30 ); | |
| 254 | - $filename .= '.xlsx'; | |
| 255 | - | |
| 256 | - $xlsData = ''; | |
| 257 | - if ( class_exists( 'PHPExcel' ) ) { | |
| 258 | - $doc = new PHPExcel(); | |
| 259 | - $doc->getActiveSheet()->fromArray( $rows, null, 'A1' ); | |
| 260 | - $doc->getActiveSheet()->setTitle( sanitize_title( $chart ) ); | |
| 261 | - $doc = apply_filters( 'visualizer_excel_doc', $doc ); | |
| 262 | - $writer = PHPExcel_IOFactory::createWriter( $doc, 'Excel2007' ); | |
| 263 | - ob_start(); | |
| 264 | - $writer->save( 'php://output' ); | |
| 265 | - $xlsData = ob_get_contents(); | |
| 266 | - ob_end_clean(); | |
| 267 | - } else { | |
| 268 | - do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PHPExcel does not exist!', 'error', __FILE__, __LINE__ ); | |
| 269 | - error_log( 'Class PHPExcel does not exist!' ); | |
| 270 | - } | |
| 271 | - return array( | |
| 272 | - 'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ), | |
| 273 | - 'name' => $filename, | |
| 274 | - ); | |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * Prepares an HTML table. | |
| 279 | - * | |
| 280 | - * @access private | |
| 281 | - * @param array $rows The array of data. | |
| 282 | - */ | |
| 283 | - private function _getHTML( $rows ) { | |
| 284 | - $css = ' | |
| 285 | - table.visualizer-print { | |
| 286 | - border-collapse: collapse; | |
| 287 | - } | |
| 288 | - table.visualizer-print, table.visualizer-print th, table.visualizer-print td { | |
| 289 | - border: 1px solid #000; | |
| 290 | - } | |
| 291 | - '; | |
| 292 | - $html = ''; | |
| 293 | - $html .= ' | |
| 294 | - <html> | |
| 295 | - <head> | |
| 296 | - <style> | |
| 297 | - ' . apply_filters( 'visualizer_print_css', $css ) . ' | |
| 298 | - </style> | |
| 299 | - </head> | |
| 300 | - <body>'; | |
| 301 | - | |
| 302 | - $table = '<table class="visualizer-print">'; | |
| 303 | - $index = 0; | |
| 304 | - foreach ( $rows as $row ) { | |
| 305 | - $table .= '<tr>'; | |
| 306 | - foreach ( $row as $col ) { | |
| 307 | - if ( $index === 0 ) { | |
| 308 | - $table .= '<th>' . $col . '</th>'; | |
| 309 | - } else { | |
| 310 | - $table .= '<td>' . $col . '</td>'; | |
| 311 | - } | |
| 312 | - } | |
| 313 | - $table .= '</tr>'; | |
| 314 | - $index++; | |
| 315 | - } | |
| 316 | - $table .= '</table>'; | |
| 317 | - | |
| 318 | - $html .= apply_filters( 'visualizer_print_table', $table ) . ' | |
| 319 | - </body> | |
| 320 | - </html>'; | |
| 321 | - return array( | |
| 322 | - 'csv' => $html, | |
| 323 | - ); | |
| 324 | - } | |
| 325 | - | |
| 326 | - /** | |
| 327 | - * Returns the language of the locale. | |
| 328 | - * | |
| 329 | - * @access protected | |
| 330 | - */ | |
| 331 | - protected function get_language() { | |
| 332 | - $locale = get_locale(); | |
| 333 | - if ( empty( $locale ) ) { | |
| 334 | - return ''; | |
| 335 | - } | |
| 336 | - $array = explode( '_', $locale ); | |
| 337 | - if ( count( $array ) < 2 ) { | |
| 338 | - return ''; | |
| 339 | - } | |
| 340 | - return reset( $array ); | |
| 341 | - } | |
| 342 | 146 | } |