-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinksController.php
More file actions
276 lines (232 loc) · 6.77 KB
/
LinksController.php
File metadata and controls
276 lines (232 loc) · 6.77 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Links Controller
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('LinksAppController', 'Links.Controller');
/**
* Links Controller
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Links\Controller
*
* @property Link $Link
*/
class LinksController extends LinksAppController {
public $helpers = array('Links.LinksStatus');
/**
* use model
*
* @var array
*/
public $uses = array(
'Links.Link',
'Links.LinkCategory',
);
/**
* use component
*
* @var array
*/
public $components = array(
'NetCommons.NetCommonsBlock', //use Announcement model
'NetCommons.NetCommonsFrame',
'NetCommons.NetCommonsRoomRole',
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow();
$frameId = (isset($this->params['pass'][0]) ? (int)$this->params['pass'][0] : 0);
//Frameのデータをviewにセット
if (! $this->NetCommonsFrame->setView($this, $frameId)) {
// 何もかえしてこなくなった
// $this->response->statusCode(400);
// return;
}
//Roleのデータをviewにセット
if (! $this->NetCommonsRoomRole->setView($this)) {
// $this->response->statusCode(400);
// return;
}
}
/**
* index method
*
* @param int $frameId frames.id
* @param string $lang ex)"en" or "ja" etc.
* @return CakeResponse A response object containing the rendered view.
*/
public function index($frameId = 0, $type = 'list') {
// $type = 'dropdown';
$this->set('type', $type);
// カテゴリ一覧を取得
$categories = $this->LinkCategory->getCategories(
$this->viewVars['blockId'] // MyTodo まだブロックレコードがないときは0なので、どうする?s
);
foreach($categories as &$category){
//Linkデータを取得
$links = $this->Link->getLinksByCategoryId(
$category['LinkCategory']['id'],
$this->viewVars['blockId'],
$this->viewVars['contentEditable']
);
$category['links'] = $links;
}
$this->set('categories', $categories);
return $this->render('Links/index');
}
/**
* show linkAdd method
*
* @param int $frameId frames.id
* @return CakeResponse A response object containing the rendered view.
*/
public function linkAdd($frameId = 0) {
if ($this->response->statusCode() !== 200) {
return $this->render(false);
}
//編集権限チェック
if (! $this->viewVars['contentEditable']) {
$this->response->statusCode(403);
return $this->render(false);
}
return $this->render('Links/link_add', false);
}
/**
* show manage method
*
* @param int $frameId frames.id
* @return CakeResponse A response object containing the rendered view.
*/
public function manage($frameId = 0) {
if ($this->response->statusCode() !== 200) {
return $this->render(false);
}
//編集権限チェック
if (! $this->viewVars['contentEditable']) {
$this->response->statusCode(403);
return $this->render(false);
}
return $this->render('Links/manage', false);
}
public function add_form($frameId = 0) {
return $this->render('Links/add_form', false);
}
public function add($frameId) {
if (! $this->request->isPost()) {
throw new MethodNotAllowedException();
}
$postData = $this->data;
//保存
if ($this->Link->add($this->data)) {
$result = array(
'name' => __d('net_commons', 'Successfully finished.'),
'link' => $postData,
);
$this->set(compact('result'));
$this->set('_serialize', 'result');
return $this->render(false);
} else {
$error = __d('net_commons', 'Failed to register data.');
$error .= $this->formatValidationErrors($this->Link->validationErrors);
throw new ForbiddenException($error);
}
}
// json で呼ぶこと前提 indexとほぼ一緒
// MyTodo indexもjson apiでデータもらう形に変えたらよさげ
public function all($frameId = 0) {
// カテゴリ一覧を取得
$categories = $this->LinkCategory->getCategories(
$this->viewVars['blockId'] // MyTodo まだブロックレコードがないときは0なので、どうする?s
);
foreach($categories as &$category){
//Linkデータを取得
$links = $this->Link->getLinksByCategoryId(
$category['LinkCategory']['id'],
$this->viewVars['blockId'],
$this->viewVars['contentEditable']
);
$category['links'] = $links;
}
$this->set('categories', $categories);
$this->set('_serialize', 'categories');
return $this->render(false);
}
public function update_weight_form($frameId = 0) {
// return $this->render('Links/update_weight_form', false);
}
public function delete_form($frameId = 0, $linkId) {
$this->set(compact('frameId', 'linkId'));
}
public function delete($frameId = 0) {
if (! $this->request->isPost()) {
throw new MethodNotAllowedException();
}
// MyTodo リンクIDから削除する権限があるか確認
$id = $this->data['Link']['id'];
//保存
if ($this->Link->delete($id)) {
// MyTodo 同一リンクの別バリエーションデータが存在しなければorderも削除。
$result = array(
'name' => __d('net_commons', 'Successfully finished.'),
);
$this->set(compact('result'));
$this->set('_serialize', 'result');
return $this->render(false);
} else {
throw new ForbiddenException(__d('net_commons', 'Failed to register data.'));
}
}
public function edit_form($frameId = 0, $linkId) {
$this->set(compact('frameId', 'linkId'));
}
public function edit($frameId, $linkId) {
if (! $this->request->isPost()) {
throw new MethodNotAllowedException();
}
$postData = $this->data;
//保存
if ($this->Link->save($this->data)) {
$result = array(
'name' => __d('net_commons', 'Successfully finished.'),
'link' => $postData,
);
$this->set(compact('result'));
$this->set('_serialize', 'result');
return $this->render(false);
} else {
$error = __d('net_commons', 'Failed to register data.');
$error .= $this->formatValidationErrors($this->Link->validationErrors);
throw new ForbiddenException($error);
}
}
public function visit($linkId) {
$link = $this->Link->findById($linkId);
$link['Link']['click_number']++;
$this->Link->save($link);
$this->redirect($link['Link']['url']);
return false;
}
// MyTodo モデルに移動するか、ヘルパかコンポーネントかビヘイビアにする…
protected function formatValidationErrors($validationErrors) {
$errors = array();
foreach($validationErrors as $field => $fieldErrors){
foreach($fieldErrors as $errorMessage){
$errors[] = __d('links', $field) . ':' . __d('links', $errorMessage);
}
}
$returnMessage = implode("\n", $errors);
return $returnMessage;
}
public function test(){
}
}