dolibarr 23.0.3
printing.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014-2024 Frédéric France <frederic.france@free.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 */
19
26// Load Dolibarr environment
27require '../../main.inc.php';
28
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
32require_once DOL_DOCUMENT_ROOT.'/printing/lib/printing.lib.php';
33use OAuth\Common\Storage\DoliStorage;
34
43// Load translation files required by the page
44$langs->loadLangs(array('admin', 'printing', 'oauth'));
45
46$action = GETPOST('action', 'aZ09');
47$mode = GETPOST('mode', 'alpha');
48$value = GETPOST('value', 'alpha', 0, null, null, 1); // The value may be __google__docs so we force disable of replace
49$varname = GETPOST('varname', 'alpha');
50$driver = GETPOST('driver', 'alpha');
51
52if (!empty($driver)) {
53 $langs->load($driver);
54}
55
56if (!$mode) {
57 $mode = 'config';
58}
59
60$OAUTH_SERVICENAME_GOOGLE = 'Google';
61
62if (!$user->admin) {
64}
65
66
67/*
68 * Action
69 */
70$error = 0;
71
72if (($mode == 'test' || $mode == 'setup') && empty($driver)) {
73 setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
74 header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
75 exit;
76}
77
78if ($action == 'setconst' && $user->admin) {
79 $db->begin();
80 foreach ($_POST['setupdriver'] as $setupconst) {
81 '@phan-var-force array<string,string> $setupconst';
82 //print '<pre>'.print_r($setupconst, true).'</pre>';
83 $result = dolibarr_set_const($db, $setupconst['varname'], $setupconst['value'], 'chaine', 0, '', $conf->entity);
84 if (!($result > 0)) {
85 $error++;
86 }
87 }
88
89 if (!$error) {
90 $db->commit();
91 setEventMessages($langs->trans("SetupSaved"), null);
92 } else {
93 $db->rollback();
94 dol_print_error($db);
95 }
96 $action = '';
97}
98
99if ($action == 'setvalue' && $user->admin) {
100 $db->begin();
101
102 $result = dolibarr_set_const($db, $varname, $value, 'chaine', 0, '', $conf->entity);
103 if (!($result > 0)) {
104 $error++;
105 }
106
107 if (!$error) {
108 $db->commit();
109 setEventMessages($langs->trans("SetupSaved"), null);
110 } else {
111 $db->rollback();
112 dol_print_error($db);
113 }
114 $action = '';
115}
116
117
118/*
119 * View
120 */
121
122$form = new Form($db);
123
124llxHeader('', $langs->trans("PrintingSetup"));
125
126$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
127print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup');
128
129$head = printingAdminPrepareHead($mode);
130
131if ($mode == 'setup' && $user->admin) {
132 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&driver='.urlencode($driver).'" autocomplete="off">';
133 print '<input type="hidden" name="token" value="'.newToken().'">';
134 print '<input type="hidden" name="action" value="setconst">';
135
136 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
137
138 print $langs->trans("PrintingDriverDesc".$driver)."<br><br>\n";
139
140 print '<table class="noborder centpercent">'."\n";
141 print '<tr class="liste_titre">';
142 print '<th>'.$langs->trans("Parameters").'</th>';
143 print '<th></th>';
144 print '<th>&nbsp;</th>';
145 print "</tr>\n";
146 $submit_enabled = 0;
147
148 if (!empty($driver)) {
149 if (!empty($conf->modules_parts['printing'])) {
150 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
151 } else {
152 $dirmodels = array('/core/modules/printing/');
153 }
154
155 foreach ($dirmodels as $dir) {
156 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
157 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
158 break;
159 }
160 }
161 require_once $classfile;
162 $classname = 'printing_'.$driver;
163 $printer = new $classname($db); // Example: new printing_printgcp(). This run the construct that load the token. TODO Move this into another load function().
164
165 $i = 0;
166 $submit_enabled = 0;
167 foreach ($printer->conf as $key) {
168 switch ($key['type']) {
169 case "text":
170 case "password":
171 print '<tr class="oddeven">';
172 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
173 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="'.getDolGlobalString($key['varname']).'"';
174 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
175 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
176 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
177 print '</tr>'."\n";
178 break;
179 case "checkbox":
180 print '<tr class="oddeven">';
181 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
182 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="1" '.((getDolGlobalInt($key['varname'])) ? 'checked' : '');
183 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
184 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
185 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
186 print '</tr>'."\n";
187 break;
188 case "info": // Google Api setup or Google OAuth Token
189 print '<tr class="oddeven">';
190 print '<td'.($key['required'] ? ' class=required' : '').'>';
191 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
192 print $langs->trans("IsTokenGenerated");
193 } else {
194 print $langs->trans($key['varname']);
195 }
196 print '</td>';
197 print '<td>';
198 // Example $key['info'] = $langs->trans("GoogleAuthNotConfigured");
199 if ($key['info'] == 'GoogleAuthNotConfigured') {
200 $keyforprovider = 'googleprint';
201 print $langs->trans($key['info']);
202 print '. You must use Label "'.$keyforprovider.'" with scope "cloud_print"';
203 } else {
204 print $langs->trans($key['info']);
205 }
206 print '</td>';
207 print '<td>';
208 //var_dump($key);
209 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
210 // Delete remote tokens
211 if (!empty($key['delete'])) {
212 print '<a class="button" href="'.$key['delete'].'">'.$langs->trans('DeleteAccess').'</a><br><br>';
213 }
214 // Request remote token
215 print '<a class="button" href="'.$key['renew'].'">'.$langs->trans('RequestAccess').'</a><br><br>';
216 // Check remote access
217 print $langs->trans("ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME_GOOGLE).': <a href="https://security.google.com/settings/security/permissions" target="_google">https://security.google.com/settings/security/permissions</a>';
218 }
219 print '</td>';
220 print '</tr>'."\n";
221 break;
222 case "submit":
223 if ($key['enabled']) {
224 $submit_enabled = 1;
225 }
226 break;
227 }
228 $i++;
229
230 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
231 $keyforprovider = 'googleprint';
232
233 // Token
234 print '<tr class="oddeven">';
235 print '<td>'.$langs->trans("Token").'</td>';
236 print '<td colspan="2">';
237 $tokenobj = null;
238 // Dolibarr storage
239 $storage = new DoliStorage($db, $conf, $keyforprovider);
240 try {
241 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME_GOOGLE);
242 } catch (Exception $e) {
243 // Return an error if token not found
244 }
245 if (is_object($tokenobj)) {
246 //var_dump($tokenobj);
247 print $tokenobj->getAccessToken().'<br>';
248 //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
249 //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
250 //var_dump($tokenobj->getExtraParams());
251 /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
252 print ''.join(',',$tokenobj->getExtraParams());
253 print '</textarea>';*/
254 }
255 print '</td>';
256 print '</tr>'."\n";
257 }
258 }
259 } else {
260 print $langs->trans('PleaseSelectaDriverfromList');
261 }
262
263 print '</table>';
264
265 print dol_get_fiche_end();
266
267 if (!empty($driver)) {
268 if ($submit_enabled) {
269 print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
270 }
271 }
272
273 print '</form>';
274}
275if ($mode == 'config' && $user->admin) {
276 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
277
278 print $langs->trans("PrintingDesc")."<br><br>\n";
279
280 print '<table class="noborder centpercent">'."\n";
281
282 print '<tr class="liste_titre">';
283 print '<th>'.$langs->trans("Description").'</th>';
284 print '<th class="center">'.$langs->trans("Active").'</th>';
285 print '<th class="center">'.$langs->trans("Setup").'</th>';
286 print '<th class="center">'.$langs->trans("TargetedPrinter").'</th>';
287 print "</tr>\n";
288
289 $object = new PrintingDriver($db);
290 $result = $object->listDrivers($db, 10);
291
292 if (!empty($conf->modules_parts['printing'])) {
293 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
294 } else {
295 $dirmodels = array('/core/modules/printing/');
296 }
297
298 foreach ($result as $tmpdriver) {
299 foreach ($dirmodels as $dir) {
300 if (file_exists(dol_buildpath($dir, 0).$tmpdriver.'.modules.php')) {
301 $classfile = dol_buildpath($dir, 0).$tmpdriver.'.modules.php';
302 break;
303 }
304 }
305 require_once $classfile;
306 $classname = 'printing_'.$tmpdriver;
307 $printer = new $classname($db);
308 $langs->load('printing');
309 //print '<pre>'.print_r($printer, true).'</pre>';
310
311 print '<tr class="oddeven">';
312 print '<td>'.img_picto('', $printer->picto).' '.$langs->trans($printer->desc).'</td>';
313 print '<td class="center">';
314 if (!empty($conf->use_javascript_ajax)) {
315 print ajax_constantonoff($printer->active);
316 } else {
317 if (!getDolGlobalString($printer->conf)) {
318 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=1">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
319 } else {
320 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=0">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
321 }
322 }
323 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=setup&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
324 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=test&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
325 print '</tr>'."\n";
326 }
327
328 print '</table>';
329
330 print dol_get_fiche_end();
331}
332
333if ($mode == 'test' && $user->admin) {
334 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
335
336 print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
337
338 print '<table class="noborder centpercent">';
339 if (!empty($driver)) {
340 if (!empty($conf->modules_parts['printing'])) {
341 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
342 } else {
343 $dirmodels = array('/core/modules/printing/');
344 }
345
346 foreach ($dirmodels as $dir) {
347 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
348 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
349 break;
350 }
351 }
352 require_once $classfile;
353 $classname = 'printing_'.$driver;
354 $printer = new $classname($db);
355 '@phan-var-force PrintingDriver $printer';
356 $langs->load($driver);
357 $langs->load('printing');
358
359 //print '<pre>'.print_r($printer, true).'</pre>';
360 if (count($printer->getlistAvailablePrinters())) {
361 if ($printer->listAvailablePrinters() == 0) {
362 print $printer->resprint;
363 } else {
364 setEventMessages($printer->error, $printer->errors, 'errors');
365 }
366 } else {
367 print $langs->trans('PleaseConfigureDriverfromList');
368 }
369 } else {
370 print $langs->trans('PleaseSelectaDriverfromList');
371 }
372 print '</table>';
373
374 print dol_get_fiche_end();
375}
376
377if ($mode == 'userconf' && $user->admin) {
378 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
379
380 print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
381
382 print '<table class="noborder centpercent">';
383 print '<tr class="liste_titre">';
384 print '<th>'.$langs->trans("User").'</th>';
385 print '<th>'.$langs->trans("PrintModule").'</th>';
386 print '<th>'.$langs->trans("PrintDriver").'</th>';
387 print '<th>'.$langs->trans("Printer").'</th>';
388 print '<th>'.$langs->trans("PrinterLocation").'</th>';
389 print '<th>'.$langs->trans("PrinterId").'</th>';
390 print '<th>'.$langs->trans("NumberOfCopy").'</th>';
391 print '<th class="center">'.$langs->trans("Delete").'</th>';
392 print "</tr>\n";
393 $sql = 'SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login FROM '.MAIN_DB_PREFIX.'printing as p, '.MAIN_DB_PREFIX.'user as u WHERE p.userid=u.rowid';
394 $resql = $db->query($sql);
395 while ($row = $db->fetch_array($resql)) {
396 print '<tr class="oddeven">';
397 print '<td>'.$row['login'].'</td>';
398 print '<td>'.$row['module'].'</td>';
399 print '<td>'.$row['driver'].'</td>';
400 print '<td>'.$row['printer_name'].'</td>';
401 print '<td>'.$row['printer_location'].'</td>';
402 print '<td>'.$row['printer_id'].'</td>';
403 print '<td>'.$row['copy'].'</td>';
404 print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
405 print "</tr>\n";
406 }
407 print '</table>';
408
409 print dol_get_fiche_end();
410}
411
412// End of page
413llxFooter();
414$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
Parent class of emailing target selectors modules.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
printingAdminPrepareHead($mode)
Define head array for tabs of printing tools setup pages.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.