| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.4 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 |
use FPFramework\Base\Ui\Tabs; |
| 20 |
use FPFramework\Base\FieldsParser; |
| 21 |
|
| 22 |
abstract class ToolbarItem |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Popup Settings |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $settings; |
| 30 |
|
| 31 |
/** |
| 32 |
* Selected Toolbar Items |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
protected $selectedToolbarItems; |
| 37 |
|
| 38 |
/** |
| 39 |
* Whether we are fetching data for previous period |
| 40 |
* |
| 41 |
* @var boolean |
| 42 |
*/ |
| 43 |
protected $fetchPreviousPeriodData; |
| 44 |
|
| 45 |
/** |
| 46 |
* Analytics configuration |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
protected $configuration; |
| 51 |
|
| 52 |
/** |
| 53 |
* Manipulating metric |
| 54 |
* |
| 55 |
* @var Metric |
| 56 |
*/ |
| 57 |
protected $metric; |
| 58 |
|
| 59 |
/** |
| 60 |
* Current query data that we are manipulating. |
| 61 |
* This is passed to each toolbar item class that it |
| 62 |
* can use to modify. |
| 63 |
* |
| 64 |
* @var array |
| 65 |
*/ |
| 66 |
protected $current_query_data; |
| 67 |
|
| 68 |
/** |
| 69 |
* DB |
| 70 |
* |
| 71 |
* @var DB |
| 72 |
*/ |
| 73 |
protected $db; |
| 74 |
|
| 75 |
public function __construct($db = null) |
| 76 |
{ |
| 77 |
if (!$db) |
| 78 |
{ |
| 79 |
$db = new DB(); |
| 80 |
} |
| 81 |
$this->db = $db; |
| 82 |
|
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Setup variables for the toolbar item |
| 88 |
* |
| 89 |
* @param array $configuration |
| 90 |
* @param array $selectedToolbarItems |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function setupVariables($configuration, $selectedToolbarItems) |
| 95 |
{ |
| 96 |
$this->configuration = $configuration; |
| 97 |
$this->selectedToolbarItems = $selectedToolbarItems; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Holds the query data needed to return box log data for this type |
| 102 |
* |
| 103 |
* @param array $$current_query_data |
| 104 |
* @param array $payload |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function getQueryData($metric, $current_query_data, $payload) |
| 109 |
{ |
| 110 |
$this->metric = $metric; |
| 111 |
|
| 112 |
// get the current query data to manipulate |
| 113 |
$this->current_query_data = $current_query_data; |
| 114 |
|
| 115 |
if (!$payload && !is_array($payload) && !isset($payload['method'])) |
| 116 |
{ |
| 117 |
return []; |
| 118 |
} |
| 119 |
|
| 120 |
$method = $payload['method']; |
| 121 |
|
| 122 |
if (!is_string($method)) |
| 123 |
{ |
| 124 |
return []; |
| 125 |
} |
| 126 |
|
| 127 |
if (!in_array(strtolower($method), ['filter', 'compare'])) |
| 128 |
{ |
| 129 |
return []; |
| 130 |
} |
| 131 |
|
| 132 |
if (isset($payload['previous_period'])) |
| 133 |
{ |
| 134 |
$this->fetchPreviousPeriodData = $payload['previous_period']; |
| 135 |
} |
| 136 |
|
| 137 |
// Send back the WHERE and the manipulated query data |
| 138 |
$data = [ |
| 139 |
'where' => $this->getWhere($payload), |
| 140 |
'query_data' => $this->current_query_data |
| 141 |
]; |
| 142 |
|
| 143 |
return $data; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Return the popup title |
| 148 |
* |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
public function getPopupTitle() |
| 152 |
{ |
| 153 |
return fpframework()->_($this->popup_title); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return the results table title |
| 158 |
* |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function getResultsTableTitle() |
| 162 |
{ |
| 163 |
return $this->getPopupTitle(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Return the plural popup title |
| 168 |
* |
| 169 |
* @return array |
| 170 |
*/ |
| 171 |
public function getPluralPopupTitle() |
| 172 |
{ |
| 173 |
return fpframework()->_($this->popup_title_plural); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the CURDATE() minus interval of 1 month if we are fetching previous period data |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
protected function getSQLCurrentDateOrWithMinusInterval1Month() |
| 184 |
{ |
| 185 |
return 'CURDATE()' . ($this->fetchPreviousPeriodData ? ' - INTERVAL 1 MONTH' : ''); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the CURDATE() minus interval of 1 week if we are fetching previous period data |
| 190 |
* |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
protected function getSQLCurrentDateOrWithMinusInterval1Week() |
| 194 |
{ |
| 195 |
return 'CURDATE()' . ($this->fetchPreviousPeriodData ? ' - INTERVAL 1 WEEK' : ''); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the CURDATE() minus interval of 1 year |
| 200 |
* |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
protected function getSQLCurrentDateWithMinusInterval1Year() |
| 204 |
{ |
| 205 |
return 'CURDATE() - INTERVAL 1 YEAR'; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Returns the CURDATE() minus interval of 1 week |
| 210 |
* |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
protected function getSQLCurrentDateMinusInterval1Week() |
| 214 |
{ |
| 215 |
return 'CURDATE() - INTERVAL 1 WEEK'; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns the CURDATE() |
| 220 |
* |
| 221 |
* @return string |
| 222 |
*/ |
| 223 |
protected function getSQLCurrentDate() |
| 224 |
{ |
| 225 |
return 'CURDATE()'; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Calculate the current date |
| 230 |
* |
| 231 |
* @return string |
| 232 |
*/ |
| 233 |
protected function calculateSQLCurrentDate() |
| 234 |
{ |
| 235 |
if (!is_string($this->fetchPreviousPeriodData)) |
| 236 |
{ |
| 237 |
return $this->getSQLCurrentDate(); |
| 238 |
} |
| 239 |
|
| 240 |
if ($this->fetchPreviousPeriodData == 'month') |
| 241 |
{ |
| 242 |
return $this->getSQLCurrentDateOrWithMinusInterval1Month(); |
| 243 |
} |
| 244 |
|
| 245 |
return $this->getSQLCurrentDate(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Returns the WHERE statement depending on the filtering method |
| 250 |
* |
| 251 |
* @param array $payload |
| 252 |
* |
| 253 |
* @return mixed |
| 254 |
*/ |
| 255 |
protected function getWhere($payload) |
| 256 |
{ |
| 257 |
if (!isset($payload['method'])) |
| 258 |
{ |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
$methodName = 'get' . $payload['method'] . 'where'; |
| 263 |
|
| 264 |
if (!method_exists($this, $methodName)) |
| 265 |
{ |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
return $this->$methodName($payload); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
abstract protected function getFilterWhere($payload); |
| 274 |
abstract protected function getCompareWhere($payload); |
| 275 |
} |