| @@ -57,8 +57,16 @@ | ||
| 57 | 57 | */ |
| 58 | 58 | protected $_series = array(); |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | + * The error message. | |
| 62 | + * | |
| 63 | + * @access protected | |
| 64 | + * @var string | |
| 65 | + */ | |
| 66 | + protected $_error; | |
| 67 | + | |
| 68 | + /** | |
| 61 | 69 | * Return allowed types |
| 62 | 70 | * |
| 63 | 71 | * @since 1.0.1 |
| 64 | 72 | * |
| @@ -83,9 +91,9 @@ | ||
| 83 | 91 | * @return boolean TRUE if sereis types are valid, otherwise FALSE. |
| 84 | 92 | */ |
| 85 | 93 | protected static function _validateTypes( $types ) { |
| 86 | 94 | foreach ( $types as $type ) { |
| 87 | - if ( ! in_array( $type, self::$allowed_types ) ) { | |
| 95 | + if ( ! in_array( $type, self::$allowed_types, true ) ) { | |
| 88 | 96 | return false; |
| 89 | 97 | } |
| 90 | 98 | } |
| 91 | 99 | |
| @@ -193,9 +201,8 @@ | ||
| 193 | 201 | * @return array Normalized row of data. |
| 194 | 202 | */ |
| 195 | 203 | protected function _normalizeData( $data ) { |
| 196 | 204 | // normalize values |
| 197 | - // error_log(print_r($data,true)); | |
| 198 | 205 | foreach ( $this->_series as $i => $series ) { |
| 199 | 206 | // if no value exists for the seires, then add null |
| 200 | 207 | if ( ! isset( $data[ $i ] ) ) { |
| 201 | 208 | $data[ $i ] = null; |
| @@ -207,9 +214,9 @@ | ||
| 207 | 214 | case 'number': |
| 208 | 215 | $data[ $i ] = ( is_numeric( $data[ $i ] ) ) ? floatval( $data[ $i ] ) : ( is_numeric( str_replace( ',', '', $data[ $i ] ) ) ? floatval( str_replace( ',', '', $data[ $i ] ) ) : null ); |
| 209 | 216 | break; |
| 210 | 217 | case 'boolean': |
| 211 | - $data[ $i ] = ! empty( $data[ $i ] ) ? filter_validate( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 218 | + $data[ $i ] = ! empty( $data[ $i ] ) ? filter_var( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 212 | 219 | break; |
| 213 | 220 | case 'timeofday': |
| 214 | 221 | $date = new DateTime( '1984-03-16T' . $data[ $i ] ); |
| 215 | 222 | if ( $date ) { |
| @@ -220,18 +227,163 @@ | ||
| 220 | 227 | 0, |
| 221 | 228 | ); |
| 222 | 229 | } |
| 223 | 230 | break; |
| 231 | + case 'datetime': | |
| 232 | + // let's check if the date is a Unix epoch | |
| 233 | + $value = DateTime::createFromFormat( 'U', $data[ $i ] ); | |
| 234 | + if ( $value !== false && ! is_wp_error( $value ) ) { | |
| 235 | + $data[ $i ] = $value->format( 'Y-m-d H:i:s' ); | |
| 236 | + } | |
| 237 | + break; | |
| 224 | 238 | case 'string': |
| 225 | - // condition introduced for Issue with non-English text #240 where languages such as Hebrew get messed up. | |
| 226 | - if ( function_exists( 'mb_detect_encoding' ) && mb_detect_encoding( $data[ $i ] ) !== 'UTF-8' ) { | |
| 227 | - $data[ $i ] = utf8_encode( $data[ $i ] ); | |
| 228 | - } | |
| 239 | + $data[ $i ] = $this->toUTF8( $data[ $i ] ); | |
| 229 | 240 | break; |
| 230 | 241 | } |
| 231 | 242 | } |
| 232 | 243 | |
| 233 | - // error_log(print_r($data,true)); | |
| 234 | - return $data; | |
| 244 | + return apply_filters( 'visualizer_format_data', $data, $this->_series ); | |
| 235 | 245 | } |
| 246 | + | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Converts values to UTF8, if required. | |
| 250 | + * | |
| 251 | + * @access protected | |
| 252 | + * | |
| 253 | + * @param string $datum The data to convert. | |
| 254 | + * | |
| 255 | + * @return string The converted data. | |
| 256 | + */ | |
| 257 | + protected final function toUTF8( $datum ) { | |
| 258 | + if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) { | |
| 259 | + $datum = \ForceUTF8\Encoding::toUTF8( $datum ); | |
| 260 | + } | |
| 261 | + return $datum; | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * Determines the formats of date/time columns. | |
| 266 | + * | |
| 267 | + * @access public | |
| 268 | + * | |
| 269 | + * @param array $series The actual array of series. | |
| 270 | + * @param array $data The actual array of data. | |
| 271 | + * | |
| 272 | + * @return array | |
| 273 | + */ | |
| 274 | + public static final function get_date_formats_if_exists( $series, $data ) { | |
| 275 | + $date_formats = array(); | |
| 276 | + $types = array(); | |
| 277 | + $index = 0; | |
| 278 | + foreach ( $series as $column ) { | |
| 279 | + if ( in_array( $column['type'], array( 'date', 'datetime', 'timeofday' ), true ) ) { | |
| 280 | + $types[] = array( 'index' => $index, 'type' => $column['type'] ); | |
| 281 | + } | |
| 282 | + $index++; | |
| 283 | + } | |
| 284 | + | |
| 285 | + if ( ! $types ) { | |
| 286 | + return $date_formats; | |
| 287 | + } | |
| 288 | + | |
| 289 | + $random = $data; | |
| 290 | + // let's randomly pick 5 data points instead of cycling through the entire data set. | |
| 291 | + if ( count( $data ) > 5 ) { | |
| 292 | + $random = array(); | |
| 293 | + for ( $x = 0; $x < 5; $x++ ) { | |
| 294 | + $random[] = $data[ rand( 0, count( $data ) - 1 ) ]; | |
| 295 | + } | |
| 296 | + } | |
| 297 | + | |
| 298 | + foreach ( $types as $type ) { | |
| 299 | + $formats = array(); | |
| 300 | + foreach ( $random as $datum ) { | |
| 301 | + $f = self::determine_date_format( $datum[ $type['index'] ], $type['type'] ); | |
| 302 | + if ( $f ) { | |
| 303 | + $formats[] = $f; | |
| 304 | + } | |
| 305 | + } | |
| 306 | + // if there are multiple formats, use the most frequent format. | |
| 307 | + $formats = array_filter( $formats ); | |
| 308 | + if ( $formats ) { | |
| 309 | + $formats = array_count_values( $formats ); | |
| 310 | + arsort( $formats ); | |
| 311 | + $formats = array_keys( $formats ); | |
| 312 | + $final_format = reset( $formats ); | |
| 313 | + // we have determined the PHP format; now we have to change this into the JS format where m = MM, d = DD etc. | |
| 314 | + $date_formats[] = array( 'index' => $type['index'], 'format' => str_replace( array( 'Y', 'm', 'd', 'H', 'i', 's' ), array( 'YYYY', 'MM', 'DD', 'HH', 'mm', 'ss' ), $final_format ) ); | |
| 315 | + } | |
| 316 | + } | |
| 317 | + return $date_formats; | |
| 318 | + } | |
| 319 | + | |
| 320 | + /** | |
| 321 | + * Determines the date/time format of the given string. | |
| 322 | + * | |
| 323 | + * @access private | |
| 324 | + * | |
| 325 | + * @param string $value The string. | |
| 326 | + * @param string $type 'date', 'timeofday' or 'datetime'. | |
| 327 | + * | |
| 328 | + * @return string|null | |
| 329 | + */ | |
| 330 | + private static final function determine_date_format( $value, $type ) { | |
| 331 | + if ( version_compare( phpversion(), '5.3.0', '<' ) ) { | |
| 332 | + return null; | |
| 333 | + } | |
| 334 | + | |
| 335 | + $formats = array( | |
| 336 | + 'Y/m/d', | |
| 337 | + 'Y-m-d', | |
| 338 | + 'm/d/Y', | |
| 339 | + 'm-d-Y', | |
| 340 | + 'd-m-Y', | |
| 341 | + 'd/m/Y', | |
| 342 | + ); | |
| 343 | + | |
| 344 | + switch ( $type ) { | |
| 345 | + case 'datetime': | |
| 346 | + $formats = array_merge( | |
| 347 | + $formats, array( | |
| 348 | + 'U', | |
| 349 | + 'Y/m/d H:i:s', | |
| 350 | + 'Y-m-d H:i:s', | |
| 351 | + 'm/d/Y H:i:s', | |
| 352 | + 'm-d-Y H:i:s', | |
| 353 | + ) | |
| 354 | + ); | |
| 355 | + break; | |
| 356 | + case 'timeofday': | |
| 357 | + $formats = array_merge( | |
| 358 | + $formats, array( | |
| 359 | + 'H:i:s', | |
| 360 | + 'H:i', | |
| 361 | + ) | |
| 362 | + ); | |
| 363 | + break; | |
| 364 | + } | |
| 365 | + | |
| 366 | + $formats = apply_filters( 'visualizer_date_formats', $formats, $type ); | |
| 367 | + | |
| 368 | + foreach ( $formats as $format ) { | |
| 369 | + $return = DateTime::createFromFormat( $format, $value ); | |
| 370 | + if ( $return !== false && ! is_wp_error( $return ) ) { | |
| 371 | + return $format; | |
| 372 | + } | |
| 373 | + } | |
| 374 | + // invalid format | |
| 375 | + return null; | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * Returns the error, if any. | |
| 380 | + * | |
| 381 | + * @access public | |
| 382 | + * @return string | |
| 383 | + */ | |
| 384 | + public function get_error() { | |
| 385 | + return $this->_error; | |
| 386 | + } | |
| 387 | + | |
| 236 | 388 | |
| 237 | 389 | } |