Passed
Push — GENERAL_BUG_REVIEW_240911 ( 3362b2...8cbbee )
by Rafael
49:13
created

ModelePDFLabels   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A liste_modeles() 0 10 1
1
<?php
2
3
/* Copyright (C) 2003-2005  Rodolphe Quiedeville        <[email protected]>
4
 * Copyright (C) 2004-2009  Laurent Destailleur         <[email protected]>
5
 * Copyright (C) 2004       Eric Seigne                 <[email protected]>
6
 * Copyright (C) 2005-2009  Regis Houssin               <[email protected]>
7
 * Copyright (C) 2024		MDW							<[email protected]>
8
 * Copyright (C) 2024       Rafael San José             <[email protected]>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 * or see https://www.gnu.org/
23
 */
24
25
namespace Dolibarr\Code\PrintSheet\Classes;
26
27
use Dolibarr\Code\Core\Classes\Translate;
28
use DoliDB;
29
30
/**
31
 *  \file       htdocs/core/modules/printsheet/modules_labels.php
32
 *  \ingroup    member
33
 *  \brief      File of parent class of document generator for members labels sheets.
34
 */
35
36
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/pdf.lib.php';
37
38
/**
39
 *  Parent class of document generator for address sheet.
40
 */
41
class ModelePDFLabels
42
{
43
    /**
44
     * @var string Error code (or message)
45
     */
46
    public $error = '';
47
48
49
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
50
51
    /**
52
     *  Return list of active generation modules
53
     *
54
     * @param DoliDB $db Database handler
55
     * @param integer $maxfilenamelength Max length of value to show
56
     * @return array                       List of templates
57
     */
58
    public function liste_modeles($db, $maxfilenamelength = 0)
59
    {
60
        // phpcs:enable
61
        $type = 'members_labels';
62
        $list = array();
63
64
        include_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';
65
        $list = getListOfModels($db, $type, $maxfilenamelength);
66
67
        return $list;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $list also could return the type integer which is incompatible with the documented return type array.
Loading history...
68
    }
69
}
70
71
72
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
73
/**
74
 *  Create a document onto disk according to template module.
75
 *
76
 * @param DoliDB $db Database handler
77
 * @param array $arrayofrecords Array of records
78
 * @param string $modele Force le modele a utiliser ('' to not force)
79
 * @param Translate $outputlangs Object lang a utiliser pour traduction
80
 * @param string $outputdir Output directory
81
 * @param string $template pdf generenate document class to use default 'standardlabel'
82
 * @param string $filename Short file name of PDF output file
83
 * @return int                             Return integer <0 if KO, >0 if OK
84
 */
85
function doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir = '', $template = 'standardlabel', $filename = 'tmp_address_sheet.pdf')
86
{
87
    // phpcs:enable
88
    global $conf, $langs;
89
    $langs->load("members");
90
91
    $error = 0;
92
93
    // Increase limit for PDF build
94
    $err = error_reporting();
95
    error_reporting(0);
96
    @set_time_limit(120);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for set_time_limit(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

96
    /** @scrutinizer ignore-unhandled */ @set_time_limit(120);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
    error_reporting($err);
98
99
    $code = '';
100
    $srctemplatepath = '';
101
102
    // Positionne le modele sur le nom du modele a utiliser
103
    if (!dol_strlen($modele)) {
104
        if (getDolGlobalString('ADHERENT_ETIQUETTE_TYPE')) {
105
            $code = getDolGlobalString('ADHERENT_ETIQUETTE_TYPE');
106
        } else {
107
            $code = $modele;
108
        }
109
    } else {
110
        $code = $modele;
111
    }
112
113
    // If selected modele is a filename template (then $modele="modelname:filename")
114
    $tmp = explode(':', $template, 2);
115
    if (!empty($tmp[1])) {
116
        $template = $tmp[0];
117
        $srctemplatepath = $tmp[1];
118
    } else {
119
        $srctemplatepath = $code;
120
    }
121
122
    dol_syslog("modele=" . $modele . " outputdir=" . $outputdir . " template=" . $template . " code=" . $code . " srctemplatepath=" . $srctemplatepath . " filename=" . $filename, LOG_DEBUG);
123
124
    // Search template files
125
    $file = '';
126
    $classname = '';
127
    $dirmodels = array('/');
128
    if (is_array($conf->modules_parts['models'])) {
129
        $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
130
    }
131
    foreach ($dirmodels as $reldir) {
132
        foreach (array('doc', 'pdf') as $prefix) {
133
            $file = $prefix . "_" . $template . ".class.php";
134
135
            // Determine the model path and validate that it exists
136
            $file = dol_buildpath($reldir . "core/modules/printsheet/doc/" . $file, 0);
137
            if (file_exists($file)) {
138
                $classname = $prefix . '_' . $template;
139
                break;
140
            }
141
        }
142
        if ($classname !== '') {
143
            break;
144
        }
145
    }
146
147
    // Load the model
148
    if ($classname !== '') {
149
        require_once $file;
150
151
        $obj = new $classname($db);
152
153
        // We save charset_output to restore it because write_file can change it if needed for
154
        // output format that does not support UTF8.
155
        $sav_charset_output = $outputlangs->charset_output;
156
        if ($obj->write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir, $filename) > 0) {
157
            $outputlangs->charset_output = $sav_charset_output;
158
159
            $fullpath = $obj->result['fullpath'];
160
161
            // Output to http stream
162
            clearstatcache();
163
164
            $attachment = true;
165
            if (getDolGlobalString('MAIN_DISABLE_FORCE_SAVEAS')) {
166
                $attachment = false;
167
            }
168
            $type = dol_mimetype($filename);
169
170
            //if ($encoding)   header('Content-Encoding: '.$encoding);
171
            if ($type) {
172
                header('Content-Type: ' . $type);
173
            }
174
            if ($attachment) {
175
                header('Content-Disposition: attachment; filename="' . $filename . '"');
176
            } else {
177
                header('Content-Disposition: inline; filename="' . $filename . '"');
178
            }
179
180
            // Ajout directives pour resoudre bug IE
181
            header('Cache-Control: Public, must-revalidate');
182
            header('Pragma: public');
183
184
            readfile($fullpath);
185
186
            return 1;
187
        } else {
188
            $outputlangs->charset_output = $sav_charset_output;
189
            dol_print_error($db, "doc_label_pdf_create Error: " . $obj->error);
190
            return -1;
191
        }
192
    } else {
193
        dol_print_error(null, $langs->trans("Error") . " " . $langs->trans("ErrorFileDoesNotExists", $file));
194
        return -1;
195
    }
196
}
197