| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.5 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics\QueryBuilders; |
| 13 |
|
| 14 |
use FireBox\Core\Analytics\QueryBuilders\Interfaces\QueryStrategyInterface; |
| 15 |
|
| 16 |
if (!defined('ABSPATH')) |
| 17 |
{ |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
abstract class BaseQueryStrategy implements QueryStrategyInterface |
| 22 |
{ |
| 23 |
protected $metric; |
| 24 |
|
| 25 |
public function __construct($metric) |
| 26 |
{ |
| 27 |
$this->metric = $metric; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the FROM/JOIN clauses for the query |
| 32 |
* Default implementation - should be overridden by subclasses for specific table structures |
| 33 |
*/ |
| 34 |
public function getFromAndJoins(): string |
| 35 |
{ |
| 36 |
return $this->getDefaultFromAndJoins(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get the WHERE period clause for the query |
| 41 |
* Uses the configured date column and actual date values |
| 42 |
*/ |
| 43 |
public function getWherePeriod(): string |
| 44 |
{ |
| 45 |
$options = $this->metric->getOptions(); |
| 46 |
|
| 47 |
if (!$options['start_date'] ?? null || !$options['end_date'] ?? null) |
| 48 |
{ |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
$dateColumn = $this->getDateColumn(); |
| 53 |
$startDate = $options['start_date']; |
| 54 |
$endDate = $options['end_date']; |
| 55 |
|
| 56 |
return " AND {$dateColumn} BETWEEN '{$startDate}' AND '{$endDate}'"; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the filters clause for the query |
| 61 |
* Uses the configured table alias for filtering |
| 62 |
*/ |
| 63 |
public function getFilters(): string |
| 64 |
{ |
| 65 |
$filters = $this->metric->getFilters(); |
| 66 |
|
| 67 |
if (empty($filters)) |
| 68 |
{ |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
// Build filters using the FilterBuilder with correct table aliases |
| 73 |
$table_aliases = $this->getTableAliases(); |
| 74 |
$filter_builder = new \FireBox\Core\Analytics\Filters\FilterBuilder([], $table_aliases); |
| 75 |
$result = $filter_builder->build($filters); |
| 76 |
|
| 77 |
return $result['sql']; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get table aliases for FilterBuilder |
| 82 |
* Can be overridden by subclasses to provide metric-specific aliases |
| 83 |
*/ |
| 84 |
protected function getTableAliases(): array |
| 85 |
{ |
| 86 |
// Default aliases for Views/ConversionRate pattern |
| 87 |
return [ |
| 88 |
'logs' => 'l', |
| 89 |
'details' => 'bld' |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the LIMIT/OFFSET clause for the query |
| 95 |
*/ |
| 96 |
public function getLimitOffset(): string |
| 97 |
{ |
| 98 |
$limit = $this->getLimit(); |
| 99 |
$offset = $this->getOffset(); |
| 100 |
|
| 101 |
$result = ''; |
| 102 |
if ($limit) { |
| 103 |
$result .= $limit; |
| 104 |
} |
| 105 |
if ($offset) { |
| 106 |
$result .= ' ' . $offset; |
| 107 |
} |
| 108 |
|
| 109 |
return $result; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the LIMIT clause |
| 114 |
*/ |
| 115 |
protected function getLimit(): string |
| 116 |
{ |
| 117 |
$limit = $this->metric->getOptions()['limit'] ?? null; |
| 118 |
|
| 119 |
if (!is_scalar($limit)) |
| 120 |
{ |
| 121 |
return ''; |
| 122 |
} |
| 123 |
|
| 124 |
return "LIMIT {$limit}"; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the OFFSET clause |
| 129 |
*/ |
| 130 |
protected function getOffset(): string |
| 131 |
{ |
| 132 |
$offset = $this->metric->getOptions()['offset'] ?? null; |
| 133 |
|
| 134 |
if (!is_scalar($offset)) |
| 135 |
{ |
| 136 |
return ''; |
| 137 |
} |
| 138 |
|
| 139 |
return "OFFSET {$offset}"; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Default HAVING implementation - can be overridden |
| 144 |
*/ |
| 145 |
public function getHaving(): string |
| 146 |
{ |
| 147 |
return ''; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Default ORDER BY implementation - can be overridden |
| 152 |
*/ |
| 153 |
public function getOrderBy(): string |
| 154 |
{ |
| 155 |
$dateColumn = $this->getDateColumn(); |
| 156 |
return "ORDER BY {$dateColumn} DESC"; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Default WHERE implementation - can be overridden |
| 161 |
*/ |
| 162 |
public function getWhere(): string |
| 163 |
{ |
| 164 |
return ''; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Default GROUP BY implementation - can be overridden |
| 169 |
*/ |
| 170 |
public function getGroupBy(): string |
| 171 |
{ |
| 172 |
return ''; |
| 173 |
} |
| 174 |
|
| 175 |
// Abstract methods that subclasses must implement to define their specific behavior |
| 176 |
|
| 177 |
/** |
| 178 |
* Get the default FROM/JOIN structure for this metric type |
| 179 |
* Should be implemented by metric-specific base classes |
| 180 |
*/ |
| 181 |
protected abstract function getDefaultFromAndJoins(): string; |
| 182 |
|
| 183 |
/** |
| 184 |
* Get the date column used for period filtering and ordering |
| 185 |
* Should be implemented by metric-specific base classes (e.g., 'l.date', 'bld.date') |
| 186 |
*/ |
| 187 |
protected abstract function getDateColumn(): string; |
| 188 |
|
| 189 |
/** |
| 190 |
* Must be implemented by concrete strategy classes |
| 191 |
*/ |
| 192 |
public abstract function getSelect(): string; |
| 193 |
|
| 194 |
/** |
| 195 |
* Template for standard ORDER BY with total DESC fallback and options support |
| 196 |
*/ |
| 197 |
protected function getTotalOrderBy(): string |
| 198 |
{ |
| 199 |
$options = $this->metric->getOptions(); |
| 200 |
|
| 201 |
if (isset($options['orderby'])) |
| 202 |
{ |
| 203 |
return 'ORDER BY ' . $options['orderby']; |
| 204 |
} |
| 205 |
|
| 206 |
return 'ORDER BY total DESC'; |
| 207 |
} |
| 208 |
} |
| 209 |
|