1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Publication\Controller; |
4
|
|
|
|
5
|
|
|
use Application\Mvc\Controller; |
6
|
|
|
use Publication\Model\Publication; |
7
|
|
|
use Publication\Form\PublicationForm; |
8
|
|
|
use Publication\Model\Type; |
9
|
|
|
|
10
|
|
|
class AdminController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function initialize() |
14
|
|
|
{ |
15
|
|
|
$this->setAdminEnvironment(); |
16
|
|
|
$this->helper->activeMenu()->setActive('admin-publication'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function indexAction() |
20
|
|
|
{ |
21
|
|
|
$page = $this->request->getQuery('page', 'int', 1); |
22
|
|
|
$type = $this->dispatcher->getParam('type'); |
|
|
|
|
23
|
|
|
$type_id = null; |
24
|
|
|
|
25
|
|
|
$types = Type::find(); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$cond_array = []; |
28
|
|
|
if ($type) { |
29
|
|
|
$typeEntity = Type::getCachedBySlug($type); |
|
|
|
|
30
|
|
|
$type_id = $typeEntity->getId(); |
|
|
|
|
31
|
|
|
$cond_array[] = "type_id = $type_id"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$conditions = implode(' AND ', $cond_array); |
35
|
|
|
|
36
|
|
|
$publications = Publication::find([ |
|
|
|
|
37
|
|
|
"conditions" => $conditions, |
38
|
|
|
"order" => "date DESC, id DESC" |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$paginator = new \Phalcon\Paginator\Adapter\Model([ |
42
|
|
|
"data" => $publications, |
43
|
|
|
"limit" => 20, |
44
|
|
|
"page" => $page |
45
|
|
|
]); |
46
|
|
|
$this->view->paginate = $paginator->getPaginate(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$this->view->types = $types; |
|
|
|
|
49
|
|
|
$this->view->type = $type; |
|
|
|
|
50
|
|
|
$this->view->type_id = $type_id; |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->helper->title($this->helper->at('Manage Publications'), true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function addAction() |
56
|
|
|
{ |
57
|
|
|
$this->view->pick(['admin/edit']); |
58
|
|
|
$form = new PublicationForm(); |
59
|
|
|
$model = new Publication(); |
60
|
|
|
|
61
|
|
|
$type = $this->dispatcher->getParam('type'); |
|
|
|
|
62
|
|
|
if ($type) { |
63
|
|
|
$typeEntity = Type::getCachedBySlug($type); |
|
|
|
|
64
|
|
|
$form->get('type_id')->setDefault($typeEntity->getId()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->request->isPost()) { |
68
|
|
|
$post = $this->request->getPost(); |
69
|
|
|
$form->bind($post, $model); |
70
|
|
|
|
71
|
|
|
if ($form->isValid()) { |
72
|
|
|
if ($model->create()) { |
73
|
|
|
$form->bind($post, $model); |
74
|
|
|
$model->updateFields($post); |
75
|
|
|
if ($model->update()) { |
76
|
|
|
$this->flash->success($this->helper->at('Publication created')); |
77
|
|
|
return $this->redirect($this->url->get() . 'publication/admin/edit/' . $model->getId() . '?lang=' . LANG); |
78
|
|
|
} else { |
79
|
|
|
$this->flashErrors($model); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
$this->flashErrors($model); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$this->flashErrors($form); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->view->type = $type; |
|
|
|
|
90
|
|
|
$this->view->model = $model; |
|
|
|
|
91
|
|
|
$this->view->form = $form; |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->helper->title($this->helper->at('Create a publication'), true); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function editAction($id) |
98
|
|
|
{ |
99
|
|
|
$id = (int) $id; |
100
|
|
|
$form = new PublicationForm(); |
101
|
|
|
$model = Publication::findFirst($id); |
|
|
|
|
102
|
|
|
|
103
|
|
|
if ($model->getTypeId()) { |
|
|
|
|
104
|
|
|
$this->view->type = $model->getType()->getSlug(); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->request->isPost()) { |
108
|
|
|
$post = $this->request->getPost(); |
109
|
|
|
$form->bind($post, $model); |
110
|
|
|
if ($form->isValid()) { |
111
|
|
|
$model->updateFields($post); |
|
|
|
|
112
|
|
|
if ($model->save()) { |
|
|
|
|
113
|
|
|
$this->uploadImage($model); |
114
|
|
|
$this->flash->success($this->helper->at('Publication edited')); |
115
|
|
|
|
116
|
|
|
return $this->redirect($this->url->get() . 'publication/admin/edit/' . $model->getId() . '?lang=' . LANG); |
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
$this->flashErrors($model); |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
$this->flashErrors($form); |
122
|
|
|
} |
123
|
|
|
} else { |
124
|
|
|
$form->setEntity($model); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->view->model = $model; |
|
|
|
|
128
|
|
|
$this->view->form = $form; |
|
|
|
|
129
|
|
|
$this->helper->title($this->helper->at('Edit publication'), true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function deleteAction($id) |
133
|
|
|
{ |
134
|
|
|
$model = Publication::findFirst($id); |
|
|
|
|
135
|
|
|
|
136
|
|
|
if ($this->request->isPost()) { |
137
|
|
|
$model->delete(); |
|
|
|
|
138
|
|
|
if ($model->getTypeId()) { |
|
|
|
|
139
|
|
|
$this->redirect($this->url->get() . 'publication/admin/' . $model->getType()->getSlug()); |
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
$this->redirect($this->url->get() . 'publication/admin'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->view->model = $model; |
|
|
|
|
146
|
|
|
$this->helper->title($this->helper->at('Unpublishing'), true); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function uploadImage($model) |
150
|
|
|
{ |
151
|
|
|
if ($this->request->isPost()) { |
152
|
|
|
if ($this->request->hasFiles() == true) { |
153
|
|
|
foreach ($this->request->getUploadedFiles() as $file) { |
154
|
|
|
if (!$file->getTempName()) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
if (!in_array($file->getType(), [ |
158
|
|
|
'image/bmp', |
159
|
|
|
'image/jpeg', |
160
|
|
|
'image/png', |
161
|
|
|
]) |
162
|
|
|
) { |
163
|
|
|
return $this->flash->error($this->helper->at('Only allow image formats jpg, jpeg, png, bmp')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$imageFilter = new \Image\Storage([ |
167
|
|
|
'id' => $model->getId(), |
168
|
|
|
'type' => 'publication', |
169
|
|
|
]); |
170
|
|
|
$imageFilter->removeCached(); |
171
|
|
|
|
172
|
|
|
$resize_x = 1000; |
173
|
|
|
$image = new \Phalcon\Image\Adapter\GD($file->getTempName()); |
174
|
|
|
if ($image->getWidth() > $resize_x) { |
175
|
|
|
$image->resize($resize_x); |
176
|
|
|
} |
177
|
|
|
$image->save($imageFilter->originalAbsPath()); |
178
|
|
|
|
179
|
|
|
$model->setPreviewSrc($imageFilter->originalRelPath()); |
180
|
|
|
$model->update(); |
181
|
|
|
|
182
|
|
|
$this->flash->success($this->helper->at('Photo added')); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
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: