| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.16 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 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; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class MetricData |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Toolbar |
| 23 |
* |
| 24 |
* @var Toolbar |
| 25 |
*/ |
| 26 |
private $toolbar; |
| 27 |
|
| 28 |
/** |
| 29 |
* Analytics DB Object |
| 30 |
* |
| 31 |
* @var DB |
| 32 |
*/ |
| 33 |
private $db; |
| 34 |
|
| 35 |
/** |
| 36 |
* Metric |
| 37 |
* |
| 38 |
* @var Metric |
| 39 |
*/ |
| 40 |
private $metric; |
| 41 |
|
| 42 |
/** |
| 43 |
* Query Data that will be used to merge with |
| 44 |
* the currently used query data. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
private $modify_query_data = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* MetricData Constructor |
| 52 |
* |
| 53 |
* @param Metric $metric |
| 54 |
* @param Toolbar $toolbar |
| 55 |
* @param DB $db |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function __construct($metric = null, $toolbar = null, $db = null) |
| 60 |
{ |
| 61 |
$this->metric = $metric; |
| 62 |
|
| 63 |
if (!$toolbar) |
| 64 |
{ |
| 65 |
$toolbar = new Toolbar(); |
| 66 |
} |
| 67 |
$this->toolbar = $toolbar; |
| 68 |
|
| 69 |
if (!$db) |
| 70 |
{ |
| 71 |
$db = new DB(); |
| 72 |
} |
| 73 |
$this->db = $db; |
| 74 |
} |
| 75 |
|
| 76 |
private function getToolbarItems() |
| 77 |
{ |
| 78 |
return $this->toolbar->getToolbarItems(); |
| 79 |
} |
| 80 |
|
| 81 |
private function getSelectedToolbarItems() |
| 82 |
{ |
| 83 |
return $this->toolbar->getSelectedToolbarItems(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the payload used to retrieve data for current period |
| 88 |
* |
| 89 |
* @param mixed $previous_period |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function getResultsQueryData($previous_period = false) |
| 94 |
{ |
| 95 |
$query_data = []; |
| 96 |
|
| 97 |
$toolbar_items = $this->getToolbarItems(); |
| 98 |
|
| 99 |
foreach ($this->getSelectedToolbarItems() as $key => &$value) |
| 100 |
{ |
| 101 |
if (empty($value)) |
| 102 |
{ |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
$suffix = ''; |
| 107 |
|
| 108 |
// get previous period |
| 109 |
if ($previous_period) |
| 110 |
{ |
| 111 |
if ($key === 'date') |
| 112 |
{ |
| 113 |
$value = $toolbar_items['date']->getPreviousPeriodDateObject($value); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
$suffix .= '_prev_period'; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$method = $value['method']; |
| 122 |
|
| 123 |
$data = [ |
| 124 |
'type' => $key, |
| 125 |
'method' => $method, |
| 126 |
'options_key' => $value['options_key'] . $suffix, |
| 127 |
'value' => isset($value['value']) ? $value['value'] : '', |
| 128 |
'previous_period' => $previous_period |
| 129 |
]; |
| 130 |
|
| 131 |
// get query data for each selected toolbar item |
| 132 |
$toolbarItemData = $toolbar_items[$key]->getQueryData($this->metric, $query_data, $data); |
| 133 |
|
| 134 |
/** |
| 135 |
* Get the manipulated query data from the toolbar item and use it. |
| 136 |
* The toolbar may alter the query data for its own purposes. |
| 137 |
*/ |
| 138 |
if (isset($toolbarItemData['query_data'])) |
| 139 |
{ |
| 140 |
// we receive the query data in case we have modified it within each toolbar item |
| 141 |
$query_data = $toolbarItemData['query_data']; |
| 142 |
|
| 143 |
unset($toolbarItemData['query_data']); |
| 144 |
} |
| 145 |
|
| 146 |
// merge the query data |
| 147 |
$query_data = array_replace_recursive($toolbarItemData, $query_data); |
| 148 |
} |
| 149 |
|
| 150 |
if ($this->metric) |
| 151 |
{ |
| 152 |
$query_data = apply_filters('firebox/analytics/metric/' . $this->metric->getType() . '/filter_query_data', $query_data); |
| 153 |
} |
| 154 |
|
| 155 |
return $query_data; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get Metric Data |
| 160 |
* |
| 161 |
* @param bool $previous_period |
| 162 |
* @param array $payload |
| 163 |
* |
| 164 |
* @return array |
| 165 |
*/ |
| 166 |
public function getData($previous_period = false, $payload = null) |
| 167 |
{ |
| 168 |
if (!$payload) |
| 169 |
{ |
| 170 |
$payload = $this->getResultsQueryData($previous_period); |
| 171 |
} |
| 172 |
|
| 173 |
if (!isset($payload['select'])) |
| 174 |
{ |
| 175 |
return []; |
| 176 |
} |
| 177 |
|
| 178 |
if (isset($this->modify_query_data)) |
| 179 |
{ |
| 180 |
$payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true)); |
| 181 |
} |
| 182 |
|
| 183 |
$selected_toolbar_items = $this->getSelectedToolbarItems(); |
| 184 |
|
| 185 |
return Helpers\MetricHelper::prepareDates($selected_toolbar_items['date']['options_key'], $this->db->getResults($payload)); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get percentage change over previous period(month) |
| 190 |
* |
| 191 |
* @param int $current_total |
| 192 |
* |
| 193 |
* @return float |
| 194 |
*/ |
| 195 |
public function getPercentageChangeWithPreviousPeriod($current_total) |
| 196 |
{ |
| 197 |
if ($current_total <= 0) |
| 198 |
{ |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
$toolbar_items = $this->getToolbarItems(); |
| 203 |
|
| 204 |
$selected_toolbar_items = $this->getSelectedToolbarItems(); |
| 205 |
|
| 206 |
$previous_period = $toolbar_items['date']->getPreviousPeriodDateObject($selected_toolbar_items['date']); |
| 207 |
|
| 208 |
$selected_toolbar_items['date'] = $previous_period; |
| 209 |
|
| 210 |
$payload = $this->getResultsQueryData(true); |
| 211 |
|
| 212 |
$results = $this->db->getResults($payload); |
| 213 |
|
| 214 |
$result = isset($results[0]) ? (array) $results[0] : null; |
| 215 |
|
| 216 |
$previous_period_total = isset($result['total']) ? (int) $result['total'] : 0; |
| 217 |
|
| 218 |
if ($previous_period_total <= 0) |
| 219 |
{ |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
$percentage = (($current_total - $previous_period_total) / $previous_period_total) * 100; |
| 224 |
|
| 225 |
return $percentage; |
| 226 |
} |
| 227 |
|
| 228 |
public function setModifyQueryData($data) |
| 229 |
{ |
| 230 |
$this->modify_query_data = $data; |
| 231 |
} |
| 232 |
|
| 233 |
public function setToolbar($toolbar) |
| 234 |
{ |
| 235 |
$this->toolbar = $toolbar; |
| 236 |
} |
| 237 |
} |