-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostsController.php
executable file
·99 lines (86 loc) · 3.49 KB
/
PostsController.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
<?php
class PostsController extends AppController{
public $paginate = array(
'fields' => array('Post.id','Post.name','Post.slug','Post.created','Post.content','Category.slug','Category.name','User.username'),
'contain' => array('Category','User'),
'limit' => 5,
'paramType' => 'querystring'
);
public $helpers = array('Markdown.Markdown');
public function index(){
$conditions = null;
// Category filter
if(isset($this->request->params['category'])){
$conditions = array('Category.slug' => $this->request->params['category']);
$this->request->params['named']['category'] = $this->request->params['category'];
}
if(isset($this->request->params['user'])){
$conditions = array('Post.user_id' => $this->request->params['user']);
$this->request->params['named']['user'] = $this->request->params['user'];
}
$posts = $this->Paginate('Post', $conditions);
$this->set(compact('posts'));
}
public function view($slug){
$this->Post->contain('Category', 'User', 'Comment');
$post = $this->Post->findBySlug($slug, array('Post.id','Post.name','Post.slug','Post.created','Post.content','Category.slug','Category.name','User.username'));
if(empty($post)){
throw new NotFoundException();
}
$this->set(compact('post'));
// Comment submission
if(!empty($this->request->data)){
$this->Post->Comment->create($this->request->data, true);
$this->request->data['Comment']['post_id'] = $post['Post']['id'];
if($this->Post->Comment->save(null, true, array('mail', 'username', 'content', 'post_id'))){
$this->Session->setFlash("Thanks for your comment","success");
return $this->redirect($this->referer());
}else{
$this->Session->setFlash("You have to correct your errors first","error");
}
}
}
/**
* Requested, get datas for sidebar
**/
public function sidebar(){
$last_posts = $this->Post->find('all', array(
'limit' => 2,
'fields'=> array('Post.id','Post.slug','Post.name')
));
$categories = $this->Post->Category->find('all');
return compact('last_posts', 'categories');
}
/**
* Admin panel
**/
public function admin_index(){
$posts = $this->Paginate('Post');
$this->set(compact('posts'));
}
public function admin_edit($id = null){
if (!empty($this->request->data)) {
if($this->Post->save($this->request->data)){
$this->Session->setFlash("Post saved","success");
$this->_clean_cache();
return $this->redirect(array('action' => 'index'));
}
}elseif($id){
$this->request->data = $this->Post->findById($id);
}
$users = $this->Post->User->find('list');
$categories = $this->Post->Category->find('list');
$this->set(compact('users', 'categories'));
}
public function admin_delete($id){
$this->Post->delete($id);
$this->Session->setFlash("Post deleted","success");
$this->_clean_cache();
return $this->redirect($this->referer());
}
private function _clean_cache(){
if(file_exists(CACHE . 'cake_element__sidebar_cache_callbacks')){
unlink(CACHE . 'cake_element__sidebar_cache_callbacks');
}
}
}