dolibarr  17.0.4
printipp.modules.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2014-2021 Frederic France <frederic.france@netlogic.fr>
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 <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
26 include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
27 
32 {
36  public $name = 'printipp';
37 
41  public $desc = 'PrintIPPDesc';
42 
46  public $picto = 'printer';
47 
51  public $active = 'PRINTING_PRINTIPP';
52 
56  public $conf = array();
57 
61  public $host;
62 
66  public $port;
67 
71  public $userid;
72 
76  public $user;
77 
81  public $password;
82 
86  public $error = '';
87 
91  public $errors = array();
92 
96  public $db;
97 
98  const LANGFILE = 'printipp';
99 
100 
106  public function __construct($db)
107  {
108  global $conf;
109 
110  $this->db = $db;
111  $this->host = getDolGlobalString('PRINTIPP_HOST');
112  $this->port = getDolGlobalString('PRINTIPP_PORT');
113  $this->user = getDolGlobalString('PRINTIPP_USER');
114  $this->password = getDolGlobalString('PRINTIPP_PASSWORD');
115  $this->conf[] = array('varname'=>'PRINTIPP_HOST', 'required'=>1, 'example'=>'localhost', 'type'=>'text');
116  $this->conf[] = array('varname'=>'PRINTIPP_PORT', 'required'=>1, 'example'=>'631', 'type'=>'text');
117  $this->conf[] = array('varname'=>'PRINTIPP_USER', 'required'=>0, 'example'=>'', 'type'=>'text', 'moreattributes'=>'autocomplete="off"');
118  $this->conf[] = array('varname'=>'PRINTIPP_PASSWORD', 'required'=>0, 'example'=>'', 'type'=>'password', 'moreattributes'=>'autocomplete="off"');
119  $this->conf[] = array('enabled'=>1, 'type'=>'submit');
120  }
121 
131  public function printFile($file, $module, $subdir = '')
132  {
133  global $conf, $user;
134  $error = 0;
135 
136  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
137 
138  $ipp = new CupsPrintIPP();
139  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
140  $ipp->setHost($this->host);
141  $ipp->setPort($this->port);
142  $ipp->setJobName($file, true);
143  $ipp->setUserName($this->userid);
144  // Set default number of copy
145  $ipp->setCopies(1);
146  if (!empty($this->user)) {
147  $ipp->setAuthentication($this->user, $this->password);
148  }
149 
150  // select printer uri for module order, propal,...
151  $sql = "SELECT rowid,printer_id,copy FROM ".MAIN_DB_PREFIX."printing WHERE module = '".$this->db->escape($module)."' AND driver = 'printipp' AND userid = ".((int) $user->id);
152  $result = $this->db->query($sql);
153  if ($result) {
154  $obj = $this->db->fetch_object($result);
155  if ($obj) {
156  dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
157  $ipp->setPrinterURI($obj->printer_id);
158  // Set number of copy
159  $ipp->setCopies($obj->copy);
160  } else {
161  if (!empty($conf->global->PRINTIPP_URI_DEFAULT)) {
162  dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".getDolGlobalString('PRINTIPP_URI_DEFAULT'));
163  $ipp->setPrinterURI(getDolGlobalString('PRINTIPP_URI_DEFAULT'));
164  } else {
165  $this->errors[] = 'NoDefaultPrinterDefined';
166  $error++;
167  return $error;
168  }
169  }
170  } else {
171  dol_print_error($this->db);
172  }
173 
174  $fileprint = $conf->{$module}->dir_output;
175  if ($subdir != '') {
176  $fileprint .= '/'.$subdir;
177  }
178  $fileprint .= '/'.$file;
179  $ipp->setData($fileprint);
180  try {
181  $ipp->printJob();
182  } catch (Exception $e) {
183  $this->errors[] = $e->getMessage();
184  $error++;
185  }
186  if ($error == 0) {
187  $this->errors[] = 'PRINTIPP: Job added';
188  }
189 
190  return $error;
191  }
192 
198  public function listAvailablePrinters()
199  {
200  global $conf, $langs;
201  $error = 0;
202 
203  $html = '<tr class="liste_titre">';
204  $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
205  $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
206  $html .= '<td>'.$langs->trans('IPP_State').'</td>';
207  $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
208  $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
209  $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
210  $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
211  //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
212  $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
213  $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
214  $html .= '<td class="center">'.$langs->trans("Select").'</td>';
215  $html .= "</tr>\n";
216  $list = $this->getlistAvailablePrinters();
217  foreach ($list as $value) {
218  $printer_det = $this->getPrinterDetail($value);
219  $html .= '<tr class="oddeven">';
220  $html .= '<td>'.$value.'</td>';
221  //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
222  $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
223  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
224  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
225  $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
226  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
227  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
228  //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
229  $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
230  $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
231  // Defaut
232  $html .= '<td class="center">';
233  if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
234  $html .= img_picto($langs->trans("Default"), 'on');
235  } else {
236  $html .= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'&mode=test&varname=PRINTIPP_URI_DEFAULT&driver=printipp&value='.urlencode($value).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
237  }
238  $html .= '</td>';
239  $html .= '</tr>'."\n";
240  }
241  $this->resprint = $html;
242  return $error;
243  }
244 
250  public function getlistAvailablePrinters()
251  {
252  global $conf, $db;
253  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
254  $ipp = new CupsPrintIPP();
255  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
256  $ipp->setHost($this->host);
257  $ipp->setPort($this->port);
258  $ipp->setUserName($this->userid);
259  if (!empty($this->user)) {
260  $ipp->setAuthentication($this->user, $this->password);
261  }
262  $ipp->getPrinters();
263  return $ipp->available_printers;
264  }
265 
272  private function getPrinterDetail($uri)
273  {
274  global $conf, $db;
275 
276  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
277  $ipp = new CupsPrintIPP();
278  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
279  $ipp->setHost($this->host);
280  $ipp->setPort($this->port);
281  $ipp->setUserName($this->userid);
282  if (!empty($this->user)) {
283  $ipp->setAuthentication($this->user, $this->password);
284  }
285  $ipp->setPrinterURI($uri);
286  $ipp->getPrinterAttributes();
287  return $ipp->printer_attributes;
288  }
289 
297  public function listJobs($module)
298  {
299  global $conf;
300  $error = 0;
301  $html = '';
302  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
303  $ipp = new CupsPrintIPP();
304  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
305  $ipp->setHost($this->host);
306  $ipp->setPort($this->port);
307  $ipp->setUserName($this->userid);
308  if (!empty($this->user)) {
309  $ipp->setAuthentication($this->user, $this->password);
310  }
311  // select printer uri for module order, propal,...
312  $sql = "SELECT rowid,printer_uri,printer_name FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape($module)."'";
313  $result = $this->db->query($sql);
314  if ($result) {
315  $obj = $this->db->fetch_object($result);
316  if ($obj) {
317  $ipp->setPrinterURI($obj->printer_uri);
318  } else {
319  // All printers
320  $ipp->setPrinterURI("ipp://localhost:631/printers/");
321  }
322  }
323  // Getting Jobs
324  try {
325  $ipp->getJobs(false, 0, 'completed', false);
326  } catch (Exception $e) {
327  $this->errors[] = $e->getMessage();
328  $error++;
329  }
330  $html .= '<table width="100%" class="noborder">';
331  $html .= '<tr class="liste_titre">';
332  $html .= '<td>Id</td>';
333  $html .= '<td>Owner</td>';
334  $html .= '<td>Printer</td>';
335  $html .= '<td>File</td>';
336  $html .= '<td>Status</td>';
337  $html .= '<td>Cancel</td>';
338  $html .= '</tr>'."\n";
339  $jobs = $ipp->jobs_attributes;
340 
341  //$html .= '<pre>'.print_r($jobs,true).'</pre>';
342  foreach ($jobs as $value) {
343  $html .= '<tr class="oddeven">';
344  $html .= '<td>'.$value->job_id->_value0.'</td>';
345  $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
346  $html .= '<td>'.$value->printer_uri->_value0.'</td>';
347  $html .= '<td>'.$value->job_name->_value0.'</td>';
348  $html .= '<td>'.$value->job_state->_value0.'</td>';
349  $html .= '<td>'.$value->job_uri->_value0.'</td>';
350  $html .= '</tr>';
351  }
352  $html .= "</table>";
353  $this->resprint = $html;
354  return $error;
355  }
356 }
Parent class of emailing target selectors modules.
Class to provide printing with PrintIPP.
getPrinterDetail($uri)
Get printer detail.
printFile($file, $module, $subdir='')
Print selected file.
__construct($db)
Constructor.
listAvailablePrinters()
Return list of available printers.
listJobs($module)
List jobs print.
getlistAvailablePrinters()
Return list of available printers.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:292
$conf db user
Definition: repair.php:123
$conf db
API class for accounts.
Definition: inc.php:41