dolibarr  13.0.2
printipp.modules.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2014-2020 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 = $conf->global->PRINTIPP_HOST;
112  $this->port = $conf->global->PRINTIPP_PORT;
113  $this->user = $conf->global->PRINTIPP_USER;
114  $this->password = $conf->global->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)) $ipp->setAuthentication($this->user, $this->password);
147 
148  // select printer uri for module order, propal,...
149  $sql = "SELECT rowid,printer_id,copy FROM ".MAIN_DB_PREFIX."printing WHERE module = '".$this->db->escape($module)."' AND driver = 'printipp' AND userid = ".$user->id;
150  $result = $this->db->query($sql);
151  if ($result) {
152  $obj = $this->db->fetch_object($result);
153  if ($obj) {
154  dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
155  $ipp->setPrinterURI($obj->printer_id);
156  // Set number of copy
157  $ipp->setCopies($obj->copy);
158  } else {
159  if (!empty($conf->global->PRINTIPP_URI_DEFAULT))
160  {
161  dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".$conf->global->PRINTIPP_URI_DEFAULT);
162  $ipp->setPrinterURI($conf->global->PRINTIPP_URI_DEFAULT);
163  } else {
164  $this->errors[] = 'NoDefaultPrinterDefined';
165  $error++;
166  return $error;
167  }
168  }
169  } else {
170  dol_print_error($this->db);
171  }
172 
173  $fileprint = $conf->{$module}->dir_output;
174  if ($subdir != '') $fileprint .= '/'.$subdir;
175  $fileprint .= '/'.$file;
176  $ipp->setData($fileprint);
177  try {
178  $ipp->printJob();
179  } catch (Exception $e) {
180  $this->errors[] = $e->getMessage();
181  $error++;
182  }
183  if ($error == 0) $this->errors[] = 'PRINTIPP: Job added';
184 
185  return $error;
186  }
187 
193  public function listAvailablePrinters()
194  {
195  global $conf, $langs;
196  $error = 0;
197 
198  $html = '<tr class="liste_titre">';
199  $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
200  $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
201  $html .= '<td>'.$langs->trans('IPP_State').'</td>';
202  $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
203  $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
204  $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
205  $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
206  //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
207  $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
208  $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
209  $html .= '<td class="center">'.$langs->trans("Select").'</td>';
210  $html .= "</tr>\n";
211  $list = $this->getlistAvailablePrinters();
212  foreach ($list as $value) {
213  $printer_det = $this->getPrinterDetail($value);
214  $html .= '<tr class="oddeven">';
215  $html .= '<td>'.$value.'</td>';
216  //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
217  $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
218  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
219  $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
220  $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
221  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
222  $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
223  //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
224  $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
225  $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
226  // Defaut
227  $html .= '<td class="center">';
228  if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
229  $html .= img_picto($langs->trans("Default"), 'on');
230  } else {
231  $html .= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&amp;token='.newToken().'&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>';
232  }
233  $html .= '</td>';
234  $html .= '</tr>'."\n";
235  }
236  $this->resprint = $html;
237  return $error;
238  }
239 
245  public function getlistAvailablePrinters()
246  {
247  global $conf, $db;
248  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
249  $ipp = new CupsPrintIPP();
250  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
251  $ipp->setHost($this->host);
252  $ipp->setPort($this->port);
253  $ipp->setUserName($this->userid);
254  if (!empty($this->user)) {
255  $ipp->setAuthentication($this->user, $this->password);
256  }
257  $ipp->getPrinters();
258  return $ipp->available_printers;
259  }
260 
267  private function getPrinterDetail($uri)
268  {
269  global $conf, $db;
270 
271  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
272  $ipp = new CupsPrintIPP();
273  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
274  $ipp->setHost($this->host);
275  $ipp->setPort($this->port);
276  $ipp->setUserName($this->userid);
277  if (!empty($this->user)) {
278  $ipp->setAuthentication($this->user, $this->password);
279  }
280  $ipp->setPrinterURI($uri);
281  $ipp->getPrinterAttributes();
282  return $ipp->printer_attributes;
283  }
284 
292  public function listJobs($module)
293  {
294  global $conf;
295  $error = 0;
296  $html = '';
297  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
298  $ipp = new CupsPrintIPP();
299  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
300  $ipp->setHost($this->host);
301  $ipp->setPort($this->port);
302  $ipp->setUserName($this->userid);
303  if (!empty($this->user)) {
304  $ipp->setAuthentication($this->user, $this->password);
305  }
306  // select printer uri for module order, propal,...
307  $sql = 'SELECT rowid,printer_uri,printer_name FROM '.MAIN_DB_PREFIX.'printer_ipp WHERE module="'.$module.'"';
308  $result = $this->db->query($sql);
309  if ($result) {
310  $obj = $this->db->fetch_object($result);
311  if ($obj) {
312  $ipp->setPrinterURI($obj->printer_uri);
313  } else {
314  // All printers
315  $ipp->setPrinterURI("ipp://localhost:631/printers/");
316  }
317  }
318  // Getting Jobs
319  try {
320  $ipp->getJobs(false, 0, 'completed', false);
321  } catch (Exception $e) {
322  $this->errors[] = $e->getMessage();
323  $error++;
324  }
325  $html .= '<table width="100%" class="noborder">';
326  $html .= '<tr class="liste_titre">';
327  $html .= '<td>Id</td>';
328  $html .= '<td>Owner</td>';
329  $html .= '<td>Printer</td>';
330  $html .= '<td>File</td>';
331  $html .= '<td>Status</td>';
332  $html .= '<td>Cancel</td>';
333  $html .= '</tr>'."\n";
334  $jobs = $ipp->jobs_attributes;
335 
336  //$html .= '<pre>'.print_r($jobs,true).'</pre>';
337  foreach ($jobs as $value) {
338  $html .= '<tr class="oddeven">';
339  $html .= '<td>'.$value->job_id->_value0.'</td>';
340  $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
341  $html .= '<td>'.$value->printer_uri->_value0.'</td>';
342  $html .= '<td>'.$value->job_name->_value0.'</td>';
343  $html .= '<td>'.$value->job_state->_value0.'</td>';
344  $html .= '<td>'.$value->job_uri->_value0.'</td>';
345  $html .= '</tr>';
346  }
347  $html .= "</table>";
348  $this->resprint = $html;
349  return $error;
350  }
351 }
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:262
listJobs($module)
List jobs print.
getPrinterDetail($uri)
Get printer detail.
Parent class of emailing target selectors modules.
printFile($file, $module, $subdir= '')
Print selected file.
$conf db
API class for accounts.
Definition: inc.php:54
Class to provide printing with PrintIPP.
$conf db user
Definition: repair.php:109
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
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...
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
listAvailablePrinters()
Return list of available printers.
__construct($db)
Constructor.