dolibarr  9.0.0
printipp.modules.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2014-2018 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 <http://www.gnu.org/licenses/>.
17  * or see http://www.gnu.org/
18  */
19 
26 include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
27 
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 
47  public $error='';
48 
52  public $errors = array();
53 
57  public $db;
58 
59 
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 
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);
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 
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 
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 
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 
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 ) {
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 }
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:292
listJobs($module)
List jobs print.
getPrinterDetail($uri)
Get printer detail.
Parent class of emailing target selectors modules.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
Class to provide printing with PrintIPP.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
printFile($file, $module, $subdir='')
Print selected file.
$conf db user
Definition: repair.php:104
getlistAvailablePrinters()
Return list of available printers.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
listAvailablePrinters()
Return list of available printers.
__construct($db)
Constructor.