dolibarr  20.0.0-beta
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  * or see https://www.gnu.org/
19  */
20 
27 include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
28 
33 {
37  public $name = 'printipp';
38 
42  public $desc = 'PrintIPPDesc';
43 
47  public $picto = 'printer';
48 
52  public $active = 'PRINTING_PRINTIPP';
53 
57  public $conf = array();
58 
62  public $host;
63 
67  public $port;
68 
72  public $userid;
73 
77  public $user;
78 
82  public $password;
83 
87  public $ssl;
88 
92  public $error = '';
93 
97  public $errors = array();
98 
102  public $db;
103 
104 
110  public function __construct($db)
111  {
112  global $conf;
113 
114  $this->db = $db;
115  $this->host = getDolGlobalString('PRINTIPP_HOST');
116  $this->port = getDolGlobalString('PRINTIPP_PORT');
117  $this->user = getDolGlobalString('PRINTIPP_USER');
118  $this->password = getDolGlobalString('PRINTIPP_PASSWORD');
119  $this->ssl = getDolGlobalInt('PRINTIPP_SSL');
120  $this->conf[] = array('varname' => 'PRINTIPP_HOST', 'required' => 1, 'example' => 'localhost', 'type' => 'text');
121  $this->conf[] = array('varname' => 'PRINTIPP_PORT', 'required' => 1, 'example' => '631', 'type' => 'text');
122  $this->conf[] = array('varname' => 'PRINTIPP_USER', 'required' => 0, 'example' => '', 'type' => 'text', 'moreattributes' => 'autocomplete="off"');
123  $this->conf[] = array('varname' => 'PRINTIPP_PASSWORD', 'required' => 0, 'example' => '', 'type' => 'password', 'moreattributes' => 'autocomplete="off"');
124  $this->conf[] = array('varname' => 'PRINTIPP_SSL', 'required' => 0, 'example' => '', 'type' => 'checkbox', 'moreattributes' => 'autocomplete="off"');
125  $this->conf[] = array('enabled' => 1, 'type' => 'submit');
126  }
127 
137  public function printFile($file, $module, $subdir = '')
138  {
139  global $conf, $user;
140  $error = 0;
141 
142  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
143 
144  $ipp = new CupsPrintIPP();
145  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
146  $ipp->setHost($this->host);
147  $ipp->setPort($this->port);
148  $ipp->ssl = $this->ssl;
149  $ipp->setJobName($file, true);
150  $ipp->setUserName($this->userid);
151  // Set default number of copy
152  $ipp->setCopies(1);
153  if (!empty($this->user)) {
154  $ipp->setAuthentication($this->user, $this->password);
155  }
156 
157  // select printer uri for module order, propal,...
158  $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);
159  $result = $this->db->query($sql);
160  if ($result) {
161  $obj = $this->db->fetch_object($result);
162  if ($obj) {
163  dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
164  $ipp->setPrinterURI($obj->printer_id);
165  // Set number of copy
166  $ipp->setCopies($obj->copy);
167  } else {
168  if (getDolGlobalString('PRINTIPP_URI_DEFAULT')) {
169  dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".getDolGlobalString('PRINTIPP_URI_DEFAULT'));
170  $ipp->setPrinterURI(getDolGlobalString('PRINTIPP_URI_DEFAULT'));
171  } else {
172  $this->errors[] = 'NoDefaultPrinterDefined';
173  $error++;
174  return $error;
175  }
176  }
177  } else {
178  dol_print_error($this->db);
179  }
180 
181  $fileprint = $conf->{$module}->dir_output;
182  if ($subdir != '') {
183  $fileprint .= '/'.$subdir;
184  }
185  $fileprint .= '/'.$file;
186  $ipp->setData($fileprint);
187  try {
188  $ipp->printJob();
189  } catch (Exception $e) {
190  $this->errors[] = $e->getMessage();
191  $error++;
192  }
193  if ($error == 0) {
194  $this->errors[] = 'PRINTIPP: Job added';
195  }
196 
197  return $error;
198  }
199 
207  public function listAvailablePrinters()
208  {
209  global $conf, $langs;
210  $error = 0;
211 
212  $html = '<tr class="liste_titre">';
213  $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
214  $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
215  $html .= '<td>'.$langs->trans('IPP_State').'</td>';
216  $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
217  $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
218  $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
219  $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
220  //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
221  $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
222  $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
223  $html .= '<td class="center">'.$langs->trans("Select").'</td>';
224  $html .= "</tr>\n";
225  $list = $this->getlistAvailablePrinters();
226  foreach ($list as $value) {
227  $printer_det = $this->getPrinterDetail($value);
228  '@phan-var-force stdClass $printer_det';
229  $html .= '<tr class="oddeven">';
230  $html .= '<td>'.$value.'</td>';
231  //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
232  $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
233  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
234  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
235  $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
236  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
237  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
238  //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
239  $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
240  $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
241  // Default
242  $html .= '<td class="center">';
243  if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
244  $html .= img_picto($langs->trans("Default"), 'on');
245  } else {
246  $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>';
247  }
248  $html .= '</td>';
249  $html .= '</tr>'."\n";
250  }
251  $this->resprint = $html;
252  return $error;
253  }
254 
260  public function getlistAvailablePrinters()
261  {
262  global $conf, $db;
263  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
264  $ipp = new CupsPrintIPP();
265  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
266  $ipp->setHost($this->host);
267  $ipp->setPort($this->port);
268  $ipp->setUserName($this->userid);
269  if (!empty($this->user)) {
270  $ipp->setAuthentication($this->user, $this->password);
271  }
272  $ipp->getPrinters();
273  return $ipp->available_printers;
274  }
275 
282  private function getPrinterDetail($uri)
283  {
284  global $conf, $db;
285 
286  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
287  $ipp = new CupsPrintIPP();
288  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
289  $ipp->setHost($this->host);
290  $ipp->setPort($this->port);
291  $ipp->setUserName($this->userid);
292  if (!empty($this->user)) {
293  $ipp->setAuthentication($this->user, $this->password);
294  }
295  $ipp->setPrinterURI($uri);
296  $ipp->getPrinterAttributes();
297  return $ipp->printer_attributes;
298  }
299 
307  public function listJobs($module)
308  {
309  global $conf;
310  $error = 0;
311  $html = '';
312  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
313  $ipp = new CupsPrintIPP();
314  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
315  $ipp->setHost($this->host);
316  $ipp->setPort($this->port);
317  $ipp->setUserName($this->userid);
318  if (!empty($this->user)) {
319  $ipp->setAuthentication($this->user, $this->password);
320  }
321  // select printer uri for module order, propal,...
322  $sql = "SELECT rowid,printer_uri,printer_name FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape($module)."'";
323  $result = $this->db->query($sql);
324  if ($result) {
325  $obj = $this->db->fetch_object($result);
326  if ($obj) {
327  $ipp->setPrinterURI($obj->printer_uri);
328  } else {
329  // All printers
330  $ipp->setPrinterURI("ipp://localhost:631/printers/");
331  }
332  }
333  // Getting Jobs
334  try {
335  $ipp->getJobs(false, 0, 'completed', false);
336  } catch (Exception $e) {
337  $this->errors[] = $e->getMessage();
338  $error++;
339  }
340  $html .= '<table width="100%" class="noborder">';
341  $html .= '<tr class="liste_titre">';
342  $html .= '<td>Id</td>';
343  $html .= '<td>Owner</td>';
344  $html .= '<td>Printer</td>';
345  $html .= '<td>File</td>';
346  $html .= '<td>Status</td>';
347  $html .= '<td>Cancel</td>';
348  $html .= '</tr>'."\n";
349  $jobs = $ipp->jobs_attributes;
350 
351  //$html .= '<pre>'.print_r($jobs,true).'</pre>';
352  foreach ($jobs as $value) {
353  $html .= '<tr class="oddeven">';
354  $html .= '<td>'.$value->job_id->_value0.'</td>';
355  $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
356  $html .= '<td>'.$value->printer_uri->_value0.'</td>';
357  $html .= '<td>'.$value->job_name->_value0.'</td>';
358  $html .= '<td>'.$value->job_state->_value0.'</td>';
359  $html .= '<td>'.$value->job_uri->_value0.'</td>';
360  $html .= '</tr>';
361  }
362  $html .= "</table>";
363  $this->resprint = $html;
364  return $error;
365  }
366 }
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.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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:419
$conf db user
Active Directory does not allow anonymous connections.
Definition: repair.php:127