From 6ece5db912baeacfd9b96b2efe67babc30dd7421 Mon Sep 17 00:00:00 2001 From: Sugeng Sulistiyawan Date: Thu, 17 Sep 2020 00:52:24 +0700 Subject: [PATCH] Yii::$app->getModule('pdfjs') --- src/Module.php | 90 +++++++++++++-------------- src/PdfJs.php | 82 ++++++++++++------------ src/controllers/DefaultController.php | 3 +- src/views/viewer.php | 35 ++++++----- 4 files changed, 106 insertions(+), 104 deletions(-) diff --git a/src/Module.php b/src/Module.php index 25b3416..250d4f5 100644 --- a/src/Module.php +++ b/src/Module.php @@ -15,49 +15,49 @@ */ class Module extends \yii\base\Module { - /** - * - * @var array $buttons - */ - public $buttons = []; - - /** - * - * @var array $waterMark - */ - public $waterMark = []; - - /** - * @inheritdoc - */ - public $controllerNamespace = 'diecoding\pdfjs\controllers'; - - /** - * @inheritdoc - */ - public function init() - { - parent::init(); - - $waterMarkDefault = [ - 'text' => '', - 'alpha' => '0.5', - 'color' => 'red' - ]; - - if (!empty($this->waterMark)) { - $this->waterMark = ArrayHelper::merge($waterMarkDefault, $this->waterMark); - } else { - $this->waterMark = $waterMarkDefault; - } - - $this->buttons = ArrayHelper::merge([ - 'presentationMode' => true, - 'openFile' => true, - 'print' => true, - 'download' => true, - 'viewBookmark' => true, - 'secondaryToolbarToggle' => true, - ], $this->buttons); - } + /** + * + * @var array $buttons + */ + public $buttons = []; + + /** + * + * @var array $waterMark + */ + public $waterMark = []; + + /** + * @inheritdoc + */ + public $controllerNamespace = 'diecoding\pdfjs\controllers'; + + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + + $waterMarkDefault = [ + 'text' => '', + 'alpha' => '0.5', + 'color' => 'red' + ]; + + if (!empty($this->waterMark)) { + $this->waterMark = ArrayHelper::merge($waterMarkDefault, $this->waterMark); + } else { + $this->waterMark = $waterMarkDefault; + } + + $this->buttons = ArrayHelper::merge([ + 'presentationMode' => true, + 'openFile' => true, + 'print' => true, + 'download' => true, + 'viewBookmark' => true, + 'secondaryToolbarToggle' => true, + ], $this->buttons); + } } diff --git a/src/PdfJs.php b/src/PdfJs.php index c7851cf..3f5994e 100644 --- a/src/PdfJs.php +++ b/src/PdfJs.php @@ -18,51 +18,51 @@ */ class PdfJs extends \yii\base\Widget { - /** - * - * @var string $url - */ - public $url; + /** + * + * @var string $url + */ + public $url; - /** - * - * @var array $options - */ - public $options = []; + /** + * + * @var array $options + */ + public $options = []; - /** - * - * @var array $buttons - */ - public $buttons = []; + /** + * + * @var array $buttons + */ + public $buttons = []; - /** - * @inheritdoc - */ - public function init() - { - parent::init(); - $this->view->registerAssetBundle(PdfJsAsset::className()); + /** + * @inheritdoc + */ + public function init() + { + parent::init(); + $this->view->registerAssetBundle(PdfJsAsset::className()); - $module = new Module(); - $buttons = $module->buttons; - $this->buttons = ArrayHelper::merge($buttons, $this->buttons); - } + $module = Yii::$app->getModule('pdfjs'); + $buttons = $module->buttons; + $this->buttons = ArrayHelper::merge($buttons, $this->buttons); + } - /** - * @inheritdoc - */ - public function run() - { - if (!array_key_exists('style', $this->options)) { - $this->options['style'] = 'border:solid 2px #404040; width:' . $this->width . '; height:' . $this->height . ';'; - } + /** + * @inheritdoc + */ + public function run() + { + if (!array_key_exists('style', $this->options)) { + $this->options['style'] = 'border:solid 2px #404040; width:' . $this->width . '; height:' . $this->height . ';'; + } - return $this->render('viewer', [ - 'options' => $this->options, - 'url' => $this->url, - 'buttons' => $this->buttons, - 'id' => $this->id - ]); - } + return $this->render('viewer', [ + 'options' => $this->options, + 'url' => $this->url, + 'buttons' => $this->buttons, + 'id' => $this->id + ]); + } } diff --git a/src/controllers/DefaultController.php b/src/controllers/DefaultController.php index df750f5..37a1877 100644 --- a/src/controllers/DefaultController.php +++ b/src/controllers/DefaultController.php @@ -2,7 +2,6 @@ namespace diecoding\pdfjs\controllers; -use diecoding\pdfjs\Module; use Yii; use yii\helpers\ArrayHelper; use yii\web\Controller; @@ -29,7 +28,7 @@ class DefaultController extends Controller */ public function actionIndex() { - $module = new Module(); + $module = Yii::$app->getModule('pdfjs'); $buttons = $module->buttons; $waterMark = $module->waterMark; if (Yii::$app->request->isPost) { diff --git a/src/views/viewer.php b/src/views/viewer.php index d6b1f6a..e04b454 100644 --- a/src/views/viewer.php +++ b/src/views/viewer.php @@ -1,30 +1,33 @@ Url::to($url)]); + +$url = Url::to(['/pdfjs', 'file' => Url::to($url)]); $this->registerJs(' - $("#pdfjs-form-'.$id.'").submit(); - $("#pdfjs-'.$id.'").css("background-color","#404040"); + $("#pdfjs-form-' . $id . '").submit(); + $("#pdfjs-' . $id . '").css("background-color","#404040"); '); + ?> 'pdfjs-form-'.$id, - 'options' => [ - 'class' => 'form-horizontal', - 'target'=> 'pdfjs-'.$id - ], - 'action'=>$url + 'id' => 'pdfjs-form-' . $id, + 'options' => [ + 'class' => 'form-horizontal', + 'target' => 'pdfjs-' . $id + ], + 'action' => $url ]) ?> - $value):?> - + $value) : ?> + - 'pdfjs-'.$id, - 'name'=>'pdfjs-'.$id - ],$options)); - ?> + 'pdfjs-' . $id, + 'name' => 'pdfjs-' . $id + ], $options)); + ?>