-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlockSettingBehavior.php
More file actions
521 lines (472 loc) · 15.2 KB
/
BlockSettingBehavior.php
File metadata and controls
521 lines (472 loc) · 15.2 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
/**
* BlockSetting 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('ModelBehavior', 'Model');
App::uses('Current', 'NetCommons.Utility');
/**
* BlockSetting Behavior
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Blocks\Model\Behavior
*/
class BlockSettingBehavior extends ModelBehavior {
/**
* フィールド名
*
* @var string いいねボタンを使用する
* @var string わるいねボタンも使用する
* @var string コメントを使用する
* @var string 承認機能を使用する
* @var string コメント承認機能を使用する
*/
const
FIELD_USE_LIKE = 'use_like',
FIELD_USE_UNLIKE = 'use_unlike',
FIELD_USE_COMMENT = 'use_comment',
FIELD_USE_WORKFLOW = 'use_workflow',
FIELD_USE_COMMENT_APPROVAL = 'use_comment_approval';
/**
* フィールド名の種類
*
* @var string true or false
* @var string 数字
*/
const
TYPE_BOOLEAN = 'boolean',
TYPE_NUMERIC = 'numeric';
/**
* 余計なfieldを取得しないために取得するカラム名を定義する
*
* @var array
*/
private $__fields = [
'id', 'plugin_key', 'room_id', 'block_key', 'field_name', 'value', 'type'
];
/**
* 何度も同じ条件で取得しないようにキャッシュする
*
* @var bool
*/
private $__cache = [];
/**
* setup
*
* #### サンプルコード
* ##### Model
* ```php
* App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior');
*
* public $actsAs = array(
* 'Blocks.BlockSetting' => array(
* BlockSettingBehavior::USE_WORKFLOW,
* BlockSettingBehavior::USE_LIKE,
* BlockSettingBehavior::USE_UNLIKE,
* BlockSettingBehavior::USE_COMMENT,
* BlockSettingBehavior::USE_COMMENT_APPROVAL,
* 'auto_play',
* ),
* ),
* ```
*
* @param Model $model モデル
* @param array $settings 設定値
* @return void
*/
public function setup(Model $model, $settings = array()) {
$this->settings[$model->alias] = $settings;
}
/**
* beforeValidate
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False or null will abort the operation. Any other result will continue.
* @see Model::save()
*/
public function beforeValidate(Model $model, $options = array()) {
return $this->validateBlockSetting($model);
}
/**
* beforeSave
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False if the operation should abort. Any other result will continue.
* @see Model::save()
*/
public function beforeSave(Model $model, $options = array()) {
return $this->saveBlockSetting($model);
}
/**
* BlockSettingデータ新規作成
*
* @param Model $model モデル
* @param int $roomId ルームID
* @param bool $isRow 縦持ちデータ取得するか
* @return array
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function createBlockSetting(Model $model, $roomId = null, $isRow = false) {
$model->loadModels(array('BlockSetting' => 'Blocks.BlockSetting'));
$pluginKey = $this->_getPluginKey($model);
if (is_null($roomId)) {
$roomId = Current::read('Room.id');
}
// room_idなし, block_keyなし
$conditions = array(
'plugin_key' => $pluginKey,
'room_id' => null,
'block_key' => null,
'field_name' => $this->_getFields($model),
);
$blockSettings = $this->__findBlockSetting($model, $conditions);
// use_workflow, use_comment_approval新規作成
$blockSettings = $this->_getDefaultApproval($model, $blockSettings,
self::FIELD_USE_WORKFLOW, 1);
$blockSettings = $this->_getDefaultApproval($model, $blockSettings,
self::FIELD_USE_COMMENT_APPROVAL, 1);
// 縦持ち
// 新規登録時に不要な部分を除外
$blockSettings = Hash::remove($blockSettings, '{n}.{s}.id');
$blockSettings = Hash::insert($blockSettings, '{n}.{s}.plugin_key', $pluginKey);
$blockSettings = Hash::insert($blockSettings, '{n}.{s}.room_id', $roomId);
// 横持ち、縦持ちにデータ変換
return $this->_convertColAndRow($model, $blockSettings, $isRow);
}
/**
* フィールド取得
*
* @param Model $model モデル
* @return array フィールド
*/
protected function _getFields($model) {
return $this->settings[$model->alias];
}
/**
* プラグインキー取得
*
* @param Model $model モデル
* @return string プラグインキー
*/
protected function _getPluginKey($model) {
return Inflector::underscore($model->plugin);
}
/**
* BlockSettingのデータ取得
*
* @param Model $model モデル
* @param array $conditions 条件
* @return array ブロックセッティングデータ
*/
private function __findBlockSetting(Model $model, $conditions) {
// プラグインキーでの絞り込みを外す
$pluginKey = $conditions['plugin_key'];
// ルームIDとブロックキーが存在しない場合(新規追加の場合)は、プラグインを絞り込む
if (!is_null($conditions['room_id']) && !is_null($conditions['block_key'])) {
unset($conditions['plugin_key']);
}
// ブロックキーのがnullではない場合、絞り込みを外す
$blockKey = $conditions['block_key'];
if (!is_null($conditions['block_key'])) {
unset($conditions['block_key']);
}
// フィールドネームの絞り込みを外す
$fieldName = $conditions['field_name'];
unset($conditions['field_name']);
// キャッシュが存在しない場合
if (!isset($this->__cache[$conditions['room_id']])) {
// そのルームのブロックセッティングを全て取得する
$this->__cache[$conditions['room_id']] = $model->BlockSetting->find('all', array(
'recursive' => -1,
'fields' => $this->__fields,
'conditions' => $conditions,
));
}
$results = $this->__cache[$conditions['room_id']];
$blockSettings = [];
foreach ($results as $result) {
// 指定された、プラグインキー、ブロックキー、フィールドネームのみ抽出する
if ($result['BlockSetting']['plugin_key'] == $pluginKey
&& $result['BlockSetting']['block_key'] == $blockKey
&& in_array($result['BlockSetting']['field_name'], $fieldName)) {
$blockSettings[] = $result;
}
}
return $blockSettings;
}
/**
* BlockSettingデータ取得
*
* @param Model $model モデル
* @param string $blockKey ブロックキー
* @param int $roomId ルームID
* @param bool $isRow 縦持ちデータ取得するか
* @return array
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function getBlockSetting(Model $model, $blockKey = null, $roomId = null,
$isRow = false) {
$model->loadModels(array('BlockSetting' => 'Blocks.BlockSetting'));
if (is_null($blockKey)) {
$blockKey = Current::read('Block.key');
}
if (is_null($roomId)) {
$roomId = Current::read('Room.id');
}
$pluginKey = $this->_getPluginKey($model);
// room_idあり, block_keyあり
$conditions = array(
'plugin_key' => $pluginKey,
'room_id' => $roomId,
'block_key' => $blockKey,
'field_name' => $this->_getFields($model),
);
$blockSettings = $this->__findBlockSetting($model, $conditions);
if (!$blockSettings) {
// データなければ新規作成
$blockSettings = $this->createBlockSetting($model, $roomId, $isRow);
return $blockSettings;
}
// use_workflow, use_comment_approval取得
$blockSettings = $this->_getDefaultApproval($model, $blockSettings,
self::FIELD_USE_WORKFLOW);
$blockSettings = $this->_getDefaultApproval($model, $blockSettings,
self::FIELD_USE_COMMENT_APPROVAL);
// 横持ち、縦持ちにデータ変換
return $this->_convertColAndRow($model, $blockSettings, $isRow);
}
/**
* BlockSettingデータ存在するか
*
* @param Model $model モデル
* @param string $blockKey ブロックキー
* @param int $roomId ルームID
* @return bool
*/
public function isExsistBlockSetting(Model $model, $blockKey = null, $roomId = null) {
$model->loadModels(array('BlockSetting' => 'Blocks.BlockSetting'));
if (is_null($blockKey)) {
$blockKey = Current::read('Block.key');
}
if (is_null($roomId)) {
$roomId = Current::read('Room.id');
}
$pluginKey = $this->_getPluginKey($model);
// room_idあり, block_keyあり
$conditions = array(
'plugin_key' => $pluginKey,
'room_id' => $roomId,
'block_key' => $blockKey,
'field_name' => $this->_getFields($model),
);
$blockSettings = $this->__findBlockSetting($model, $conditions);
return !empty($blockSettings);
}
/**
* use_workflow, use_comment_approvalの初期値取得
* room.need_approvalによって、値変わる
*
* @param Model $model モデル
* @param array $blockSettings ブロックセッティングデータ
* @param string $fieldName フィールド名
* @param int $create 新規作成フラグ 0:更新、1:新規作成
* @return array ブロックセッティングデータ
*/
protected function _getDefaultApproval(Model $model, $blockSettings, $fieldName, $create = 0) {
// settingに対象fieldの指定なければ、何もしない
$fields = $this->_getFields($model);
if (!in_array($fieldName, $fields)) {
return $blockSettings;
}
// 新規作成時はデータあっても、データなしとして扱い、use_workflow, use_comment_approvalを新規作成する
if (!$create) {
// データありは、何もしない(つまりデータを使う)
$fields = [];
foreach ($blockSettings as $blockSetting) {
$fields[] = $blockSetting['BlockSetting']['field_name'];
}
if (in_array($fieldName, $fields, true)) {
return $blockSettings;
}
}
// 以降はデータなし処理
$pluginKey = $this->_getPluginKey($model);
$roomId = Current::read('Room.id');
$defaultBlockSetting = array(
'BlockSetting' => array(
'plugin_key' => $pluginKey,
'room_id' => $roomId,
'block_key' => null,
'field_name' => $fieldName,
'value' => null,
'type' => BlockSettingBehavior::TYPE_BOOLEAN,
)
);
$needApproval = Current::read('Room.need_approval');
// ルーム承認する
if ($needApproval) {
$defaultBlockSetting['BlockSetting']['value'] = '1';
$blockSettings[] = $defaultBlockSetting;
return $blockSettings;
}
// ルーム承認しない
$defaultBlockSetting['BlockSetting']['value'] = '0';
$blockSettings[] = $defaultBlockSetting;
return $blockSettings;
}
/**
* 横持ち、縦持ちにデータ変換
*
* @param Model $model モデル
* @param array $blockSettings ブロックセッティングデータ
* @param bool $isRow 縦持ちデータ取得するか
* @return array
*/
protected function _convertColAndRow(Model $model, $blockSettings, $isRow) {
// [基のモデル] 横持ちに変換 & 基のモデルに付足し
$result[$model->alias] = Hash::combine($blockSettings,
'{n}.{s}.field_name',
'{n}.{s}.value');
if (!$isRow) {
return $result;
}
// [BlockSetting] 縦持ちでindexをfield_nameに変更
$result['BlockSetting'] = Hash::combine($blockSettings, '{n}.{s}.field_name', '{n}.{s}');
return $result;
}
/**
* BlockSettingデータ保存
*
* ### 仕組み
* 各プラグインの[横]の入力値を、BlockSettingをブロックキーで検索した縦データにセット or
* データなしなら、新規作成データが取得できるので、ブロックキーをセット
* して、縦データをsaveManyでまとめて保存します。
* ```
* //(例)各プラグインのBlockSettingControllerからの登録処理 [横]データ
* array(
* 'VideoSetting' => array(
* 'use_comment' => '1',
* 'use_like' => '1',
* 'use_unlike' => '0',
* 'auto_play' => '0',
* 'use_comment' => '1',
* )
* )
* ↓
* ↓ ブロックキーで検索した[縦]データにセット
* ↓
* array(
* 'use_comment' => array(
* 'id' => '1', // 新規登録時はidない
* 'plugin_key' => 'videos',
* 'room_id' => '2',
* 'block_key' => '2e86eb72e9cbd0ffa87ea23c81d4e3b7',
* 'field_name' => 'use_comment',
* 'value' => '1',
* 'type' => 'boolean',
* ),
* 'use_like' => array(
* 'id' => '1',
* 'plugin_key' => 'videos',
* 'room_id' => '2',
* 'block_key' => '2e86eb72e9cbd0ffa87ea23c81d4e3b7',
* 'field_name' => 'use_like',
* 'value' => '1',
* 'type' => 'boolean',
* ),
* 'use_unlike' => array(
* 'id' => '1',
* 'plugin_key' => 'videos',
* 'room_id' => '2',
* 'block_key' => '2e86eb72e9cbd0ffa87ea23c81d4e3b7',
* 'field_name' => 'use_unlike',
* 'value' => '0',
* 'type' => 'boolean',
* ),
* 'is_auto_play' => array(
* 'id' => '1',
* 'plugin_key' => 'videos',
* 'room_id' => '2',
* 'block_key' => '2e86eb72e9cbd0ffa87ea23c81d4e3b7',
* 'field_name' => 'auto_play',
* 'value' => '0',
* 'type' => 'boolean',
* ),
* )
* ```
*
* @param Model $model モデル
* @param string $blockKey ブロックキー
* @param int $roomId ルームID
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function saveBlockSetting(Model $model, $blockKey = null, $roomId = null) {
$model->loadModels(array('BlockSetting' => 'Blocks.BlockSetting'));
// 横の入力データを、検索した縦データにセット & 新規登録用にブロックキーをセット
if (is_null($blockKey)) {
$blockKey = Current::read('Block.key');
}
if (is_null($roomId)) {
$roomId = Current::read('Room.id');
}
$blockSetting = $this->getBlockSetting($model, $blockKey, $roomId, true);
$inputData = $model->data[$model->alias];
$saveData = null;
$fields = $this->_getFields($model);
// セッティングしたフィールドを基に、入力したフィールドのみsaveDataにする
foreach ($fields as $field) {
if (array_key_exists($field, $inputData)) {
$saveData[$field] = $blockSetting['BlockSetting'][$field];
$saveData[$field]['value'] = $inputData[$field];
// 新規登録用にブロックキーをセット
$saveData[$field]['block_key'] = $blockKey;
}
}
if ($saveData &&
!$model->BlockSetting->saveMany($saveData, ['validate' => false])) {
throw new InternalErrorException('Failed - BlockSetting ' . __METHOD__);
}
$this->__cache = [];
return true;
}
/**
* ブロックセッティングのValidate追加処理
*
* @param Model $model モデル
* @param string $blockKey ブロックキー
* @return bool
*/
public function validateBlockSetting(Model $model, $blockKey = null) {
$inputData = $model->data[$model->alias];
if (is_null($blockKey)) {
$blockKey = Current::read('Block.key');
}
$fields = $this->_getFields($model);
// 縦データ取得
$blockSetting = $this->getBlockSetting($model, $blockKey, null, true);
// セッティングしたフィールドを基に、入力したフィールドのみvalidateする
foreach ($fields as $field) {
if (array_key_exists($field, $inputData)) {
// validate追加
$rule = $blockSetting['BlockSetting'][$field]['type'];
$model->validate = Hash::merge($model->validate, array(
$field => array(
$rule => array(
'rule' => $rule,
'message' => __d('net_commons', 'Invalid request.'),
),
)
));
}
}
return true;
}
}