| @@ -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; |
| @@ -24,22 +24,8 @@ | ||
| 24 | 24 | * @var Toolbar |
| 25 | 25 | */ |
| 26 | 26 | private $toolbar; |
| 27 | 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 | 28 | /** |
| 43 | 29 | * Analytics DB Object |
| 44 | 30 | * |
| 45 | 31 | * @var DB |
| @@ -58,9 +44,9 @@ | ||
| 58 | 44 | * the currently used query data. |
| 59 | 45 | * |
| 60 | 46 | * @var array |
| 61 | 47 | */ |
| 62 | - private $modify_query_data; | |
| 48 | + private $modify_query_data = []; | |
| 63 | 49 | |
| 64 | 50 | /** |
| 65 | 51 | * MetricData Constructor |
| 66 | 52 | * |
| @@ -84,13 +70,20 @@ | ||
| 84 | 70 | { |
| 85 | 71 | $db = new DB(); |
| 86 | 72 | } |
| 87 | 73 | $this->db = $db; |
| 88 | - | |
| 89 | - $this->toolbar_items = $this->toolbar->getToolbarItems(); | |
| 90 | - $this->selected_toolbar_items = $this->toolbar->getSelectedToolbarItems(); | |
| 91 | 74 | } |
| 92 | - | |
| 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 | + | |
| 93 | 86 | /** |
| 94 | 87 | * Returns the payload used to retrieve data for current period |
| 95 | 88 | * |
| 96 | 89 | * @param mixed $previous_period |
| @@ -99,10 +92,12 @@ | ||
| 99 | 92 | */ |
| 100 | 93 | public function getResultsQueryData($previous_period = false) |
| 101 | 94 | { |
| 102 | 95 | $query_data = []; |
| 96 | + | |
| 97 | + $toolbar_items = $this->getToolbarItems(); | |
| 103 | 98 | |
| 104 | - foreach ($this->selected_toolbar_items as $key => &$value) | |
| 99 | + foreach ($this->getSelectedToolbarItems() as $key => &$value) | |
| 105 | 100 | { |
| 106 | 101 | if (empty($value)) |
| 107 | 102 | { |
| 108 | 103 | continue; |
| @@ -112,11 +107,11 @@ | ||
| 112 | 107 | |
| 113 | 108 | // get previous period |
| 114 | 109 | if ($previous_period) |
| 115 | 110 | { |
| 116 | - if ($key == 'date') | |
| 111 | + if ($key === 'date') | |
| 117 | 112 | { |
| 118 | - $value = $this->toolbar_items['date']->getPreviousPeriodDateObject($value); | |
| 113 | + $value = $toolbar_items['date']->getPreviousPeriodDateObject($value); | |
| 119 | 114 | } |
| 120 | 115 | else |
| 121 | 116 | { |
| 122 | 117 | $suffix .= '_prev_period'; |
| @@ -133,9 +128,9 @@ | ||
| 133 | 128 | 'previous_period' => $previous_period |
| 134 | 129 | ]; |
| 135 | 130 | |
| 136 | 131 | // get query data for each selected toolbar item |
| 137 | - $toolbarItemData = $this->toolbar_items[$key]->getQueryData($this->metric, $query_data, $data); | |
| 132 | + $toolbarItemData = $toolbar_items[$key]->getQueryData($this->metric, $query_data, $data); | |
| 138 | 133 | |
| 139 | 134 | /** |
| 140 | 135 | * Get the manipulated query data from the toolbar item and use it. |
| 141 | 136 | * The toolbar may alter the query data for its own purposes. |
| @@ -148,24 +143,32 @@ | ||
| 148 | 143 | unset($toolbarItemData['query_data']); |
| 149 | 144 | } |
| 150 | 145 | |
| 151 | 146 | // merge the query data |
| 152 | - $query_data = array_merge_recursive($toolbarItemData, $query_data); | |
| 147 | + $query_data = array_replace_recursive($toolbarItemData, $query_data); | |
| 153 | 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 | 154 | |
| 155 | 155 | return $query_data; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | /** |
| 159 | - * Get Current Metric Data | |
| 159 | + * Get Metric Data | |
| 160 | 160 | * |
| 161 | - * @return int | |
| 161 | + * @param bool $previous_period | |
| 162 | + * @param array $payload | |
| 163 | + * | |
| 164 | + * @return array | |
| 162 | 165 | */ |
| 163 | - public function getCurrent($payload = null) | |
| 166 | + public function getData($previous_period = false, $payload = null) | |
| 164 | 167 | { |
| 165 | 168 | if (!$payload) |
| 166 | 169 | { |
| 167 | - $payload = $this->getResultsQueryData(); | |
| 170 | + $payload = $this->getResultsQueryData($previous_period); | |
| 168 | 171 | } |
| 169 | 172 | |
| 170 | 173 | if (!isset($payload['select'])) |
| 171 | 174 | { |
| @@ -176,37 +179,14 @@ | ||
| 176 | 179 | { |
| 177 | 180 | $payload = array_replace_recursive($payload, json_decode(json_encode($this->modify_query_data), true)); |
| 178 | 181 | } |
| 179 | 182 | |
| 180 | - return $this->db->getResults($payload); | |
| 183 | + $selected_toolbar_items = $this->getSelectedToolbarItems(); | |
| 184 | + | |
| 185 | + return Helpers\MetricHelper::prepareDates($selected_toolbar_items['date']['options_key'], $this->db->getResults($payload)); | |
| 181 | 186 | } |
| 182 | 187 | |
| 183 | 188 | /** |
| 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 | 189 | * Get percentage change over previous period(month) |
| 210 | 190 | * |
| 211 | 191 | * @param int $current_total |
| 212 | 192 | * |
| @@ -218,17 +198,23 @@ | ||
| 218 | 198 | { |
| 219 | 199 | return 0; |
| 220 | 200 | } |
| 221 | 201 | |
| 222 | - $previous_period = $this->toolbar_items['date']->getPreviousPeriodDateObject($this->selected_toolbar_items['date']); | |
| 202 | + $toolbar_items = $this->getToolbarItems(); | |
| 223 | 203 | |
| 224 | - $this->selected_toolbar_items['date'] = $previous_period; | |
| 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; | |
| 225 | 209 | |
| 226 | 210 | $payload = $this->getResultsQueryData(true); |
| 227 | 211 | |
| 228 | 212 | $results = $this->db->getResults($payload); |
| 213 | + | |
| 214 | + $result = isset($results[0]) ? (array) $results[0] : null; | |
| 229 | 215 | |
| 230 | - $previous_period_total = isset($results[0]->total) ? (int) $results[0]->total : 0; | |
| 216 | + $previous_period_total = isset($result['total']) ? (int) $result['total'] : 0; | |
| 231 | 217 | |
| 232 | 218 | if ($previous_period_total <= 0) |
| 233 | 219 | { |
| 234 | 220 | return 0; |
| @@ -238,24 +224,14 @@ | ||
| 238 | 224 | |
| 239 | 225 | return $percentage; |
| 240 | 226 | } |
| 241 | 227 | |
| 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 | 228 | public function setModifyQueryData($data) |
| 253 | 229 | { |
| 254 | 230 | $this->modify_query_data = $data; |
| 255 | 231 | } |
| 256 | 232 | |
| 257 | - public function getModifyQueryData() | |
| 233 | + public function setToolbar($toolbar) | |
| 258 | 234 | { |
| 259 | - return $this->modify_query_data; | |
| 235 | + $this->toolbar = $toolbar; | |
| 260 | 236 | } |
| 261 | 237 | } |