| @@ -207,9 +207,9 @@ | ||
| 207 | 207 | case 'number': |
| 208 | 208 | $data[ $i ] = ( is_numeric( $data[ $i ] ) ) ? floatval( $data[ $i ] ) : ( is_numeric( str_replace( ',', '', $data[ $i ] ) ) ? floatval( str_replace( ',', '', $data[ $i ] ) ) : null ); |
| 209 | 209 | break; |
| 210 | 210 | case 'boolean': |
| 211 | - $data[ $i ] = ! empty( $data[ $i ] ) ? filter_validate( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 211 | + $data[ $i ] = ! empty( $data[ $i ] ) ? filter_var( $data[ $i ], FILTER_VALIDATE_BOOLEAN ) : null; | |
| 212 | 212 | break; |
| 213 | 213 | case 'timeofday': |
| 214 | 214 | $date = new DateTime( '1984-03-16T' . $data[ $i ] ); |
| 215 | 215 | if ( $date ) { |
| @@ -245,7 +245,116 @@ | ||
| 245 | 245 | if ( ! function_exists( 'mb_detect_encoding' ) || mb_detect_encoding( $datum ) !== 'ASCII' ) { |
| 246 | 246 | $datum = \ForceUTF8\Encoding::toUTF8( $datum ); |
| 247 | 247 | } |
| 248 | 248 | return $datum; |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Determines the formats of date/time columns. | |
| 253 | + * | |
| 254 | + * @access public | |
| 255 | + * | |
| 256 | + * @param array $series The actual array of series. | |
| 257 | + * @param array $data The actual array of data. | |
| 258 | + * | |
| 259 | + * @return array | |
| 260 | + */ | |
| 261 | + public static final function get_date_formats_if_exists( $series, $data ) { | |
| 262 | + $date_formats = array(); | |
| 263 | + $types = array(); | |
| 264 | + $index = 0; | |
| 265 | + foreach ( $series as $column ) { | |
| 266 | + if ( in_array( $column['type'], array( 'date', 'datetime', 'timeofday' ) ) ) { | |
| 267 | + $types[] = array( 'index' => $index, 'type' => $column['type'] ); | |
| 268 | + } | |
| 269 | + $index++; | |
| 270 | + } | |
| 271 | + | |
| 272 | + if ( ! $types ) { | |
| 273 | + return $date_formats; | |
| 274 | + } | |
| 275 | + | |
| 276 | + $random = $data; | |
| 277 | + // let's randomly pick 5 data points instead of cycling through the entire data set. | |
| 278 | + if ( count( $data ) > 5 ) { | |
| 279 | + $random = array(); | |
| 280 | + for ( $x = 0; $x < 5; $x++ ) { | |
| 281 | + $random = $data[ rand( 0, count( $data ) - 1 ) ]; | |
| 282 | + } | |
| 283 | + } | |
| 284 | + | |
| 285 | + foreach ( $types as $type ) { | |
| 286 | + $formats = array(); | |
| 287 | + foreach ( $random as $datum ) { | |
| 288 | + $f = self::determine_date_format( $datum[ $type['index'] ], $type['type'] ); | |
| 289 | + if ( $f ) { | |
| 290 | + $formats[] = $f; | |
| 291 | + } | |
| 292 | + } | |
| 293 | + // if there are multiple formats, use the most frequent format. | |
| 294 | + $formats = array_filter( $formats ); | |
| 295 | + if ( $formats ) { | |
| 296 | + $formats = array_count_values( $formats ); | |
| 297 | + arsort( $formats ); | |
| 298 | + $formats = array_keys( $formats ); | |
| 299 | + $final_format = reset( $formats ); | |
| 300 | + // we have determined the PHP format; now we have to change this into the JS format where m = MM, d = DD etc. | |
| 301 | + $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 ) ); | |
| 302 | + } | |
| 303 | + } | |
| 304 | + return $date_formats; | |
| 305 | + } | |
| 306 | + | |
| 307 | + /** | |
| 308 | + * Determines the date/time format of the given string. | |
| 309 | + * | |
| 310 | + * @access private | |
| 311 | + * | |
| 312 | + * @param string $value The string. | |
| 313 | + * @param string $type 'date', 'timeofday' or 'datetime'. | |
| 314 | + * | |
| 315 | + * @return string|null | |
| 316 | + */ | |
| 317 | + private static final function determine_date_format( $value, $type ) { | |
| 318 | + if ( version_compare( phpversion(), '5.3.0', '<' ) ) { | |
| 319 | + return null; | |
| 320 | + } | |
| 321 | + | |
| 322 | + $formats = array( | |
| 323 | + 'Y/m/d', | |
| 324 | + 'Y-m-d', | |
| 325 | + 'm/d/Y', | |
| 326 | + 'm-d-Y', | |
| 327 | + 'd-m-Y', | |
| 328 | + 'd/m/Y', | |
| 329 | + ); | |
| 330 | + | |
| 331 | + switch ( $type ) { | |
| 332 | + case 'datetime': | |
| 333 | + $formats += array( | |
| 334 | + 'Y/m/d H:i:s', | |
| 335 | + 'Y-m-d H:i:s', | |
| 336 | + 'm/d/Y H:i:s', | |
| 337 | + 'm-d-Y H:i:s', | |
| 338 | + ); | |
| 339 | + break; | |
| 340 | + case 'timeofday': | |
| 341 | + $formats += array( | |
| 342 | + 'H:i:s', | |
| 343 | + 'H:i', | |
| 344 | + ); | |
| 345 | + break; | |
| 346 | + } | |
| 347 | + | |
| 348 | + $formats = apply_filters( 'visualizer_date_formats', $formats, $type ); | |
| 349 | + | |
| 350 | + foreach ( $formats as $format ) { | |
| 351 | + $return = DateTime::createFromFormat( $format, $value ); | |
| 352 | + if ( $return !== false ) { | |
| 353 | + return $format; | |
| 354 | + } | |
| 355 | + } | |
| 356 | + // invalid format | |
| 357 | + return null; | |
| 249 | 358 | } |
| 250 | 359 | |
| 251 | 360 | } |