diff --git a/htdocs/core/modules/printing/printgcp.modules.php b/htdocs/core/modules/printing/printgcp.modules.php index 56f8d8068f9..e7d34f3666c 100644 --- a/htdocs/core/modules/printing/printgcp.modules.php +++ b/htdocs/core/modules/printing/printgcp.modules.php @@ -46,6 +46,7 @@ class printing_printgcp extends PrintingDriver const LOGIN_URL = 'https://accounts.google.com/o/oauth2/token'; const PRINTERS_SEARCH_URL = 'https://www.google.com/cloudprint/search'; + const PRINTERS_GET_JOBS = 'https://www.google.com/cloudprint/jobs'; const PRINT_URL = 'https://www.google.com/cloudprint/submit'; /** @@ -347,4 +348,86 @@ class printing_printgcp extends PrintingDriver return array('status' =>$response['success'],'errorcode' =>$response['errorCode'],'errormessage'=>$response['message']); } + + /** + * List jobs print + * + * @return void + */ + function list_jobs() + { + global $conf, $db, $bc; + // Token storage + $storage = new DoliStorage($this->db, $this->conf); + // Setup the credentials for the requests + $credentials = new Credentials( + $this->google_id, + $this->google_secret, + DOL_MAIN_URL_ROOT.'/core/modules/oauth/getgoogleoauthcallback.php' + ); + $serviceFactory = new \OAuth\ServiceFactory(); + $apiService = $serviceFactory->createService('Google', $credentials, $storage, array()); + // Check if we have auth token + $token_ok=true; + try { + $token = $storage->retrieveAccessToken('Google'); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + $token_ok = false; + } + $expire = false; + // Is token expired or will token expire in the next 30 seconds + if ($token_ok) { + $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30)); + } + + // Token expired so we refresh it + if ($token_ok && $expire) { + try { + // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois + $refreshtoken = $token->getRefreshToken(); + $token = $apiService->refreshAccessToken($token); + $token->setRefreshToken($refreshtoken); + $storage->storeAccessToken('Google', $token); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + } + } + // Getting Jobs + // Send a request with api + try { + $response = $apiService->request(self::PRINTERS_GET_JOBS); + } catch (Exception $e) { + $this->errors[] = $e->getMessage(); + print '
'.print_r($e->getMessage(),true).'
'; + } + $responsedata = json_decode($response, true); + //print '
'.print_r($responsedata,true).'
'; + print ''; + print ''; + print ""; + print ""; + print ""; + print ""; + print ""; + print ""; + print "\n"; + $var = True; + $jobs = $responsedata['jobs']; + //print '
'.print_r($jobs['0'],true).'
'; + foreach ($jobs as $value ) + { + $var=!$var; + print ""; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + print "
IdOwnerPrinterFileStatusCancel
'.$value['id'].''.$value['ownerId'].''.$value['printerName'].''.$value['title'].''.$value['status'].' 
"; + } + } diff --git a/htdocs/langs/en_US/printing.lang b/htdocs/langs/en_US/printing.lang index c41a1a9a20a..81ee8071b2e 100644 --- a/htdocs/langs/en_US/printing.lang +++ b/htdocs/langs/en_US/printing.lang @@ -20,6 +20,10 @@ UserConf=Setup per user PRINTGCP=Google Cloud Print PRINTGCP_INFO=Google Api State PRINTGCP_AUTHLINK=Authentication +PRINTGCP_TOKEN_ACCESS=Google Cloud Print Token +PRINTGCP_TOKEN_REFRESH=Token Refresh Present +PRINTGCP_TOKEN_EXPIRED=Token Expired +PRINTGCP_TOKEN_EXPIRE_AT=Token expire at RequestAccess=Request Access PrintGCPDesc=This driver allow to send documents directly to a printer with Google Cloud Print. PrintingDriverDescprintgcp=Configuration variables for printing driver Google Cloud Print. diff --git a/htdocs/printing/index.php b/htdocs/printing/index.php index c860ece5616..2bee135ca44 100644 --- a/htdocs/printing/index.php +++ b/htdocs/printing/index.php @@ -23,16 +23,25 @@ */ require '../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/core/class/dolprintipp.class.php'; +include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php'; llxHeader("",$langs->trans("Printing")); print load_fiche_titre($langs->trans("Printing")); -// TODO use modules to only activated modules // List Jobs from printing modules -$printer = new dolPrintIPP($db,$conf->global->PRINTIPP_HOST,$conf->global->PRINTIPP_PORT,$user->login,$conf->global->PRINTIPP_USER,$conf->global->PRINTIPP_PASSWORD); -$printer->list_jobs('commande'); +$object = new PrintingDriver($db); +$result = $object->listDrivers($db, 10); +foreach ($result as $driver) { + require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/'.$driver.'.modules.php'; + $classname = 'printing_'.$driver; + $langs->load($driver); + $printer = new $classname($db); + if ($conf->global->{$printer->active}) { + //$printer->list_jobs('commande'); + $printer->list_jobs(); + } +} llxFooter();