2017-09-21 15:51:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe per la gestione delle informazioni relative alle stampe installate.
|
|
|
|
*
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
|
|
|
class Prints
|
|
|
|
{
|
|
|
|
/** @var array Elenco delle stampe disponibili */
|
|
|
|
protected static $prints = [];
|
|
|
|
/** @var array Elenco delle stampe disponibili */
|
|
|
|
protected static $modules = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce tutte le informazioni di tutti i moduli installati.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getPrints()
|
|
|
|
{
|
|
|
|
if (empty(self::$prints)) {
|
|
|
|
$database = Database::getConnection();
|
|
|
|
|
|
|
|
$results = $database->fetchArray('SELECT * FROM zz_prints');
|
|
|
|
|
|
|
|
$prints = [];
|
|
|
|
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$result['full_directory'] = DOCROOT.'/templates/'.$result['directory'];
|
|
|
|
|
|
|
|
$prints[$result['id']] = $result;
|
|
|
|
$prints[$result['name']] = $result['id'];
|
|
|
|
|
|
|
|
if (!isset(self::$modules[$result['id_module']])) {
|
|
|
|
self::$modules[$result['id_module']] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$modules[$result['id_module']][] = $result['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$prints = $prints;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$prints;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative a un singolo modulo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $print
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-09-22 15:16:56 +02:00
|
|
|
public static function get($print)
|
2017-09-21 15:51:39 +02:00
|
|
|
{
|
|
|
|
if (!is_numeric($print) && !empty(self::getPrints()[$print])) {
|
|
|
|
$print = self::getPrints()[$print];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getPrints()[$print];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative alle stampe di un singolo modulo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getModulePrints($module)
|
|
|
|
{
|
2017-12-30 14:13:11 +01:00
|
|
|
$module_id = Modules::get($module)['id'];
|
2017-09-21 15:51:39 +02:00
|
|
|
|
2017-09-21 17:15:13 +02:00
|
|
|
self::getPrints();
|
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ((array) self::$modules[$module_id] as $value) {
|
2017-09-22 15:16:56 +02:00
|
|
|
$result[] = self::get($value);
|
2017-09-21 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-12-30 14:13:11 +01:00
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative alle stampe di un singolo modulo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getModuleMainPrint($module)
|
|
|
|
{
|
|
|
|
$prints = self::getModulePrints($module);
|
|
|
|
|
|
|
|
$main = array_search(1, array_column($prints, 'main'));
|
|
|
|
|
|
|
|
if ($main !== false) {
|
|
|
|
return $prints[$main];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
public static function render($print, $id_record, $filename = null)
|
|
|
|
{
|
|
|
|
ob_end_clean();
|
|
|
|
|
2017-09-22 15:16:56 +02:00
|
|
|
$infos = self::get($print);
|
2017-09-21 15:51:39 +02:00
|
|
|
|
|
|
|
Permissions::addModule($infos['id_module']);
|
|
|
|
|
|
|
|
if (empty($infos) || empty($infos['enabled']) || !Permissions::check([], false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Impostazione automatica della precisione a 2 numeri decimali
|
|
|
|
Translator::getFormatter()->setPrecision(2);
|
|
|
|
|
|
|
|
// Individuazione della configurazione
|
|
|
|
$directory = dirname($filename);
|
|
|
|
if (!empty($filename) && !directory($directory)) {
|
|
|
|
$error = tr('Non hai i permessi per creare directory e files in _DIRECTORY_', [
|
|
|
|
'_DIRECTORY_' => $directory,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$_SESSION['errors'][] = $error;
|
|
|
|
|
|
|
|
echo '
|
|
|
|
<p align="center">'.$error.'</p>';
|
|
|
|
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self::isOldStandard($print)) {
|
|
|
|
self::oldLoader($infos['id'], $id_record, $filename);
|
|
|
|
} else {
|
|
|
|
self::loader($infos['id'], $id_record, $filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function readOptions($string)
|
|
|
|
{
|
|
|
|
// Fix per contenuti con newline integrate
|
|
|
|
$string = str_replace(["\n", "\r"], ['\\n', '\\r'], $string);
|
|
|
|
|
|
|
|
$result = (array) json_decode($string, true);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-09-21 16:09:48 +02:00
|
|
|
protected static function isOldStandard($print)
|
2017-09-21 15:51:39 +02:00
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
$infos = self::get($print);
|
2017-09-21 15:51:39 +02:00
|
|
|
|
2017-09-21 16:09:48 +02:00
|
|
|
return file_exists($infos['full_directory'].'/pdfgen.'.$infos['directory'].'.php') || file_exists($infos['full_directory'].'/custom/pdfgen.'.$infos['directory'].'.php');
|
2017-09-21 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 16:09:48 +02:00
|
|
|
protected static function isNewStandard($print)
|
2017-09-21 15:51:39 +02:00
|
|
|
{
|
2017-09-21 16:09:48 +02:00
|
|
|
return !self::isOldStandard($print);
|
2017-09-21 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static function oldLoader($id_print, $id_record, $filename = null)
|
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
$infos = self::get($id_print);
|
2017-09-21 15:51:39 +02:00
|
|
|
$options = self::readOptions($infos['options']);
|
2017-09-21 16:09:48 +02:00
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
$database = Database::getConnection();
|
|
|
|
$dbo = $database;
|
|
|
|
|
2017-09-21 16:09:48 +02:00
|
|
|
$docroot = DOCROOT;
|
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
$_GET[$infos['previous']] = $id_record;
|
|
|
|
${$infos['previous']} = $id_record;
|
|
|
|
$ptype = $infos['directory'];
|
|
|
|
|
|
|
|
$orientation = 'P';
|
|
|
|
$body_table_params = "style='width:210mm;'";
|
|
|
|
$table = 'margin-left:1.7mm';
|
|
|
|
$font_size = '10pt';
|
|
|
|
|
|
|
|
// Decido se usare la stampa personalizzata (se esiste) oppure quella standard
|
2017-09-21 16:09:48 +02:00
|
|
|
if (file_exists($infos['full_directory'].'/custom/pdfgen.'.$infos['directory'].'.php')) {
|
|
|
|
include $infos['full_directory'].'/custom/pdfgen.'.$infos['directory'].'.php';
|
2017-09-21 15:51:39 +02:00
|
|
|
} else {
|
2017-09-21 16:09:48 +02:00
|
|
|
include $infos['full_directory'].'/pdfgen.'.$infos['directory'].'.php';
|
2017-09-21 15:51:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sostituzione di variabili generiche
|
|
|
|
$report = str_replace('$body$', $body, $report);
|
|
|
|
$report = str_replace('$footer$', $footer, $report);
|
|
|
|
|
|
|
|
$report = str_replace('$font_size$', $font_size, $report);
|
|
|
|
$report = str_replace('$body_table_params$', $body_table_params, $report);
|
|
|
|
$report = str_replace('$table$', $table, $report);
|
|
|
|
|
2017-12-30 14:13:11 +01:00
|
|
|
// Footer di default
|
|
|
|
if (!str_contains($report, '<page_footer>')) {
|
|
|
|
$report .= '<page_footer>$default_footer$</page_footer>';
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
// Operazioni di sostituzione
|
|
|
|
include DOCROOT.'/templates/replace.php';
|
|
|
|
|
|
|
|
$mode = !empty($filename) ? 'F' : 'I';
|
|
|
|
|
|
|
|
$filename = !empty($filename) ? $filename : sanitizeFilename($report_name);
|
|
|
|
$title = basename($filename);
|
|
|
|
|
|
|
|
$html2pdf = new Spipu\Html2Pdf\Html2Pdf($orientation, 'A4', 'it', true, 'UTF-8');
|
|
|
|
|
|
|
|
$html2pdf->writeHTML($report);
|
|
|
|
$html2pdf->pdf->setTitle($title);
|
|
|
|
|
|
|
|
$html2pdf->output($filename, $mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function loader($id_print, $id_record, $filename = null)
|
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
$infos = self::get($id_print);
|
2017-09-21 15:51:39 +02:00
|
|
|
$options = self::readOptions($infos['options']);
|
|
|
|
|
|
|
|
$database = Database::getConnection();
|
|
|
|
$dbo = $database;
|
|
|
|
|
2017-09-21 16:09:48 +02:00
|
|
|
$docroot = DOCROOT;
|
|
|
|
|
2017-09-21 15:51:39 +02:00
|
|
|
// Impostazioni di default
|
|
|
|
if (file_exists(DOCROOT.'/templates/base/custom/settings.php')) {
|
|
|
|
$default = include DOCROOT.'/templates/base/custom/settings.php';
|
|
|
|
} else {
|
|
|
|
$default = include DOCROOT.'/templates/base/settings.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Impostazioni personalizzate della stampa
|
|
|
|
if (file_exists($infos['full_directory'].'/custom/settings.php')) {
|
|
|
|
$custom = include $infos['full_directory'].'/custom/settings.php';
|
|
|
|
} elseif (file_exists($infos['full_directory'].'/settings.php')) {
|
|
|
|
$custom = include $infos['full_directory'].'/settings.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Individuazione delle impostazioni finali
|
|
|
|
$settings = array_merge($default, (array) $custom);
|
|
|
|
|
|
|
|
// Individuazione delle variabili fondamentali per la sostituzione dei contenuti
|
|
|
|
if (file_exists($infos['full_directory'].'/custom/init.php')) {
|
|
|
|
include $infos['full_directory'].'/custom/init.php';
|
|
|
|
} elseif (file_exists($infos['full_directory'].'/init.php')) {
|
|
|
|
include $infos['full_directory'].'/init.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Individuazione delle variabili per la sostituzione
|
|
|
|
include_once DOCROOT.'/templates/info.php';
|
|
|
|
|
|
|
|
// Generazione dei contenuti della stampa
|
|
|
|
ob_start();
|
|
|
|
if (file_exists($infos['full_directory'].'/custom/body.php')) {
|
|
|
|
include $infos['full_directory'].'/custom/body.php';
|
|
|
|
} else {
|
|
|
|
include $infos['full_directory'].'/body.php';
|
|
|
|
}
|
|
|
|
$report = ob_get_clean();
|
|
|
|
|
|
|
|
if (!empty($autofill)) {
|
|
|
|
$result = '';
|
|
|
|
|
|
|
|
// max($autofill['additional']) = $autofill['rows'] - 1
|
|
|
|
for ($i = (floor($autofill['count']) % $autofill['rows']); $i < $autofill['additional']; ++$i) {
|
|
|
|
$result .= '
|
|
|
|
<tr>';
|
|
|
|
for ($c = 0; $c < $autofill['columns']; ++$c) {
|
|
|
|
$result .= '
|
|
|
|
<td> </td>';
|
|
|
|
}
|
|
|
|
$result .= '
|
|
|
|
</tr>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$report = str_replace('|autofill|', $result, $report);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generazione dei contenuti dell'header
|
|
|
|
ob_start();
|
|
|
|
if (file_exists($infos['full_directory'].'/custom/header.php')) {
|
|
|
|
include $infos['full_directory'].'/custom/header.php';
|
|
|
|
} elseif (file_exists($infos['full_directory'].'/header.php')) {
|
|
|
|
include $infos['full_directory'].'/header.php';
|
|
|
|
}
|
|
|
|
$head = ob_get_clean();
|
|
|
|
|
|
|
|
// Generazione dei contenuti del footer
|
|
|
|
ob_start();
|
|
|
|
if (file_exists($infos['full_directory'].'/custom/footer.php')) {
|
|
|
|
include $infos['full_directory'].'/custom/footer.php';
|
|
|
|
} elseif (file_exists($infos['full_directory'].'/footer.php')) {
|
|
|
|
include $infos['full_directory'].'/footer.php';
|
|
|
|
}
|
|
|
|
$foot = ob_get_clean();
|
|
|
|
|
|
|
|
// Header di default
|
|
|
|
$head = !empty($head) ? $head : '$default_header$';
|
|
|
|
|
|
|
|
// Footer di default
|
|
|
|
$foot = !empty($foot) ? $foot : '$default_footer$';
|
|
|
|
|
|
|
|
// Operazioni di sostituzione
|
|
|
|
include DOCROOT.'/templates/replace.php';
|
|
|
|
|
|
|
|
$mode = !empty($filename) ? 'F' : 'I';
|
|
|
|
|
|
|
|
$filename = !empty($filename) ? $filename : sanitizeFilename($report_name);
|
|
|
|
$title = basename($filename);
|
|
|
|
|
|
|
|
$styles = [
|
|
|
|
'templates/base/bootstrap.css',
|
|
|
|
'templates/base/style.css',
|
|
|
|
];
|
|
|
|
|
|
|
|
$settings['orientation'] = strtoupper($settings['orientation']) == 'L' ? 'L' : 'P';
|
|
|
|
$settings['format'] = is_string($settings['format']) ? $settings['format'].($settings['orientation'] == 'L' ? '-L' : '') : $settings['format'];
|
|
|
|
|
|
|
|
// Instanziamento dell'oggetto mPDF
|
|
|
|
$mpdf = new mPDF(
|
|
|
|
'c',
|
|
|
|
$settings['format'],
|
|
|
|
$settings['font-size'],
|
|
|
|
'helvetica',
|
|
|
|
$settings['margins']['left'],
|
|
|
|
$settings['margins']['right'],
|
|
|
|
$settings['margins']['top'] + $settings['header-height'],
|
|
|
|
$settings['margins']['bottom'] + $settings['footer-height'],
|
|
|
|
$settings['margins']['top'],
|
|
|
|
$settings['margins']['bottom'],
|
|
|
|
$settings['orientation']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Impostazione di header e footer
|
|
|
|
$mpdf->SetHTMLFooter($foot);
|
|
|
|
$mpdf->SetHTMLHeader($head);
|
|
|
|
|
|
|
|
// Impostazione del titolo del PDF
|
|
|
|
$mpdf->SetTitle($title);
|
|
|
|
|
|
|
|
// Inclusione dei fogli di stile CSS
|
|
|
|
foreach ($styles as $value) {
|
|
|
|
$mpdf->WriteHTML(file_get_contents(DOCROOT.'/'.$value), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Impostazione del font-size
|
|
|
|
$mpdf->WriteHTML('body {font-size: '.$settings['font-size'].'pt;}', 1);
|
|
|
|
|
|
|
|
// Aggiunta dei contenuti
|
|
|
|
$mpdf->WriteHTML($report);
|
|
|
|
|
|
|
|
// Creazione effettiva del PDF
|
|
|
|
$mpdf->Output($filename, $mode);
|
|
|
|
}
|
2017-09-21 17:15:13 +02:00
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
public static function getHref($print, $id_record, $get = '')
|
2017-09-21 17:15:13 +02:00
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
$infos = self::get($print);
|
2017-09-21 17:15:13 +02:00
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
if (empty($infos)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:15:13 +02:00
|
|
|
$link = ROOTDIR.'/pdfgen.php?';
|
|
|
|
|
|
|
|
if (self::isOldStandard($infos['id'])) {
|
2017-09-21 17:48:41 +02:00
|
|
|
$link .= 'ptype='.$infos['directory'];
|
|
|
|
|
|
|
|
$link .= !empty($infos['previous']) && !empty($id_record) ? '&'.$infos['previous'].'='.$id_record : '';
|
2017-09-21 17:15:13 +02:00
|
|
|
} else {
|
2017-09-21 17:48:41 +02:00
|
|
|
$link .= 'id_print='.$infos['id'];
|
|
|
|
|
|
|
|
$link .= !empty($id_record) ? '&id_record='.$id_record : '';
|
2017-09-21 17:15:13 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
$link .= !empty($get) ? '&'.$get : '';
|
|
|
|
|
2017-09-21 17:15:13 +02:00
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
public static function getLink($print, $id_record, $btn = null, $title = null, $icon = null, $get = '')
|
2017-09-21 17:15:13 +02:00
|
|
|
{
|
2017-09-22 15:16:56 +02:00
|
|
|
$print = self::get($print);
|
2017-09-21 17:15:13 +02:00
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
if (empty($print)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = isset($btn) ? $btn : 'btn-info';
|
2017-09-21 17:15:13 +02:00
|
|
|
$class = !empty($class) ? ' class="btn '.$class.'" ' : '';
|
|
|
|
|
2017-09-21 17:48:41 +02:00
|
|
|
$title = isset($title) ? $title : $print['title'];
|
|
|
|
|
|
|
|
$icon = !empty($icon) ? $icon : $print['icon'];
|
|
|
|
$icon = str_replace('|default|', $print['icon'], $icon);
|
|
|
|
|
2017-09-21 17:15:13 +02:00
|
|
|
return '
|
2017-09-21 17:48:41 +02:00
|
|
|
<a '.$class.' href="'.self::getHref($print['id'], $id_record, $get).'" target="_blank"><i class="'.$icon.'"></i> '.$title.'</a>';
|
2017-09-21 17:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDropdown($module, $id_record, $class = 'btn-info')
|
|
|
|
{
|
|
|
|
$prints = self::getModulePrints($module);
|
|
|
|
|
|
|
|
if (empty($prints) || empty($id_record)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($prints) > 1) {
|
|
|
|
$result = '
|
|
|
|
<div class="btn-group">';
|
|
|
|
|
|
|
|
$main = array_search(1, array_column($prints, 'main'));
|
|
|
|
if ($main !== false) {
|
|
|
|
$print = $prints[$main];
|
|
|
|
|
|
|
|
$result .= self::getLink($print['id'], $id_record, $class);
|
|
|
|
|
|
|
|
unset($prints[$main]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
<button type="button" class="btn '.$class.' dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
|
|
|
'.($main === false ? '<i class="fa fa-print"></i> '.tr('Stampe').' ' : '').'<span class="caret"></span>
|
|
|
|
<span class="sr-only">Toggle Dropdown</span>
|
|
|
|
</button>
|
|
|
|
<ul class="dropdown-menu">';
|
|
|
|
|
|
|
|
foreach ($prints as $print) {
|
|
|
|
$result .= '
|
2017-09-21 18:10:53 +02:00
|
|
|
<li>'.self::getLink($print['id'], $id_record, false).'</li>';
|
2017-09-21 17:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$result .= '
|
|
|
|
</ul>
|
|
|
|
</div>';
|
|
|
|
} else {
|
|
|
|
$result = self::getLink($prints[0]['id'], $id_record, $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2017-09-21 15:51:39 +02:00
|
|
|
}
|