Modules
3 months ago
Form_Field.php
5 months ago
Form_Field_Option.php
1 year ago
Module_Refresh_Job.php
1 year ago
Overview.php
1 year ago
WP_Options_Storage.php
9 months ago
Form_Field.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Overview; |
| 4 | |
| 5 | use IAWP\Date_Range\Relative_Date_Range; |
| 6 | use IAWP\Plugin_Group; |
| 7 | use IAWP\Report; |
| 8 | use IAWP\Report_Finder; |
| 9 | use IAWP\Statistics\Statistic; |
| 10 | use IAWP\Tables\Columns\Column; |
| 11 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 12 | /** @internal */ |
| 13 | class Form_Field |
| 14 | { |
| 15 | private static $report_details; |
| 16 | private $id; |
| 17 | private $report; |
| 18 | public function __construct(string $id, ?Report $report) |
| 19 | { |
| 20 | $this->id = $id; |
| 21 | $this->report = $report; |
| 22 | } |
| 23 | public function id() : string |
| 24 | { |
| 25 | return $this->id; |
| 26 | } |
| 27 | public function name() : string |
| 28 | { |
| 29 | $name_lookup_table = ['report' => \__('Report', 'independent-analytics'), 'geo_report' => \__('Geo Report', 'independent-analytics'), 'sort_by' => \__('Sort By', 'independent-analytics'), 'sort_direction' => \__('Sort Direction', 'independent-analytics'), 'aggregatable_sort_by' => \__('Sort By', 'independent-analytics'), 'date_range' => \__('Date Range', 'independent-analytics'), 'busiest_date_range' => \__('Date Range', 'independent-analytics'), 'primary_metric' => \__('Primary Metric', 'independent-analytics'), 'secondary_metric' => \__('Secondary Metric', 'independent-analytics'), 'statistics' => \__('Statistics', 'independent-analytics'), 'recent_conversion_types' => \__('Conversions', 'independent-analytics')]; |
| 30 | return $name_lookup_table[$this->id] ?? $this->id; |
| 31 | } |
| 32 | public function type() : string |
| 33 | { |
| 34 | $field_type_lookup_table = ['report' => 'select', 'geo_report' => 'select', 'sort_by' => 'select', 'sort_direction' => 'select', 'aggregatable_sort_by' => 'select', 'date_range' => 'select', 'busiest_date_range' => 'select', 'primary_metric' => 'select', 'secondary_metric' => 'select', 'statistics' => 'checkboxes', 'recent_conversion_types' => 'checkboxes']; |
| 35 | return $field_type_lookup_table[$this->id] ?? 'select'; |
| 36 | } |
| 37 | /** |
| 38 | * Check if the provided value is valid for a given form field. |
| 39 | * |
| 40 | * @param mixed $value |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | public function is_a_supported_value($value) : bool |
| 45 | { |
| 46 | if (\is_array($value)) { |
| 47 | return $this->is_a_supported_array_value($value); |
| 48 | } |
| 49 | return \in_array($value, $this->supported_ids(), \true); |
| 50 | } |
| 51 | /** |
| 52 | * Get the grouped form field options. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function template_values() : array |
| 57 | { |
| 58 | $grouped_values = Collection::make($this->supported_values())->groupBy(function (\IAWP\Overview\Form_Field_Option $form_field_option) { |
| 59 | return $form_field_option->group(); |
| 60 | })->toArray(); |
| 61 | // If the values don't have groups, just return an array of the values |
| 62 | if (\count($grouped_values) === 1 && \array_key_exists("", $grouped_values)) { |
| 63 | return $grouped_values['']; |
| 64 | } |
| 65 | return $grouped_values; |
| 66 | } |
| 67 | /** |
| 68 | * @return Form_Field_Option[] |
| 69 | */ |
| 70 | public function supported_values() : array |
| 71 | { |
| 72 | $report_details = self::get_report_details_by_id($this->report->id()); |
| 73 | $report_values = Collection::make(Report_Finder::new()->get_reports())->filter(function (Report $report) { |
| 74 | // User Journey reports do not make sense for the Overview Report |
| 75 | if ($report->type() === 'journeys') { |
| 76 | return \false; |
| 77 | } |
| 78 | return \true; |
| 79 | })->values()->map(function (Report $report) { |
| 80 | return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name(), $report->type_label()); |
| 81 | })->all(); |
| 82 | $geo_report_values = Collection::make(Report_Finder::new()->get_reports())->filter(function (Report $report) { |
| 83 | return $report->type() === 'geo'; |
| 84 | })->map(function (Report $report) { |
| 85 | return \IAWP\Overview\Form_Field_Option::new($report->id(), $report->name()); |
| 86 | })->values()->all(); |
| 87 | $sort_direction_values = [\IAWP\Overview\Form_Field_Option::new('desc', 'High-to-Low'), \IAWP\Overview\Form_Field_Option::new('asc', 'Low-to-High')]; |
| 88 | $date_range_values = Collection::make(Relative_Date_Range::ranges())->map(function (Relative_Date_Range $range) { |
| 89 | return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label()); |
| 90 | })->all(); |
| 91 | $busiest_date_range_values = Collection::make(Relative_Date_Range::ranges())->filter(function (Relative_Date_Range $range) { |
| 92 | $busiest_ranges = ['LAST_SEVEN', 'LAST_THIRTY', 'LAST_NINETY']; |
| 93 | return \in_array($range->relative_range_id(), $busiest_ranges); |
| 94 | })->map(function (Relative_Date_Range $range) { |
| 95 | return \IAWP\Overview\Form_Field_Option::new($range->relative_range_id(), $range->label()); |
| 96 | })->all(); |
| 97 | $secondary_metric = Collection::make($report_details['statistics'] ?? [])->prepend(\IAWP\Overview\Form_Field_Option::new('no_comparison', \__('No Comparison', 'independent-analytics'), \__('No Comparison', 'independent-analytics')))->all(); |
| 98 | $recent_conversion_types = [\IAWP\Overview\Form_Field_Option::new('order', \__('Orders', 'independent-analytics')), \IAWP\Overview\Form_Field_Option::new('form_submission', \__('Form Submissions', 'independent-analytics')), \IAWP\Overview\Form_Field_Option::new('click', \__('Clicks', 'independent-analytics'))]; |
| 99 | $supported_values_lookup_table = ['report' => $report_values, 'geo_report' => $geo_report_values, 'sort_by' => $report_details['columns'] ?? [], 'sort_direction' => $sort_direction_values, 'aggregatable_sort_by' => $report_details['aggregatable_columns'] ?? [], 'date_range' => $date_range_values, 'busiest_date_range' => $busiest_date_range_values, 'primary_metric' => $report_details['statistics'] ?? [], 'secondary_metric' => $secondary_metric, 'statistics' => $report_details['statistics'] ?? [], 'recent_conversion_types' => $recent_conversion_types]; |
| 100 | return $supported_values_lookup_table[$this->id] ?? []; |
| 101 | } |
| 102 | private function supported_ids() : array |
| 103 | { |
| 104 | return Collection::make($this->supported_values())->map(function (\IAWP\Overview\Form_Field_Option $option) { |
| 105 | return $option->id(); |
| 106 | })->all(); |
| 107 | } |
| 108 | /** |
| 109 | * Check if the provided array values are valid for a given form field. |
| 110 | * |
| 111 | * @param array $values |
| 112 | * |
| 113 | * @return bool |
| 114 | */ |
| 115 | private function is_a_supported_array_value(array $values) : bool |
| 116 | { |
| 117 | // Only checkboxes support selecting more than one value |
| 118 | if ($this->type() !== 'checkboxes') { |
| 119 | return \false; |
| 120 | } |
| 121 | return Collection::make($values)->every(function ($value) { |
| 122 | return \in_array($value, $this->supported_ids(), \true); |
| 123 | }); |
| 124 | } |
| 125 | public static function get_report_details() : array |
| 126 | { |
| 127 | if (\is_array(self::$report_details)) { |
| 128 | return self::$report_details; |
| 129 | } |
| 130 | self::$report_details = Collection::make(Report_Finder::new()->get_reports())->map(function (Report $report) { |
| 131 | $columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) { |
| 132 | return \in_array($column->type(), ['int', 'date']); |
| 133 | })->map(function (Column $column) { |
| 134 | $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group()); |
| 135 | return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name()); |
| 136 | })->values()->all(); |
| 137 | $aggregatable_columns = Collection::make($report->get_supported_columns())->filter(function (Column $column) { |
| 138 | return $column->aggregatable(); |
| 139 | })->map(function (Column $column) { |
| 140 | $plugin_group = Plugin_Group::get_plugin_group($column->plugin_group()); |
| 141 | return \IAWP\Overview\Form_Field_Option::new($column->id(), $column->name(), $plugin_group->name()); |
| 142 | })->values()->all(); |
| 143 | $statistics = \array_map(function (Statistic $statistic) { |
| 144 | $plugin_group = Plugin_Group::get_plugin_group($statistic->plugin_group()); |
| 145 | return \IAWP\Overview\Form_Field_Option::new($statistic->id(), $statistic->name(), $plugin_group->name()); |
| 146 | }, $report->get_supported_statistics()); |
| 147 | return ['id' => $report->id(), 'name' => $report->name(), 'columns' => $columns, 'aggregatable_columns' => $aggregatable_columns, 'statistics' => $statistics]; |
| 148 | })->all(); |
| 149 | return self::$report_details; |
| 150 | } |
| 151 | public static function get_report_details_by_id($id) : ?array |
| 152 | { |
| 153 | return Collection::make(self::get_report_details())->first(function ($report_details) use($id) { |
| 154 | return $report_details['id'] === $id; |
| 155 | }); |
| 156 | } |
| 157 | } |
| 158 |