/
DocumentsController.php
159 lines (138 loc) · 5.04 KB
/
DocumentsController.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
class DocumentsController extends AbstractController{
private $type;
private $isArchive;
public function __construct(){
$type = $this->get('type');
if(empty($type)){
$type = '1';
}
$this->type = $type;
$title = $type == '1' ? 'Dokumenty do pobrania' : 'Materiały dla członków spółdzielni';
//archive
$isArchive = $this->get('is_archive');
if(empty($isArchive)){
$isArchive = false;
}else{
$isArchive = true;
}
$this->isArchive = $isArchive;
if($isArchive){
$title .= ' - Archiwum';
}
$this->setTitle($title);
$url = '/admin/documents?type=' . $type . '&is_archive=' . $isArchive;
$this->setBaseUrl($url);
$this->addToView('type', $type);
$this->addToView('isArchive', $isArchive);
}
private function getType(){
return $this->type;
}
public function indexAction(){
$db = new Wsm_Db_Documents();
$list = $db->getList($this->getType(), null, $this->isArchive);
$this->addToView('list', $list);
}
public function editAction(){
$db = new Wsm_Db_Documents();
$this->addToView('document', $db->get($this->get('id')));
}
public function addAction(){
$this->addToView('document', new Wsm_Document($this->getType()));
}
public function deleteAction(){
if($this->has('id')){
$doc = new Wsm_Document();
$doc->setId($this->get('id'));
$dbService = new Wsm_Db_Documents();
if($dbService->delete($doc)){
$this->redirect($this->getBaseUrl() . '&msg=saved', true);
}
}
$this->redirect($this->getBaseUrl() . '&msg=save_error', true);
}
public function archiveAction(){
if($this->has('id')){
$doc = new Wsm_Document();
$doc->setId($this->get('id'));
$dbService = new Wsm_Db_Documents();
try{
$dbService->archive($doc);
$this->redirect($this->getBaseUrl() . '&msg=saved', true);
}catch(Exception $e){
}
}
$this->redirect($this->getBaseUrl() . '&msg=save_error', true);
}
public function saveAction(){
$news = new Wsm_Document();
if($this->has('id')){
$news->setId($this->get('id'));
}
$news->setTitle($this->get('title'));
$news->setType($this->get('type'));
$news->setCategory($this->get('category'));
$news->setImportance($this->get('importance'));
$file = $_FILES['file'];
if(!empty($file) && !empty($file['name'])){
$filename = $file['name'];
$uploadDir = '/documents/';
$filePath = $uploadDir . $filename;
if(file_exists($filePath)){
unlink($filePath);
}
if(!move_uploaded_file($file['tmp_name'], $filePath)){
$this->redirect($this->getBaseUrl() . '&msg=save_error', true);
}
$news->setFilename($filename);
}else{
$news->setFilename($this->get('filename'));
}
$newsDb = new Wsm_Db_Documents();
try{
$newsDb->save($news);
$this->redirect($this->getBaseUrl() . '&msg=saved', true);
}catch(Exception $e){
$this->redirect($this->getBaseUrl() . '&msg=save_error', true);
}
}
public function moveAction(){
if($this->has('id')
&& $this->has('order')){
try{
$id = $this->get('id');
$isOrderUp = $this->get('order') == 'up';
$db = new Wsm_Db_Documents();
$doc = $db->get($id);
$category = $doc->getCategory();
$list = $db->getList($this->type, $category);
$size = count($list);
foreach($list as $key => $d){
if($d->getId() == $doc->getId()){
if($isOrderUp){
if($key != 0){
$list[$key] = $list[$key - 1];
$list[$key - 1] = $d;
}
}else{
if($key < $size - 1){
$list[$key] = $list[$key + 1];
$list[$key + 1] = $d;
}
}
break;
}
}
$j = 0;
foreach($list as $d){
$d->setOrder($j++);
$db->save($d);
}
$this->redirect($this->getBaseUrl() . '&msg=saved#id-' . $doc->getId(), true);
}catch(Exception $e){
}
}
$this->redirect($this->getBaseUrl() . '&msg=save_error', true);
}
}