dolibarr 21.0.0-alpha
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
35// Load translation files required by the page
36$langs->loadLangs(array('admin', 'printing', 'oauth'));
37
38$action = GETPOST('action', 'aZ09');
39$mode = GETPOST('mode', 'alpha');
40$value = GETPOST('value', 'alpha', 0, null, null, 1); // The value may be __google__docs so we force disable of replace
41$varname = GETPOST('varname', 'alpha');
42$driver = GETPOST('driver', 'alpha');
43
44if (!empty($driver)) {
45 $langs->load($driver);
46}
47
48if (!$mode) {
49 $mode = 'config';
50}
51
52$OAUTH_SERVICENAME_GOOGLE = 'Google';
53
54if (!$user->admin) {
56}
57
58
59/*
60 * Action
61 */
62
63if (($mode == 'test' || $mode == 'setup') && empty($driver)) {
64 setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
65 header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
66 exit;
67}
68
69if ($action == 'setconst' && $user->admin) {
70 $error = 0;
71 $db->begin();
72 foreach ($_POST['setupdriver'] as $setupconst) {
73 '@phan-var-force array<string,string> $setupconst';
74 //print '<pre>'.print_r($setupconst, true).'</pre>';
75 $result = dolibarr_set_const($db, $setupconst['varname'], $setupconst['value'], 'chaine', 0, '', $conf->entity);
76 if (!($result > 0)) {
77 $error++;
78 }
79 }
80
81 if (!$error) {
82 $db->commit();
83 setEventMessages($langs->trans("SetupSaved"), null);
84 } else {
85 $db->rollback();
86 dol_print_error($db);
87 }
88 $action = '';
89}
90
91if ($action == 'setvalue' && $user->admin) {
92 $db->begin();
93
94 $result = dolibarr_set_const($db, $varname, $value, 'chaine', 0, '', $conf->entity);
95 if (!($result > 0)) {
96 $error++;
97 }
98
99 if (!$error) {
100 $db->commit();
101 setEventMessages($langs->trans("SetupSaved"), null);
102 } else {
103 $db->rollback();
104 dol_print_error($db);
105 }
106 $action = '';
107}
108
109
110/*
111 * View
112 */
113
114$form = new Form($db);
115
116llxHeader('', $langs->trans("PrintingSetup"));
117
118$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
119print load_fiche_titre($langs->trans("PrintingSetup"), $linkback, 'title_setup');
120
121$head = printingAdminPrepareHead($mode);
122
123if ($mode == 'setup' && $user->admin) {
124 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
125 print '<input type="hidden" name="token" value="'.newToken().'">';
126 print '<input type="hidden" name="action" value="setconst">';
127
128 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
129
130 print $langs->trans("PrintingDriverDesc".$driver)."<br><br>\n";
131
132 print '<table class="noborder centpercent">'."\n";
133 print '<tr class="liste_titre">';
134 print '<th>'.$langs->trans("Parameters").'</th>';
135 print '<th>'.$langs->trans("Value").'</th>';
136 print '<th>&nbsp;</th>';
137 print "</tr>\n";
138 $submit_enabled = 0;
139
140 if (!empty($driver)) {
141 if (!empty($conf->modules_parts['printing'])) {
142 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
143 } else {
144 $dirmodels = array('/core/modules/printing/');
145 }
146
147 foreach ($dirmodels as $dir) {
148 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
149 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
150 break;
151 }
152 }
153 require_once $classfile;
154 $classname = 'printing_'.$driver;
155 $printer = new $classname($db);
156 $langs->load('printing');
157
158 $i = 0;
159 $submit_enabled = 0;
160 foreach ($printer->conf as $key) {
161 switch ($key['type']) {
162 case "text":
163 case "password":
164 print '<tr class="oddeven">';
165 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
166 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="'.getDolGlobalString($key['varname']).'"';
167 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
168 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
169 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
170 print '</tr>'."\n";
171 break;
172 case "checkbox":
173 print '<tr class="oddeven">';
174 print '<td'.($key['required'] ? ' class=required' : '').'>'.$langs->trans($key['varname']).'</td>';
175 print '<td><input class="width100" type="'.(empty($key['type']) ? 'text' : $key['type']).'" name="setupdriver['.$i.'][value]" value="1" '.((getDolGlobalInt($key['varname'])) ? 'checked' : '');
176 print isset($key['moreattributes']) ? ' '.$key['moreattributes'] : '';
177 print '><input type="hidden" name="setupdriver['.$i.'][varname]" value="'.$key['varname'].'"></td>';
178 print '<td>&nbsp;'.($key['example'] != '' ? $langs->trans("Example").' : '.$key['example'] : '').'</td>';
179 print '</tr>'."\n";
180 break;
181 case "info": // Google Api setup or Google OAuth Token
182 print '<tr class="oddeven">';
183 print '<td'.($key['required'] ? ' class=required' : '').'>';
184 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
185 print $langs->trans("IsTokenGenerated");
186 } else {
187 print $langs->trans($key['varname']);
188 }
189 print '</td>';
190 print '<td>'.$langs->trans($key['info']).'</td>';
191 print '<td>';
192 //var_dump($key);
193 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
194 // Delete remote tokens
195 if (!empty($key['delete'])) {
196 print '<a class="button" href="'.$key['delete'].'">'.$langs->trans('DeleteAccess').'</a><br><br>';
197 }
198 // Request remote token
199 print '<a class="button" href="'.$key['renew'].'">'.$langs->trans('RequestAccess').'</a><br><br>';
200 // Check remote access
201 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>';
202 }
203 print '</td>';
204 print '</tr>'."\n";
205 break;
206 case "submit":
207 if ($key['enabled']) {
208 $submit_enabled = 1;
209 }
210 break;
211 }
212 $i++;
213
214 if ($key['varname'] == 'PRINTGCP_TOKEN_ACCESS') {
215 $keyforprovider = ''; // @BUG This must be set
216
217 // Token
218 print '<tr class="oddeven">';
219 print '<td>'.$langs->trans("Token").'</td>';
220 print '<td colspan="2">';
221 $tokenobj = null;
222 // Dolibarr storage
223 $storage = new DoliStorage($db, $conf, $keyforprovider);
224 try {
225 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME_GOOGLE);
226 } catch (Exception $e) {
227 // Return an error if token not found
228 }
229 if (is_object($tokenobj)) {
230 //var_dump($tokenobj);
231 print $tokenobj->getAccessToken().'<br>';
232 //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
233 //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
234 //var_dump($tokenobj->getExtraParams());
235 /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
236 print ''.join(',',$tokenobj->getExtraParams());
237 print '</textarea>';*/
238 }
239 print '</td>';
240 print '</tr>'."\n";
241 }
242 }
243 } else {
244 print $langs->trans('PleaseSelectaDriverfromList');
245 }
246
247 print '</table>';
248
249 print dol_get_fiche_end();
250
251 if (!empty($driver)) {
252 if ($submit_enabled) {
253 print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'"></div>';
254 }
255 }
256
257 print '</form>';
258}
259if ($mode == 'config' && $user->admin) {
260 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
261
262 print $langs->trans("PrintingDesc")."<br><br>\n";
263
264 print '<table class="noborder centpercent">'."\n";
265
266 print '<tr class="liste_titre">';
267 print '<th>'.$langs->trans("Description").'</th>';
268 print '<th class="center">'.$langs->trans("Active").'</th>';
269 print '<th class="center">'.$langs->trans("Setup").'</th>';
270 print '<th class="center">'.$langs->trans("TargetedPrinter").'</th>';
271 print "</tr>\n";
272
273 $object = new PrintingDriver($db);
274 $result = $object->listDrivers($db, 10);
275
276 if (!empty($conf->modules_parts['printing'])) {
277 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
278 } else {
279 $dirmodels = array('/core/modules/printing/');
280 }
281
282 foreach ($result as $tmpdriver) {
283 foreach ($dirmodels as $dir) {
284 if (file_exists(dol_buildpath($dir, 0).$tmpdriver.'.modules.php')) {
285 $classfile = dol_buildpath($dir, 0).$tmpdriver.'.modules.php';
286 break;
287 }
288 }
289 require_once $classfile;
290 $classname = 'printing_'.$tmpdriver;
291 $printer = new $classname($db);
292 $langs->load('printing');
293 //print '<pre>'.print_r($printer, true).'</pre>';
294
295 print '<tr class="oddeven">';
296 print '<td>'.img_picto('', $printer->picto).' '.$langs->trans($printer->desc).'</td>';
297 print '<td class="center">';
298 if (!empty($conf->use_javascript_ajax)) {
299 print ajax_constantonoff($printer->active);
300 } else {
301 if (!getDolGlobalString($printer->conf)) {
302 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=1">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
303 } else {
304 print '<a href="'.$_SERVER['PHP_SELF'].'?action=setvalue&token='.newToken().'&varname='.urlencode($printer->active).'&value=0">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
305 }
306 }
307 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=setup&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
308 print '<td class="center"><a href="'.$_SERVER['PHP_SELF'].'?mode=test&token='.newToken().'&driver='.urlencode($printer->name).'">'.img_picto('', 'setup').'</a></td>';
309 print '</tr>'."\n";
310 }
311
312 print '</table>';
313
314 print dol_get_fiche_end();
315}
316
317if ($mode == 'test' && $user->admin) {
318 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
319
320 print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
321
322 print '<table class="noborder centpercent">';
323 if (!empty($driver)) {
324 if (!empty($conf->modules_parts['printing'])) {
325 $dirmodels = array_merge(array('/core/modules/printing/'), (array) $conf->modules_parts['printing']);
326 } else {
327 $dirmodels = array('/core/modules/printing/');
328 }
329
330 foreach ($dirmodels as $dir) {
331 if (file_exists(dol_buildpath($dir, 0).$driver.'.modules.php')) {
332 $classfile = dol_buildpath($dir, 0).$driver.'.modules.php';
333 break;
334 }
335 }
336 require_once $classfile;
337 $classname = 'printing_'.$driver;
338 $printer = new $classname($db);
339 '@phan-var-force PrintingDriver $printer';
340 $langs->load($driver);
341 $langs->load('printing');
342
343 //print '<pre>'.print_r($printer, true).'</pre>';
344 if (count($printer->getlistAvailablePrinters())) {
345 if ($printer->listAvailablePrinters() == 0) {
346 print $printer->resprint;
347 } else {
348 setEventMessages($printer->error, $printer->errors, 'errors');
349 }
350 } else {
351 print $langs->trans('PleaseConfigureDriverfromList');
352 }
353 } else {
354 print $langs->trans('PleaseSelectaDriverfromList');
355 }
356 print '</table>';
357
358 print dol_get_fiche_end();
359}
360
361if ($mode == 'userconf' && $user->admin) {
362 print dol_get_fiche_head($head, $mode, $langs->trans("ModuleSetup"), -1, 'technic');
363
364 print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
365
366 print '<table class="noborder centpercent">';
367 print '<tr class="liste_titre">';
368 print '<th>'.$langs->trans("User").'</th>';
369 print '<th>'.$langs->trans("PrintModule").'</th>';
370 print '<th>'.$langs->trans("PrintDriver").'</th>';
371 print '<th>'.$langs->trans("Printer").'</th>';
372 print '<th>'.$langs->trans("PrinterLocation").'</th>';
373 print '<th>'.$langs->trans("PrinterId").'</th>';
374 print '<th>'.$langs->trans("NumberOfCopy").'</th>';
375 print '<th class="center">'.$langs->trans("Delete").'</th>';
376 print "</tr>\n";
377 $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';
378 $resql = $db->query($sql);
379 while ($row = $db->fetch_array($resql)) {
380 print '<tr class="oddeven">';
381 print '<td>'.$row['login'].'</td>';
382 print '<td>'.$row['module'].'</td>';
383 print '<td>'.$row['driver'].'</td>';
384 print '<td>'.$row['printer_name'].'</td>';
385 print '<td>'.$row['printer_location'].'</td>';
386 print '<td>'.$row['printer_id'].'</td>';
387 print '<td>'.$row['copy'].'</td>';
388 print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
389 print "</tr>\n";
390 }
391 print '</table>';
392
393 print dol_get_fiche_end();
394}
395
396// End of page
397llxFooter();
398$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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).
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:70
Class to manage generation of HTML components Only common components must be here.
Parent class of emailing target selectors modules.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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)
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)
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...
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.