* @link https://www.fireplugins.com * @copyright Copyright © 2022 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core\Admin\Analytics\Helpers; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } use FPFramework\Libs\Registry; use Firebox\Core\Admin\Analytics\Toolbar; use Firebox\Core\Admin\Analytics\MetricData; class AnalyticsHelper { /** * Analytics Configuration data * * @var array */ private static $configuration_data = []; /** * Returns the data based on metrics, toolbar items and other data * * @param array $metrics * @param array $toolbar_data * @param array $other_data * * @return array */ public static function getData($metrics = [], $toolbar_data = [], $other_data = []) { // ensure we were given metrics and toolbar data if (!$metrics || !$toolbar_data || !is_array($metrics) || !is_array($toolbar_data)) { return []; } $other_data = new Registry($other_data); // The metrics we were given may get data from db and other metrics. // We may only need to get data from a specific data source. i.e. db/metrics $allowed_data_sources = $other_data->get('data_source', []); $data = []; foreach ($metrics as $metric_key) { // initialize the metric to receive the data $class = '\FireBox\Core\Admin\Analytics\Metrics\\' . ucfirst(strtolower($metric_key)); // create a Toolbar and set the selected toolbar items $toolbar = new Toolbar(self::$configuration_data); $toolbar->setSelectedToolbarItemsValue($toolbar_data); $toolbar->setUpAvailableToolbarItems(); $metric = new $class($toolbar); // If we were given which data sources to process, check that the current metric has a valid data source if ($allowed_data_sources) { if (!in_array($metric->getDataSource(), $allowed_data_sources)) { continue; } } // Check if we were given extra query data that modify the base query and apply them. $metric_modify_query_data = $other_data->get('modify_query_data', []); if ($metric_modify_query_data) { $metric->getMetricData()->setModifyQueryData($metric_modify_query_data); } // Check if we have extra options to set for the Metric $metric_options = $other_data->get('options', []); if ($metric_options) { $metric->setOptions($metric_options); } // get the metric data $metric->setData(); // store them if (count($metrics) == 1) { $data = $metric->getData(); } else { $data[$metric->getType()] = $metric->getData(); } } return $data; } public static function setConfigurationData($data) { if (self::$configuration_data) { return; } self::$configuration_data = $data; } }