-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideoValidationBehavior.php
More file actions
227 lines (206 loc) · 6.9 KB
/
VideoValidationBehavior.php
File metadata and controls
227 lines (206 loc) · 6.9 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
<?php
/**
* Video Validation Behavior
*
* @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('Video', 'Videos.Model');
/**
* Summary for Video Validation Behavior
*/
class VideoValidationBehavior extends ModelBehavior {
/**
* セッティングの種類(setSettingで利用)
*
* @var string ffmpeg 有効フラグ
*/
const IS_FFMPEG_ENABLE = 'isFfmpegEnable';
/**
* setup
*
* @param Model $model モデル
* @param array $settings 設定値
* @return void
* @link http://book.cakephp.org/2.0/ja/models/behaviors.html#ModelBehavior::setup
*/
public function setup(Model $model, $settings = array()) {
$this->settings[$model->alias] = $settings;
}
/**
* セッティング セット
*
* @param Model $model モデル
* @param string $settingKey セッティングのキー
* @param string|array $settingValue セッティングの値
* @return void
* @see VideoValidationBehavior::IS_FFMPEG_ENABLE
*/
public function setVideoValidationSettings(Model $model, $settingKey, $settingValue) {
$this->settings[$model->alias][$settingKey] = $settingValue;
}
/**
* beforeValidate is called before a model is validated, you can use this callback to
* add behavior validation rules into a models validate array. Returning false
* will allow you to make the validation fail.
*
* @param Model $model モデル
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
parent::beforeValidate($model, $options);
if (in_array('edit', $options, true)) {
// 編集時の動画 任意 対応
if (isset($model->data[$model->alias][Video::VIDEO_FILE_FIELD]['size']) &&
$model->data[$model->alias][Video::VIDEO_FILE_FIELD]['size'] === 0) {
unset($model->data[$model->alias][Video::VIDEO_FILE_FIELD]);
}
// 編集時のサムネイル 任意 対応
if (isset($model->data[$model->alias][Video::THUMBNAIL_FIELD]['size']) &&
$model->data[$model->alias][Video::THUMBNAIL_FIELD]['size'] === 0) {
unset($model->data[$model->alias][Video::THUMBNAIL_FIELD]);
}
}
return true;
}
/**
* ルール定義
*
* @param Model $model モデル
* @param array $options Options passed from Model::save().
* @return array
*/
public function rules(Model $model, $options = array()) {
$rules = $this->__initValidate($model);
$isFfmpegEnable = Hash::get($this->settings, $model->alias . '.' . self::IS_FFMPEG_ENABLE);
if ($isFfmpegEnable) {
// ffmpeg=ON
$extension = Video::VIDEO_EXTENSION;
$mimeType = Video::VIDEO_MIME_TYPE;
} else {
// ffmpeg=OFF
$extension = 'mp4';
$mimeType = 'video/mp4';
}
if (in_array('add', $options, true)) {
// --- 登録時
if (!$isFfmpegEnable) {
$rules = ValidateMerge::merge($rules, array(
// 必須
Video::THUMBNAIL_FIELD => array(
// http://book.cakephp.org/2.0/ja/models/data-validation.html#Validation::uploadError
'upload-file' => array(
'rule' => array('uploadError'),
'message' => array(__d('files', 'Please specify the file'))
),
// http://book.cakephp.org/2.0/ja/models/data-validation.html#Validation::extension
'extension' => array(
'rule' => array('extension', explode(',', Video::THUMBNAIL_EXTENSION)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
// http://book.cakephp.org/2.0/ja/models/data-validation.html#Validation::mimeType
'mimetype' => array(
'rule' => array('mimeType', explode(',', Video::THUMBNAIL_MIME_TYPE)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
),
));
}
// 必須
$rules = ValidateMerge::merge($rules, array(
Video::VIDEO_FILE_FIELD => array(
'upload-file' => array(
'rule' => array('uploadError'),
'message' => array(__d('files', 'Please specify the file'))
),
'extension' => array(
'rule' => array('extension', explode(',', $extension)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
'mimetype' => array(
'rule' => array('mimeType', explode(',', $mimeType)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
),
));
} elseif (in_array('edit', $options, true)) {
// --- 編集時
$rules = ValidateMerge::merge($rules, array(
// 任意
Video::VIDEO_FILE_FIELD => array(
'extension' => array(
'rule' => array('extension', explode(',', $extension)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
'mimetype' => array(
'rule' => array('mimeType', explode(',', $mimeType)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
),
// 任意
Video::THUMBNAIL_FIELD => array(
'extension' => array(
'rule' => array('extension', explode(',', Video::THUMBNAIL_EXTENSION)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
'mimetype' => array(
'rule' => array('mimeType', explode(',', Video::THUMBNAIL_MIME_TYPE)),
'message' => array(__d('files', 'It is upload disabled file format'))
),
),
));
//NC2からNC3で移行するとサムネイルが移行されないため、サムネイルのupload_idが空であれば、
//エラーにならないようにunsetする。
//@see https://github.com/NetCommons3/NetCommons3/issues/1617
if (isset($model->data['UploadFile']['thumbnail']['id']) &&
! $model->data['UploadFile']['thumbnail']['id']) {
unset($model->data['UploadFile']['thumbnail']);
}
}
return $rules;
}
/**
* 初期ルール
*
* @param Model $model モデル
* @return array 初期ルール
*/
private function __initValidate(Model $model) {
return ValidateMerge::merge($model->validate, array(
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'block_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
'title' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('videos', 'title')),
'required' => true,
),
),
'category_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'allowEmpty' => true,
),
),
));
}
}