| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.0 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Admin\Analytics\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
use Firebox\Core\Admin\Analytics\Toolbar; |
| 21 |
use Firebox\Core\Admin\Analytics\MetricData; |
| 22 |
|
| 23 |
class AnalyticsHelper |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Analytics Configuration data |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private static $configuration_data = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the data based on metrics, toolbar items and other data |
| 34 |
* |
| 35 |
* @param array $metrics |
| 36 |
* @param array $toolbar_data |
| 37 |
* @param array $other_data |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public static function getData($metrics = [], $toolbar_data = [], $other_data = []) |
| 42 |
{ |
| 43 |
// ensure we were given metrics and toolbar data |
| 44 |
if (!$metrics || !$toolbar_data || !is_array($metrics) || !is_array($toolbar_data)) |
| 45 |
{ |
| 46 |
return []; |
| 47 |
} |
| 48 |
|
| 49 |
$other_data = new Registry($other_data); |
| 50 |
|
| 51 |
// The metrics we were given may get data from db and other metrics. |
| 52 |
// We may only need to get data from a specific data source. i.e. db/metrics |
| 53 |
$allowed_data_sources = $other_data->get('data_source', []); |
| 54 |
|
| 55 |
$data = []; |
| 56 |
|
| 57 |
foreach ($metrics as $metric_key) |
| 58 |
{ |
| 59 |
// initialize the metric to receive the data |
| 60 |
$class = '\FireBox\Core\Admin\Analytics\Metrics\\' . ucfirst(strtolower($metric_key)); |
| 61 |
|
| 62 |
// create a Toolbar and set the selected toolbar items |
| 63 |
$toolbar = new Toolbar(self::$configuration_data); |
| 64 |
$toolbar->setSelectedToolbarItemsValue($toolbar_data); |
| 65 |
$toolbar->setUpAvailableToolbarItems(); |
| 66 |
|
| 67 |
$metric = new $class($toolbar); |
| 68 |
|
| 69 |
// If we were given which data sources to process, check that the current metric has a valid data source |
| 70 |
if ($allowed_data_sources) |
| 71 |
{ |
| 72 |
if (!in_array($metric->getDataSource(), $allowed_data_sources)) |
| 73 |
{ |
| 74 |
continue; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Check if we were given extra query data that modify the base query and apply them. |
| 79 |
$metric_modify_query_data = $other_data->get('modify_query_data', []); |
| 80 |
if ($metric_modify_query_data) |
| 81 |
{ |
| 82 |
$metric->getMetricData()->setModifyQueryData($metric_modify_query_data); |
| 83 |
} |
| 84 |
|
| 85 |
// Check if we have extra options to set for the Metric |
| 86 |
$metric_options = $other_data->get('options', []); |
| 87 |
if ($metric_options) |
| 88 |
{ |
| 89 |
$metric->setOptions($metric_options); |
| 90 |
} |
| 91 |
|
| 92 |
// get the metric data |
| 93 |
$metric->setData(); |
| 94 |
|
| 95 |
// store them |
| 96 |
if (count($metrics) == 1) |
| 97 |
{ |
| 98 |
$data = $metric->getData(); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
$data[$metric->getType()] = $metric->getData(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $data; |
| 107 |
} |
| 108 |
|
| 109 |
public static function setConfigurationData($data) |
| 110 |
{ |
| 111 |
if (self::$configuration_data) |
| 112 |
{ |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
self::$configuration_data = $data; |
| 117 |
} |
| 118 |
} |