AJAX.php
2 years ago
AJAX_Manager.php
2 years ago
Confirm_Cache_Cleared.php
2 years ago
Copy_Report.php
2 years ago
Create_Campaign.php
2 years ago
Create_Report.php
2 years ago
Delete_Campaign.php
2 years ago
Delete_Data.php
2 years ago
Delete_Report.php
2 years ago
Export_Campaigns.php
2 years ago
Export_Devices.php
2 years ago
Export_Geo.php
2 years ago
Export_Pages.php
2 years ago
Export_Referrers.php
2 years ago
Export_Reports.php
2 years ago
Filter.php
2 years ago
Import_Reports.php
2 years ago
Last_Update_Viewed.php
2 years ago
Migration_Status.php
2 years ago
Real_Time_Data.php
2 years ago
Rename_Report.php
2 years ago
Reset_Analytics.php
2 years ago
Save_Report.php
2 years ago
Set_Favorite_Report.php
2 years ago
Sort_Reports.php
2 years ago
Test_Email.php
2 years ago
Update_Capabilities.php
2 years ago
Update_User_Settings.php
2 years ago
Filter.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP\AJAX; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP_SCOPED\IAWP\Chart; |
| 7 | use IAWP_SCOPED\IAWP\Chart_Geo; |
| 8 | use IAWP_SCOPED\IAWP\Date_Range\Date_Range; |
| 9 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 10 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 11 | use IAWP_SCOPED\IAWP\Quick_Stats; |
| 12 | use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals; |
| 13 | use IAWP_SCOPED\IAWP\Tables\Table_Campaigns; |
| 14 | use IAWP_SCOPED\IAWP\Tables\Table_Devices; |
| 15 | use IAWP_SCOPED\IAWP\Tables\Table_Geo; |
| 16 | use IAWP_SCOPED\IAWP\Tables\Table_Pages; |
| 17 | use IAWP_SCOPED\IAWP\Tables\Table_Referrers; |
| 18 | use IAWP_SCOPED\Proper\Timezone; |
| 19 | use Throwable; |
| 20 | /** @internal */ |
| 21 | class Filter extends AJAX |
| 22 | { |
| 23 | protected function action_name() : string |
| 24 | { |
| 25 | return 'iawp_filter'; |
| 26 | } |
| 27 | protected function action_required_fields() : array |
| 28 | { |
| 29 | return ['table_type', 'columns']; |
| 30 | } |
| 31 | protected function action_callback() : void |
| 32 | { |
| 33 | $date_range = $this->get_date_range(); |
| 34 | $is_new_date_range = $this->get_field('is_new_date_range') === 'true'; |
| 35 | $filters = $this->get_field('filters') ?? []; |
| 36 | $visible_columns = $this->get_field('columns'); |
| 37 | $visible_datasets = $this->get_field('visible_datasets') ?? []; |
| 38 | $sort_column = $this->get_field('sort_column') ?? null; |
| 39 | $sort_direction = $this->get_field('sort_direction') ?? null; |
| 40 | $group = $this->get_field('group') ?? null; |
| 41 | $as_csv = $this->get_field('as_csv') ?? \false; |
| 42 | $is_new_group = $this->get_field('is_new_group') === 'true'; |
| 43 | $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval')); |
| 44 | $page = \intval($this->get_field('page') ?? 1); |
| 45 | $number_of_rows = $page * \IAWP_SCOPED\iawp()->pagination_page_size(); |
| 46 | $table_type = $this->get_field('table_type'); |
| 47 | $is_geo_table = $table_type === 'geo'; |
| 48 | $table_class = $this->get_table_by_type($table_type); |
| 49 | if (\is_null($table_class)) { |
| 50 | return; |
| 51 | } |
| 52 | $table = new $table_class($visible_columns, $group, $is_new_group); |
| 53 | $filters = $table->sanitize_filters($filters); |
| 54 | $sort_configuration = $table->sanitize_sort_parameters($sort_column, $sort_direction); |
| 55 | $rows_class = $table->group()->rows_class(); |
| 56 | $statistics_class = $table->group()->statistics_class(); |
| 57 | if ($as_csv) { |
| 58 | $rows_query = new $rows_class($date_range, null, $filters, $sort_configuration); |
| 59 | $rows = $rows_query->rows(); |
| 60 | $csv = $table->csv($rows, \true); |
| 61 | echo $csv->to_string(); |
| 62 | return; |
| 63 | } |
| 64 | if ($is_geo_table) { |
| 65 | $rows_query = new $rows_class($date_range, null, $filters, $sort_configuration); |
| 66 | } else { |
| 67 | $rows_query = new $rows_class($date_range, $number_of_rows, $filters, $sort_configuration); |
| 68 | } |
| 69 | $rows = $rows_query->rows(); |
| 70 | if (empty($filters)) { |
| 71 | $filtered_statistics = null; |
| 72 | $unfiltered_statistics = new $statistics_class($date_range, null, $chart_interval); |
| 73 | $total_number_of_rows = $unfiltered_statistics->total_number_of_rows(); |
| 74 | } else { |
| 75 | $unfiltered_statistics = new $statistics_class($date_range, null, $chart_interval); |
| 76 | $filtered_statistics = new $statistics_class($date_range, $rows_query, $chart_interval); |
| 77 | $total_number_of_rows = $filtered_statistics->total_number_of_rows(); |
| 78 | } |
| 79 | if ($is_geo_table) { |
| 80 | $chart = new Chart_Geo($rows, $date_range->label()); |
| 81 | $rows = \array_slice($rows, 0, $number_of_rows); |
| 82 | } else { |
| 83 | $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label()); |
| 84 | } |
| 85 | $table->set_statistics($filtered_statistics ?? $unfiltered_statistics); |
| 86 | $quick_stats = new Quick_Stats($filtered_statistics, $unfiltered_statistics); |
| 87 | \wp_send_json_success(['rows' => $table->get_rendered_template($rows, \true, $sort_configuration->column(), $sort_configuration->direction()), 'table' => $table->get_rendered_template($rows, \false, $sort_configuration->column(), $sort_configuration->direction()), 'totalNumberOfRows' => $total_number_of_rows, 'chart' => $chart->get_html($visible_datasets), 'stats' => $quick_stats->get_html(), 'label' => $date_range->label(), 'isLastPage' => \count($rows) < \IAWP_SCOPED\iawp()->pagination_page_size() * $page, 'columns' => $table->visible_column_ids(), 'columnsHTML' => $table->column_picker_html(), 'groupId' => $table->group()->id(), 'filters' => $filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'filtersButtonsHTML' => $table->filters_condition_buttons_html($filters), 'chartInterval' => $chart_interval->id()]); |
| 88 | } |
| 89 | /** |
| 90 | * Get the date range for the filter request |
| 91 | * |
| 92 | * The date info can be supplied in one of two ways. |
| 93 | * |
| 94 | * The first is to provide a relative_range_id which is converted into start, end, and label. |
| 95 | * |
| 96 | * The second is to provide explicit start and end fields which will be used as is. |
| 97 | * |
| 98 | * @return Date_Range |
| 99 | */ |
| 100 | private function get_date_range() : Date_Range |
| 101 | { |
| 102 | $relative_range_id = $this->get_field('relative_range_id'); |
| 103 | $start_timestamp = $this->get_field('exact_start'); |
| 104 | $end_timestamp = $this->get_field('exact_end'); |
| 105 | if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) { |
| 106 | try { |
| 107 | $start = new DateTime($start_timestamp, Timezone::site_timezone()); |
| 108 | $end = new DateTime($end_timestamp, Timezone::site_timezone()); |
| 109 | return new Exact_Date_Range($start, $end); |
| 110 | } catch (Throwable $e) { |
| 111 | // Do nothing and fall back to default relative date range |
| 112 | } |
| 113 | } |
| 114 | return new Relative_Date_Range($relative_range_id); |
| 115 | } |
| 116 | private function get_table_by_type(string $type) : ?string |
| 117 | { |
| 118 | switch ($this->get_field('table_type')) { |
| 119 | case 'views': |
| 120 | return Table_Pages::class; |
| 121 | case 'referrers': |
| 122 | return Table_Referrers::class; |
| 123 | case 'geo': |
| 124 | return Table_Geo::class; |
| 125 | case 'campaigns': |
| 126 | return Table_Campaigns::class; |
| 127 | case 'devices': |
| 128 | return Table_Devices::class; |
| 129 | default: |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 |