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

ModelePDFCards   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
<?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\Members\Classes;
26
27
use Dolibarr\Code\Core\Classes\Translate;
28
use DoliDB;
29
30
/**
31
 *  \file       htdocs/core/modules/member/modules_cards.php
32
 *  \ingroup    member
33
 *  \brief      File of parent class of document generator for members cards.
34
 */
35
36
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/pdf.lib.php';
37
38
39
/**
40
 *  Parent class of document generator for members cards.
41
 */
42
class ModelePDFCards
43
{
44
    /**
45
     * @var string Error code (or message)
46
     */
47
    public $error = '';
48
49
50
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
51
52
    /**
53
     *  Return list of active generation modules
54
     *
55
     * @param DoliDB $db Database handler
56
     * @param integer $maxfilenamelength Max length of value to show
57
     * @return array                       List of templates
58
     */
59
    public static function liste_modeles($db, $maxfilenamelength = 0)
60
    {
61
        // phpcs:enable
62
        $type = 'member';
63
        $list = array();
64
65
        include_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';
66
        $list = getListOfModels($db, $type, $maxfilenamelength);
67
68
        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...
69
    }
70
}
71
72
73
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
74
/**
75
 *  Cree un fichier de cartes de visites en fonction du modele de ADHERENT_CARDS_ADDON_PDF
76
 *
77
 * @param DoliDB $db Database handler
78
 * @param array $arrayofmembers Array of members
79
 * @param string $modele Force modele to use ('' to not force)
80
 * @param Translate $outputlangs Object langs to use for translation
81
 * @param string $outputdir Output directory
82
 * @param string $template pdf generenate document class to use default 'standard'
83
 * @param string $filename Name of output file (without extension)
84
 * @return int                         Return integer <0 if KO, >0 if OK
85
 */
86
function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs, $outputdir = '', $template = 'standard', $filename = 'tmp_cards')
87
{
88
    // phpcs:enable
89
    global $conf, $langs;
90
    $langs->load("members");
91
92
    $error = 0;
93
94
    // Increase limit for PDF build
95
    $err = error_reporting();
96
    error_reporting(0);
97
    @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

97
    /** @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...
98
    error_reporting($err);
99
100
    $code = '';
101
    $srctemplatepath = '';
102
103
    // Positionne le modele sur le nom du modele a utiliser
104
    if (!dol_strlen($modele)) {
105
        if (getDolGlobalString('ADHERENT_CARDS_ADDON_PDF')) {
106
            $code = getDolGlobalString('ADHERENT_CARDS_ADDON_PDF');
107
        } else {
108
            $code = $modele;
109
        }
110
    } else {
111
        $code = $modele;
112
    }
113
114
    // If selected modele is a filename template (then $modele="modelname:filename")
115
    $tmp = explode(':', $template, 2);
116
    if (!empty($tmp[1])) {
117
        $template = $tmp[0];
118
        $srctemplatepath = $tmp[1];
119
    } else {
120
        $srctemplatepath = $code;
121
    }
122
123
    // Search template files
124
    $file = '';
125
    $classname = '';
126
    $dirmodels = array('/');
127
    if (is_array($conf->modules_parts['models'])) {
128
        $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
129
    }
130
    foreach ($dirmodels as $reldir) {
131
        foreach (array('doc', 'pdf') as $prefix) {
132
            $file = $prefix . "_" . $template . ".class.php";
133
134
            // We check that file of doc generaotr exists
135
            $file = dol_buildpath($reldir . "core/modules/member/doc/" . $file, 0);
136
            if (file_exists($file)) {
137
                $classname = $prefix . '_' . $template;
138
                break;
139
            }
140
        }
141
        if ($classname !== '') {
142
            break;
143
        }
144
    }
145
146
147
    // Charge le modele
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($arrayofmembers, $outputlangs, $srctemplatepath, 'member', 0, $filename) > 0) {
157
            $outputlangs->charset_output = $sav_charset_output;
158
            return 1;
159
        } else {
160
            $outputlangs->charset_output = $sav_charset_output;
161
            dol_print_error($db, "members_card_pdf_create Error: " . $obj->error);
162
            return -1;
163
        }
164
    } else {
165
        dol_print_error(null, $langs->trans("Error") . " " . $langs->trans("ErrorFileDoesNotExists", $file));
166
        return -1;
167
    }
168
}
169