dolibarr  7.0.0-beta
printipp.modules.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2014-2015 Frederic France <frederic.france@free.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  var $name='printipp';
34  var $desc='PrintIPPDesc';
35  var $picto='printer';
36  var $active='PRINTING_PRINTIPP';
37  var $conf=array();
38  var $host;
39  var $port;
40  var $userid; /* user login */
41  var $user;
42  var $password;
43  var $error;
44  var $errors = array();
45  var $db;
46 
47 
53  function __construct($db)
54  {
55  global $conf;
56 
57  $this->db=$db;
58  $this->host=$conf->global->PRINTIPP_HOST;
59  $this->port=$conf->global->PRINTIPP_PORT;
60  $this->user=$conf->global->PRINTIPP_USER;
61  $this->password=$conf->global->PRINTIPP_PASSWORD;
62  $this->conf[] = array('varname'=>'PRINTIPP_HOST', 'required'=>1, 'example'=>'localhost', 'type'=>'text');
63  $this->conf[] = array('varname'=>'PRINTIPP_PORT', 'required'=>1, 'example'=>'631', 'type'=>'text');
64  $this->conf[] = array('varname'=>'PRINTIPP_USER', 'required'=>0, 'example'=>'', 'type'=>'text', 'moreattributes'=>'autocomplete="off"');
65  $this->conf[] = array('varname'=>'PRINTIPP_PASSWORD', 'required'=>0, 'example'=>'', 'type'=>'password', 'moreattributes'=>'autocomplete="off"');
66  $this->conf[] = array('enabled'=>1, 'type'=>'submit');
67  }
68 
78  function print_file($file, $module, $subdir='')
79  {
80  global $conf, $user;
81  $error = 0;
82 
83  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
84 
85  $ipp = new CupsPrintIPP();
86  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
87  $ipp->setHost($this->host);
88  $ipp->setPort($this->port);
89  $ipp->setJobName($file,true);
90  $ipp->setUserName($this->userid);
91  if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
92 
93  // select printer uri for module order, propal,...
94  $sql = "SELECT rowid,printer_id,copy FROM ".MAIN_DB_PREFIX."printing WHERE module = '".$module."' AND driver = 'printipp' AND userid = ".$user->id;
95  $result = $this->db->query($sql);
96  if ($result)
97  {
98  $obj = $this->db->fetch_object($result);
99  if ($obj)
100  {
101  dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
102  $ipp->setPrinterURI($obj->printer_id);
103  }
104  else
105  {
106  if (! empty($conf->global->PRINTIPP_URI_DEFAULT))
107  {
108  dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".$conf->global->PRINTIPP_URI_DEFAULT);
109  $ipp->setPrinterURI($conf->global->PRINTIPP_URI_DEFAULT);
110  }
111  else
112  {
113  $this->errors[] = 'NoDefaultPrinterDefined';
114  $error++;
115  return $error;
116  }
117  }
118  }
119  else dol_print_error($this->db);
120 
121  // Set number of copy
122  $ipp->setCopies($obj->copy);
123  $fileprint=$conf->{$module}->dir_output;
124  if ($subdir!='') $fileprint.='/'.$subdir;
125  $fileprint.='/'.$file;
126  $ipp->setData($fileprint);
127  try {
128  $ipp->printJob();
129  } catch (Exception $e) {
130  $this->errors[] = $e->getMessage();
131  $error++;
132  }
133  if ($error==0) $this->errors[] = 'PRINTIPP: Job added';
134 
135  return $error;
136  }
137 
144  {
145  global $bc, $conf, $langs;
146  $error = 0;
147  $var=true;
148 
149  $html = '<tr class="liste_titre">';
150  $html.= '<td>'.$langs->trans('IPP_Uri').'</td>';
151  $html.= '<td>'.$langs->trans('IPP_Name').'</td>';
152  $html.= '<td>'.$langs->trans('IPP_State').'</td>';
153  $html.= '<td>'.$langs->trans('IPP_State_reason').'</td>';
154  $html.= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
155  $html.= '<td>'.$langs->trans('IPP_BW').'</td>';
156  $html.= '<td>'.$langs->trans('IPP_Color').'</td>';
157  //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
158  $html.= '<td>'.$langs->trans('IPP_Media').'</td>';
159  $html.= '<td>'.$langs->trans('IPP_Supported').'</td>';
160  $html.= '<td align="center">'.$langs->trans("Select").'</td>';
161  $html.= "</tr>\n";
162  $list = $this->getlist_available_printers();
163  $var = true;
164  foreach ($list as $value)
165  {
166 
167  $printer_det = $this->get_printer_detail($value);
168  $html.= "<tr ".$bc[$var].">";
169  $html.= '<td>'.$value.'</td>';
170  //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
171  $html.= '<td>'.$printer_det->printer_name->_value0.'</td>';
172  $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
173  $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
174  $html.= '<td>'.(! empty($printer_det->printer_state_reasons->_value1)?$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1):'').'</td>';
175  $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
176  $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
177  //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
178  $html.= '<td>'.$printer_det->media_default->_value0.'</td>';
179  $html.= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
180  // Defaut
181  $html.= '<td align="center">';
182  if ($conf->global->PRINTIPP_URI_DEFAULT == $value)
183  {
184  $html.= img_picto($langs->trans("Default"),'on');
185  }
186  else
187  {
188  $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>';
189  }
190  $html.= '</td>';
191  $html.= '</tr>'."\n";
192  }
193  $this->resprint = $html;
194  return $error;
195  }
196 
203  {
204  global $conf,$db;
205  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
206  $ipp = new CupsPrintIPP();
207  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
208  $ipp->setHost($this->host);
209  $ipp->setPort($this->port);
210  $ipp->setUserName($this->userid);
211  if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
212  $ipp->getPrinters();
213  return $ipp->available_printers;
214  }
215 
222  function get_printer_detail($uri)
223  {
224  global $conf,$db;
225 
226  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
227  $ipp = new CupsPrintIPP();
228  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
229  $ipp->setHost($this->host);
230  $ipp->setPort($this->port);
231  $ipp->setUserName($this->userid);
232  if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
233  $ipp->setPrinterURI($uri);
234  $ipp->getPrinterAttributes();
235  return $ipp->printer_attributes;
236  }
237 
245  function list_jobs($module)
246  {
247  global $conf, $db, $bc;
248  $error = 0;
249  $html = '';
250  include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
251  $ipp = new CupsPrintIPP();
252  $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
253  $ipp->setHost($this->host);
254  $ipp->setPort($this->port);
255  $ipp->setUserName($this->userid);
256  if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
257  // select printer uri for module order, propal,...
258  $sql = 'SELECT rowid,printer_uri,printer_name FROM '.MAIN_DB_PREFIX.'printer_ipp WHERE module="'.$module.'"';
259  $result = $this->db->query($sql);
260  if ($result)
261  {
262  $obj = $this->db->fetch_object($result);
263  if ($obj)
264  {
265  $ipp->setPrinterURI($obj->printer_uri);
266  }
267  else
268  {
269  // All printers
270  $ipp->setPrinterURI("ipp://localhost:631/printers/");
271  }
272  }
273  // Getting Jobs
274  try {
275  $ipp->getJobs(false,0,'completed',false);
276  } catch (Exception $e) {
277  $this->errors[] = $e->getMessage();
278  $error++;
279  }
280  $html .= '<table width="100%" class="noborder">';
281  $html .= '<tr class="liste_titre">';
282  $html .= '<td>Id</td>';
283  $html .= '<td>Owner</td>';
284  $html .= '<td>Printer</td>';
285  $html .= '<td>File</td>';
286  $html .= '<td>Status</td>';
287  $html .= '<td>Cancel</td>';
288  $html .= '</tr>'."\n";
289  $jobs = $ipp->jobs_attributes;
290  $var = True;
291  //$html .= '<pre>'.print_r($jobs,true).'</pre>';
292  foreach ($jobs as $value )
293  {
294  $var = !$var;
295  $html .= '<tr '.$bc[$var].'>';
296  $html .= '<td>'.$value->job_id->_value0.'</td>';
297  $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
298  $html .= '<td>'.$value->printer_uri->_value0.'</td>';
299  $html .= '<td>'.$value->job_name->_value0.'</td>';
300  $html .= '<td>'.$value->job_state->_value0.'</td>';
301  $html .= '<td>'.$value->job_uri->_value0.'</td>';
302  $html .= '</tr>';
303  }
304  $html .= "</table>";
305  $this->resprint = $html;
306  return $error;
307  }
308 
309 }
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition: inc.php:282
list_jobs($module)
List jobs print.
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.
get_printer_detail($uri)
Get printer detail.
$conf db user
Definition: repair.php:105
print_file($file, $module, $subdir='')
Print selected file.
getlist_available_printers()
Return list of available printers.
listAvailablePrinters()
Return list of available printers.
__construct($db)
Constructor.