BrowsersConverter.php
1 year ago
DataConverterInterface.php
4 years ago
FilterConverter.php
2 years ago
NumberConverter.php
1 year ago
PagesTitleConverter.php
1 year ago
PagesUrlConverter.php
1 year ago
PlatformConverter.php
4 years ago
ReferrersConverter.php
4 years ago
SearchEngineConverter.php
1 year ago
SearchQueryConverter.php
1 year ago
SubtableConverter.php
1 year ago
UserCityConverter.php
4 years ago
UserCountryConverter.php
4 years ago
UserRegionConverter.php
4 years ago
VisitorsConverter.php
1 year ago
VisitsTimeConverter.php
7 months ago
SubtableConverter.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WpMatomo\WpStatistics\DataConverters; |
| 4 | |
| 5 | use Piwik\DataTable; |
| 6 | use WpMatomo\WpStatistics\Importers\Actions\RecordImporter; |
| 7 | |
| 8 | /** |
| 9 | * @package WpMatomo |
| 10 | * @subpackage WpStatisticsImport |
| 11 | */ |
| 12 | class SubtableConverter { |
| 13 | |
| 14 | public static function aggregate_by_key( array $wp_statistics_data, $first_key, $second_key ) { |
| 15 | $data = []; |
| 16 | if ( count( $wp_statistics_data ) ) { |
| 17 | foreach ( $wp_statistics_data as $row ) { |
| 18 | if ( ! array_key_exists( $row[ $first_key ], $data ) ) { |
| 19 | $data[ $row[ $first_key ] ] = [ |
| 20 | 'label' => RecordImporter::get_label( $row, $first_key ), |
| 21 | 'data' => [], |
| 22 | 'nb_uniq_visitors' => 0, |
| 23 | 'nb_visits' => 0, |
| 24 | ]; |
| 25 | } |
| 26 | $data[ $row[ $first_key ] ]['data'][ $row[ $second_key ] ] = [ |
| 27 | 'label' => RecordImporter::get_label( $row, $second_key ), |
| 28 | 'nb_uniq_visitors' => $row['nb'], |
| 29 | 'nb_visits' => $row['nb'], |
| 30 | ]; |
| 31 | $data[ $row[ $first_key ] ]['nb_visits'] += $row['nb']; |
| 32 | $data[ $row[ $first_key ] ]['nb_uniq_visitors'] += $row['nb']; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | $data_table = new DataTable(); |
| 37 | foreach ( $data as $key => $row ) { |
| 38 | $data = $row['data']; |
| 39 | unset( $row['data'] ); |
| 40 | $top_level_row = self::add_row_to_table( $data_table, new DataTable\Row( array( 0 => $row ) ), $key ); |
| 41 | foreach ( $data as $sub_key => $sub_row ) { |
| 42 | self::add_row_to_subtable( $top_level_row, new DataTable\Row( array( 0 => $sub_row ) ), $sub_key ); |
| 43 | } |
| 44 | } |
| 45 | return $data_table; |
| 46 | } |
| 47 | |
| 48 | protected static function add_row_to_subtable( DataTable\Row $top_level_row, DataTable\Row $row_to_add, $new_label ) { |
| 49 | $sub_table = $top_level_row->getSubtable(); |
| 50 | if ( ! $sub_table ) { |
| 51 | $sub_table = new DataTable(); |
| 52 | $top_level_row->setSubtable( $sub_table ); |
| 53 | } |
| 54 | |
| 55 | return self::add_row_to_table( $sub_table, $row_to_add, $new_label ); |
| 56 | } |
| 57 | |
| 58 | protected static function add_row_to_table( DataTable $record, DataTable\Row $row, $new_label ) { |
| 59 | $found_row = $record->getRowFromLabel( $new_label ); |
| 60 | if ( empty( $found_row ) ) { |
| 61 | $found_row = clone $row; |
| 62 | $found_row->deleteMetadata(); |
| 63 | $found_row->setColumn( 'label', $new_label ); |
| 64 | $record->addRow( $found_row ); |
| 65 | } else { |
| 66 | $found_row->sumRow( $row, $copy_metadata = false ); |
| 67 | } |
| 68 | |
| 69 | return $found_row; |
| 70 | } |
| 71 | } |
| 72 |