Hello guys, I hope everyone is well and healthy
I was on GitHub and found that you opened an similar issue (but to me was in the Time Connected Report) that I was able to solve a few months ago (in V3.3.*, And V4), but I forgot to share this with the community:
Initial Problem
The SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty error indicated that the SQL query was either empty or had a syntax error. This issue occurred when the user was not of type 1 (superadmin). The logic for constructing the SQL query was not correctly handling cases for non-superadmin users, resulting in an empty SQL query.
This was the approach we took here to fix the issue:
→ lib → Report → TimeConnected.php
//
// Output Results
// --------------
if ($this->getUser()->isSuperAdmin()) {
$sql = 'SELECT displayId, display FROM display WHERE 1 = 1';
if (count($displayIds) > 0) {
$sql .= ' AND displayId IN (' . implode(',', $displayIds) . ')';
}
} else {
// Construir a consulta SQL para usuários que não são superadmin
$sql = 'SELECT displayId, display FROM display WHERE 1 = 1';
// Adicionar lógica adicional aqui se necessário para filtrar displays para usuários não superadmin
if (count($displayIds) > 0) {
$sql .= ' AND displayId IN (' . implode(',', $displayIds) . ')';
}
// Verificar se a consulta SQL não está vazia
if (trim($sql) === 'SELECT displayId, display FROM display WHERE 1 = 1') {
// Se a consulta estiver vazia, lançar uma exceção ou retornar um erro apropriado
throw new \Exception("A consulta SQL está vazia para usuários não superadmin");
}
}
// Executar a consulta SQL
$timeConnected = [];
$displays = [];
$i = 0;
$key = 0;
foreach ($this->store->select($sql, []) as $row) {
$displayId = intval($row['displayId']);
$displayName = $row['display'];
// Set the display name for the displays in this row.
$displays[$key][$displayId] = $displayName;
// Go through each period
foreach ($result['periods'] as $resPeriods) {
//
$temp = $resPeriods['customLabel'];
if (empty($timeConnected[$temp][$displayId]['percent'])) {
$timeConnected[$key][$temp][$displayId]['percent'] = 100;
}
if (empty($timeConnected[$temp][$displayId]['label'])) {
$timeConnected[$key][$temp][$displayId]['label'] = $resPeriods['customLabel'];
}
foreach ($result['result'] as $res) {
if ($res['displayId'] == $displayId && $res['customLabel'] == $resPeriods['customLabel']) {
$timeConnected[$key][$temp][$displayId]['percent'] = 100 - round($res['percent'], 2);
$timeConnected[$key][$temp][$displayId]['label'] = $resPeriods['customLabel'];
} else {
continue;
}
}
}
$i++;
if ($i >= 3) {
$i = 0;
$key++;
}
}