Completed
Branch develop (edb367)
by
unknown
28:40
created

printing_printipp::printFile()   B

Complexity

Conditions 8
Paths 50

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 50
nop 3
dl 0
loc 59
rs 7.6501
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright (C) 2014-2018  Frederic France      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 * or see http://www.gnu.org/
18
 */
19
20
/**
21
 *      \file       htdocs/core/modules/printing/printipp.modules.php
22
 *      \ingroup    printing
23
 *      \brief      File to provide printing with PrintIPP
24
 */
25
26
include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
27
28
/**
29
 *   Class to provide printing with PrintIPP
30
 */
31
class printing_printipp extends PrintingDriver
32
{
33
    public $name = 'printipp';
34
    public $desc = 'PrintIPPDesc';
35
    public $picto = 'printer';
36
    public $active = 'PRINTING_PRINTIPP';
37
    public $conf = array();
38
    public $host;
39
    public $port;
40
    public $userid;    /* user login */
41
    public $user;
42
    public $password;
43
44
    /**
45
     * @var string Error code (or message)
46
     */
47
    public $error='';
48
49
    /**
50
     * @var string[] Error codes (or messages)
51
     */
52
    public $errors = array();
53
54
    /**
55
     * @var DoliDB Database handler.
56
     */
57
    public $db;
58
59
60
    /**
61
     *  Constructor
62
     *
63
     *  @param      DoliDB      $db      Database handler
64
     */
65
    function __construct($db)
66
    {
67
        global $conf;
68
69
        $this->db=$db;
70
        $this->host=$conf->global->PRINTIPP_HOST;
71
        $this->port=$conf->global->PRINTIPP_PORT;
72
        $this->user=$conf->global->PRINTIPP_USER;
73
        $this->password=$conf->global->PRINTIPP_PASSWORD;
74
        $this->conf[] = array('varname'=>'PRINTIPP_HOST', 'required'=>1, 'example'=>'localhost', 'type'=>'text');
75
        $this->conf[] = array('varname'=>'PRINTIPP_PORT', 'required'=>1, 'example'=>'631', 'type'=>'text');
76
        $this->conf[] = array('varname'=>'PRINTIPP_USER', 'required'=>0, 'example'=>'', 'type'=>'text', 'moreattributes'=>'autocomplete="off"');
77
        $this->conf[] = array('varname'=>'PRINTIPP_PASSWORD', 'required'=>0, 'example'=>'', 'type'=>'password', 'moreattributes'=>'autocomplete="off"');
78
        $this->conf[] = array('enabled'=>1, 'type'=>'submit');
79
    }
80
81
    /**
82
     *  Print selected file
83
     *
84
     * @param   string      $file       file
85
     * @param   string      $module     module
86
     * @param   string      $subdir     subdirectory of document like for expedition subdir is sendings
87
     *
88
     * @return  int                     0 if OK, >0 if KO
89
     */
90
    public function printFile($file, $module, $subdir='')
91
    {
92
        global $conf, $user;
93
        $error = 0;
94
95
        include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
96
97
        $ipp = new CupsPrintIPP();
98
        $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
99
        $ipp->setHost($this->host);
100
        $ipp->setPort($this->port);
101
        $ipp->setJobName($file,true);
102
        $ipp->setUserName($this->userid);
103
        if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
104
105
        // select printer uri for module order, propal,...
106
        $sql = "SELECT rowid,printer_id,copy FROM ".MAIN_DB_PREFIX."printing WHERE module = '".$module."' AND driver = 'printipp' AND userid = ".$user->id;
107
        $result = $this->db->query($sql);
108
        if ($result) {
109
            $obj = $this->db->fetch_object($result);
110
            if ($obj)
111
            {
112
            	dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
113
                $ipp->setPrinterURI($obj->printer_id);
114
            }
115
            else
116
            {
117
                if (! empty($conf->global->PRINTIPP_URI_DEFAULT))
118
                {
119
                    dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".$conf->global->PRINTIPP_URI_DEFAULT);
120
                    $ipp->setPrinterURI($conf->global->PRINTIPP_URI_DEFAULT);
121
                }
122
                else
123
                {
124
                    $this->errors[] = 'NoDefaultPrinterDefined';
125
                    $error++;
126
                    return $error;
127
                }
128
            }
129
        } else {
130
            dol_print_error($this->db);
131
        }
132
133
        // Set number of copy
134
        $ipp->setCopies($obj->copy);
0 ignored issues
show
Bug introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
135
        $fileprint=$conf->{$module}->dir_output;
136
        if ($subdir!='') $fileprint.='/'.$subdir;
137
        $fileprint.='/'.$file;
138
        $ipp->setData($fileprint);
139
        try {
140
            $ipp->printJob();
141
        } catch (Exception $e) {
142
            $this->errors[] = $e->getMessage();
143
            $error++;
144
        }
145
        if ($error==0) $this->errors[] = 'PRINTIPP: Job added';
146
147
        return $error;
148
    }
149
150
    /**
151
     *  Return list of available printers
152
     *
153
     *  @return  int                     0 if OK, >0 if KO
154
     */
155
    function listAvailablePrinters()
156
    {
157
        global $conf, $langs;
158
        $error = 0;
159
160
        $html = '<tr class="liste_titre">';
161
        $html.= '<td>'.$langs->trans('IPP_Uri').'</td>';
162
        $html.= '<td>'.$langs->trans('IPP_Name').'</td>';
163
        $html.= '<td>'.$langs->trans('IPP_State').'</td>';
164
        $html.= '<td>'.$langs->trans('IPP_State_reason').'</td>';
165
        $html.= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
166
        $html.= '<td>'.$langs->trans('IPP_BW').'</td>';
167
        $html.= '<td>'.$langs->trans('IPP_Color').'</td>';
168
        //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
169
        $html.= '<td>'.$langs->trans('IPP_Media').'</td>';
170
        $html.= '<td>'.$langs->trans('IPP_Supported').'</td>';
171
        $html.= '<td align="center">'.$langs->trans("Select").'</td>';
172
        $html.= "</tr>\n";
173
        $list = $this->getlistAvailablePrinters();
174
        foreach ($list as $value) {
175
            $printer_det = $this->getPrinterDetail($value);
176
            $html.= '<tr class="oddeven">';
177
            $html.= '<td>'.$value.'</td>';
178
            //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
179
            $html.= '<td>'.$printer_det->printer_name->_value0.'</td>';
180
            $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
181
            $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
182
            $html.= '<td>'.(! empty($printer_det->printer_state_reasons->_value1)?$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1):'').'</td>';
183
            $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
184
            $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
185
            //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
186
            $html.= '<td>'.$printer_det->media_default->_value0.'</td>';
187
            $html.= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
188
            // Defaut
189
            $html.= '<td align="center">';
190
            if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
191
                $html.= img_picto($langs->trans("Default"),'on');
192
            } else {
193
                $html.= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&amp;mode=test&amp;varname=PRINTIPP_URI_DEFAULT&amp;driver=printipp&amp;value='.urlencode($value).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"),'off').'</a>';
194
            }
195
            $html.= '</td>';
196
            $html.= '</tr>'."\n";
197
        }
198
        $this->resprint = $html;
199
        return $error;
200
    }
201
202
    /**
203
     *  Return list of available printers
204
     *
205
     *  @return array                list of printers
206
     */
207
    public function getlistAvailablePrinters()
208
    {
209
        global $conf, $db;
210
        include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
211
        $ipp = new CupsPrintIPP();
212
        $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
213
        $ipp->setHost($this->host);
214
        $ipp->setPort($this->port);
215
        $ipp->setUserName($this->userid);
216
        if (! empty($this->user)) {
217
            $ipp->setAuthentication($this->user, $this->password);
218
        }
219
        $ipp->getPrinters();
220
        return $ipp->available_printers;
221
    }
222
223
    /**
224
     *  Get printer detail
225
     *
226
     *  @param  string  $uri    URI
227
     *  @return array           List of attributes
228
     */
229
    private function getPrinterDetail($uri)
230
    {
231
        global $conf,$db;
232
233
        include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
234
        $ipp = new CupsPrintIPP();
235
        $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
236
        $ipp->setHost($this->host);
237
        $ipp->setPort($this->port);
238
        $ipp->setUserName($this->userid);
239
        if (! empty($this->user)) {
240
            $ipp->setAuthentication($this->user, $this->password);
241
        }
242
        $ipp->setPrinterURI($uri);
243
        $ipp->getPrinterAttributes();
244
        return $ipp->printer_attributes;
245
    }
246
247
    /**
248
     *  List jobs print
249
     *
250
     *  @param   string      $module     module
251
     *
252
     *  @return  int                     0 if OK, >0 if KO
253
     */
254
    public function listJobs($module)
255
    {
256
        global $conf;
257
        $error = 0;
258
        $html = '';
259
        include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
260
        $ipp = new CupsPrintIPP();
261
        $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
262
        $ipp->setHost($this->host);
263
        $ipp->setPort($this->port);
264
        $ipp->setUserName($this->userid);
265
        if (! empty($this->user)) {
266
            $ipp->setAuthentication($this->user,$this->password);
267
        }
268
        // select printer uri for module order, propal,...
269
        $sql = 'SELECT rowid,printer_uri,printer_name FROM '.MAIN_DB_PREFIX.'printer_ipp WHERE module="'.$module.'"';
270
        $result = $this->db->query($sql);
271
        if ($result) {
272
            $obj = $this->db->fetch_object($result);
273
            if ($obj) {
274
                $ipp->setPrinterURI($obj->printer_uri);
275
            } else {
276
                // All printers
277
                $ipp->setPrinterURI("ipp://localhost:631/printers/");
278
            }
279
        }
280
        // Getting Jobs
281
        try {
282
            $ipp->getJobs(false,0,'completed',false);
283
        } catch (Exception $e) {
284
            $this->errors[] = $e->getMessage();
285
            $error++;
286
        }
287
        $html .= '<table width="100%" class="noborder">';
288
        $html .= '<tr class="liste_titre">';
289
        $html .= '<td>Id</td>';
290
        $html .= '<td>Owner</td>';
291
        $html .= '<td>Printer</td>';
292
        $html .= '<td>File</td>';
293
        $html .= '<td>Status</td>';
294
        $html .= '<td>Cancel</td>';
295
        $html .= '</tr>'."\n";
296
        $jobs = $ipp->jobs_attributes;
297
298
        //$html .= '<pre>'.print_r($jobs,true).'</pre>';
299
        foreach ($jobs as $value ) {
0 ignored issues
show
Bug introduced by
The expression $jobs of type object<stdClass> is not traversable.
Loading history...
300
            $html .= '<tr class="oddeven">';
301
            $html .= '<td>'.$value->job_id->_value0.'</td>';
302
            $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
303
            $html .= '<td>'.$value->printer_uri->_value0.'</td>';
304
            $html .= '<td>'.$value->job_name->_value0.'</td>';
305
            $html .= '<td>'.$value->job_state->_value0.'</td>';
306
            $html .= '<td>'.$value->job_uri->_value0.'</td>';
307
            $html .= '</tr>';
308
        }
309
        $html .= "</table>";
310
        $this->resprint = $html;
311
        return $error;
312
    }
313
}
314