| 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; |
| 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 |
* Toolbar Items |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $toolbar_items; |
| 34 |
|
| 35 |
/** |
| 36 |
* Selected Toolbar items |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
private $selected_toolbar_items; |
| 41 |
|
| 42 |
/** |
| 43 |
* Analytics DB Object |
| 44 |
* |
| 45 |
* @var DB |
| 46 |
*/ |
| 47 |
private $db; |
| 48 |
|
| 49 |
/** |
| 50 |
* Metric |
| 51 |
* |
| 52 |
* @var Metric |
| 53 |
*/ |
| 54 |
private $metric; |
| 55 |
|
| 56 |
/** |
| 57 |
* Query Data that will be used to merge with |
| 58 |
* the currently used query data. |
| 59 |
* |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
private $modify_query_data; |
| 63 |
|
| 64 |
/** |
| 65 |
* MetricData Constructor |
| 66 |
* |
| 67 |
* @param Metric $metric |
| 68 |
* @param Toolbar $toolbar |
| 69 |
* @param DB $db |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function __construct($metric = null, $toolbar = null, $db = null) |
| 74 |
{ |
| 75 |
$this->metric = $metric; |
| 76 |
|
| 77 |
if (!$toolbar) |
| 78 |
{ |
| 79 |
$toolbar = new Toolbar(); |
| 80 |
} |
| 81 |
$this->toolbar = $toolbar; |
| 82 |
|
| 83 |
if (!$db) |
| 84 |
{ |
| 85 |
$db = new DB(); |
| 86 |
} |
| 87 |
$this->db = $db; |
| 88 |
|
| 89 |
$this->toolbar_items = $this->toolbar->getToolbarItems(); |
| 90 |
$this->selected_toolbar_items = $this->toolbar->getSelectedToolbarItems(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the payload used to retrieve data for current period |
| 95 |
* |
| 96 |
* @param mixed $previous_period |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function getResultsQueryData($previous_period = false) |
| 101 |
{ |
| 102 |
$query_data = []; |
| 103 |
|
| 104 |
foreach ($this->selected_toolbar_items as $key => &$value) |
| 105 |
{ |
| 106 |
if (empty($value)) |
| 107 |
{ |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$suffix = ''; |
| 112 |
|
| 113 |
// get previous period |
| 114 |
if ($previous_period) |
| 115 |
{ |
| 116 |
if ($key == 'date') |
| 117 |
{ |
| 118 |
$value = $this->toolbar_items['date']->getPreviousPeriodDateObject($value); |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
$suffix .= '_prev_period'; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$method = $value['method']; |
| 127 |
|
| 128 |
$data = [ |
| 129 |
'type' => $key, |
| 130 |
'method' => $method, |
| 131 |
'options_key' => $value['options_key'] . $suffix, |
| 132 |
'value' => isset($value['value']) ? $value['value'] : '', |
| 133 |
'previous_period' => $previous_period |
| 134 |
]; |
| 135 |
|
| 136 |
// get query data for each selected toolbar item |
| 137 |
$toolbarItemData = $this->toolbar_items[$key]->getQueryData($this->metric, $query_data, $data); |
| 138 |
|
| 139 |
/** |
| 140 |
* Get the manipulated query data from the toolbar item and use it. |
| 141 |
* The toolbar may alter the query data for its own purposes. |
| 142 |
*/ |
| 143 |
if (isset($toolbarItemData['query_data'])) |
| 144 |
{ |
| 145 |
// we receive the query data in case we have modified it within each toolbar item |
| 146 |
$query_data = $toolbarItemData['query_data']; |
| 147 |
|
| 148 |
unset($toolbarItemData['query_data']); |
| 149 |
} |
| 150 |
|
| 151 |
// merge the query data |
| 152 |
$query_data = array_merge_recursive($toolbarItemData, $query_data); |
| 153 |
} |
| 154 |
|
| 155 |
return $query_data; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get Current Metric Data |
| 160 |
* |
| 161 |
* @return int |
| 162 |
*/ |
| 163 |
public function getCurrent($payload = null) |
| 164 |
{ |
| 165 |
if (!$payload) |
| 166 |
{ |
| 167 |
$payload = $this->getResultsQueryData(); |
| 168 |
} |
| 169 |
|
| 170 |
if (!isset($payload['select'])) |
| 171 |
{ |
| 172 |
return []; |
| 173 |
} |
| 174 |
|
| 175 |
if (isset($this->modify_query_data)) |
| 176 |
{ |
| 177 |
$payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true)); |
| 178 |
} |
| 179 |
|
| 180 |
return $this->db->getResults($payload); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get Last Period Metric Data |
| 185 |
* |
| 186 |
* @return int |
| 187 |
*/ |
| 188 |
public function getLastPeriod($payload = null) |
| 189 |
{ |
| 190 |
if (!$payload) |
| 191 |
{ |
| 192 |
$payload = $this->getResultsQueryData(true); |
| 193 |
} |
| 194 |
|
| 195 |
if (!isset($payload['select'])) |
| 196 |
{ |
| 197 |
return []; |
| 198 |
} |
| 199 |
|
| 200 |
if (isset($this->modify_query_data)) |
| 201 |
{ |
| 202 |
$payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true)); |
| 203 |
} |
| 204 |
|
| 205 |
return $this->db->getResults($payload); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get percentage change over previous period(month) |
| 210 |
* |
| 211 |
* @param int $current_total |
| 212 |
* |
| 213 |
* @return float |
| 214 |
*/ |
| 215 |
public function getPercentageChangeWithPreviousPeriod($current_total) |
| 216 |
{ |
| 217 |
if ($current_total <= 0) |
| 218 |
{ |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
$previous_period = $this->toolbar_items['date']->getPreviousPeriodDateObject($this->selected_toolbar_items['date']); |
| 223 |
|
| 224 |
$this->selected_toolbar_items['date'] = $previous_period; |
| 225 |
|
| 226 |
$payload = $this->getResultsQueryData(true); |
| 227 |
|
| 228 |
$results = $this->db->getResults($payload); |
| 229 |
|
| 230 |
$previous_period_total = isset($results[0]->total) ? (int) $results[0]->total : 0; |
| 231 |
|
| 232 |
if ($previous_period_total <= 0) |
| 233 |
{ |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
$percentage = (($current_total - $previous_period_total) / $previous_period_total) * 100; |
| 238 |
|
| 239 |
return $percentage; |
| 240 |
} |
| 241 |
|
| 242 |
public function setSelectedToolbarItems($items) |
| 243 |
{ |
| 244 |
$this->selected_toolbar_items = $items; |
| 245 |
} |
| 246 |
|
| 247 |
public function getSelectedItems() |
| 248 |
{ |
| 249 |
return $this->selected_toolbar_items; |
| 250 |
} |
| 251 |
|
| 252 |
public function setModifyQueryData($data) |
| 253 |
{ |
| 254 |
$this->modify_query_data = $data; |
| 255 |
} |
| 256 |
|
| 257 |
public function getModifyQueryData() |
| 258 |
{ |
| 259 |
return $this->modify_query_data; |
| 260 |
} |
| 261 |
} |