1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\DocumentGenerator\PdfLetters\Controllers; |
4
|
|
|
|
5
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\PdfLettersTrait; |
6
|
|
|
use ByTIC\DocumentGenerator\PdfLetters\PdfLetterTrait; |
7
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
8
|
|
|
use Nip\Controllers\Traits\AbstractControllerTrait; |
9
|
|
|
use Nip\Records\Record; |
10
|
|
|
use Nip\Records\RecordManager; |
11
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait AdminPdfLetterControllerTrait |
15
|
|
|
* @package ByTIC\DocumentGenerator\PdfLetters |
16
|
|
|
* |
17
|
|
|
* @method PdfLetterTrait getModelFromRequest() |
18
|
|
|
* @method PdfLettersTrait getModelManager() |
19
|
|
|
*/ |
20
|
|
|
trait AdminPdfLetterControllerTrait |
21
|
|
|
{ |
22
|
|
|
use AbstractControllerTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $letterType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RecordManager |
31
|
|
|
*/ |
32
|
|
|
protected $parentManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Record |
36
|
|
|
*/ |
37
|
|
|
protected $parent; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function upload() |
41
|
|
|
{ |
42
|
|
|
if ($_FILES['letter']) { |
43
|
|
|
$parent = $this->getParent(); |
44
|
|
|
if ($parent) { |
45
|
|
|
/** @var UploadedFile $uploadedFile */ |
46
|
|
|
$uploadedFile = $this->getRequest()->files->get('letter'); |
47
|
|
|
|
48
|
|
|
if (!$uploadedFile->isValid()) { |
49
|
|
|
$this->flashRedirectLetter($parent, $uploadedFile->getErrorMessage(), 'error'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$letter = $this->getModelFromRequest(); |
53
|
|
|
if (!$letter) { |
54
|
|
|
$letter = $this->newPdfLetterRecordFromItemType($parent, $this->getLetterType()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$result = $letter->uploadFromRequest($uploadedFile); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if ($result instanceof Media) { |
60
|
|
|
$this->flashRedirectLetter($parent, $this->getModelManager()->getMessage('add')); |
|
|
|
|
61
|
|
|
} else { |
62
|
|
|
$letter->delete(); |
63
|
|
|
$this->flashRedirectLetter($parent, $result, 'error'); |
64
|
|
|
} |
65
|
|
|
die('end'); |
66
|
|
|
} |
67
|
|
|
die('no valid item'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function downloadExample() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$letter = $this->getModelFromRequest(); |
74
|
|
|
$item = $letter->getItem(); |
75
|
|
|
|
76
|
|
|
if ($letter->hasFile()) { |
77
|
|
|
$letter->downloadExample(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->flashRedirectLetter($item, $this->getModelManager()->getMessage('no-file'), 'error'); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function downloadBlank() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$letter = $this->getModelFromRequest(); |
86
|
|
|
$parent = $letter->getItem(); |
87
|
|
|
if ($letter->hasFile()) { |
88
|
|
|
$letter->downloadBlank(); |
89
|
|
|
die(''); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->flashRedirectLetter($parent, $this->getModelManager()->getMessage('no-file'), 'error'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Record $parent |
97
|
|
|
* @param $message |
98
|
|
|
* @param string $type |
99
|
|
|
*/ |
100
|
|
|
protected function flashRedirectLetter($parent, $message, $type = 'success') |
101
|
|
|
{ |
102
|
|
|
$this->flashRedirect( |
103
|
|
|
$message, |
104
|
|
|
$this->getPdfLettersPageUrl($parent), |
105
|
|
|
$type, |
106
|
|
|
$this->getPdfLettersPageController($parent) |
|
|
|
|
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Record $parent |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
protected function getPdfLettersPageUrl($parent) |
115
|
|
|
{ |
116
|
|
|
return $parent->getURL(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Record $parent |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
protected function getPdfLettersPageController($parent) |
124
|
|
|
{ |
125
|
|
|
return $parent->getManager()->getController(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
protected function viewCheckItem() |
132
|
|
|
{ |
133
|
|
|
$letter = $this->getModelFromRequest(); |
134
|
|
|
|
135
|
|
|
if (!$letter) { |
136
|
|
|
$this->redirect($this->getModelManager()->getUploadURL($_GET)); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
return $letter; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $item |
143
|
|
|
* @param $type |
144
|
|
|
* @return PdfLetterTrait|Record |
145
|
|
|
*/ |
146
|
|
|
protected function newPdfLetterRecordFromItemType($item, $type) |
147
|
|
|
{ |
148
|
|
|
$letter = $this->newPdfLetterRecord(); |
149
|
|
|
$letter->id_item = $item->id; |
150
|
|
|
$letter->type = $type; |
151
|
|
|
$letter->insert(); |
|
|
|
|
152
|
|
|
return $letter; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return Record|PdfLetterTrait |
157
|
|
|
*/ |
158
|
|
|
protected function newPdfLetterRecord() |
159
|
|
|
{ |
160
|
|
|
return $this->getModelManager()->getNew(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return RecordManager |
165
|
|
|
*/ |
166
|
|
|
protected function getParentManager() |
167
|
|
|
{ |
168
|
|
|
return $this->parentManager; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return Record |
173
|
|
|
*/ |
174
|
|
|
protected function getParent() |
175
|
|
|
{ |
176
|
|
|
return $this->parent; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
protected function getLetterType() |
183
|
|
|
{ |
184
|
|
|
return $this->letterType; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Called before action |
189
|
|
|
*/ |
190
|
|
|
protected function parseRequestPdfLetter() |
191
|
|
|
{ |
192
|
|
|
if ($this->getRequest()->get('id_item') && $this->getRequest()->get('type')) { |
193
|
|
|
$this->checkRequestForParent(); |
194
|
|
|
} else { |
195
|
|
|
$this->checkRequestForItem(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function checkRequestForParent() |
200
|
|
|
{ |
201
|
|
|
$this->letterType = $this->getRequest()->get('type'); |
202
|
|
|
$this->parentManager = $this->getModelManager()->getParentManagerFromType($this->getRequest()->get('type')); |
|
|
|
|
203
|
|
|
$this->parent = $this->parentManager->findOne($this->getRequest()->get('id_item')); |
|
|
|
|
204
|
|
|
if ($this->parent) { |
205
|
|
|
$this->setModelFromRequest($this->getModelManager()->getByItem($this->letterType, $this->parent->id)); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function checkRequestForItem() |
210
|
|
|
{ |
211
|
|
|
$letter = $this->getModelFromRequest(); |
212
|
|
|
$this->checkRequestSetFromLetter($letter); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param PdfLetterTrait $letter |
|
|
|
|
217
|
|
|
*/ |
218
|
|
|
protected function checkRequestSetFromLetter($letter) |
219
|
|
|
{ |
220
|
|
|
$this->letterType = $letter->type; |
221
|
|
|
$this->parent = $letter->getItem(); |
|
|
|
|
222
|
|
|
$this->parentManager = $letter->getItemsManager(); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @inheritdoc |
227
|
|
|
*/ |
228
|
|
|
protected function setBreadcrumbs() |
229
|
|
|
{ |
230
|
|
|
$this->call('setClassBreadcrumbs', $this->parentManager->getController()); |
|
|
|
|
231
|
|
|
$this->call('setItemBreadcrumbs', $this->parentManager->getController(), false, [$this->parent]); |
|
|
|
|
232
|
|
|
|
233
|
|
|
$this->setClassBreadcrumbs(); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: