forked from kveldscholten/gamelist
/
Index.php
150 lines (124 loc) · 4.81 KB
/
Index.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
<?php
/**
* @copyright Ilch 2.0
* @package ilch
*/
namespace Modules\Gamelist\Controllers\Admin;
use Modules\Gamelist\Mappers\Games as GamesMapper;
use Modules\Gamelist\Models\Games as GamesModel;
use Ilch\Validation;
class Index extends \Ilch\Controller\Admin
{
public function init()
{
$items = [
[
'name' => 'manage',
'active' => false,
'icon' => 'fa fa-th-list',
'url' => $this->getLayout()->getUrl(['controller' => 'index', 'action' => 'index']),
[
'name' => 'add',
'active' => false,
'icon' => 'fa fa-plus-circle',
'url' => $this->getLayout()->getUrl(['controller' => 'index', 'action' => 'treat'])
]
]
];
if ($this->getRequest()->getActionName() == 'treat') {
$items[0][0]['active'] = true;
} else {
$items[0]['active'] = true;
}
$this->getLayout()->addMenu
(
'menuGames',
$items
);
}
public function indexAction()
{
$gamesMapper = new GamesMapper();
$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuGames'), ['action' => 'index'])
->add($this->getTranslator()->trans('manage'), ['action' => 'index']);
if ($this->getRequest()->getPost('check_entries')) {
if ($this->getRequest()->getPost('action') == 'delete') {
foreach ($this->getRequest()->getPost('check_entries') as $id) {
$gamesMapper->delete($id);
}
}
}
$this->getView()->set('entries', $gamesMapper->getEntries());
}
public function treatAction()
{
$gamesMapper = new GamesMapper();
if ($this->getRequest()->getParam('id')) {
$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuGames'), ['action' => 'index'])
->add($this->getTranslator()->trans('edit'), ['action' => 'treat']);
$this->getView()->set('entry', $gamesMapper->getEntryById($this->getRequest()->getParam('id')));
} else {
$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuGames'), ['action' => 'index'])
->add($this->getTranslator()->trans('add'), ['action' => 'treat']);
}
if ($this->getRequest()->isPost()) {
// Add BASE_URL if image starts with application to get a complete URL for validation
$image = trim($this->getRequest()->getPost('image'));
if (!empty($image)) {
if (substr($image, 0, 11) == 'application') {
$image = BASE_URL.'/'.$image;
}
}
$post = [
'title' => $this->getRequest()->getPost('title'),
'videourl' => $this->getRequest()->getPost('videourl'),
'image' => $image
];
$newvideoUrl = str_replace("https://www.youtube.com/watch?v=","", $post['videourl']);
$validation = Validation::create($post, [
'title' => 'required',
'image' => 'required|url'
]);
$post['image'] = trim($this->getRequest()->getPost('image'));
if ($validation->isValid()) {
$model = new GamesModel();
if ($this->getRequest()->getParam('id')) {
$model->setId($this->getRequest()->getParam('id'));
}
$model->setTitle($post['title'])
->setVideourl($newvideoUrl)
->setImage($post['image']);
$gamesMapper->save($model);
$this->redirect()
->withMessage('saveSuccess')
->to(['action' => 'index']);
}
$this->addMessage($validation->getErrorBag()->getErrorMessages(), 'danger', true);
$this->redirect()
->withInput()
->withErrors($validation->getErrorBag())
->to(['action' => 'treat']);
}
}
public function updateAction()
{
if ($this->getRequest()->isSecure()) {
$gamesMapper = new GamesMapper();
$gamesMapper->update($this->getRequest()->getParam('id'));
$this->addMessage('saveSuccess');
}
$this->redirect(['action' => 'index']);
}
public function delAction()
{
if ($this->getRequest()->isSecure()) {
$gamesMapper = new GamesMapper();
$gamesMapper->delete($this->getRequest()->getParam('id'));
$this->addMessage('deleteSuccess');
}
$this->redirect(['action' => 'index']);
}
}