AJAX.php
1 year ago
AJAX_Manager.php
6 months ago
Archive_Link.php
1 year ago
Click_Tracking_Cache_Cleared.php
1 year ago
Configure_Pruner.php
1 year ago
Copy_Report.php
1 year ago
Create_Campaign.php
2 years ago
Create_Report.php
1 year ago
Delete_Campaign.php
2 years ago
Delete_Data.php
1 year ago
Delete_Link.php
8 months ago
Delete_Module.php
1 year ago
Delete_Report.php
1 year ago
Dismiss_Notice.php
1 year ago
Edit_Link.php
5 months ago
Edit_Module.php
1 year ago
Export_Report_Statistics.php
9 months ago
Export_Report_Table.php
9 months ago
Export_Reports.php
5 months ago
Filter.php
6 months ago
Get_Journey_Timeline.php
6 months ago
Get_Markup_For_Module.php
1 year ago
Get_Markup_For_Modules.php
1 year ago
Import_Reports.php
1 year ago
Migration_Status.php
5 months ago
Pause_Email_Reports.php
6 months ago
Preview_Email.php
2 years ago
Real_Time_Data.php
2 years ago
Refresh_Modules.php
5 months ago
Rename_Report.php
1 year ago
Reorder_Modules.php
1 year ago
Reset_Analytics.php
2 years ago
Reset_Overview.php
1 year ago
Save_Module.php
1 year ago
Save_Report.php
1 year ago
Set_Favorite_Report.php
1 year ago
Set_WooCommerce_Statuses_To_Track.php
1 year ago
Sort_Links.php
1 year ago
Sort_Reports.php
1 year ago
Test_Email.php
2 years ago
Update_Capabilities.php
1 year ago
Update_User_Settings.php
2 years ago
Filter.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\AJAX; |
| 4 | |
| 5 | use DateTime; |
| 6 | use IAWP\Chart; |
| 7 | use IAWP\Date_Range\Date_Range; |
| 8 | use IAWP\Date_Range\Exact_Date_Range; |
| 9 | use IAWP\Date_Range\Relative_Date_Range; |
| 10 | use IAWP\Env; |
| 11 | use IAWP\Examiner_Config; |
| 12 | use IAWP\Map; |
| 13 | use IAWP\Map_Data; |
| 14 | use IAWP\Quick_Stats; |
| 15 | use IAWP\Rows\Rows; |
| 16 | use IAWP\Statistics\Intervals\Intervals; |
| 17 | use IAWP\Statistics\Statistics; |
| 18 | use IAWP\Tables\Table; |
| 19 | use IAWP\Utils\Timezone; |
| 20 | use Throwable; |
| 21 | /** @internal */ |
| 22 | class Filter extends \IAWP\AJAX\AJAX |
| 23 | { |
| 24 | protected function action_name() : string |
| 25 | { |
| 26 | return 'iawp_filter'; |
| 27 | } |
| 28 | protected function action_required_fields() : array |
| 29 | { |
| 30 | return ['table_type', 'columns']; |
| 31 | } |
| 32 | protected function action_callback() : void |
| 33 | { |
| 34 | $date_range = $this->get_date_range(); |
| 35 | $is_new_date_range = $this->get_field('is_new_date_range') === 'true'; |
| 36 | $filters = $this->get_field('filters') ?? []; |
| 37 | $sort_column = $this->get_field('sort_column') ?? null; |
| 38 | $sort_direction = $this->get_field('sort_direction') ?? null; |
| 39 | $group = $this->get_field('group') ?? null; |
| 40 | $as_csv = $this->get_field('as_csv') ?? \false; |
| 41 | $is_new_group = $this->get_field('is_new_group') === 'true'; |
| 42 | $chart_interval = $is_new_date_range ? Intervals::default_for($date_range->number_of_days()) : Intervals::find_by_id($this->get_field('chart_interval')); |
| 43 | $page = \intval($this->get_field('page') ?? 1); |
| 44 | $number_of_rows = $page * \IAWPSCOPED\iawp()->pagination_page_size(); |
| 45 | $table_type = $this->get_field('table_type'); |
| 46 | $is_geo_table = $table_type === 'geo'; |
| 47 | $is_journeys_table = $table_type === 'journeys'; |
| 48 | $table_class = Env::get_table(); |
| 49 | $filter_logic = $this->get_filter_logic(); |
| 50 | $examiner_config = Examiner_Config::make(['type' => $this->get_field('examiner_type'), 'group' => $this->get_field('examiner_group'), 'id' => $this->get_int_field('examiner_id')]); |
| 51 | /** @var Table $table */ |
| 52 | $table = new $table_class($group, $is_new_group); |
| 53 | $filters = $table->sanitize_filters($filters); |
| 54 | $filters = $table->resolve_filter_conflicts($filters, $filter_logic); |
| 55 | $sort_configuration = $table->sanitize_sort_parameters($sort_column, $sort_direction); |
| 56 | $rows_class = $table->group()->rows_class(); |
| 57 | $statistics_class = $table->group()->statistics_class(); |
| 58 | $get_all_rows = $as_csv || $is_geo_table; |
| 59 | /** @var Rows $rows_query */ |
| 60 | $rows_query = new $rows_class($date_range, $sort_configuration, $get_all_rows ? null : $number_of_rows, $filters, $filter_logic); |
| 61 | if ($examiner_config) { |
| 62 | $rows_query->for_examiner($examiner_config); |
| 63 | } |
| 64 | $rows = $rows_query->rows(); |
| 65 | if ($as_csv) { |
| 66 | $csv = $table->csv($rows, \true); |
| 67 | echo $csv->to_string(); |
| 68 | return; |
| 69 | } |
| 70 | if (empty($filters)) { |
| 71 | /** @var Statistics $statistics */ |
| 72 | $statistics = new $statistics_class($date_range, null, $chart_interval); |
| 73 | } else { |
| 74 | $statistics = new $statistics_class($date_range, $rows_query, $chart_interval); |
| 75 | } |
| 76 | $total_number_of_rows = $statistics->total_number_of_rows(); |
| 77 | $table->set_statistics($statistics); |
| 78 | $quick_stat_statistics = $statistics; |
| 79 | $hide_unfiltered_statistics = \false; |
| 80 | // The examiner isn't showing quick stats for the table rows. It's showing quick stats for the |
| 81 | // examined record. |
| 82 | if ($examiner_config) { |
| 83 | $examiner_table_class = Env::get_table($examiner_config->type()); |
| 84 | $examiner_table = new $examiner_table_class($examiner_config->group()); |
| 85 | $examiner_rows_class = $examiner_table->group()->rows_class(); |
| 86 | $examiner_statistics_class = $examiner_table->group()->statistics_class(); |
| 87 | /** @var Rows $rows_query */ |
| 88 | $rows_query = new $examiner_rows_class($date_range, $examiner_table->sanitize_sort_parameters()); |
| 89 | $rows_query->limit_to($examiner_config->id()); |
| 90 | $quick_stat_statistics = new $examiner_statistics_class($date_range, $rows_query, $chart_interval); |
| 91 | $hide_unfiltered_statistics = \true; |
| 92 | $table->set_statistics($quick_stat_statistics); |
| 93 | } |
| 94 | $chart_markup = null; |
| 95 | $chart_interval_id = $chart_interval->id(); |
| 96 | if ($is_geo_table) { |
| 97 | $map_data = new Map_Data($rows); |
| 98 | $chart = new Map($map_data->get_country_data(), $date_range->label()); |
| 99 | $chart_markup = $chart->get_html(); |
| 100 | $rows = \array_slice($rows, 0, $number_of_rows); |
| 101 | } elseif ($is_journeys_table) { |
| 102 | $chart_interval_id = null; |
| 103 | $chart_markup = null; |
| 104 | } else { |
| 105 | $chart = new Chart($quick_stat_statistics); |
| 106 | $chart_markup = $chart->get_html(); |
| 107 | } |
| 108 | $quick_stats = new Quick_Stats($quick_stat_statistics, \false, \false, $hide_unfiltered_statistics); |
| 109 | $raw_filters = \array_map(function (\IAWP\Rows\Filter $filter) { |
| 110 | return $filter->as_associative_array(); |
| 111 | }, $filters); |
| 112 | \wp_send_json_success(['stats' => $quick_stats->get_html(), 'chart' => $chart_markup, 'chartInterval' => $chart_interval_id, 'table' => $table->get_rendered_template($rows, \false, $sort_configuration->column(), $sort_configuration->direction()), 'rows' => $table->get_rendered_template($rows, \true, $sort_configuration->column(), $sort_configuration->direction()), 'tableToolbar' => $table->get_table_toolbar_markup(), 'totalNumberOfRows' => $total_number_of_rows, 'label' => $date_range->label(), 'isLastPage' => \count($rows) < \IAWPSCOPED\iawp()->pagination_page_size() * $page, 'columns' => $table->visible_column_ids(), 'columnsHTML' => $table->column_picker_html(), 'groupId' => $table->group()->id(), 'filters' => $raw_filters, 'filtersTemplateHTML' => $table->filters_template_html(), 'filtersButtonsHTML' => $table->filters_condition_buttons_html($filters)]); |
| 113 | } |
| 114 | /** |
| 115 | * Get the date range for the filter request |
| 116 | * |
| 117 | * The date info can be supplied in one of two ways. |
| 118 | * |
| 119 | * The first is to provide a relative_range_id which is converted into start, end, and label. |
| 120 | * |
| 121 | * The second is to provide explicit start and end fields which will be used as is. |
| 122 | * |
| 123 | * @return Date_Range |
| 124 | */ |
| 125 | private function get_date_range() : Date_Range |
| 126 | { |
| 127 | $relative_range_id = $this->get_field('relative_range_id'); |
| 128 | $exact_start = $this->get_field('exact_start'); |
| 129 | $exact_end = $this->get_field('exact_end'); |
| 130 | if (!\is_null($exact_start) && !\is_null($exact_end)) { |
| 131 | try { |
| 132 | $start = new DateTime($exact_start, Timezone::site_timezone()); |
| 133 | $end = new DateTime($exact_end, Timezone::site_timezone()); |
| 134 | return new Exact_Date_Range($start, $end); |
| 135 | } catch (Throwable $e) { |
| 136 | // Do nothing and fall back to default relative date range |
| 137 | } |
| 138 | } |
| 139 | return new Relative_Date_Range($relative_range_id); |
| 140 | } |
| 141 | private function get_filter_logic() : string |
| 142 | { |
| 143 | $value = $this->get_field('filter_logic'); |
| 144 | if (!\in_array($value, ['and', 'or'])) { |
| 145 | return 'and'; |
| 146 | } |
| 147 | return $value; |
| 148 | } |
| 149 | } |
| 150 |