openstamanager/src/Prints.php

599 lines
17 KiB
PHP
Raw Normal View History

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 = [];
2018-02-20 16:46:23 +01:00
/** @var array Elenco delle stampe per modulo */
2017-09-21 15:51:39 +02:00
protected static $modules = [];
/**
* Restituisce tutte le informazioni di tutti i moduli installati.
*
* @return array
*/
public static function getPrints()
{
if (empty(self::$prints)) {
2018-09-20 12:05:22 +02:00
$database = database();
2017-09-21 15:51:39 +02:00
$results = $database->fetchArray('SELECT * FROM zz_prints WHERE enabled = 1 ORDER BY `order`');
2017-09-21 15:51:39 +02:00
$prints = [];
2018-06-23 15:41:32 +02:00
// Inizializzazione dei riferimenti
2018-09-06 09:45:49 +02:00
$modules = Modules::getModules();
foreach ($modules as $module) {
2018-06-23 15:41:32 +02:00
self::$modules[$module['id']] = [];
}
2017-09-21 15:51:39 +02:00
foreach ($results as $result) {
$result['full_directory'] = DOCROOT.'/templates/'.$result['directory'];
$prints[$result['id']] = $result;
$prints[$result['name']] = $result['id'];
self::$modules[$result['id_module']][] = $result['id'];
}
self::$prints = $prints;
}
return self::$prints;
}
/**
2018-02-20 16:46:23 +01:00
* Restituisce le informazioni relative a una singolo stampa specificata.
2017-09-21 15:51:39 +02:00
*
* @param string|int $print
*
* @return array
*/
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)
{
$module_id = Modules::get($module)['id'];
2017-09-21 15:51:39 +02:00
self::getPrints();
2017-09-21 15:51:39 +02:00
$result = [];
foreach ((array) self::$modules[$module_id] as $value) {
2018-03-23 14:42:56 +01:00
$print = self::get($value);
if (!empty($print['is_record'])) {
$result[] = $print;
}
2017-09-21 15:51:39 +02:00
}
return $result;
}
/**
* Restituisce le informazioni relative alle stampe di un singolo modulo specificato.
*
* @param string|int $module
*
* @return array
*/
2018-09-26 10:49:38 +02:00
public static function getModulePredefinedPrint($module)
{
$prints = self::getModulePrints($module);
2018-09-26 10:49:38 +02:00
$predefined = array_search(1, array_column($prints, 'predefined'));
2018-09-26 10:49:38 +02:00
if ($predefined !== false) {
return $prints[$predefined];
2018-09-14 11:39:02 +02:00
} elseif (!empty($prints)) {
return $prints[0];
}
return false;
}
/**
* Genera la stampa in PDF richiesta.
*
* @param string|int $print
* @param int $id_record
* @param string $directory
2019-11-29 17:19:22 +01:00
* @param bool $return_string
*/
2019-11-29 17:19:22 +01:00
public static function render($print, $id_record, $directory = null, $return_string = false)
2017-09-21 15:51:39 +02:00
{
2018-09-28 15:32:54 +02:00
//ob_end_clean(); // Compatibilità con versioni vecchie delle stampe
2019-11-18 17:43:27 +01:00
$dbo = $database = database();
$infos = self::get($print);
2017-09-21 15:51:39 +02:00
2019-12-20 11:42:42 +01:00
$additional_checks = false;
2019-12-20 11:40:22 +01:00
if (!$return_string) {
Permissions::addModule($infos['id_module']);
2017-09-21 15:51:39 +02:00
2019-12-20 11:40:22 +01:00
$has_access = true;
if (!empty($infos['is_record'])) {
$module = Modules::get($infos['id_module']);
2019-07-15 12:39:24 +02:00
2019-12-20 11:40:22 +01:00
Util\Query::setSegments(false);
$query = Util\Query::getQuery($module, [
'id' => $id_record,
]);
Util\Query::setSegments(true);
2019-07-15 12:39:24 +02:00
2019-12-20 11:40:22 +01:00
$has_access = !empty($query) ? $dbo->fetchNum($query) !== 0 : true;
}
2019-07-15 12:39:24 +02:00
2019-12-20 11:42:42 +01:00
$additional_checks = !Permissions::check([], false) || !$has_access;
}
if (empty($infos) || empty($infos['enabled']) || $additional_checks) {
return false;
2017-09-21 15:51:39 +02:00
}
// Individuazione della configurazione
if (!empty($directory) && !directory($directory)) {
2017-09-21 15:51:39 +02:00
$error = tr('Non hai i permessi per creare directory e files in _DIRECTORY_', [
'_DIRECTORY_' => $directory,
]);
2018-07-19 17:29:21 +02:00
flash()->error($error);
2017-09-21 15:51:39 +02:00
echo '
<p align="center">'.$error.'</p>';
exit();
}
if (self::isOldStandard($print)) {
2019-11-29 17:19:22 +01:00
return self::oldLoader($infos['id'], $id_record, $directory, $return_string);
2017-09-21 15:51:39 +02:00
} else {
2019-11-29 17:19:22 +01:00
return self::loader($infos['id'], $id_record, $directory, $return_string);
2017-09-21 15:51:39 +02:00
}
}
2018-12-29 12:03:22 +01:00
/**
* Individua il link per la stampa.
*
* @param string|int $print
* @param int $id_record
* @param string $get
*
* @return string
*/
public static function getHref($print, $id_record, $get = '')
{
$infos = self::get($print);
if (empty($infos)) {
return false;
}
$link = ROOTDIR.'/pdfgen.php?';
if (self::isOldStandard($infos['id'])) {
$link .= 'ptype='.$infos['directory'];
$link .= !empty($infos['previous']) && !empty($id_record) ? '&'.$infos['previous'].'='.$id_record : '';
} else {
$link .= 'id_print='.$infos['id'];
$link .= !empty($id_record) ? '&id_record='.$id_record : '';
}
$link .= !empty($get) ? '&'.$get : '';
return $link;
}
/**
* Restituisce il codice semplificato per il link alla stampa.
*
* @deprecated 2.4.1
*
* @param string|int $print
* @param int $id_record
* @param string $btn
* @param string $title
* @param string $icon
* @param string $get
*
* @return string
*/
public static function getLink($print, $id_record, $btn = null, $title = null, $icon = null, $get = '')
{
return '{( "name": "button", "type": "print", "id": "'.$print.'", "id_record": "'.$id_record.'", "label": "'.$title.'", "icon": "'.$icon.'", "parameters": "'.$get.'", "class": "'.$btn.'" )}';
}
/**
* Restituisce il link per la visualizzazione della stampa.
*
* @param string|int $print
* @param int $id_record
* @param string $directory
2018-12-29 12:03:22 +01:00
*
* @return string
*/
public static function getPreviewLink($print, $id_record, $directory)
2018-12-29 12:03:22 +01:00
{
$info = self::render($print, $id_record, $directory);
2018-12-29 12:03:22 +01:00
return self::getPDFLink($info['path']);
2018-12-29 12:03:22 +01:00
}
/**
* Restituisce il link per la visualizzazione del PDF.
*
2019-07-10 15:02:09 +02:00
* @param string $path
2018-12-29 12:03:22 +01:00
*
* @return string
*/
public static function getPDFLink($path)
2018-12-29 12:03:22 +01:00
{
//http://localhost/openstamanager/
$folders = explode('/', dirname($_SERVER['PHP_SELF']));
2020-02-05 14:51:39 +01:00
$base = (stripos($_SERVER['SERVER_PROTOCOL'], 'https') === 0 ? 'https://' : 'http://').$_SERVER['SERVER_NAME'].'/'.$folders[1].'/';
return ROOTDIR.'/assets/dist/pdfjs/web/viewer.html?file='.$base.ltrim(str_replace(DOCROOT, '', $path), '/');
2018-12-29 12:03:22 +01:00
}
/**
* Individua il percorso per il file.
*
* @param string|int $template
* @param string $file
*
* @return string|null
*/
public static function filepath($template, $file)
{
$template = self::get($template);
$directory = 'templates/'.$template['directory'].'|custom|';
return App::filepath($directory, $file);
}
/**
* Restituisce un array associativo dalla codifica JSON delle opzioni di stampa.
*
* @param string $string
*
* @return array
*/
2017-09-21 15:51:39 +02:00
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;
}
/**
* Controlla se la stampa segue lo standard HTML2PDF.
*
* @param string|int $print
*
* @return bool
*/
protected static function isOldStandard($print)
2017-09-21 15:51:39 +02:00
{
$infos = self::get($print);
2017-09-21 15:51:39 +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
}
/**
* Controlla se la stampa segue lo standard MPDF.
*
* @param string|int $print
*
* @return bool
*/
protected static function isNewStandard($print)
2017-09-21 15:51:39 +02:00
{
return !self::isOldStandard($print);
2017-09-21 15:51:39 +02:00
}
/**
* Crea la stampa secondo il formato deprecato HTML2PDF.
*
* @param string|int $id_print
* @param int $id_record
* @param string $directory
2019-11-29 17:19:22 +01:00
* @param bool $return_string
*/
2019-11-29 17:19:22 +01:00
protected static function oldLoader($id_print, $id_record, $directory = null, $return_string = false)
2017-09-21 15:51:39 +02:00
{
2019-11-29 17:19:22 +01:00
$format = 'A4';
$infos = self::get($id_print);
2017-09-21 15:51:39 +02:00
$options = self::readOptions($infos['options']);
2018-09-06 09:45:49 +02:00
$docroot = DOCROOT;
2018-09-20 12:05:22 +02:00
$dbo = $database = database();
2018-03-24 13:56:49 +01:00
$user = Auth::user();
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
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 {
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);
// 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($directory) ? 'F' : 'I';
2019-11-29 17:19:22 +01:00
$mode = !empty($return_string) ? 'S' : $mode;
2017-09-21 15:51:39 +02:00
$file = self::getFile($infos, $id_record, $directory, $replaces);
$title = $file['name'];
$path = $file['path'];
2017-09-21 15:51:39 +02:00
$html2pdf = new Spipu\Html2Pdf\Html2Pdf($orientation, $format, 'it', true, 'UTF-8');
2017-09-21 15:51:39 +02:00
$html2pdf->writeHTML($report);
$html2pdf->pdf->setTitle($title);
2019-11-29 17:19:22 +01:00
$pdf = $html2pdf->output($path, $mode);
$file['pdf'] = $pdf;
return $file;
}
2019-07-10 15:02:09 +02:00
protected static function getFile($record, $id_record, $directory, $original_replaces)
{
$module = Modules::get($record['id_module']);
$name = $record['filename'].'.pdf';
$name = $module->replacePlaceholders($id_record, $name);
$replaces = [];
2019-07-10 15:02:09 +02:00
foreach ($original_replaces as $key => $value) {
2019-07-11 17:20:58 +02:00
$key = str_replace('$', '', $key);
$replaces['{'.$key.'}'] = $value;
}
$name = replace($name, $replaces);
$filename = sanitizeFilename($name);
2019-11-29 17:19:22 +01:00
$file = (empty($directory)) ? $filename : rtrim($directory, '/').'/'.$filename;
return [
'name' => $name,
'path' => $file,
];
2017-09-21 15:51:39 +02:00
}
/**
* Crea la stampa secondo il formato modulare MPDF.
*
* @param string|int $id_print
* @param int $id_record
* @param string $directory
2019-11-29 17:19:22 +01:00
* @param bool $return_string
*/
2019-11-29 17:19:22 +01:00
protected static function loader($id_print, $id_record, $directory = null, $return_string = false)
2017-09-21 15:51:39 +02:00
{
$infos = self::get($id_print);
2017-09-21 15:51:39 +02:00
$options = self::readOptions($infos['options']);
2018-09-20 12:05:22 +02:00
$dbo = $database = database();
2018-03-24 13:56:49 +01:00
$user = Auth::user();
2019-07-11 15:05:36 +02:00
// Generazione a singoli pezzi
$single_pieces = self::filepath($id_print, 'piece.php');
2017-09-21 15:51:39 +02:00
// Impostazioni di default
2018-09-19 10:44:32 +02:00
$default = include App::filepath('templates/base|custom|', 'settings.php');
2017-09-21 15:51:39 +02:00
// Impostazioni personalizzate della stampa
$custom = include self::filepath($id_print, 'settings.php');
2017-09-21 15:51:39 +02:00
// Individuazione delle impostazioni finali
$settings = array_merge($default, (array) $custom);
2019-07-11 15:05:36 +02:00
// Individuazione delle variabili fondamentali per la sostituzione dei contenuti
include self::filepath($id_print, 'init.php');
// Individuazione delle variabili per la sostituzione
include DOCROOT.'/templates/info.php';
// Instanziamento dell'oggetto mPDF
$mpdf = new \Mpdf\Mpdf([
'mode' => 'c',
'format' => $settings['format'],
'orientation' => strtoupper($settings['orientation']) == 'L' ? 'L' : 'P',
'font-size' => $settings['font-size'],
'margin_left' => $settings['margins']['left'],
'margin_right' => $settings['margins']['right'],
'setAutoBottomMargin' => 'stretch',
'setAutoTopMargin' => 'stretch',
'default_font' => 'helvetica',
2017-09-21 15:51:39 +02:00
// Abilitazione per lo standard PDF/A
//'PDFA' => true,
//'PDFAauto' => true,
]);
2020-01-21 18:08:05 +01:00
2020-02-05 14:51:39 +01:00
if (setting('Filigrana stampe')) {
2020-01-21 18:08:05 +01:00
$mpdf->SetWatermarkImage(
DOCROOT.'/files/anagrafiche/'.setting('Filigrana stampe'),
0.5,
'F',
'F'
);
// false = 'showWatermarkImage' => false,
2020-02-05 14:51:39 +01:00
if ($settings['showWatermarkImage'] == null) {
2020-01-21 18:08:05 +01:00
$mpdf->showWatermarkImage = true;
2020-02-05 14:51:39 +01:00
} else {
2020-01-21 18:08:05 +01:00
$mpdf->showWatermarkImage = intval($settings['showWatermarkImage']);
}
}
2017-09-21 15:51:39 +02:00
// Inclusione dei fogli di stile CSS
$styles = [
'templates/base/bootstrap.css',
'templates/base/style.css',
];
2017-09-21 15:51:39 +02:00
foreach ($styles as $value) {
$mpdf->WriteHTML(file_get_contents(DOCROOT.'/'.$value), 1);
2017-09-21 15:51:39 +02:00
}
// Impostazione del font-size
$mpdf->WriteHTML('body {font-size: '.$settings['font-size'].'pt;}', 1);
2019-07-11 15:05:36 +02:00
// Generazione totale
2019-07-11 17:20:58 +02:00
if (empty($single_pieces)) {
2019-07-11 15:05:36 +02:00
ob_start();
include self::filepath($id_print, 'body.php');
$report = ob_get_clean();
2019-07-11 15:05:36 +02:00
if (!empty($autofill)) {
2019-07-12 12:40:13 +02:00
$result = $autofill->generate();
2019-07-11 15:05:36 +02:00
$report = str_replace('|autofill|', $result, $report);
}
}
2017-09-21 15:51:39 +02:00
// Generazione dei contenuti dell'header
ob_start();
include self::filepath($id_print, 'header.php');
2017-09-21 15:51:39 +02:00
$head = ob_get_clean();
// Header di default
$head = !empty($head) ? $head : '$default_header$';
2017-09-21 15:51:39 +02:00
// Generazione dei contenuti del footer
ob_start();
include self::filepath($id_print, 'footer.php');
2017-09-21 15:51:39 +02:00
$foot = ob_get_clean();
// Footer di default
$foot = !empty($foot) ? $foot : '$default_footer$';
// Operazioni di sostituzione
include DOCROOT.'/templates/replace.php';
// Impostazione di header e footer
$mpdf->SetHTMLHeader($head);
$mpdf->SetHTMLFooter($foot);
2017-09-21 15:51:39 +02:00
// Generazione dei contenuti della stampa
2019-07-11 15:05:36 +02:00
if (!empty($single_pieces)) {
ob_start();
include self::filepath($id_print, 'top.php');
$top = ob_get_clean();
2017-09-21 15:51:39 +02:00
2019-07-15 11:16:12 +02:00
$top = str_replace(array_keys($replaces), array_values($replaces), $top);
$mpdf->WriteHTML($top);
2017-09-21 15:51:39 +02:00
foreach ($records as $record) {
ob_start();
include self::filepath($id_print, 'piece.php');
$piece = ob_get_clean();
2018-05-11 10:34:11 +02:00
$mpdf->WriteHTML($piece);
}
2017-09-21 15:51:39 +02:00
ob_start();
include self::filepath($id_print, 'bottom.php');
$bottom = ob_get_clean();
2017-09-21 15:51:39 +02:00
2019-07-15 11:16:12 +02:00
$bottom = str_replace(array_keys($replaces), array_values($replaces), $bottom);
$mpdf->WriteHTML($bottom);
2017-09-21 15:51:39 +02:00
$report = '';
2017-09-21 15:51:39 +02:00
}
// Footer per l'ultima pagina
if (!empty($options['last-page-footer'])) {
$is_last_page = true;
// Generazione dei contenuti del footer
ob_start();
include self::filepath($id_print, 'footer.php');
$foot = ob_get_clean();
}
// Operazioni di sostituzione
include DOCROOT.'/templates/replace.php';
2017-09-21 15:51:39 +02:00
$mode = !empty($directory) ? 'F' : 'I';
2019-11-29 17:19:22 +01:00
$mode = !empty($return_string) ? 'S' : $mode;
$file = self::getFile($infos, $id_record, $directory, $replaces);
$title = $file['name'];
$path = $file['path'];
// Impostazione del titolo del PDF
$mpdf->SetTitle($title);
2017-09-21 15:51:39 +02:00
// Aggiunta dei contenuti
$mpdf->WriteHTML($report);
// Impostazione footer per l'ultima pagina
if (!empty($options['last-page-footer'])) {
$mpdf->WriteHTML('<div class="fake-footer">'.$foot.'</div>');
$mpdf->WriteHTML('<div style="position:absolute; bottom: 13mm; margin-right: '.($settings['margins']['right']).'mm">'.$foot.'</div>');
}
2017-09-21 15:51:39 +02:00
// Creazione effettiva del PDF
2019-11-29 17:19:22 +01:00
$pdf = $mpdf->Output($path, $mode);
$file['pdf'] = $pdf;
return $file;
2017-09-21 15:51:39 +02:00
}
}