Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
446 righe
15 KiB
446 righe
15 KiB
<? |
|
include_once __DIR__ . DIRECTORY_SEPARATOR . "reportClasses.php"; |
|
Class reportManager { |
|
public $from; |
|
public $to; |
|
public $group; |
|
public $filters; |
|
public $tokenElaborazione; |
|
public $generalSettings; |
|
public $richieste; |
|
public $errors; |
|
private $data; |
|
private $ente; |
|
private $root; |
|
private $enrichment; |
|
private $graphs; |
|
|
|
public static function moduliDisponibili() { |
|
return [ |
|
"report" => "reportCentrale", |
|
"albo_fornitori" => "reportAlboFornitori", |
|
"gare" => "reportGare", |
|
"pcp" => "reportPCP", |
|
"concorsi" => "reportConcorsi", |
|
]; |
|
} |
|
|
|
public function __construct() { |
|
global $root; |
|
$this->ente = (!empty($_SESSION["ente"])) ? $_SESSION["ente"]->getInfo() : null; |
|
$this->root = $root; |
|
if (!empty($this->ente)) { |
|
$enrichmentClassPath = Ente::getTenantExtensionPath($this->ente["codice"]) . DIRECTORY_SEPARATOR . "reportEnrichment.class.php"; |
|
if (file_exists($enrichmentClassPath)) { |
|
require_once $enrichmentClassPath; |
|
$class_name = "reportEnrichment{$this->ente["codice"]}"; |
|
$this->enrichment = new $class_name(); |
|
} |
|
} |
|
$this->generalSettings = $this->getReportDisponibili(); |
|
} |
|
|
|
public function getData(array $moduli) { |
|
$this->tokenElaborazione = base64_encode(generateStrongPassword()); |
|
foreach($moduli AS $modulo) { |
|
$settings = $this->generalSettings[$modulo] ?? null; |
|
if (!empty($settings)) { |
|
$base = $settings["class"]::{$settings["method"]}($this->from,$this->to,$this->filters[$modulo] ?? null); |
|
if (!empty($base)) { |
|
if (!empty($this->enrichment) && method_exists($this->enrichment,"addData")) { |
|
$base = $this->enrichment->addData($modulo,$base); |
|
} |
|
$this->data[$modulo] = $base; |
|
} |
|
} |
|
} |
|
$_SESSION["reportManagerData"][$this->tokenElaborazione] = $this->data; |
|
} |
|
|
|
private function groupData(array $rows,string $key,array $fieldTypes, array $nextKeys,$column,$operazione) { |
|
$fieldInfo = $fieldTypes[$key]; |
|
$type = $fieldInfo["type"]; |
|
$return = []; |
|
foreach($rows AS $row) { |
|
if (isset($row[$key])) { |
|
if ($type == "date") { |
|
$date = new DateTime($row[$key]); |
|
$year = $date->format("Y"); |
|
$groupID = $year; |
|
$subId = ""; |
|
if ($this->group == "M") { |
|
$subId = $date->format("m"); |
|
} else if ($this->group == "W") { |
|
$subId = $date->format("W"); |
|
} |
|
if (!empty($subId)) { |
|
$groupID .= "-".$subId; |
|
} |
|
} else { |
|
$groupID = $row[$key]; |
|
} |
|
if (!isset($return[$groupID])) { |
|
$return[$groupID] = []; |
|
} |
|
$return[$groupID][] = $row; |
|
} |
|
} |
|
if (!empty($return)) { |
|
if (!empty($nextKeys)) { |
|
$copyKeys = $nextKeys; |
|
$key = array_shift($nextKeys); |
|
} |
|
foreach($return AS $groupID => $rows) { |
|
if (!empty($copyKeys)) { |
|
$return[$groupID] = $this->groupData($rows,$key,$fieldTypes,((!empty($nextKeys))?$nextKeys:[]),$column,$operazione); |
|
} else { |
|
$return[$groupID] = $this->getFinalValue($rows,$column,$operazione,$fieldTypes); |
|
} |
|
} |
|
ksort($return); |
|
return $return; |
|
} |
|
} |
|
|
|
private function getFinalValue($data,$column,$operazione,$settings) { |
|
$fieldInfo = $settings[$column]; |
|
$ksort = false; |
|
foreach($data AS $row) { |
|
if ($column == "id" || $column == "codice") { |
|
if ($operazione == "count") { |
|
if (!isset($return)) { |
|
$return = 0; |
|
} |
|
$return++; |
|
} else { |
|
$this->errors[] = "{$column} - Configurazione errata"; |
|
return null; |
|
} |
|
} else { |
|
$value = $row[$column]; |
|
if ($operazione == "count") { |
|
if (!isset($return)) { |
|
$return = []; |
|
} |
|
if (!isset($return[$value])) { |
|
$ksort = true; |
|
$return[$value] = 0; |
|
} |
|
$return[$value]++; |
|
} else if ($operazione == "sum") { |
|
if (!isset($return)) { |
|
$return = 0; |
|
} |
|
if (is_numeric($value)) { |
|
$value = truncate($value,2); |
|
} |
|
$return += $value; |
|
} else if ($operazione == "avg") { |
|
if (!isset($return)) { |
|
$return = []; |
|
} |
|
if (is_numeric($value)) { |
|
$value = truncate($value,2); |
|
} |
|
$return[] = $value; |
|
} |
|
} |
|
} |
|
if ($operazione == "avg" && !empty($return)) { |
|
$return = array_filter($return); |
|
if (!empty($return)) { |
|
$return = array_sum($return)/count($return); |
|
} |
|
} |
|
if ($ksort) { |
|
ksort($return); |
|
} |
|
$fieldInfo["operazione"] = $operazione; |
|
return ["info"=>$fieldInfo,"data"=>$return]; |
|
} |
|
|
|
public static function arrangeDataForGraph($serie) { |
|
$return = []; |
|
$return["dati"] = []; |
|
$return["categorie"] = []; |
|
$return["subCategorie"] = []; |
|
$return["groupsInfo"] = []; |
|
$groupId = 0; |
|
|
|
foreach($serie AS $gruppo) { |
|
foreach($gruppo AS $label => $data) { |
|
if (!empty($data)) { |
|
if (isset($data["info"])) { |
|
$return["groupsInfo"][$groupId] = $data["info"]; |
|
$return["dati"][$groupId][$label] = $data["data"]; |
|
if (is_array($data["data"])) { |
|
$return["subCategorie"] = array_merge($return["subCategorie"],array_keys($data["data"])); |
|
} |
|
} else { |
|
$updatedLabel = !(count($serie) > 1); |
|
foreach($data AS $subLabel => $subData) { |
|
if (!$updatedLabel) { |
|
$updatedLabel = true; |
|
$label .= " - {$subData["info"]["label"]}"; |
|
} |
|
$return["subCategorie"][] = $subLabel; |
|
$return["groupsInfo"][$groupId] = $subData["info"]; |
|
$return["dati"][$groupId][$label][$subLabel] = $subData["data"]; |
|
} |
|
} |
|
} |
|
$return["categorie"][] = $label; |
|
} |
|
$groupId++; |
|
} |
|
$return["categorie"] = array_filter($return["categorie"]); |
|
$return["categorie"] = array_unique($return["categorie"]); |
|
sort($return["categorie"]); |
|
if (!empty($return["subCategorie"])) { |
|
$return["subCategorie"] = array_filter($return["subCategorie"]); |
|
$return["subCategorie"] = array_unique($return["subCategorie"]); |
|
sort($return["subCategorie"]); |
|
} |
|
foreach ($return["dati"] AS $groupId => $firstLevel) { |
|
foreach($return["categorie"] AS $categoria) { |
|
if (!isset($firstLevel[$categoria])) { |
|
if (isset($return["subCategorie"])) { |
|
foreach($return["subCategorie"] AS $sub) { |
|
$return["dati"][$groupId][$categoria][$sub] = 0; |
|
} |
|
} else { |
|
$return["dati"][$groupId] = 0; |
|
} |
|
} else { |
|
if (isset($return["subCategorie"])) { |
|
foreach($return["subCategorie"] AS $sub) { |
|
if (!isset($firstLevel[$categoria][$sub])) { |
|
$return["dati"][$groupId][$categoria][$sub] = 0; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
if (empty($return["subCategorie"])) { |
|
unset($return["subCategorie"]); |
|
} |
|
return $return; |
|
} |
|
|
|
private function generaSerie($modulo,$column,$operazione,$groupKeys = null) { |
|
$base = $this->data[$modulo]; |
|
if (!empty($base)) { |
|
$settings = $this->generalSettings[$modulo]; |
|
$fieldInfo = $settings["fields"][$column]; |
|
if (in_array($operazione,$fieldInfo["operazioni"]) !== false) { |
|
if (!isset($groupKeys)) { |
|
$groupKeys = [$settings["defaultGroupField"] ?? null]; |
|
} |
|
if (!empty($groupKeys)) { |
|
$key = array_shift($groupKeys); |
|
return $this->groupData($base,$key,$settings["fields"],((!empty($groupKeys))?$groupKeys:[]),$column,$operazione); |
|
} else { |
|
$this->errors[] = "{$modulo} - defaultGroupField mancante"; |
|
} |
|
} else { |
|
$this->errors[] = "{$modulo} - {$column} - Operazione non concessa"; |
|
return null; |
|
} |
|
} |
|
} |
|
|
|
public static function graphTypes() { |
|
return [ |
|
"pie" => [ |
|
"descrizione" => "Aerogramma", |
|
"icon" => "fa fa-chart-pie", |
|
"file" => "pie.php", |
|
"serieMultiple" => false, |
|
"disponibile" => true, |
|
"maxGroups" => 2 |
|
], |
|
"half-pie" => [ |
|
"descrizione" => "Aerogramma semicricolare", |
|
"icon" => "fa fa-chart-pie", |
|
"file" => "half-pie.php", |
|
"serieMultiple" => false, |
|
"disponibile" => false, |
|
"maxGroups" => 2 |
|
], |
|
"web" => [ |
|
"descrizione" => "Ragnatela", |
|
"icon" => "fa fa-spider", |
|
"file" => "web.php", |
|
"serieMultiple" => true, |
|
"disponibile" => false, |
|
"maxGroups" => 1 |
|
], |
|
"columns" => [ |
|
"descrizione" => "Istogramma", |
|
"icon" => "fa fa-chart-bar", |
|
"file" => "columns.php", |
|
"serieMultiple" => true, |
|
"disponibile" => true, |
|
"maxGroups" => 2 |
|
], |
|
"line" => [ |
|
"descrizione" => "Cartesiano", |
|
"icon" => "fa fa-chart-line", |
|
"file" => "line.php", |
|
"serieMultiple" => true, |
|
"disponibile" => true, |
|
"maxGroups" => 2 |
|
] |
|
]; |
|
} |
|
|
|
public static function printGraph($type,$titolo,$descrizione,$serie) { |
|
$graphInfo = self::graphTypes()[$type] ?? null; |
|
if (isset($graphInfo) && ($graphInfo["serieMultiple"] || count($serie) === 1)) { |
|
$path = __DIR__ . "/views/{$graphInfo["file"]}"; |
|
if (file_exists($path)) { |
|
$chartHeight = 500; |
|
$idGraph = generateStrongPassword(16,false,'lud'); |
|
ob_start(); |
|
include $path; |
|
return ob_get_clean(); |
|
} |
|
} |
|
} |
|
|
|
public function genera() { |
|
if (!empty($this->richieste)) { |
|
ini_set('memory_limit', '500M'); |
|
ini_set("max_execution_time", "600"); |
|
$this->getData(array_keys($this->richieste)); |
|
if (!empty($this->data)) { |
|
foreach($this->richieste AS $modulo => $reports) { |
|
if ($this->data[$modulo]) { |
|
foreach($reports AS $details) { |
|
$graph = $details; |
|
$graph["serie"] = []; |
|
unset($graph["columns"]); |
|
$graphInfo = self::graphTypes()[$graph["type"]] ?? null; |
|
if (!empty($graphInfo)) { |
|
foreach($details["columns"] AS $column) { |
|
$graph["serie"][] = $this->generaSerie($modulo,$column["name"],$column["operazione"],$graph["group_keys"] ?? null); |
|
} |
|
$this->graphs[$modulo][] = $graph; |
|
} else { |
|
$this->errors[] = "{$graphInfo["titolo"]} - Tipologia di grafico non previsto"; |
|
} |
|
} |
|
if (!empty($this->graphs)) { |
|
$return = []; |
|
foreach($this->graphs AS $modulo => $grafici) { |
|
foreach($grafici AS $grafico) { |
|
$return[$modulo][] = ["info"=>$grafico,"html"=>self::printGraph($grafico["type"],$grafico["titolo"],$grafico["descrizione"],$grafico["serie"])]; |
|
} |
|
} |
|
} |
|
} else { |
|
$this->errors[] = "Nessuna base dati disponibile per il modulo {$modulo}"; |
|
} |
|
} |
|
return $return; |
|
} else { |
|
$this->errors[] = "Nessuna base dati disponibile"; |
|
} |
|
} else { |
|
$this->errors[] = "Nessuna richiesta selezionata"; |
|
} |
|
} |
|
|
|
public static function getSelfManagedReport($codice) { |
|
global $pdo; |
|
$bind = [":codice" => $codice]; |
|
if (isset($_SESSION["ente"])) { |
|
$bind[":gestore"] = $_SESSION["ente"]->codice; |
|
} else { |
|
$bind[":gestore"] = 0; |
|
} |
|
$sql = "SELECT * FROM b_report_personalizzati WHERE (codice_gestore = :gestore OR codice_gestore = 0) AND codice = :codice"; |
|
return $pdo->go($sql,$bind)->fetch(PDO::FETCH_ASSOC); |
|
} |
|
|
|
public static function getSelfManagedReports(bool $all = false,string $modulo = null) { |
|
global $pdo; |
|
$sql = "SELECT * FROM b_report_personalizzati WHERE (codice_gestore = :gestore "; |
|
if (isset($_SESSION["ente"])) { |
|
$bind[":gestore"] = $_SESSION["ente"]->codice; |
|
} else { |
|
$bind[":gestore"] = 0; |
|
} |
|
if ($all) { |
|
$sql .= " OR codice_gestore = 0"; |
|
} |
|
$sql .= ")"; |
|
if (!empty($modulo)) { |
|
$sql .= " AND modulo = :modulo "; |
|
$bind[":modulo"] = $modulo; |
|
} |
|
return $pdo->go($sql,$bind)->fetchAll(PDO::FETCH_ASSOC); |
|
} |
|
|
|
public static function parseSelfManagedReport($report) { |
|
$return = []; |
|
$return["titolo"] = $report["titolo"]; |
|
$return["descrizione"] = $report["descrizione"]; |
|
$settings = json_decode($report["settings"],true); |
|
$return += $settings; |
|
return $return; |
|
} |
|
|
|
public function getSpecificSettings(String $modulo) : ?Array { |
|
$class= self::moduliDisponibili()[$modulo] ?? null; |
|
if (!empty($class) && class_exists($class)) { |
|
$return = $class::getReportSettings(); |
|
if (!empty($return)) { |
|
$return["class"] = $class; |
|
if (!empty($this->enrichment) && method_exists($this->enrichment,"enrichConfig")) { |
|
$return = $this->enrichment->enrichConfig($return); |
|
} |
|
return $return; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
public function getReportDisponibili($libraryRequest = false) { |
|
global $beRoot; |
|
$return = null; |
|
if (isset($_SESSION["utente"])) { |
|
if ($libraryRequest && empty($_SESSION["ente"])) { |
|
$moduli = [Modulo::getModuli()]; |
|
} else { |
|
$moduli = $_SESSION["utente"]->moduliDisponibili(); |
|
} |
|
if (!empty($moduli)) { |
|
$return = []; |
|
foreach($moduli AS $sezione) { |
|
$sezione = array_keys($sezione); |
|
foreach($sezione AS $modulo) { |
|
$configModulo = $this->getSpecificSettings($modulo); |
|
if (!empty($configModulo) && is_array($configModulo)) { |
|
$return[$modulo] = $configModulo; |
|
$reportPersonalizzati = self::getSelfManagedReports(true,$modulo); |
|
if (!empty($reportPersonalizzati)) { |
|
foreach($reportPersonalizzati AS $report) { |
|
$reportParsed = self::parseSelfManagedReport($report); |
|
if (empty($return[$modulo]["templates"])) { |
|
$return[$modulo]["templates"] = []; |
|
} |
|
$return[$modulo]["templates"]["self-managed-{$report["codice"]}"] = $reportParsed; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
} |
|
?>
|