| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.2 Free | |
| 4 | + * @version 2.0.10 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2023 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core\Admin\Analytics; |
| @@ -43,9 +43,9 @@ | ||
| 43 | 43 | * Type of metric |
| 44 | 44 | * |
| 45 | 45 | * @var string |
| 46 | 46 | */ |
| 47 | - private $type; | |
| 47 | + protected $type; | |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * Suffix that appears next to the metric total, chart values |
| 51 | 51 | * |
| @@ -99,9 +99,9 @@ | ||
| 99 | 99 | * Current period data |
| 100 | 100 | * |
| 101 | 101 | * @var array |
| 102 | 102 | */ |
| 103 | - private $current_period_data; | |
| 103 | + protected $current_period_data; | |
| 104 | 104 | |
| 105 | 105 | /** |
| 106 | 106 | * Current period total |
| 107 | 107 | * |
| @@ -106,9 +106,9 @@ | ||
| 106 | 106 | * Current period total |
| 107 | 107 | * |
| 108 | 108 | * @var array |
| 109 | 109 | */ |
| 110 | - private $current_period_total; | |
| 110 | + protected $current_period_total; | |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * Last period data |
| 114 | 114 | * |
| @@ -113,9 +113,9 @@ | ||
| 113 | 113 | * Last period data |
| 114 | 114 | * |
| 115 | 115 | * @var array |
| 116 | 116 | */ |
| 117 | - private $last_period_data; | |
| 117 | + protected $last_period_data; | |
| 118 | 118 | |
| 119 | 119 | /** |
| 120 | 120 | * Last period total |
| 121 | 121 | * |
| @@ -120,9 +120,9 @@ | ||
| 120 | 120 | * Last period total |
| 121 | 121 | * |
| 122 | 122 | * @var array |
| 123 | 123 | */ |
| 124 | - private $last_period_total; | |
| 124 | + protected $last_period_total; | |
| 125 | 125 | |
| 126 | 126 | /** |
| 127 | 127 | * Tooltip of metric |
| 128 | 128 | * |
| @@ -141,9 +141,9 @@ | ||
| 141 | 141 | * The toolbar items from which the metric will not appear in the search results. |
| 142 | 142 | * |
| 143 | 143 | * @var string |
| 144 | 144 | */ |
| 145 | - private $hide_from_results_toolbar_items; | |
| 145 | + private $hide_from_results_toolbar_items = []; | |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * Percentage Change of metric |
| 149 | 149 | * |
| @@ -148,9 +148,9 @@ | ||
| 148 | 148 | * Percentage Change of metric |
| 149 | 149 | * |
| 150 | 150 | * @var float |
| 151 | 151 | */ |
| 152 | - private $percentage_change; | |
| 152 | + protected $percentage_change; | |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | 155 | * Toolbar |
| 156 | 156 | * |
| @@ -172,11 +172,26 @@ | ||
| 172 | 172 | */ |
| 173 | 173 | protected $metric_data; |
| 174 | 174 | |
| 175 | 175 | /** |
| 176 | + * These metrics are used to calculate the metric value | |
| 177 | + * when other metrics are used. | |
| 178 | + * | |
| 179 | + * @var array | |
| 180 | + */ | |
| 181 | + private $custom_metrics = []; | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Options Raw | |
| 185 | + * | |
| 186 | + * @var array | |
| 187 | + */ | |
| 188 | + protected $options_raw; | |
| 189 | + | |
| 190 | + /** | |
| 176 | 191 | * Options |
| 177 | 192 | * |
| 178 | - * @var array | |
| 193 | + * @var object | |
| 179 | 194 | */ |
| 180 | 195 | protected $options; |
| 181 | 196 | |
| 182 | 197 | public function __construct($toolbar = null, $metric_data = null, $payload = null, $options = []) |
| @@ -195,9 +210,9 @@ | ||
| 195 | 210 | if (!$payload && method_exists($this, 'getMetricSpecificData')) |
| 196 | 211 | { |
| 197 | 212 | $payload = $this->getMetricSpecificData(); |
| 198 | 213 | } |
| 199 | - $this->payload = $payload; | |
| 214 | + $this->payload = new Registry($payload); | |
| 200 | 215 | |
| 201 | 216 | if (!$metric_data) |
| 202 | 217 | { |
| 203 | 218 | $metric_data = new MetricData($this, $this->toolbar); |
| @@ -203,49 +218,42 @@ | ||
| 203 | 218 | $metric_data = new MetricData($this, $this->toolbar); |
| 204 | 219 | } |
| 205 | 220 | $this->metric_data = $metric_data; |
| 206 | 221 | |
| 222 | + $this->options_raw = $options; | |
| 207 | 223 | $this->options = new Registry($options); |
| 224 | + | |
| 225 | + $this->setupBasicData(); | |
| 208 | 226 | } |
| 209 | 227 | |
| 210 | - /** | |
| 211 | - * Sets the metric basic data | |
| 212 | - * | |
| 213 | - * @return void | |
| 214 | - */ | |
| 215 | - public function setBasicData() | |
| 228 | + public function setupBasicData() | |
| 216 | 229 | { |
| 217 | - $payload = new Registry($this->payload); | |
| 218 | - | |
| 219 | - // set metric data | |
| 220 | - $this->type = $payload->get('type'); | |
| 221 | - $this->decimal_points = $payload->get('decimal_points'); | |
| 222 | - $this->color = $payload->get('color'); | |
| 223 | - $this->hover_color = $payload->get('hover_color'); | |
| 224 | - $this->label = $payload->get('label'); | |
| 225 | - $this->clean_label = $payload->get('clean_label'); | |
| 226 | - $this->tooltip = $payload->get('tooltip'); | |
| 227 | - $this->read_more_link = $payload->get('read_more_link'); | |
| 228 | - $this->hide_from_results_toolbar_items = $payload->get('hide_from_results_toolbar_items'); | |
| 230 | + // set basic metric data | |
| 231 | + $this->type = $this->payload->get('type'); | |
| 232 | + $this->decimal_points = $this->payload->get('decimal_points'); | |
| 233 | + $this->color = $this->payload->get('color'); | |
| 234 | + $this->hover_color = $this->payload->get('hover_color'); | |
| 235 | + $this->label = $this->payload->get('label'); | |
| 236 | + $this->clean_label = $this->payload->get('clean_label'); | |
| 237 | + $this->tooltip = $this->payload->get('tooltip'); | |
| 238 | + $this->read_more_link = $this->payload->get('read_more_link'); | |
| 239 | + $this->hide_from_results_toolbar_items = $this->payload->get('hide_from_results_toolbar_items', []); | |
| 229 | 240 | } |
| 230 | 241 | |
| 231 | 242 | /** |
| 232 | - * Sets the metric data | |
| 243 | + * Updates metric. | |
| 233 | 244 | * |
| 234 | 245 | * @return void |
| 235 | 246 | */ |
| 236 | - public function setData() | |
| 247 | + public function update() | |
| 237 | 248 | { |
| 249 | + $this->setupBasicData(); | |
| 250 | + | |
| 238 | 251 | $skip_filling_dates = (bool) $this->options->get('skip_filling_dates', false); |
| 239 | 252 | $skip_compare_check_to_retrieve_previous_data = (bool) $this->options->get('skip_compare_check_to_retrieve_previous_data', false); |
| 240 | - | |
| 241 | - $payload = new Registry($this->payload); | |
| 242 | 253 | |
| 243 | - // set the basic data | |
| 244 | - $this->setBasicData(); | |
| 245 | - | |
| 246 | 254 | // current period data |
| 247 | - $this->current_period_data = $this->metric_data->getCurrent(); | |
| 255 | + $this->current_period_data = $this->getPeriodData(false); | |
| 248 | 256 | $this->current_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->current_period_data); |
| 249 | 257 | |
| 250 | 258 | $toolbarItems = $this->toolbar->getToolbarItems(); |
| 251 | 259 | $fillDatesPayload = [ |
| @@ -253,13 +261,13 @@ | ||
| 253 | 261 | 'total_days' => $toolbarItems['date']->getTotalDays(), |
| 254 | 262 | |
| 255 | 263 | 'options_key' => $toolbarItems['date']->getOptionsKey() |
| 256 | 264 | ]; |
| 257 | - | |
| 265 | + | |
| 258 | 266 | if (!$skip_filling_dates) |
| 259 | 267 | { |
| 260 | 268 | // fill missing current data dates |
| 261 | - $this->current_period_data = Helpers\MetricHelper::fillMissingData($this->current_period_data, $fillDatesPayload); | |
| 269 | + $this->current_period_data = Helpers\MetricHelper::fillMissingData($this->current_period_data, $fillDatesPayload, 'd/m/Y'); | |
| 262 | 270 | } |
| 263 | 271 | |
| 264 | 272 | // Parse the metric total via the metric itself |
| 265 | 273 | // This is useful if the metric needs to run extra logic prior to showing the total |
| @@ -267,10 +275,12 @@ | ||
| 267 | 275 | { |
| 268 | 276 | $this->current_period_total = $this->parseMetricTotal($this->current_period_total, $this->current_period_data); |
| 269 | 277 | } |
| 270 | 278 | |
| 279 | + | |
| 271 | 280 | $this->is_compare = false; |
| 272 | 281 | |
| 282 | + | |
| 273 | 283 | |
| 274 | 284 | $percentage_change = 0; |
| 275 | 285 | // if we don't have a compare selected toolbar item, then get percentage from previous period |
| 276 | 286 | if (!$skip_compare_check_to_retrieve_previous_data && !$this->is_compare) |
| @@ -276,23 +286,27 @@ | ||
| 276 | 286 | if (!$skip_compare_check_to_retrieve_previous_data && !$this->is_compare) |
| 277 | 287 | { |
| 278 | 288 | // get last period data only for db related metrics |
| 279 | 289 | // non-db related metrics handle their percentage change in Metrics.php method setMetricsData() |
| 280 | - if ($this->getDataSource() == 'db') | |
| 290 | + if ($this->getDataSource() === 'db') | |
| 281 | 291 | { |
| 282 | 292 | $percentage_change = $this->metric_data->getPercentageChangeWithPreviousPeriod($this->current_period_total); |
| 283 | 293 | } |
| 294 | + else | |
| 295 | + { | |
| 296 | + // TODO: Adds last period data for custom metrics | |
| 297 | + } | |
| 284 | 298 | } |
| 285 | 299 | else |
| 286 | 300 | { |
| 287 | 301 | // last period data |
| 288 | - $this->last_period_data = $this->metric_data->getLastPeriod(); | |
| 302 | + $this->last_period_data = $this->getPeriodData(true); | |
| 289 | 303 | $this->last_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->last_period_data); |
| 290 | 304 | |
| 291 | 305 | // check if the metric needs to parse the metric total |
| 292 | 306 | if (method_exists($this, 'parseMetricTotal')) |
| 293 | 307 | { |
| 294 | - $this->last_period_total = $this->parseMetricTotal($this->last_period_total, $this->last_period_data); | |
| 308 | + $this->last_period_total = $this->parseMetricTotal($this->last_period_total, $this->last_period_data, true); | |
| 295 | 309 | } |
| 296 | 310 | |
| 297 | 311 | if (!$skip_filling_dates) |
| 298 | 312 | { |
| @@ -297,20 +311,34 @@ | ||
| 297 | 311 | if (!$skip_filling_dates) |
| 298 | 312 | { |
| 299 | 313 | // We are comparing and we have re-calculated the start date, so fetch it and set it so display accurate data on x axis |
| 300 | 314 | $fillDatesPayload['start_date'] = $toolbarItems['date']->getStartDate(); |
| 301 | - | |
| 315 | + | |
| 302 | 316 | // fill missing last period data dates |
| 303 | - $this->last_period_data = Helpers\MetricHelper::fillMissingData($this->last_period_data, $fillDatesPayload); | |
| 317 | + $this->last_period_data = Helpers\MetricHelper::fillMissingData($this->last_period_data, $fillDatesPayload, 'd/m/Y'); | |
| 304 | 318 | } |
| 305 | 319 | |
| 306 | - // otherwise if we are comparing, calculate a local percentage change from the 2 actual metrics(current and last period) | |
| 320 | + // Calculate a local percentage change from the current and last period | |
| 307 | 321 | $percentage_change = $this->getLocalMetricPercentageChange($this->current_period_total, $this->last_period_total); |
| 308 | 322 | } |
| 309 | 323 | $this->percentage_change = $percentage_change; |
| 324 | + | |
| 325 | + return $this; | |
| 310 | 326 | } |
| 311 | 327 | |
| 312 | 328 | /** |
| 329 | + * Returns the metric period data. | |
| 330 | + * | |
| 331 | + * @param bool $previous_period Whether to get the data for the previous period. | |
| 332 | + * | |
| 333 | + * @return array | |
| 334 | + */ | |
| 335 | + private function getPeriodData($previous_period = false) | |
| 336 | + { | |
| 337 | + return $this->getDataSource() === 'db' ? $this->metric_data->getData($previous_period) : $this->getCustomMetricData($previous_period); | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 313 | 341 | * Returns all metric data |
| 314 | 342 | * |
| 315 | 343 | * @return array |
| 316 | 344 | */ |
| @@ -371,29 +399,15 @@ | ||
| 371 | 399 | |
| 372 | 400 | return $percentage; |
| 373 | 401 | } |
| 374 | 402 | |
| 375 | - /** | |
| 376 | - * If we are fetching the metric data from other metrics data then let | |
| 377 | - * each metric handle the process | |
| 378 | - * | |
| 379 | - * @param array $metrics_data | |
| 380 | - * | |
| 381 | - * @return array | |
| 382 | - */ | |
| 383 | - public function getMetricDataFromOtherMetrics($metrics_data) | |
| 384 | - { | |
| 385 | - $data = []; | |
| 386 | - | |
| 387 | - $data = apply_filters('firebox/analytics/metrics/' . $this->type . '/filter/source_type/metrics', $metrics_data); | |
| 388 | - | |
| 389 | - return $data; | |
| 390 | - } | |
| 391 | - | |
| 392 | 403 | public function getMetricData() |
| 393 | 404 | { |
| 394 | 405 | return $this->metric_data; |
| 395 | 406 | } |
| 407 | + | |
| 408 | + public function onResults() | |
| 409 | + {} | |
| 396 | 410 | |
| 397 | 411 | /** |
| 398 | 412 | * Get type |
| 399 | 413 | * |
| @@ -548,10 +562,15 @@ | ||
| 548 | 562 | { |
| 549 | 563 | $this->options = new Registry($options); |
| 550 | 564 | } |
| 551 | 565 | |
| 552 | - protected function getOptions() | |
| 566 | + public function getOptionsRaw() | |
| 553 | 567 | { |
| 568 | + return $this->options_raw; | |
| 569 | + } | |
| 570 | + | |
| 571 | + public function getOptions() | |
| 572 | + { | |
| 554 | 573 | return $this->options; |
| 555 | 574 | } |
| 556 | 575 | |
| 557 | 576 | public function getPayload() |
| @@ -561,6 +580,22 @@ | ||
| 561 | 580 | |
| 562 | 581 | public function setToolbar($toolbar) |
| 563 | 582 | { |
| 564 | 583 | $this->toolbar = $toolbar; |
| 584 | + $this->metric_data->setToolbar($toolbar); | |
| 585 | + } | |
| 586 | + | |
| 587 | + public function getHideFromResultsToolbarItems() | |
| 588 | + { | |
| 589 | + return $this->hide_from_results_toolbar_items; | |
| 590 | + } | |
| 591 | + | |
| 592 | + public function setCustomMetrics($custom_metrics) | |
| 593 | + { | |
| 594 | + $this->custom_metrics = $custom_metrics; | |
| 595 | + } | |
| 596 | + | |
| 597 | + public function getCustomMetrics() | |
| 598 | + { | |
| 599 | + return $this->custom_metrics; | |
| 565 | 600 | } |
| 566 | 601 | } |