-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTopicsBehavior.php
More file actions
344 lines (306 loc) · 9.62 KB
/
TopicsBehavior.php
File metadata and controls
344 lines (306 loc) · 9.62 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
<?php
/**
* Topics Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @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('TopicsBaseBehavior', 'Topics.Model/Behavior');
/**
* Topics Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Topics\Model\Behavior
*/
class TopicsBehavior extends TopicsBaseBehavior {
/**
* ビヘイビアの設定
*
* @var array
* @see ModelBehavior::$settings
*/
protected $_settings = array(
'fields' => array(
'title' => '', //必須項目
'summary' => '', //必須項目
'content_key' => 'key',
'content_id' => 'id',
'path' => '/:plugin_key/:plugin_key/view/:block_id/:content_key',
'title_icon' => null,
'category_id' => null,
'public_type' => null,
'publish_start' => null,
'publish_end' => null,
'is_no_member_allow' => null,
'is_answer' => false,
'answer_period_start' => null,
'answer_period_end' => null,
'is_active' => null,
'is_latest' => null,
'status' => null,
'is_in_room' => null,
'is_origin' => null,
'is_translation' => null,
),
'search_contents' => array(
//ここにフィールドを追加、デフォルトでfields.titleとfields.summaryの内容が含まれる
),
'users' => array('0'),
'is_workflow' => true,
'data' => array(),
'titleHtml' => false,
'summaryWysiwyg' => false,
);
/**
* 削除するデータ保持用配列
*
* @var array
*/
protected $_deletedRow = array();
/**
* Setup this behavior with the specified configuration settings.
*
* @param Model $model 呼び出し元のモデル
* @param array $config Configuration settings for $model
* @return void
*/
public function setup(Model $model, $config = array()) {
parent::setup($model, $config);
$this->settings[$model->alias] = Hash::merge($this->_settings, $config);
//コンテンツは配列とする
if (is_string(Hash::get($this->settings[$model->alias], 'fields.summary'))) {
$this->settings[$model->alias]['fields']['summary'] = array(
Hash::get($this->settings[$model->alias], 'fields.summary')
);
}
//モデル名を付与する
$this->_setupFields($model);
//検索項目にfields.summaryの内容を含む
$this->_setupSearchContents($model);
$fields = $this->settings[$model->alias]['fields'];
$isAnswer = $fields['answer_period_start'] || $fields['answer_period_end'];
$this->settings[$model->alias]['fields']['is_answer'] = $isAnswer;
}
/**
* saveTopics
*
* @param Model $model 呼び出し元のモデル
* @return bool
*/
public function saveTopics(Model $model) {
$model->loadModels([
'Topic' => 'Topics.Topic',
'TopicUserStatus' => 'Topics.TopicUserStatus',
'TopicReadable' => 'Topics.TopicReadable',
]);
//新着データの登録
$this->_saveTopic($model);
//既読データのクリア
$this->_deleteTopicUserStatus($model);
//新着に表示させる会員のリスト登録
foreach ($model->data[$model->Topic->alias] as $topic) {
if (isset($topic['id'])) {
$this->_saveTopicReadable($model, $topic['id']);
}
}
return true;
}
/**
* beforeDeleteTopics
*
* @param Model $model 呼び出し元のモデル
* @return bool
*
* 速度改善の修正に伴って発生したため抑制
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function beforeDeleteTopics(Model $model) {
$model->loadModels([
'Block' => 'Blocks.Block',
'Topic' => 'Topics.Topic',
]);
//idからkey取得
if (! $model->blockId && ! $model->blockKey && ! $model->contentKey && $model->hasField('key')) {
$content = $model->find('first', array(
'recursive' => -1,
'fields' => array('id', 'key'),
'conditions' => array('id' => $model->id)
));
$model->contentKey = isset($content[$model->alias]['key'])
? $content[$model->alias]['key']
: null;
} elseif ($model->blockId && ! $model->blockKey) {
$block = $model->Block->find('first', array(
'recursive' => -1,
'fields' => array('id', 'key'),
'conditions' => array('id' => $model->blockId)
));
$model->blockKey = isset($block[$model->Block->alias]['key'])
? $block[$model->Block->alias]['key']
: null;
}
//削除するトピックID取得
$this->_deleteRow = array();
if ($model->blockKey) {
$blockIds = $model->Block->find('list', array(
'recursive' => -1,
'fields' => array('id', 'id'),
'conditions' => array('key' => $model->blockKey)
));
$result = $model->Topic->find('list', array(
'recursive' => -1,
'fields' => array('id', 'id'),
'conditions' => array('block_id' => $blockIds)
));
$this->_deleteRow = array_values($result);
} elseif ($model->contentKey) {
$result = $model->Topic->find('list', array(
'recursive' => -1,
'fields' => array('id', 'id'),
'conditions' => array('content_key' => $model->contentKey)
));
$this->_deleteRow = array_values($result);
}
return true;
}
/**
* afterDeleteTopics
*
* @param Model $model 呼び出し元のモデル
* @return void
* @throws InternalErrorException
*/
public function afterDeleteTopics(Model $model) {
$model->loadModels([
'Topic' => 'Topics.Topic',
'TopicReadable' => 'Topics.TopicReadable',
'TopicUserStatus' => 'Topics.TopicUserStatus',
]);
if ($this->_deleteRow) {
//読めるかどうかデータ削除
$conditions = array($model->TopicReadable->alias . '.topic_id' => $this->_deleteRow);
$result = $model->TopicReadable->deleteAll($conditions, false, false);
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//既読データ削除
$conditions = array($model->TopicUserStatus->alias . '.topic_id' => $this->_deleteRow);
$result = $model->TopicUserStatus->deleteAll($conditions, false, false);
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//新着データの削除
$conditions = array($model->Topic->alias . '.id' => $this->_deleteRow);
$result = $model->Topic->deleteAll($conditions, false, false);
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
}
/**
* afterSave is called after a model is saved.
*
* @param Model $model 呼び出し元のモデル
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return bool
* @see Model::save()
*/
public function afterSave(Model $model, $created, $options = array()) {
//$options['fieldList']がセットされているときは、saveFieldから実行されたとして判断し、処理しない
if ($options['fieldList']) {
return true;
}
$this->saveTopics($model);
return parent::afterSave($model, $created, $options);
}
/**
* Before delete is called before any delete occurs on the attached model, but after the model's
* beforeDelete is called. Returning false from a beforeDelete will abort the delete.
*
* @param Model $model Model using this behavior
* @param bool $cascade If true records that depend on this record will also be deleted
* @return mixed False if the operation should abort. Any other result will continue.
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function beforeDelete(Model $model, $cascade = true) {
return $this->beforeDeleteTopics($model);
}
/**
* After delete is called after any delete occurs on the attached model.
*
* @param Model $model Model using this behavior
* @return void
*/
public function afterDelete(Model $model) {
$this->afterDeleteTopics($model);
}
/**
* 既読データの登録
*
* @param Model $model 呼び出し元のモデル
* @param array $content コンテンツ
* @param bool $answered 回答したかどうか
* @return array
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function saveTopicUserStatus(Model $model, $content, $answered = false) {
$model->loadModels([
'Topic' => 'Topics.Topic',
'TopicUserStatus' => 'Topics.TopicUserStatus',
]);
$topicAlias = $model->Topic->alias;
$conditions = array(
$topicAlias . '.plugin_key' => Current::read('Plugin.key'),
$topicAlias . '.language_id' => Current::read('Language.id'),
$topicAlias . '.block_id' => Current::read('Block.id', '0'),
$topicAlias . '.content_id' => Hash::get(
$content, $this->settings[$model->alias]['fields']['content_id'], '0'
),
);
//既読データ登録
$update = array(
'read' => true,
'answered' => $answered
);
$model->TopicUserStatus->saveTopicUserStatus($content, $conditions, $update);
return true;
}
/**
* 読めるユーザIDをセットする
*
* @param Model $model 呼び出し元のモデル
* @param array $users ユーザIDのリスト
* @param bool $append 追加かどうか
* @return void
* @throws InternalErrorException
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function setTopicUsers(Model $model, $users, $append = false) {
if ($append) {
$this->settings[$model->alias]['users'] = array_merge(
$this->settings[$model->alias]['users'], $users
);
} else {
$this->settings[$model->alias]['users'] = $users;
}
}
/**
* 新着のデータをセットする
*
* @param Model $model 呼び出し元のモデル
* @param string $key キー
* @param mixed $value 値
* @return void
* @throws InternalErrorException
*/
public function setTopicValue(Model $model, $key, $value) {
$this->settings[$model->alias]['data'][$key] = $value;
}
}