Skip to content

Commit 5968116

Browse files
author
Martin Reitschmied
committed
Merge remote-tracking branch 'origin/master'
2 parents fb7c3ac + fa15dd3 commit 5968116

File tree

8 files changed

+134
-19
lines changed

8 files changed

+134
-19
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $signed = false
3030

3131
Route::document(
3232
"invoices/{id}",
33-
"/invoices",
33+
"invoices",
3434
\App\Printables\Invoice::class,
3535
[
3636
// Middleware for getting the document
@@ -68,6 +68,7 @@ class Invoice extends DocumentAbstract
6868

6969
// Define the blade you want to return as printable document
7070
public String $blade = "ambersive.documentviewer::printable_default";
71+
public bool $useValidationEndpoint = false;
7172

7273
public function setData(){
7374
// Request is available in $this->request
@@ -85,12 +86,24 @@ class Invoice extends DocumentAbstract
8586

8687
}
8788

89+
public function validateDocumentHandler(Request $request) {
90+
91+
// Handle the validation
92+
// This information is a helper
93+
// Requires a response (preferable json)
94+
return ['status' => 200];
95+
96+
}
97+
8898
}
8999
```
90100

91-
### Step 4: Generate a valid blade file
101+
### Step 4: Modify the blade file
102+
103+
The make command also creates a blade file within the resource folder.
104+
It will come with some basic scaffold settings so you will be able to create beautiful documents in no time.
92105

93-
Create a blade file. If you want to get our print optimizations you need to use our file document base layout
106+
But you can also define or resource the files.
94107

95108
```php
96109
@extends('ambersive.documentviewer::printable')
@@ -110,6 +123,10 @@ A full example might look like:
110123

111124
```
112125

126+
## Need a way to create PDF files?
127+
128+
We created a [microservice](https://github.com/AMBERSIVE/print-api) which is also open-source to create pdf files. If you need a smooth way to interact with this microservice you can use our [package](https://github.com/AMBERSIVE/laravel-print-api) for that.
129+
113130
## Security Vulnerabilities
114131

115132
If you discover a security vulnerability within this package, please send an e-mail to Manuel Pirker-Ihl via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.

src/Abstracts/DocumentAbstract.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ abstract class DocumentAbstract implements DocumentInterface
1111

1212
public array $data = [];
1313
public array $params = [];
14+
public bool $useValidationEndpoint = false;
1415

1516
public String $blade = "ambersive.documentviewer::printable_default";
1617

@@ -47,6 +48,25 @@ public function uploadDocument(Request $request) {
4748
}
4849
return $result;
4950
}
51+
52+
/**
53+
* Handle the validation request for the document
54+
* Should return a json object if document requirement exists.
55+
*
56+
* @param mixed $request
57+
* @return void
58+
*/
59+
public function validateDocument(Request $request) {
60+
$this->params = $request->route()->parameters;
61+
if (method_exists($this, 'validateDocumentHandler') === false) {
62+
abort(400, 'Document cannot be validated.');
63+
}
64+
$result = $this->validateDocumentHandler($request);
65+
if ($result === null) {
66+
abort(400, 'Error while validating the file.');
67+
}
68+
return $result;
69+
}
5070

5171
/**
5272
* Set the data of this printable

src/Console/Commands/Dev/DocumentAdd.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DocumentAdd extends GeneratorCommand
1717
*
1818
* @var string
1919
*/
20-
protected $signature = 'make:printable {name}';
20+
protected $signature = 'make:printable {name} {--force}';
2121

2222
/**
2323
* The console command description.
@@ -36,7 +36,8 @@ public function handle()
3636

3737
$name = $this->qualifyClass($this->getNameInput());
3838

39-
$path = $this->getPath(ucfirst($name));
39+
$path = $this->getPath($name);
40+
$pathForView = $this->getPathForView($name);
4041

4142
if ((! $this->hasOption('force') ||
4243
! $this->option('force')) &&
@@ -47,10 +48,12 @@ public function handle()
4748
}
4849

4950
$this->makeDirectory($path);
51+
$this->makeDirectory($pathForView);
5052

51-
$this->files->put($path, $this->sortImports($this->buildClass($name)));
53+
$this->files->put($path, $this->sortImports($this->buildClassCustom($name, 'Printable')));
54+
$this->files->put($pathForView, $this->sortImports($this->buildClassCustom($name, 'PrintableView')));
5255

53-
$this->info($this->type.' created successfully.');
56+
$this->info($this->type.' created successfully (data class and blade file).');
5457

5558
}
5659

@@ -62,9 +65,9 @@ public function handle()
6265
*
6366
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
6467
*/
65-
protected function buildClass($name)
68+
protected function buildClassCustom(String $name, String $stubname)
6669
{
67-
$stub = $this->files->get($this->getStub());
70+
$stub = $this->files->get($this->getStubFilePath($stubname));
6871

6972
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
7073
}
@@ -74,31 +77,61 @@ protected function buildClass($name)
7477
*
7578
* @return string
7679
*/
77-
protected function rootNamespace()
80+
protected function rootNamespace():String
7881
{
7982
return $this->laravel->getNamespace()."Printables";
8083
}
8184

85+
/**
86+
* Returns the path to the stubs folder
87+
*/
88+
protected function getStub(): String {
89+
return __DIR__."/../../../Stubs/";
90+
}
91+
8292
/**
8393
* Get the stub file for the generator.
8494
*
8595
* @return string
8696
*/
87-
protected function getStub():String
97+
protected function getStubFilePath(String $stubname):String
8898
{
89-
return __DIR__.'/../../../Stubs/Printable.stub';
99+
return $this->getStub()."${stubname}.stub";
90100
}
91101

92102
/**
93-
* Returns the path for the document
103+
* Returns the path for the document class
94104
*
95105
* @param mixed $name
96106
* @return String
97107
*/
98-
protected function getPath($name):String
99-
{
108+
protected function getPath($name):String {
109+
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
110+
return $this->getPathFolder($name, "app/Printables");
111+
}
112+
113+
/**
114+
* Returns the path for the view (blade) file
115+
*
116+
* @param mixed $name
117+
* @return String
118+
*/
119+
protected function getPathForView($name):String {
120+
$name = Str::replaceFirst($this->rootNamespace(), '', $name).".blade";
121+
return strtolower(base_path("resources/views/printables/".str_replace('\\', '/', $name).'.php'));
122+
}
123+
124+
125+
/**
126+
* Returns the base path for the file
127+
*
128+
* @param mixed $name
129+
* @param mixed $folder
130+
* @return String
131+
*/
132+
protected function getPathFolder(String $name, String $folder = ''): String {
100133
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
101-
return $this->laravel['path'].'/Printables/'.str_replace('\\', '/', $name).'.php';
134+
return base_path($folder.str_replace('\\', '/', $name).'.php');
102135
}
103136

104137
/**
@@ -110,10 +143,12 @@ protected function getPath($name):String
110143
*/
111144
protected function replaceNamespace(&$stub, $name)
112145
{
146+
147+
$bladename = strtolower(str_replace("\\", ".", str_replace($this->rootNamespace()."\\", "printables.", $name)));
113148

114149
$stub = str_replace(
115-
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel'],
116-
[$this->getNamespace($name), $this->rootNamespace(), $this->userProviderModel()],
150+
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel', 'DummyBladeName'],
151+
[$this->getNamespace($name), $this->rootNamespace(), $this->userProviderModel(), $bladename],
117152
$stub
118153
);
119154

src/Interfaces/DocumentInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public function createDocument(Request $request);
1010
public function setData();
1111
public function returnView();
1212
public function uploadDocument(Request $request);
13+
public function validateDocument(Request $request);
1314

1415
}

src/Macros/ResponseDocument.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public function register(){
3535

3636
Route::post($route, "\\${documentClassUpload}@uploadDocument")->middleware($middlewareUpload)->name("${name}.upload");
3737

38+
// Validation endpoint
39+
if ($documentClassInstance->useValidationEndpoint === false) {
40+
return;
41+
}
42+
43+
Route::get("${route}/validation", "\\${documentClass}@validateDocument")->middleware($middleware)->name($name);
44+
3845
});
3946

4047
}

src/Stubs/Printable.stub

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class DummyClass extends DocumentAbstract
1010
{
1111

1212
public array $data = [];
13-
public String $blade = "ambersive.documentviewer::printable_default";
13+
public String $blade = "DummyBladeName";
14+
public bool $useValidationEndpoint = false;
1415

1516
/**
1617
* Set the data of this printable
@@ -28,4 +29,11 @@ class DummyClass extends DocumentAbstract
2829

2930
}
3031

32+
public function validateDocumentHandler(Request $request) {
33+
34+
// Handle the validation
35+
return ['status' => 200];
36+
37+
}
38+
3139
}

src/Stubs/PrintableView.stub

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@extends('ambersive.documentviewer::printable')
2+
3+
@section('styles')
4+
5+
<style>
6+
body {
7+
background:#efefef;
8+
}
9+
</style>
10+
11+
@endsection
12+
13+
@section('document')
14+
15+
<div>Create your printable document here.</div>
16+
17+
@endsection

src/Views/printable_default.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
@extends('ambersive.documentviewer::printable')
22

3+
@section('styles')
4+
5+
<style>
6+
body {
7+
background:#efefef;
8+
}
9+
</style>
10+
11+
@endsection
12+
313
@section('document')
414

515
<div>TODO: Print document</div>

0 commit comments

Comments
 (0)