AJAX.php
3 years ago
Cleared_Cache_AJAX.php
3 years ago
Create_Campaign_AJAX.php
3 years ago
Delete_Campaign_AJAX.php
3 years ago
Delete_Data_AJAX.php
3 years ago
Export_Campaigns_AJAX.php
3 years ago
Export_Devices_AJAX.php
2 years ago
Export_Geo_AJAX.php
3 years ago
Export_Pages_AJAX.php
3 years ago
Export_Referrers_AJAX.php
3 years ago
Filters_AJAX.php
2 years ago
Migration_Status_AJAX.php
3 years ago
Real_Time_AJAX.php
3 years ago
Test_Email_AJAX.php
3 years ago
Update_Capabilities_AJAX.php
3 years ago
Filters_AJAX.php
149 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\City_To_Country_Converter; |
| 9 | use IAWP_SCOPED\IAWP\Date_Range\Date_Range; |
| 10 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range; |
| 11 | use IAWP_SCOPED\IAWP\Date_Range\Relative_Date_Range; |
| 12 | use IAWP_SCOPED\IAWP\Quick_Stats; |
| 13 | use IAWP_SCOPED\IAWP\Statistics\Intervals\Intervals; |
| 14 | use IAWP_SCOPED\IAWP\Tables\Table_Campaigns; |
| 15 | use IAWP_SCOPED\IAWP\Tables\Table_Devices; |
| 16 | use IAWP_SCOPED\IAWP\Tables\Table_Geo; |
| 17 | use IAWP_SCOPED\IAWP\Tables\Table_Pages; |
| 18 | use IAWP_SCOPED\IAWP\Tables\Table_Referrers; |
| 19 | use IAWP_SCOPED\Proper\Timezone; |
| 20 | use Throwable; |
| 21 | class Filters_AJAX 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') ?? null; |
| 38 | $table_type = $this->get_field('table_type'); |
| 39 | $sort_by = $this->get_field('sort_by') ?? null; |
| 40 | $sort_direction = $this->get_field('sort_direction') ?? null; |
| 41 | $group = $this->get_field('group') ?? null; |
| 42 | $as_csv = $this->get_field('as_csv') ?? \false; |
| 43 | $is_new_group = $this->get_field('is_new_group') === 'true'; |
| 44 | $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval')); |
| 45 | $page = $this->get_field('page') ?? 1; |
| 46 | $is_pages_table = $table_type === 'views'; |
| 47 | $is_referrers_table = $table_type === 'referrers'; |
| 48 | $is_geo_table = $table_type === 'geo'; |
| 49 | $is_campaigns_table = $table_type === 'campaigns'; |
| 50 | $is_devices_table = $table_type === 'devices'; |
| 51 | if ($is_pages_table) { |
| 52 | // Todo - A table should know how to generate the resource it's going to work with |
| 53 | $table = new Table_Pages($visible_columns); |
| 54 | $filters = $table->sanitize_filters($filters); |
| 55 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 56 | $table_data_class = $table->group()->table_data_class(); |
| 57 | $resources = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval); |
| 58 | $rows = $resources->rows(); |
| 59 | $unfiltered_statistics = $resources->unfiltered_statistics(); |
| 60 | $filtered_statistics = $resources->statistics(); |
| 61 | $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label()); |
| 62 | } elseif ($is_referrers_table) { |
| 63 | $table = new Table_Referrers($visible_columns); |
| 64 | $filters = $table->sanitize_filters($filters); |
| 65 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 66 | $table_data_class = $table->group()->table_data_class(); |
| 67 | $referrers = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval); |
| 68 | $rows = $referrers->rows(); |
| 69 | $unfiltered_statistics = $referrers->unfiltered_statistics(); |
| 70 | $filtered_statistics = $referrers->statistics(); |
| 71 | $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label()); |
| 72 | } elseif ($is_geo_table) { |
| 73 | $table = new Table_Geo($visible_columns, $group, $is_new_group); |
| 74 | $filters = $table->sanitize_filters($filters); |
| 75 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 76 | $table_data_class = $table->group()->table_data_class(); |
| 77 | $geos = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval); |
| 78 | $rows = $geos->rows(); |
| 79 | $unfiltered_statistics = $geos->unfiltered_statistics(); |
| 80 | $filtered_statistics = $geos->statistics(); |
| 81 | if ($table->group()->has_id('city')) { |
| 82 | $country_rows = City_To_Country_Converter::convert($rows); |
| 83 | $chart = new Chart_Geo($country_rows, $date_range->label()); |
| 84 | } else { |
| 85 | $chart = new Chart_Geo($rows, $date_range->label()); |
| 86 | } |
| 87 | } elseif ($is_devices_table) { |
| 88 | $table = new Table_Devices($visible_columns, $group, $is_new_group); |
| 89 | $filters = $table->sanitize_filters($filters); |
| 90 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 91 | $table_data_class = $table->group()->table_data_class(); |
| 92 | $devices = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval); |
| 93 | $rows = $devices->rows(); |
| 94 | $unfiltered_statistics = $devices->unfiltered_statistics(); |
| 95 | $filtered_statistics = $devices->statistics(); |
| 96 | $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label()); |
| 97 | } elseif ($is_campaigns_table) { |
| 98 | $table = new Table_Campaigns($visible_columns); |
| 99 | $filters = $table->sanitize_filters($filters); |
| 100 | $sort_configuration = $table->sanitize_sort_parameters($sort_by, $sort_direction); |
| 101 | $table_data_class = $table->group()->table_data_class(); |
| 102 | $campaigns = new $table_data_class($date_range, $filters, $sort_configuration, $chart_interval); |
| 103 | $rows = $campaigns->rows(); |
| 104 | $unfiltered_statistics = $campaigns->unfiltered_statistics(); |
| 105 | $filtered_statistics = $campaigns->statistics(); |
| 106 | $chart = new Chart($filtered_statistics ?? $unfiltered_statistics, $date_range->label()); |
| 107 | } else { |
| 108 | return; |
| 109 | } |
| 110 | $table->set_statistics($filtered_statistics ?? $unfiltered_statistics); |
| 111 | if ($as_csv) { |
| 112 | echo $table->csv($rows, \true); |
| 113 | return; |
| 114 | } |
| 115 | $page_size = \IAWP_SCOPED\iawp()->pagination_page_size(); |
| 116 | $paged_rows = \array_slice($rows, 0, $page_size * $page); |
| 117 | $is_last_page = \count($rows) === \count($paged_rows); |
| 118 | $quick_stats = new Quick_Stats($filtered_statistics, $unfiltered_statistics); |
| 119 | \wp_send_json_success(['rows' => $table->get_row_markup($paged_rows), 'table' => $table->get_row_markup($paged_rows, \false, $sort_configuration->column(), $sort_configuration->is_ascending() ? 'asc' : 'desc'), 'totalRowCount' => \count($rows), 'chart' => $chart->get_html($visible_datasets), 'stats' => $quick_stats->get_html(), 'label' => $date_range->label(), 'isLastPage' => $is_last_page, 'columns' => $table->visible_column_ids(), 'columnsHTML' => $table->column_picker_html(), 'groupId' => $table->group()->id(), 'groupName' => $table->group()->singular(), 'pluralGroupName' => $table->group()->plural(), 'filters' => $filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'chartInterval' => $chart_interval->id()]); |
| 120 | } |
| 121 | /** |
| 122 | * Get the date range for the filter request |
| 123 | * |
| 124 | * The date info can be supplied in one of two ways. |
| 125 | * |
| 126 | * The first is to provide a relative_range_id which is converted into start, end, and label. |
| 127 | * |
| 128 | * The second is to provide explicit start and end fields which will be used as is. |
| 129 | * |
| 130 | * @return Date_Range |
| 131 | */ |
| 132 | private function get_date_range() : Date_Range |
| 133 | { |
| 134 | $relative_range_id = $this->get_field('relative_range_id'); |
| 135 | $start_timestamp = $this->get_field('exact_start'); |
| 136 | $end_timestamp = $this->get_field('exact_end'); |
| 137 | if (!\is_null($start_timestamp) && !\is_null($end_timestamp)) { |
| 138 | try { |
| 139 | $start = new DateTime($start_timestamp, Timezone::site_timezone()); |
| 140 | $end = new DateTime($end_timestamp, Timezone::site_timezone()); |
| 141 | return new Exact_Date_Range($start, $end); |
| 142 | } catch (Throwable $e) { |
| 143 | // Do nothing and fall back to default relative date range |
| 144 | } |
| 145 | } |
| 146 | return new Relative_Date_Range($relative_range_id); |
| 147 | } |
| 148 | } |
| 149 |