/
News2.php
124 lines (97 loc) · 3.3 KB
/
News2.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
<?php
namespace App\Controllers;
use App\Models\NewsModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\ResponseInterface;
class News2 extends BaseController
{
private NewsModel $model;
public function __construct()
{
$this->model = model(NewsModel::class);
}
public function getIndex(): string
{
$data = [
'news' => $this->model->getNews(),
'title' => 'News archive',
];
$data['controller'] = strtolower(class_basename(__CLASS__));
return view('templates/header', $data)
. view('news/overview2', $data)
. view('templates/footer', $data);
}
public function getView($slug = null): string
{
$data['news'] = $this->model->getNews($slug);
if (empty($data['news'])) {
throw new PageNotFoundException('Cannot find the news item: ' . $slug);
}
$data['title'] = $data['news']['title'];
$data['controller'] = strtolower(class_basename(__CLASS__));
return view('templates/header', $data)
. view('news/view', $data)
. view('templates/footer', $data);
}
public function getCreate(): string
{
helper('form');
$data['controller'] = strtolower(class_basename(__CLASS__));
return view('templates/header', ['title' => 'Create a news item'])
. view('news/create', $data)
. view('templates/footer');
}
public function postCreate(): string
{
if ($this->validate([
'title' => 'required|min_length[3]|max_length[255]',
'body' => 'required',
])) {
$this->model->save([
'title' => $this->request->getPost('title'),
'slug' => url_title($this->request->getPost('title'), '-', true),
'body' => $this->request->getPost('body'),
]);
return view('news/success');
}
return $this->getCreate();
}
public function getEdit($id = ''): string
{
helper('form');
if ($id === '') {
throw new PageNotFoundException('Cannot find the news item: ' . $id);
}
$data['news'] = $this->model->find($id);
if (empty($data['news'])) {
throw new PageNotFoundException('Cannot find the news item: ' . $id);
}
$data['controller'] = strtolower(class_basename(__CLASS__));
return view('templates/header', ['title' => 'Edit news item'])
. view('news/edit2', $data)
. view('templates/footer');
}
/**
* @param false|int $id
*
* @return ResponseInterface|string
*/
public function postUpdate($id = false)
{
if ($this->validate([
'title' => 'required|min_length[3]|max_length[255]',
'body' => 'required',
])) {
$title = $this->request->getVar('title');
$slug = url_title($title, '-', true);
$data = [
'title' => $title,
'slug' => $slug,
'body' => $this->request->getVar('body'),
];
$this->model->update($id, $data);
return $this->response->redirect(site_url('news2/view/' . $slug));
}
return $this->getEdit($id);
}
}