-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideosEditController.php
More file actions
220 lines (192 loc) · 6.07 KB
/
VideosEditController.php
File metadata and controls
220 lines (192 loc) · 6.07 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
<?php
/**
* 動画編集系 Controller
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('VideosAppController', 'Videos.Controller');
App::uses('VideosAppModel', 'Videos.Model');
App::uses('Video', 'Videos.Model');
/**
* 動画編集系 Controller
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Videos\Controller
*/
class VideosEditController extends VideosAppController {
/**
* use model
*
* @var array
*/
public $uses = array(
'Categories.Category',
'Videos.Video',
);
/**
* use components
*
* @var array
*/
public $components = array(
'Files.FileUpload',
'NetCommons.Permission' => array(
//アクセスの権限
'allow' => array(
'add,edit,delete' => 'content_creatable',
),
),
);
/**
* use helpers
*
* @var array
*/
public $helpers = array(
'Categories.Category',
'NetCommons.TitleIcon',
'Workflow.Workflow',
);
/**
* beforeFilter
*
* @return bool
* @see NetCommonsAppController::beforeFilter()
*/
public function beforeFilter() {
parent::beforeFilter();
// bugfix: post_max_size を超えると $_POSTが空っぽになり(php標準の動き)、postした値とサーバ側で受け取った値がずれて予期しないトークンエラー対応
// post & $_FILESが空 & add|editアクションのみ、セキュリティコンポーネントを除外
// 補足1: 除外すると、FileUploadComponent::startup()で、「post_max_size を超えると $_POSTが空っぽ」でエラーメッセージを表示してくれる。
// 補足2: 登録前にアップロード空チェックは、バリデーションで必須チェックしているため、empty($_FILES)を通しても問題なし
// 補足3: post_max_sizeを超えなければ$_FILESの値は消えない & upload_max_filesizeを超えた場合は、バリデーションのuploadErrorでチェックしているため、問題なし
// 補足4: $this->NetCommonsForm->unlockField(Video::VIDEO_FILE_FIELD);指定、$this->Security->csrfCheck = false;を指定しても「予期しないトークンエラー」となったため、$this->Security->unlockedActionsを使用
$unlockedActions = ['add', 'edit'];
if ($this->request->is('post') && empty($_FILES) && in_array($this->action, $unlockedActions)) {
$this->Security->unlockedActions = $unlockedActions;
}
// ブロック未選択は、何も表示しない
if (! Current::read('Block.id')) {
$this->setAction('emptyRender');
return false;
}
// FFMPEG有効フラグ
/* @see VideoBehavior::isFfmpegEnable() */
$this->set('isFfmpegEnable', $this->Video->isFfmpegEnable());
}
/**
* 登録
*
* @return CakeResponse
*/
public function add() {
$this->set('video', null);
$categories = $this->Category->getCategories(Current::read('Block.id'),
Current::read('Room.id'));
$this->set('categories', $categories);
if ($this->request->is('post')) {
//登録処理
$data = $this->data;
$data['Video']['status'] = $this->Workflow->parseStatus();
unset($data['Video']['id']);
// 登録
if ($this->Video->saveVideo($data)) {
return $this->redirect(NetCommonsUrl::backToPageUrl());
}
$this->NetCommons->handleValidationError($this->Video->validationErrors);
} else {
//表示処理
$this->request->data = Hash::merge($this->request->data,
$this->Video->create()
);
$this->request->data['Frame'] = Current::read('Frame');
$this->request->data['Block'] = Current::read('Block');
}
}
/**
* 編集
*
* @return CakeResponse
*/
public function edit() {
//動画の取得
$videoKey = $this->params['key'];
/** @see WorkflowBehavior::getWorkflowContents() */
$video = $this->Video->getWorkflowContents('first', array(
'recursive' => 1,
'conditions' => array(
'Video.key' => $videoKey
)
));
if (empty($video)) {
return $this->throwBadRequest();
}
$this->set('video', $video);
if (! $this->Video->canEditWorkflowContent($video)) {
return $this->throwBadRequest();
}
$categories = $this->Category->getCategories(Current::read('Block.id'),
Current::read('Room.id'));
$this->set('categories', $categories);
/* @see WorkflowCommentBehavior::getCommentsByContentKey() */
$comments = $this->Video->getCommentsByContentKey($videoKey);
$this->set('comments', $comments);
if ($this->request->is('put')) {
$data = $this->data;
$data['Video']['status'] = $this->Workflow->parseStatus();
unset($data['Video']['id']);
// 登録(ワークフロー対応のため、編集でも常にinsert)
if ($video = $this->Video->saveVideo($data, 1)) {
$url = NetCommonsUrl::actionUrl(array(
'controller' => 'videos',
'action' => 'view',
'block_id' => Current::read('Block.id'),
'frame_id' => Current::read('Frame.id'),
'key' => $video['Video']['key']
));
return $this->redirect($url);
}
$this->NetCommons->handleValidationError($this->Video->validationErrors);
} else {
$this->request->data = $video;
$this->request->data['Frame'] = Current::read('Frame');
$this->request->data['Block'] = Current::read('Block');
}
}
/**
* 削除
*
* @return CakeResponse
*/
public function delete() {
if (! $this->request->is('delete')) {
return $this->throwBadRequest();
}
$video = $this->Video->getWorkflowContents('first', array(
'recursive' => 1,
'conditions' => array(
$this->Video->alias . '.key' => $this->data['Video']['key']
)
));
//削除権限チェック
if (! $this->Video->canDeleteWorkflowContent($video)) {
return $this->throwBadRequest();
}
// 削除
if (!$this->Video->deleteVideo($this->data)) {
return $this->throwBadRequest();
}
// 一覧へ
$url = NetCommonsUrl::actionUrl(array(
'controller' => 'videos',
'action' => 'index',
'block_id' => $this->data['Block']['id'],
'frame_id' => $this->data['Frame']['id'],
));
$this->redirect($url);
}
}