dolibarr 24.0.0-beta
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-2026 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();
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();
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 $classfile = null;
156 foreach ($dirmodels as $dir) {
157 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
158 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
159 break;
160 }
161 }
162 if ($classfile === null) {
163 dol_syslog("Could not load printing module".$driver, LOG_ERR);
164 exit(1);
165 }
166 require_once $classfile;
167 $classname = 'printing_'.$driver;
168 $printer = new $classname($db); // Example: new printing_printgcp(). This run the construct that load the token. TODO Move this into another load function().
169
170 $i = 0;
171 $submit_enabled = 0;
172 foreach ($printer->conf as $key) {
173 switch ($key['type']) {
174 case "text":
175 case "password":
176 print '<tr class="oddeven">';
177 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
178 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="'.getDolGlobalString($key['varname']).'"';
179 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
180 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
181 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
182 print '</tr>'."\n";
183 break;
184 case "checkbox":
185 print '<tr class="oddeven">';
186 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
187 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="1" '.((getDolGlobalInt($key['varname'])) ? 'checked' : '');
188 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
189 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
190 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
191 print '</tr>'."\n";
192 break;
193 case "info": // Google Api setup or Google OAuth Token
194 print '<tr class="oddeven">';
195 print '<td'.($key['required'] ? ' class=required' : '').'>';
196 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
197 print $langs->trans("IsTokenGenerated");
198 } else {
199 print $langs->trans($key['varname']);
200 }
201 print '</td>';
202 print '<td>';
203 // Example $key['info'] = $langs->trans("GoogleAuthNotConfigured");
204 if ($key['info'] == 'GoogleAuthNotConfigured') {
205 $keyforprovider = 'googleprint';
206 print $langs->trans($key['info']);
207 print '. You must use Label "'.$keyforprovider.'" with scope "cloud_print"';
208 } else {
209 print $langs->trans($key['info']);
210 }
211 print '</td>';
212 print '<td>';
213 //var_dump($key);
214 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
215 // Delete remote tokens
216 if (!empty($key['delete'])) {
217 print '<a class="button" href="'.$key['delete'].'">'.$langs->trans('DeleteAccess').'</a><br><br>';
218 }
219 // Request remote token
220 print '<a class="button" href="'.$key['renew'].'">'.$langs->trans('RequestAccess').'</a><br><br>';
221 // Check remote access
222 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>';
223 }
224 print '</td>';
225 print '</tr>'."\n";
226 break;
227 case "submit":
228 if ($key['enabled']) {
229 $submit_enabled = 1;
230 }
231 break;
232 }
233 $i++;
234
235 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
236 $keyforprovider = 'googleprint';
237
238 // Token
239 print '<tr class="oddeven">';
240 print '<td>'.$langs->trans("Token").'</td>';
241 print '<td colspan="2">';
242 $tokenobj = null;
243 // Dolibarr storage
244 $storage = new DoliStorage($db, $conf, $keyforprovider);
245 try {
246 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME_GOOGLE);
247 } catch (Exception $e) {
248 // Return an error if token not found
249 }
250 if (is_object($tokenobj)) {
251 //var_dump($tokenobj);
252 print $tokenobj->getAccessToken().'<br>';
253 //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
254 //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
255 //var_dump($tokenobj->getExtraParams());
256 /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
257 print ''.join(',',$tokenobj->getExtraParams());
258 print '</textarea>';*/
259 }
260 print '</td>';
261 print '</tr>'."\n";
262 }
263 }
264 } else {
265 print $langs->trans('PleaseSelectaDriverfromList');
266 }
267
268 print '</table>';
269
270 print dol_get_fiche_end();
271
272 if (!empty($driver)) {
273 if ($submit_enabled) {
274 print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
275 }
276 }
277
278 print '</form>';
279}
280if ($mode == 'config' && $user->admin) {
281 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
282
283 print $langs->trans("PrintingDesc")."<br><br>\n";
284
285 print '<table class="noborder centpercent">'."\n";
286
287 print '<tr class="liste_titre">';
288 print '<th>'.$langs->trans("Description").'</th>';
289 print '<th class="center">'.$langs->trans("Active").'</th>';
290 print '<th class="center">'.$langs->trans("Setup").'</th>';
291 print '<th class="center">'.$langs->trans("TargetedPrinter").'</th>';
292 print "</tr>\n";
293
295 $result = $object->listDrivers($db, 10);
296
297 if (!empty($conf->modules_parts['printing'])) {
298 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
299 } else {
300 $dirmodels = array('/core/modules/printing/');
301 }
302
303 foreach ($result as $tmpdriver) {
304 $classfile = null;
305 foreach ($dirmodels as $dir) {
306 if (file_exists(dol_buildpath($dir, 0).$tmpdriver.'.modules.php')) {
307 $classfile = dol_buildpath($dir, 0).$tmpdriver.'.modules.php';
308 break;
309 }
310 }
311 if ($classfile === null) {
312 dol_syslog("Could not load printing module".$driver, LOG_ERR);
313 exit(1);
314 }
315 require_once $classfile;
316 $classname = 'printing_'.$tmpdriver;
317 $printer = new $classname($db);
318 $langs->load('printing');
319 //print '<pre>'.print_r($printer, true).'</pre>';
320
321 print '<tr class="oddeven">';
322 print '<td>'.img_picto('', $printer->picto).' '.$langs->trans($printer->desc).'</td>';
323 print '<td class="center">';
324 if (!empty($conf->use_javascript_ajax)) {
325 print ajax_constantonoff($printer->active);
326 } else {
327 if (!getDolGlobalString($printer->conf)) {
328 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=1">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
329 } else {
330 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=0">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
331 }
332 }
333 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=setup&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
334 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=test&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
335 print '</tr>'."\n";
336 }
337
338 print '</table>';
339
340 print dol_get_fiche_end();
341}
342
343if ($mode == 'test' && $user->admin) {
344 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
345
346 print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
347
348 print '<table class="noborder centpercent">';
349 if (!empty($driver)) {
350 if (!empty($conf->modules_parts['printing'])) {
351 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
352 } else {
353 $dirmodels = array('/core/modules/printing/');
354 }
355
356 $classfile = null;
357 foreach ($dirmodels as $dir) {
358 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
359 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
360 break;
361 }
362 }
363 if ($classfile === null) {
364 dol_syslog("Could not load printing module".$driver, LOG_ERR);
365 exit(1);
366 }
367 require_once $classfile;
368 $classname = 'printing_'.$driver;
369 $printer = new $classname($db);
370 '@phan-var-force PrintingDriver $printer';
371 $langs->load($driver);
372 $langs->load('printing');
373
374 //print '<pre>'.print_r($printer, true).'</pre>';
375 if (count($printer->getlistAvailablePrinters())) {
376 if ($printer->listAvailablePrinters() == 0) {
377 print $printer->resprint;
378 } else {
379 setEventMessages($printer->error, $printer->errors, 'errors');
380 }
381 } else {
382 print $langs->trans('PleaseConfigureDriverfromList');
383 }
384 } else {
385 print $langs->trans('PleaseSelectaDriverfromList');
386 }
387 print '</table>';
388
389 print dol_get_fiche_end();
390}
391
392if ($mode == 'userconf' && $user->admin) {
393 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
394
395 print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
396
397 print '<table class="noborder centpercent">';
398 print '<tr class="liste_titre">';
399 print '<th>'.$langs->trans("User").'</th>';
400 print '<th>'.$langs->trans("PrintModule").'</th>';
401 print '<th>'.$langs->trans("PrintDriver").'</th>';
402 print '<th>'.$langs->trans("Printer").'</th>';
403 print '<th>'.$langs->trans("PrinterLocation").'</th>';
404 print '<th>'.$langs->trans("PrinterId").'</th>';
405 print '<th>'.$langs->trans("NumberOfCopy").'</th>';
406 print '<th class="center">'.$langs->trans("Delete").'</th>';
407 print "</tr>\n";
408 $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';
409 $resql = $db->query($sql);
410 while ($row = $db->fetch_array($resql)) {
411 print '<tr class="oddeven">';
412 print '<td>'.$row['login'].'</td>';
413 print '<td>'.$row['module'].'</td>';
414 print '<td>'.$row['driver'].'</td>';
415 print '<td>'.$row['printer_name'].'</td>';
416 print '<td>'.$row['printer_location'].'</td>';
417 print '<td>'.$row['printer_id'].'</td>';
418 print '<td>'.$row['copy'].'</td>';
419 print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
420 print "</tr>\n";
421 }
422 print '</table>';
423
424 print dol_get_fiche_end();
425}
426
427// End of page
428llxFooter();
429$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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.