| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
/** |
| 23 |
* Base class for all modules. Implements routine methods required by all modules. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Module |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class Visualizer_Module { |
| 31 |
|
| 32 |
/** |
| 33 |
* The instance of wpdb class. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @access protected |
| 38 |
* @var wpdb |
| 39 |
*/ |
| 40 |
protected $_wpdb = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* The plugin instance. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
* @access protected |
| 48 |
* @var Visualizer_Plugin |
| 49 |
*/ |
| 50 |
protected $_plugin = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @global wpdb $wpdb Current database connection. |
| 57 |
* |
| 58 |
* @access public |
| 59 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 60 |
*/ |
| 61 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
$this->_wpdb = $wpdb; |
| 65 |
$this->_plugin = $plugin; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Registers an action hook. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* @uses add_action() To register action hook. |
| 73 |
* |
| 74 |
* @access protected |
| 75 |
* @param string $tag The name of the action to which the $method is hooked. |
| 76 |
* @param string $method The name of the method to be called. |
| 77 |
* @param bool $methodClass The root of the method. |
| 78 |
* @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. |
| 79 |
* @param int $accepted_args optional. The number of arguments the function accept (default 1). |
| 80 |
* @return Visualizer_Module |
| 81 |
*/ |
| 82 |
protected function _addAction( $tag, $method, $methodClass = null, $priority = 10, $accepted_args = 1 ) { |
| 83 |
add_action( $tag, array( $methodClass ? $methodClass : $this, $method ), $priority, $accepted_args ); |
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Registers AJAX action hook. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @access public |
| 93 |
* @param string $tag The name of the AJAX action to which the $method is hooked. |
| 94 |
* @param string $method Optional. The name of the method to be called. If the name of the method is not provided, tag name will be used as method name. |
| 95 |
* @param bool $methodClass The root of the method. |
| 96 |
* @param boolean $private Optional. Determines if we should register hook for logged in users. |
| 97 |
* @param boolean $public Optional. Determines if we should register hook for not logged in users. |
| 98 |
* @return Visualizer_Module |
| 99 |
*/ |
| 100 |
protected function _addAjaxAction( $tag, $method = '', $methodClass = null, $private = true, $public = false ) { |
| 101 |
if ( $private ) { |
| 102 |
$this->_addAction( 'wp_ajax_' . $tag, $method, $methodClass ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( $public ) { |
| 106 |
$this->_addAction( 'wp_ajax_nopriv_' . $tag, $method, $methodClass ); |
| 107 |
} |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Registers a filter hook. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* @uses add_filter() To register filter hook. |
| 117 |
* |
| 118 |
* @access protected |
| 119 |
* @param string $tag The name of the filter to hook the $method to. |
| 120 |
* @param type $method The name of the method to be called when the filter is applied. |
| 121 |
* @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action. |
| 122 |
* @param int $accepted_args optional. The number of arguments the function accept (default 1). |
| 123 |
* @return Visualizer_Module |
| 124 |
*/ |
| 125 |
protected function _addFilter( $tag, $method, $priority = 10, $accepted_args = 1 ) { |
| 126 |
add_filter( $tag, array( $this, $method ), $priority, $accepted_args ); |
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Registers a hook for shortcode tag. |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* @uses add_shortcode() To register shortcode hook. |
| 135 |
* |
| 136 |
* @access protected |
| 137 |
* @param string $tag Shortcode tag to be searched in post content. |
| 138 |
* @param string $method Hook to run when shortcode is found. |
| 139 |
* @return Visualizer_Module |
| 140 |
*/ |
| 141 |
protected function _addShortcode( $tag, $method ) { |
| 142 |
add_shortcode( $tag, array( $this, $method ) ); |
| 143 |
return $this; |
| 144 |
} |
| 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 |
|