From 1e76572990de896f3e3a49c74cd4c86a05b05c7c Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 20 Nov 2020 13:39:12 +0100 Subject: [PATCH] add products types edition --- lib/Controller/PrinterController.php | 98 +++++++++++++++------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/lib/Controller/PrinterController.php b/lib/Controller/PrinterController.php index e390408..70b7de0 100644 --- a/lib/Controller/PrinterController.php +++ b/lib/Controller/PrinterController.php @@ -1,59 +1,69 @@ language = \OC::$server->getL10N('printer'); + } - public function __construct($appName, IRequest $request) { + /** + * callback function to get md5 hash of a file. + * + * @NoAdminRequired + * + * @param (string) $sourcefile - filename + * @param (string) $orientation - Orientation of printed file + */ + public function printfile($sourcefile, $orientation) + { + $filefullpath = \OC\Files\Filesystem::getLocalFile($sourcefile); - parent::__construct($appName, $request); + $options = [ + 'landscape' => [ + 'lpr', + $filefullpath, + ], + 'portrait' => [ + 'lpr', + '-o', + 'orientation-requested=4', + $filefullpath, + ], + ]; - // get i10n - $this->language = \OC::$server->getL10N('printer'); + $success = [ + 'response' => 'success', + 'msg' => $this->language->t('Print succeeded!'), + ]; - } + $error = [ + 'response' => 'error', + 'msg' => $this->language->t('Print failed'), + ]; - /** - * callback function to get md5 hash of a file - * @NoAdminRequired - * @param (string) $sourcefile - filename - * @param (string) $orientation - Orientation of printed file - */ - public function printfile($sourcefile, $orientation) { - if($orientation === "landscape") { - $filefullpath = \OC\Files\Filesystem::getLocalFile($sourcefile); - exec('lpr "' . $filefullpath . '"'); - return new JSONResponse( - array( - 'response' => 'success', - 'msg' => $this->language->t('Print succeeded!') - ) - ); - } + if (!isset($options[$orientation])) { + return new JSONResponse($error); + } - if($orientation === "portrait"){ - $filefullpath = \OC\Files\Filesystem::getLocalFile($sourcefile); - exec('lpr -o orientation-requested=4 "' . $filefullpath . '"'); - return new JSONResponse( - array( - 'response' => 'success', - 'msg' => $this->language->t('Print succeeded!') - ) - ); - } else { - return new JSONResponse( - array( - 'response' => 'error', - 'msg' => $this->language->t('Print failed') - ) - ); - }; - } + $process = new Process($options[$orientation]); + $process->run(); + + if ($process->isSuccessful()) { + return new JSONResponse($success); + } + + return new JSONResponse($error); + } }