-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVideoSetting.php
More file actions
362 lines (322 loc) · 9.26 KB
/
VideoSetting.php
File metadata and controls
362 lines (322 loc) · 9.26 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* VideoSetting Model
*
* @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');
App::uses('VideosAppModel', 'Videos.Model');
App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior');
/**
* VideoSetting Model
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Videos\Model
*/
class VideoSetting extends VideosAppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* use behaviors
*
* @var array
* @see NetCommonsAppModel::$actAs
* @see BlockBehavior
* @see OriginalKeyBehavior
* @see MailSettingBehavior::beforeDelete()
* @see CategoryBehavior
*/
public $actsAs = array(
'Blocks.Block' => array(
'name' => 'VideoSetting.name',
// save, delete時にloadModels()
// delete時にblock_id, block_keyで紐づいてるデータ削除
'loadModels' => array(
'BlockSetting' => 'Blocks.BlockSetting',
'BlocksLanguage' => 'Blocks.BlocksLanguage',
'Category' => 'Categories.Category',
'CategoryOrder' => 'Categories.CategoryOrder',
'ContentComment' => 'ContentComments.ContentComment',
'Like' => 'Likes.Like',
'Tag' => 'Tags.Tag',
'Video' => 'Videos.Video',
// メール関連
'MailSetting' => 'Mails.MailSetting',
'MailSettingFixedPhrase' => 'Mails.MailSettingFixedPhrase',
'MailQueue' => 'Mails.MailQueue',
'MailQueueUser' => 'Mails.MailQueueUser',
)
),
'Blocks.BlockSetting' => array(
BlockSettingBehavior::FIELD_USE_WORKFLOW,
BlockSettingBehavior::FIELD_USE_LIKE,
BlockSettingBehavior::FIELD_USE_UNLIKE,
BlockSettingBehavior::FIELD_USE_COMMENT,
BlockSettingBehavior::FIELD_USE_COMMENT_APPROVAL,
'auto_play',
),
'Categories.Category',
'NetCommons.OriginalKey',
'Blocks.BlockRolePermission',
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Block' => array(
'className' => 'Blocks.Block',
'foreignKey' => false,
'conditions' => array(
'Block.key = VideoSetting.block_key',
),
'fields' => '',
'order' => ''
),
);
/**
* beforeValidate
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
if (!parent::beforeValidate($options)) {
return false;
}
if (isset($this->data['BlocksLanguage']['name'])) {
$this->loadModels(array(
'BlocksLanguage' => 'Blocks.BlocksLanguage',
));
$this->BlocksLanguage->validate = array(
'name' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('videos', 'Channel name')),
'required' => true,
),
)
);
$this->BlocksLanguage->set($this->data['BlocksLanguage']);
if (!$this->BlocksLanguage->validates()) {
$this->validationErrors = Hash::merge(
$this->validationErrors,
$this->BlocksLanguage->validationErrors
);
return false;
}
}
}
/**
* VideoSettingデータ新規作成
*
* @return array
*/
public function createVideoSetting() {
$videoSetting = $this->createAll();
$this->loadModels(array(
'BlocksLanguage' => 'Blocks.BlocksLanguage',
));
$videoSetting = Hash::merge($videoSetting, $this->BlocksLanguage->createAll(array(
'BlocksLanguage' => array(
'name' => __d('videos', 'New channel %s', date('YmdHis')),
),
)));
/** @see BlockSettingBehavior::createBlockSetting() */
return Hash::merge($videoSetting, $this->createBlockSetting());
}
/**
* VideoSettingデータ取得
*
* @return array
* @throws InternalErrorException
*/
public function getVideoSetting() {
$conditions = array(
$this->alias . '.block_key' => Current::read('Block.key'),
);
$belongsTo = $this->Block->bindModelBlockLang();
$this->bindModel($belongsTo, true);
$videoSetting = $this->find('first', array(
'recursive' => 0,
'conditions' => $conditions,
'order' => $this->alias . '.id DESC'
));
if (!$videoSetting) {
throw new InternalErrorException('Not found ' . __METHOD__);
}
/** @see BlockSettingBehavior::getBlockSetting() */
return Hash::merge($videoSetting, $this->getBlockSetting());
}
/**
* VideoSettingデータ保存
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveVideoSetting($data) {
//トランザクションBegin
$this->begin();
// 値をセット
$this->set($data);
if (! $this->validates()) {
return false;
}
try {
if (! $this->save(null, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
/**
* 総容量のみ更新
*
* @param int $totalSize 総容量
* @return bool
* @throws InternalErrorException
*/
public function saveTotalSize($totalSize) {
$videoSetting = $this->getVideoSetting();
$this->id = $videoSetting['VideoSetting']['id'];
// コールバックoff
$validate = array(
'validate' => false,
'callbacks' => false,
);
// 総容量のみ更新
if (! $this->saveField('total_size', $totalSize, $validate)) {
throw new InternalErrorException('Failed ' . __METHOD__);
}
return true;
}
/**
* VideoSettingデータ削除
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function deleteVideoSetting($data) {
$this->loadModels(array(
'Like' => 'Likes.Like',
'LikesUser' => 'Likes.LikesUser',
'Tag' => 'Tags.Tag',
'TagsContent' => 'Tags.TagsContent',
'UploadFile' => 'Files.UploadFile',
'Video' => 'Videos.Video',
));
//トランザクションBegin
$this->begin();
$blockKey = $data[$this->alias]['block_key'];
// 多言語コンテンツ削除対応
// 各IDの配列
$blockIds = $this->__getIds($this->Block, 'key', $blockKey);
$likeIds = $this->__getIds($this->Like, 'block_key', $blockKey);
$tagIds = $this->__getIds($this->Tag, 'block_id', $blockIds);
// コンテンツキーの配列
$contentKeys = $this->__getContentKeys($blockIds);
// アップロードファイル
$uploadFiles = $this->UploadFile->find('all', array(
'recursive' => 1,
'conditions' => array($this->UploadFile->alias . '.content_key' => $contentKeys),
'callbacks' => false,
));
try {
// VideoSetting削除
$this->__deleteAll($this, 'block_key', $blockKey);
// 動画とサムネイルのデータと物理ファイル削除
foreach ($uploadFiles as $uploadFile) {
foreach ($uploadFile['UploadFilesContent'] as $uploadFilesContent) {
$this->UploadFile->removeFile($uploadFilesContent['content_id'],
$uploadFilesContent['upload_file_id']);
}
}
// アップロードファイル 削除
$this->__deleteAll($this->UploadFile, 'content_key', $contentKeys, true);
// タグコンテンツ 削除
$this->__deleteAll($this->TagsContent, 'tag_id', $tagIds);
// いいねユーザー 削除
$this->__deleteAll($this->LikesUser, 'like_id', $likeIds);
//Blockデータ削除
/** @see BlockBehavior::deleteBlock() */
$this->deleteBlock($blockKey);
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
/**
* コンテンツIDの配列
*
* @param Model $model モデル
* @param string $filed フィールド名
* @param string $value 値
* @return array
*/
private function __getIds(Model $model, $filed, $value) {
$ids = $model->find('list', array(
'recursive' => -1,
'conditions' => array($model->alias . '.' . $filed => $value),
'callbacks' => false,
));
$ids = array_keys($ids);
return $ids;
}
/**
* コンテンツキーの配列
*
* @param array $blockIds ブロックID複数
* @return array
*/
private function __getContentKeys($blockIds) {
$contentKeys = $this->Video->find('list', array(
'fields' => array($this->Video->alias . '.key'),
'recursive' => -1,
'conditions' => array($this->Video->alias . '.block_id' => $blockIds),
'callbacks' => false,
));
$contentKeys = array_values($contentKeys);
return $contentKeys;
}
/**
* 全削除
*
* @param Model $model モデル
* @param string $filed フィールド名
* @param string $value 値
* @param bool $callBackFlag callBack実行
* @return void
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
private function __deleteAll(Model $model, $filed, $value, $callBackFlag = false) {
if (empty($value)) {
return;
}
$conditions = array($model->alias . '.' . $filed => $value);
if (! $model->deleteAll($conditions, false, $callBackFlag)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}