-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservation.php
More file actions
390 lines (356 loc) · 10.4 KB
/
Reservation.php
File metadata and controls
390 lines (356 loc) · 10.4 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
<?php
/**
* Reservation 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('ReservationsAppModel', 'Reservations.Model');
App::uses('BlockSettingBehavior', 'Blocks.Model/Behavior');
/**
* Reservation Model
*
* @author AllCreator Co., Ltd. <info@allcreator.net>
* @package NetCommons\Reservations\Model
*/
class Reservation extends ReservationsAppModel {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
//'NetCommons.OriginalKey',
//'Workflow.WorkflowComment',
//'Workflow.Workflow',
//'Blocks.BlockSetting' => array(
// BlockSettingBehavior::FIELD_USE_WORKFLOW,
//),
'Categories.Category'
);
/**
* 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(
//'ReservationRrule' => array(
// 'className' => 'Reservations.ReservationRrule',
// 'foreignKey' => 'reservation_id',
// 'dependent' => true,
// 'conditions' => '',
// 'fields' => '',
// 'order' => array('id' => 'ASC'),
// 'limit' => '',
// 'offset' => '',
// 'exclusive' => '',
// 'finderQuery' => '',
// 'counterQuery' => ''
//)
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* 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 = Hash::merge($this->validate, array(
'block_key' => array(
'rule1' => array(
'rule' => array('notBlank'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true,
),
),
));
return parent::beforeValidate($options);
}
/**
* 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'])) {
return $data;
}
$this->begin();
$this->loadModels([
'Frame' => 'Frames.Frame',
]);
try {
if (empty($data['Frame'])) {
throw new BadRequestException(__d('net_commons', 'Bad Request'));
}
//$frame = $data['Frame']; //FrameモデルですでにFrameモデルデータは登録済み
//Blockモデルに存在するか調べる
$block = $this->getBlock();
// まだない場合
if (empty($block)) {
// ブロックを作成する
$block = $this->saveBlock();
} elseif (! $this->_saveReservation($block)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//取得したBlockを$current[Block]に記録しておく。
Current::$current['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'));
//}
////権限設定
//if (! $this->_saveReservation($block)) {
// throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
//}
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return $data;
}
/**
* フレームも何もなくても予定登録のときはこいつをたたいて準備しないといけない
*
* @return mixed 見つかった、もしくは作成したブロック
* @throws InternalErrorException
*/
public function prepareBlock() {
//Blockを取得
$block = $this->getBlock();
try {
// まだない場合
if (empty($block)) {
$this->begin();
// ブロックを作成する
if (! $this->saveBlock()) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$this->commit();
}
} catch (Exception $ex) {
$this->rollback($ex);
}
Current::$current['Block'] = $block['Block'];
return $block;
}
///**
// * 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 = Hash::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) {
// $this->loadModels([
// 'ReservationFrameSetting' => 'Reservations.ReservationFrameSetting',
// ]);
//
// $frameKey = $frame['key'];
// $frameSetting = $this->ReservationFrameSetting->find('first', array(
// 'recursive' => -1,
// 'conditions' => array(
// 'frame_key' => $frameKey
// ),
// ));
// if ($frameSetting) {
// return $frameSetting;
// }
// $frameSetting = $this->ReservationFrameSetting->create();
// $this->ReservationFrameSetting->setDefaultValue($frameSetting); //Modelの初期値設定
// $frameSetting['ReservationFrameSetting']['frame_key'] = $frame['key'];
//// $frameSetting['ReservationFrameSetting']['room_id'] = Current::read('Room.id');
//
// return $this->ReservationFrameSetting->saveFrameSetting($frameSetting);
// }
/**
* 施設予約のBlockデータの登録
*
* @param array $block ブロック
* @return array 生成したデータ
* @throws InternalErrorException
*/
protected function _saveReservation($block) {
// 今現在ブロックに対応した施設予約があるか
$count = $this->find('count', array(
'recursive' => -1,
'conditions' => array(
'block_key' => $block['Block']['key']
)
));
// ない場合は作成する
if (! $count) {
$this->set($this->create([
'block_key' => $block['Block']['key']
]));
if (!$this->save()) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
return true;
}
//
///**
// * _makeBlock($frame);
// *
// * ブロックを作成する
// *
// * @param array $frame フレーム
// * @return array 生成したブロック
// * @throws InternalErrorException
// */
// protected function _makeBlock($frame) {
// $block = $this->Block->save(array(
// 'room_id' => $frame['room_id'],
// 'plugin_key' => $frame['plugin_key'],
// ));
// if (! $block) {
// throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
// }
// //新規に生成したBlockを$current[Block]に記録しておく。
// Current::$current['Block'] = $block['Block'];
// return $block;
// }
/**
* ブロックを作成する
*
* @return array 生成したブロック
* @throws InternalErrorException
*/
public function saveBlock() {
$block = $this->Block->save(array(
'room_id' => Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID),
'plugin_key' => Inflector::underscore($this->plugin),
));
if (! $block) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
// ない場合は作成する
if (! $this->_saveReservation($block)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
return $block;
}
/**
* ブロックを取得
*
* @return array
*/
public function getBlock() {
return $this->Block->find('first', [
'recursive' => -1,
'conditions' => [
'room_id' => Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID),
'plugin_key' => Inflector::underscore($this->plugin),
]
]);
}
/**
* ブロックが作成されているかどうか
*
* @return bool
*/
public function isCreatedBlock() {
return (bool)$this->Block->find('count', [
'recursive' => -1,
'conditions' => [
'room_id' => Space::getRoomIdRoot(Space::PUBLIC_SPACE_ID),
'plugin_key' => Inflector::underscore($this->plugin),
]
]);
}
}