-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalendar.php
More file actions
381 lines (352 loc) · 10.9 KB
/
Calendar.php
File metadata and controls
381 lines (352 loc) · 10.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
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
<?php
/**
* Calendar Model
*
* @property Block $Block
* @property Room $Room
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('CalendarsAppModel', 'Calendars.Model');
App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior');
/**
* Calendar Model
*
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @package NetCommons\Calendars\Model
*/
class Calendar extends CalendarsAppModel {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
//'Workflow.WorkflowComment',
//'Workflow.Workflow',
'Blocks.BlockSetting' => array(
BlockSettingBehavior::FIELD_USE_WORKFLOW,
),
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Block' => array(
'className' => 'Blocks.Block',
'foreignKey' => 'block_key',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'CalendarRrule' => array(
'className' => 'Calendars.CalendarRrule',
'foreignKey' => 'calendar_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => array('id' => 'ASC'),
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* Validation rules
*
* @var array
*/
public $validate = array(
);
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @see Model::__construct()
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->loadModels([
'Frame' => 'Frames.Frame',
'CalendarFrameSetting' => 'Calendars.CalendarFrameSetting',
////'CalendarSetting' => 'Calendars.CalendarSetting',
////'MailSetting' => 'Mails.MailSetting',
]);
}
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @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/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = array_merge($this->validate, array(
'block_key' => array(
'rule1' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
'on' => 'update', // 新規の時はブロックIDがなかったりすることがあるので
),
),
//'name' => array(
// 'notBlank' => array(
// 'rule' => array('notBlank'),
// 'message' => sprintf(__d('net_commons', 'Please input %s.'), __d('calendars', 'CALENDAR Name')), //カレンダー名は人間で入れない。プログラムが挿入すること。
// 'allowEmpty' => false,
// 'required' => true,
// ),
//),
//key,language_id は、NetCommonsプラグインがafterSaveで差し込むので、ノーチェック
));
//カレンダーの場合、配置直後の場合、配下にCalenarCompRruleが1件もないことがあり得るので、
//配下のレコード有無は調べない。
return parent::beforeValidate($options);
}
/**
* ブロック・カレンダーデータ準備
*
* @param array $data 保存対象データ
* @return mixed
* @throws InternalErrorException
*/
public function prepareBlockWithoutFrame($data) {
$this->begin();
try {
$block = array();
if (isset($data['Block'])) {
$block = $this->Block->findById($data['Block']['id']);
}
// まだない場合
if (empty($block)) {
// ブロックを作成する
$block = $this->_makeBlock($data['CalendarActionPlan']['plan_room_id']);
if (! $block) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//取得したBlockを$current[Block]に記録しておく。
Current::$current['Block'] = $block['Block'];
$data['Block'] = $block['Block'];
}
//権限設定
$calendar = $this->_saveCalendar($block);
if (! $calendar) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$data['Calendar'] = $calendar['Calendar'];
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback();
//エラー出力
CakeLog::error($ex);
throw $ex; //再throw
}
return $data;
}
/**
* After frame save hook
*
* このルーム・この言語で、アンケートブロックが存在しない場合、Blockモデルにsaveで新規登録する。
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws BadRequestException
* @throws InternalErrorException
*/
public function afterFrameSave($data) {
// すでに結びついていて実在が確認できる場合は何もしないでよい
if (! empty($data['Frame']['block_id']) && $this->Block->findById($data['Frame']['block_id'])) {
return $data;
}
$this->begin();
try {
// Frameなしでやってくることは想定外
if (empty($data['Frame'])) {
throw new BadRequestException(__d('net_commons', 'Bad Request'));
}
$frame = $data['Frame']; //FrameモデルですでにFrameモデルデータは登録済み
// ブロックを取得、または作成する
$block = $this->_makeBlock($frame['room_id']);
//取得したBlockを$data[Block]に記録しておく。
$data['Block'] = $block['Block'];
//Frameモデルに、このブロックのidを記録しておく。 カレンダーの場合、Frame:Blockの関係は n:1
$data['Frame']['block_id'] = $block['Block']['id'];
if (! $this->Frame->save($data)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//新規に生成したBlockのidをCurrent[Frame]に追記しておく。
Current::$current['Frame']['block_id'] = $block['Block']['id'];
//このフレーム用の「表示方法変更」「権限設定」「メール設定」のレコードを
//1セット用意します。
//このフレームの「表示方法変更」
if (! $this->_saveFrameChangeAppearance($data['Frame'])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//権限設定
$calendar = $this->_saveCalendar($block);
if (! $calendar) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$data['Calendar'] = $calendar['Calendar'];
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback();
//エラー出力
CakeLog::error($ex);
throw $ex; //再throw
}
return $data;
}
/**
* saveMailSetting
*
* メール設定データを登録する
*
* @return mixed On success Model::$data if its not empty or true, false on failure
*/
protected function _saveMailSetting() {
//$data = $this->_generateMailSettingData();
//$this->MailSetting->set($data);
//if (! $this->MailSetting->validates($data, false)) {
// CakeLog::error(serialize($this->MailSetting->validationErrors));
// return false;
//}
//$data = $this->MailSetting->save($data, false);
//if (! $data) {
// CakeLog::error(serialize($this->MailSetting->validationErrors));
// return false;
//}
//return $data;
return array(); //暫定
}
/**
* generateMailSettingData
*
* メール設定データを生成する
*
* @return array 生成したメール設定データ
*/
protected function _generateMailSettingData() {
$data = $this->MailSetting->create();
$data = array_merge($data,
array(
$this->MailSetting->alias => array(
'block_key' => Current::read('Block.key'),
'plugin_key' => Current::read('Block.plugin_key'),
'type_key' => 'aaa', //定型文の種類',
'mail_fixed_phrase_subject' => 'bbb', //定型文 件名
'mail_fixed_phrase_body' => 'ccc', //定型文 本文
'replay_to' => 'ddd', //返信先アドレス
)
)
);
return $data;
}
/**
* _saveFrameChangeAppearance
*
* フレームの「表示方法変更」のデータの登録
*
* @param array $frame フレーム
* @return array 生成したデータ
*/
protected function _saveFrameChangeAppearance($frame) {
$frameKey = $frame['key'];
$frameSetting = $this->CalendarFrameSetting->find('first', array(
'conditions' => array(
'frame_key' => $frameKey
),
'recursive' => -1
));
if ($frameSetting) {
return $frameSetting;
}
$frameSetting = $this->CalendarFrameSetting->create();
$this->CalendarFrameSetting->setDefaultValue($frameSetting); //Modelの初期値設定
$frameSetting['CalendarFrameSetting']['frame_key'] = $frame['key'];
return $this->CalendarFrameSetting->saveFrameSetting($frameSetting);
}
/**
* _saveCalendar
*
* 権限設定のデータの登録
*
* @param array $block ブロック
* @return array 生成したデータ
*/
protected function _saveCalendar($block) {
// 今現在ブロックに対応したカレンダーがあるか
$calendar = $this->find('first', array(
'conditions' => array(
'block_key' => $block['Block']['key']
)
));
// ない場合は作成する
if (! $calendar) {
$this->create();
$this->Behaviors->disable('Blocks.BlockSetting');
$calendar = $this->save(array(
'block_key' => $block['Block']['key'],
//'use_workflow' => true
));
$this->Behaviors->enable('Blocks.BlockSetting');
// BlockSettingの use_workflow をroom_id指定で保存
$blockSetting = $this->createBlockSetting($block['Block']['room_id']);
$this->set($blockSetting);
$this->saveBlockSetting($block['Block']['key'], $block['Block']['room_id']);
}
return $calendar;
}
/**
* _makeBlock($frame);
*
* ブロックを作成する
*
* @param int $roomId ルームID
* @return array 生成したブロック
* @throws InternalErrorException
*/
protected function _makeBlock($roomId) {
//Frameモデルに記録されているのと同じ「ルーム,言語,plugin_key=カレンダ」のレコードが
//Blockモデルに存在するか調べる
$block = $this->Block->findByRoomIdAndPluginKey($roomId, 'calendars');
if ($block) {
return $block;
}
$this->Block->create();
$block = $this->Block->save(array(
'room_id' => $roomId,
'plugin_key' => 'calendars',
));
if (! $block) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
Current::$current['Block'] = $block['Block'];
return $block;
}
}