php public function editBlog form new Contrario Forms EditBlogForm if

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
public function editBlog(){
$form = new Contrario\Forms\EditBlogForm();
if ($this->request->isPost() and $this->security->checkToken()) {
if ($form->isValid($this->request->getPost())) {
$file = $this->request->getUploadedFiles()[0];
$file->getName = time().$file->getName();
$blog = Blogs::findFirstById($this->request->getPost('id', 'int'));
$blog->name = $this->request->getPost('name', 'string');
$blog->text = $this->request->getPost('text', 'string');
if($file and in_array($file->getExtension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'ico']) and in_array($file->getRealType(), ['image/jpg', 'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/ico'])){
unlink($_SERVER['DOCUMENT_ROOT'].'/public/images/previews/'.$blog->preview);
$blog->preview = $file->getName;
$file->moveTo($_SERVER['DOCUMENT_ROOT'].'/public/images/previews/'.$file->getName);
}
else {
$this->flash->error('Ошибка загрузки файла, файл имеет неверное расширение, ну или это шелл.');
$this->response->redirect('rifiks/editBlog/'.$this->request->getPost('id', 'int').'/');
$this->view->disable();
}
$blog->category = $this->request->getPost('category', 'int');
if($blog->save()) {
foreach ($this->cached->queryKeys() as $key) $this->cached->delete($key);
$this->response->redirect('blog/'.$this->request->getPost('id', 'int').'/');
$this->view->disable();
}
else foreach ($blog->getMessages() as $message) {
$this->flash->error($message);
$this->response->redirect('rifiks/editBlog/'.$this->request->getPost('id', 'int').'/');
$this->view->disable();
}
}
else {
foreach ($form->getMessages() as $message) $this->flash->error($message);
$this->response->redirect('rifiks/editBlog/'.$this->request->getPost('id', 'int').'/');
$this->view->disable();
}
}
}