1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* NewsFeed controller |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class NewsFeedController extends Controller{ |
|
|
|
|
11
|
|
|
|
12
|
|
|
public function beforeAction(){ |
13
|
|
|
|
14
|
|
|
parent::beforeAction(); |
15
|
|
|
|
16
|
|
|
Config::addJsConfig('curPage', "newsfeed"); |
17
|
|
|
|
18
|
|
|
$action = $this->request->param('action'); |
19
|
|
|
$actions = ['create', 'getUpdateForm', 'update', 'getById', 'delete']; |
20
|
|
|
$this->Security->requirePost($actions); |
|
|
|
|
21
|
|
|
|
22
|
|
|
switch($action){ |
23
|
|
|
case "create": |
24
|
|
|
$this->Security->config("form", [ 'fields' => ['content']]); |
|
|
|
|
25
|
|
|
break; |
26
|
|
|
case "getUpdateForm": |
27
|
|
|
$this->Security->config("form", [ 'fields' => ['newsfeed_id']]); |
|
|
|
|
28
|
|
|
break; |
29
|
|
|
case "update": |
30
|
|
|
$this->Security->config("form", [ 'fields' => ['newsfeed_id', 'content']]); |
|
|
|
|
31
|
|
|
break; |
32
|
|
|
case "getById": |
33
|
|
|
case "delete": |
34
|
|
|
$this->Security->config("form", [ 'fields' => ['newsfeed_id']]); |
|
|
|
|
35
|
|
|
break; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function index(){ |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->user->clearNotifications(Session::getUserId(), $this->newsfeed->table); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$pageNum = $this->request->query("page"); |
44
|
|
|
|
45
|
|
|
echo $this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('VIEWS_PATH') . 'newsfeed/index.php', ['pageNum' => $pageNum]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function create(){ |
|
|
|
|
49
|
|
|
|
50
|
|
|
$content = $this->request->data("content"); |
51
|
|
|
|
52
|
|
|
$newsfeed = $this->newsfeed->create(Session::getUserId(), $content); |
|
|
|
|
53
|
|
|
|
54
|
|
|
if(!$newsfeed){ |
55
|
|
|
|
56
|
|
|
Session::set('newsfeed-errors', $this->newsfeed->errors()); |
|
|
|
|
57
|
|
|
Redirector::root("NewsFeed"); |
58
|
|
|
|
59
|
|
|
}else{ |
60
|
|
|
|
61
|
|
|
Redirector::root("NewsFeed"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function getUpdateForm(){ |
|
|
|
|
66
|
|
|
|
67
|
|
|
$newsfeedId = Encryption::decryptIdWithDash($this->request->data("newsfeed_id")); |
68
|
|
|
|
69
|
|
|
if(!$this->newsfeed->exists($newsfeedId)){ |
|
|
|
|
70
|
|
|
$this->error(404); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$newsfeed = $this->newsfeed->getById($newsfeedId); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$html = $this->view->render(Config::get('VIEWS_PATH') . 'newsfeed/updateForm.php', array("newsfeed" => $newsfeed[0])); |
76
|
|
|
echo $this->view->JSONEncode(array("data" => $html)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function update(){ |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Remember? each news feed has an id that looks like this: feed-51b2cfa |
82
|
|
|
$newsfeedId = Encryption::decryptIdWithDash($this->request->data("newsfeed_id")); |
83
|
|
|
$content = $this->request->data("content"); |
84
|
|
|
|
85
|
|
|
if(!$this->newsfeed->exists($newsfeedId)){ |
|
|
|
|
86
|
|
|
$this->error(404); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$newsfeed = $this->newsfeed->update($newsfeedId, $content); |
|
|
|
|
90
|
|
|
if(!$newsfeed){ |
91
|
|
|
echo $this->view->renderErrors($this->newsfeed->errors()); |
|
|
|
|
92
|
|
|
}else{ |
93
|
|
|
|
94
|
|
|
$html = $this->view->render(Config::get('VIEWS_PATH') . 'newsfeed/newsfeed.php', array("newsfeed" => $newsfeed)); |
95
|
|
|
echo $this->view->JSONEncode(array("data" => $html)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function getById(){ |
|
|
|
|
100
|
|
|
|
101
|
|
|
$newsfeedId = Encryption::decryptIdWithDash($this->request->data("newsfeed_id")); |
102
|
|
|
|
103
|
|
|
if(!$this->newsfeed->exists($newsfeedId)){ |
|
|
|
|
104
|
|
|
$this->error(404); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$newsfeed = $this->newsfeed->getById($newsfeedId); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$html = $this->view->render(Config::get('VIEWS_PATH') . 'newsfeed/newsfeed.php', array("newsfeed" => $newsfeed)); |
110
|
|
|
echo $this->view->JSONEncode(array("data" => $html)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function delete(){ |
114
|
|
|
|
115
|
|
|
$newsfeedId = Encryption::decryptIdWithDash($this->request->data("newsfeed_id")); |
116
|
|
|
|
117
|
|
|
$this->newsfeed->deleteById($newsfeedId); |
|
|
|
|
118
|
|
|
echo $this->view->JSONEncode(array("success" => true)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isAuthorized(){ |
122
|
|
|
|
123
|
|
|
$action = $this->request->param('action'); |
124
|
|
|
$role = Session::getUserRole(); |
125
|
|
|
$resource = "newsfeed"; |
126
|
|
|
|
127
|
|
|
// only for admins |
128
|
|
|
Permission::allow('admin', $resource, ['*']); |
129
|
|
|
|
130
|
|
|
// only for normal users |
131
|
|
|
Permission::allow('user', $resource, ['index', 'getById', 'create']); |
132
|
|
|
Permission::allow('user', $resource, ['update', 'delete', 'getUpdateForm'], 'owner'); |
133
|
|
|
|
134
|
|
|
$newsfeedId = $this->request->data("newsfeed_id"); |
135
|
|
|
if(!empty($newsfeedId)){ |
136
|
|
|
$newsfeedId = Encryption::decryptIdWithDash($newsfeedId); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$config = [ |
140
|
|
|
"user_id" => Session::getUserId(), |
141
|
|
|
"table" => "newsfeed", |
142
|
|
|
"id" => $newsfeedId]; |
143
|
|
|
|
144
|
|
|
return Permission::check($role, $resource, $action, $config); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.