printipp.modules.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. * or see http://www.gnu.org/
  16. */
  17. /**
  18. * \file htdocs/core/modules/printing/printipp.modules.php
  19. * \ingroup mailing
  20. * \brief File to provide printing with PrintIPP
  21. */
  22. include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
  23. /**
  24. * \class mailing_example
  25. * \brief Class to provide printing with PrintIPP
  26. */
  27. class printing_printipp extends PrintingDriver
  28. {
  29. var $name='printipp';
  30. var $desc='PrintIPPDesc';
  31. var $picto='printer';
  32. var $active='PRINTING_PRINTIPP';
  33. var $conf=array();
  34. var $host;
  35. var $port;
  36. var $userid; /* user login */
  37. var $user;
  38. var $password;
  39. var $error;
  40. var $db;
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDB $db Database handler
  45. */
  46. function __construct($db)
  47. {
  48. global $conf;
  49. $this->db=$db;
  50. $this->host=$conf->global->PRINTIPP_HOST;
  51. $this->port=$conf->global->PRINTIPP_PORT;
  52. $this->user=$conf->global->PRINTIPP_USER;
  53. $this->password=$conf->global->PRINTIPP_PASSWORD;
  54. $this->conf[] = array('varname'=>'PRINTIPP_HOST', 'required'=>1, 'example'=>'localhost', 'type'=>'text');
  55. $this->conf[] = array('varname'=>'PRINTIPP_PORT', 'required'=>1, 'example'=>'631', 'type'=>'text');
  56. $this->conf[] = array('varname'=>'PRINTIPP_USER', 'required'=>0, 'example'=>'', 'type'=>'text');
  57. $this->conf[] = array('varname'=>'PRINTIPP_PASSWORD', 'required'=>0, 'example'=>'', 'type'=>'password');
  58. }
  59. /**
  60. * Print selected file
  61. *
  62. * @param string $file file
  63. * @param string $module module
  64. * @param string $subdir subdirectory of document like for expedition subdir is sendings
  65. *
  66. * @return string '' if OK, Error message if KO
  67. */
  68. function print_file($file, $module, $subdir='')
  69. {
  70. global $conf,$db;
  71. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  72. $ipp = new CupsPrintIPP();
  73. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
  74. $ipp->setHost($this->host);
  75. $ipp->setPort($this->port);
  76. $ipp->setJobName($file,true);
  77. $ipp->setUserName($this->userid);
  78. if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
  79. // select printer uri for module order, propal,...
  80. $sql = 'SELECT rowid,printer_id,copy FROM '.MAIN_DB_PREFIX.'printing WHERE module="'.$module.'" AND driver="printipp"';
  81. $result = $db->query($sql);
  82. if ($result)
  83. {
  84. $obj = $this->db->fetch_object($result);
  85. if ($obj)
  86. {
  87. $ipp->setPrinterURI($obj->printer_id);
  88. }
  89. else
  90. {
  91. if (! empty($conf->global->PRINTIPP_URI_DEFAULT))
  92. {
  93. $ipp->setPrinterURI($conf->global->PRINTIPP_URI_DEFAULT);
  94. }
  95. else
  96. {
  97. return 'NoDefaultPrinterDefined';
  98. }
  99. }
  100. }
  101. // Set number of copy
  102. $ipp->setCopies($obj->copy);
  103. $fileprint=$conf->{$module}->dir_output;
  104. if ($subdir!='') $fileprint.='/'.$subdir;
  105. $fileprint.='/'.$file;
  106. $ipp->setData($fileprint);
  107. $ipp->printJob();
  108. return '';
  109. }
  110. /**
  111. * Return list of available printers
  112. *
  113. * @return string html list of printers
  114. */
  115. function listAvailablePrinters()
  116. {
  117. global $bc, $conf, $langs;
  118. $var=true;
  119. $html = '<table width="100%" class="noborder">';
  120. $html.= '<tr class="liste_titre">';
  121. $html.= '<td>'.$langs->trans('IPP_Uri').'</td>';
  122. $html.= '<td>'.$langs->trans('IPP_Name').'</td>';
  123. $html.= '<td>'.$langs->trans('IPP_State').'</td>';
  124. $html.= '<td>'.$langs->trans('IPP_State_reason').'</td>';
  125. $html.= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
  126. $html.= '<td>'.$langs->trans('IPP_BW').'</td>';
  127. $html.= '<td>'.$langs->trans('IPP_Color').'</td>';
  128. //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
  129. $html.= '<td>'.$langs->trans('IPP_Media').'</td>';
  130. $html.= '<td>'.$langs->trans('IPP_Supported').'</td>';
  131. $html.= '<td align="center">'.$langs->trans("Select").'</td>';
  132. $html.= "</tr>\n";
  133. $list = $this->getlist_available_printers();
  134. $var = true;
  135. foreach ($list as $value)
  136. {
  137. $var=!$var;
  138. $printer_det = $this->get_printer_detail($value);
  139. $html.= "<tr ".$bc[$var].">";
  140. $html.= '<td>'.$value.'</td>';
  141. //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
  142. $html.= '<td>'.$printer_det->printer_name->_value0.'</td>';
  143. $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
  144. $html.= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
  145. $html.= '<td>'.(! empty($printer_det->printer_state_reasons->_value1)?$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1):'').'</td>';
  146. $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
  147. $html.= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
  148. //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
  149. $html.= '<td>'.$printer_det->media_default->_value0.'</td>';
  150. $html.= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
  151. // Defaut
  152. $html.= '<td align="center">';
  153. if ($conf->global->PRINTIPP_URI_DEFAULT == $value)
  154. {
  155. $html.= img_picto($langs->trans("Default"),'on');
  156. }
  157. else
  158. $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>';
  159. $html.= '</td>';
  160. $html.= '</tr>'."\n";
  161. }
  162. return $html;
  163. }
  164. /**
  165. * Return list of available printers
  166. *
  167. * @return array list of printers
  168. */
  169. function getlist_available_printers()
  170. {
  171. global $conf,$db;
  172. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  173. $ipp = new CupsPrintIPP();
  174. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
  175. $ipp->setHost($this->host);
  176. $ipp->setPort($this->port);
  177. $ipp->setUserName($this->userid);
  178. if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
  179. $ipp->getPrinters();
  180. return $ipp->available_printers;
  181. }
  182. /**
  183. * Get printer detail
  184. *
  185. * @param string $uri URI
  186. * @return array List of attributes
  187. */
  188. function get_printer_detail($uri)
  189. {
  190. global $conf,$db;
  191. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  192. $ipp = new CupsPrintIPP();
  193. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
  194. $ipp->setHost($this->host);
  195. $ipp->setPort($this->port);
  196. $ipp->setUserName($this->userid);
  197. if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
  198. $ipp->setPrinterURI($uri);
  199. $ipp->getPrinterAttributes();
  200. return $ipp->printer_attributes;
  201. }
  202. /**
  203. * List jobs print
  204. *
  205. * @param string $module module
  206. *
  207. * @return void
  208. */
  209. function list_jobs($module)
  210. {
  211. global $conf, $db, $bc;
  212. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  213. $ipp = new CupsPrintIPP();
  214. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log','file',3); // logging very verbose
  215. $ipp->setHost($this->host);
  216. $ipp->setPort($this->port);
  217. $ipp->setUserName($this->userid);
  218. if (! empty($this->user)) $ipp->setAuthentication($this->user,$this->password);
  219. // select printer uri for module order, propal,...
  220. $sql = 'SELECT rowid,printer_uri,printer_name FROM '.MAIN_DB_PREFIX.'printer_ipp WHERE module="'.$module.'"';
  221. $result = $this->db->query($sql);
  222. if ($result)
  223. {
  224. $obj = $this->db->fetch_object($result);
  225. if ($obj)
  226. {
  227. $ipp->setPrinterURI($obj->printer_uri);
  228. }
  229. else
  230. {
  231. // All printers
  232. $ipp->setPrinterURI("ipp://localhost:631/printers/");
  233. }
  234. }
  235. // Getting Jobs
  236. $ipp->getJobs(false,0,'completed',false);
  237. print '<table width="100%" class="noborder">';
  238. print '<tr class="liste_titre">';
  239. print "<td>Id</td>";
  240. print "<td>Owner</td>";
  241. print "<td>Printer</td>";
  242. print "<td>File</td>";
  243. print "<td>Status</td>";
  244. print "<td>Cancel</td>";
  245. print "</tr>\n";
  246. $jobs = $ipp->jobs_attributes;
  247. $var = True;
  248. //print '<pre>'.print_r($jobs,true).'</pre>';
  249. foreach ($jobs as $value )
  250. {
  251. $var=!$var;
  252. print "<tr ".$bc[$var].">";
  253. print '<td>'.$value->job_id->_value0.'</td>';
  254. print '<td>'.$value->job_originating_user_name->_value0.'</td>';
  255. print '<td>'.$value->printer_uri->_value0.'</td>';
  256. print '<td>'.$value->job_name->_value0.'</td>';
  257. print '<td>'.$value->job_state->_value0.'</td>';
  258. print '<td>'.$value->job_uri->_value0.'</td>';
  259. print '</tr>';
  260. }
  261. print "</table>";
  262. }
  263. }