-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReservationTimeframe.php
More file actions
390 lines (355 loc) · 10.3 KB
/
ReservationTimeframe.php
File metadata and controls
390 lines (355 loc) · 10.3 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
/**
* ReservationTimeframe Model
*
* @property Language $Language
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @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');
/**
* Summary for ReservationTimeframe Model
*/
class ReservationTimeframe extends ReservationsAppModel {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
//多言語
'M17n.M17n' => array(
'commonFields' => array( // 言語が異なっても同じにするフィールド
'color',
),
'afterCallback' => false,
),
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Language' => array(
'className' => 'M17n.Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* beforeValidate
*
* @param array $options options
* @return bool
*/
public function beforeValidate($options = array()) {
$this->validate = ValidateMerge::merge($this->validate,
array(
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
'title' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d(
'net_commons',
'Please input %s.', __d('reservations', 'Time frame name')
),
),
),
'start_time' => array(
'rule1' => array(
'rule' => array('validateTime'),
'message' => __d('reservations', 'Invalid value.'),
),
'rule2' => array(
'rule' => array('validateTimeRange', 'end_time'),
'message' => __d('reservations', 'Invalid value.'),
),
'rule3' => [
'rule' => ['validateTimeFrameNotExist'],
'message' => __d('reservations', 'Duplicate time frames.')
]
),
'end_time' => array(
'rule1' => array(
'rule' => array('validateTime'),
'message' => __d('reservations', 'Invalid value.'),
),
),
'color' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => __d(
'net_commons',
'Please input %s.', __d('reservations', 'Time frame color')
),
),
),
)
);
return parent::beforeValidate($options);
}
/**
* Called after each find operation. Can be used to modify any results returned by find().
* Return value should be the (modified) results.
*
* @param mixed $results The results of the find operation
* @param bool $primary Whether this model is being queried directly (vs. being queried as an association)
* @return mixed Result of the find operation
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
*/
public function afterFind($results, $primary = false) {
foreach ($results as $key => $value) {
if (
Hash::check($value, $this->alias . '.start_time') &&
Hash::check($value, $this->alias . '.end_time')
) {
$results[$key][$this->alias]['openText'] = $this->_openText($value);
}
//if (array_key_exists('start_time', $results[$key][$this->alias]) &&
// array_key_exists('end_time', $results[$key][$this->alias])) {
// $results[$key][$this->alias]['openText'] = $this->_openText($value);
//}
}
return $results;
}
/**
* 時間枠の登録
*
* @param array $data 登録データ
* @return bool
* @throws InternalErrorException
*/
public function saveTimeframe($data) {
$this->begin();
try {
// 先にvalidate 失敗したらfalse返す
$this->set($data);
if (!$this->validates($data)) {
return false;
}
// start_time end_timeをUTCに変換
$startDateTime = Date('Y-m-d') . $data[$this->alias]['start_time'] . ':00';
$endDateTime = Date('Y-m-d') . $data[$this->alias]['end_time'] . ':00';
$ncTime = new NetCommonsTime();
$startDateTime4UTC = $ncTime->toServerDatetime($startDateTime,
$data[$this->alias]['timezone']);
$endDateTime4UTC = $ncTime->toServerDatetime($endDateTime,
$data[$this->alias]['timezone']);
$data[$this->alias]['start_time'] = $startDateTime4UTC;
$data[$this->alias]['end_time'] = $endDateTime4UTC;
$savedData = $this->save($data, false);
if (! $savedData) {
//このsaveで失敗するならvalidate以外なので例外なげる
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//多言語化の処理
$this->set($savedData);
$this->saveM17nData();
$this->commit();
} catch (Exception $e) {
$this->rollback($e);
}
return $savedData;
}
/**
* 時間枠の削除
*
* @param array $data 登録データ
* @return bool
* @throws InternalErrorException
*/
public function deleteTimeframe($data) {
$this->begin();
try {
$conditions = array(
$this->alias . '.key' => $data[$this->alias]['key']
);
if (! $this->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//0件になったら、ReservationFrameSetting.display_timeframeを0にする
if ($this->find('count', ['recursive' => -1]) === 0) {
$this->loadModels([
'ReservationFrameSetting' => 'Reservations.ReservationFrameSetting',
]);
$update = array(
$this->ReservationFrameSetting->alias . '.display_timeframe' => false
);
if (! $this->ReservationFrameSetting->updateAll($update, null)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
$this->commit();
} catch (Exception $e) {
$this->rollback($e);
}
return true;
}
/**
* 時刻バリデーション H:i形式か。
*
* @param array $check チェックする値の配列
* @return bool
*/
public function validateTime($check) {
$values = array_values($check);
$time = $values[0];
if (!preg_match('/^[0-9]{2}:[0-9]{2}$/', $time)) {
return false;
}
list($hour, $min) = explode(':', $time);
if (intval($hour) < 0 || intval($hour) > 24) {
return false;
}
if (intval($min) < 0 || intval($min) > 59) {
return false;
}
return true;
}
/**
* 時刻範囲バリデーション
*
* @param array $check 開始の配列
* @param string $endKey 終了値の入るキー名
* @return bool
*/
public function validateTimeRange($check, $endKey) {
$values = array_values($check);
$startTime = $values[0];
$endTime = $this->data[$this->alias][$endKey];
return ($startTime < $endTime);
}
/**
* 時間枠の重複防止バリデーション
*
* @param array $check チェック対象配列
* @return bool
*/
public function validateTimeFrameNotExist($check) {
$startTime = $this->data[$this->alias]['start_time'];
$endTime = $this->data[$this->alias]['end_time'];
// UTCに変換してから比較する。
$startDateTime = Date('Y-m-d') . $startTime . ':00';
$endDateTime = Date('Y-m-d') . $endTime . ':00';
$ncTime = new NetCommonsTime();
$startDateTime4UTC = $ncTime->toServerDatetime($startDateTime,
$this->data[$this->alias]['timezone']);
$endDateTime4UTC = $ncTime->toServerDatetime($endDateTime,
$this->data[$this->alias]['timezone']);
$startTime = date('H:i:s', strtotime($startDateTime4UTC));
$endTime = date('H:i:s', strtotime($endDateTime4UTC));
// start > endになったら24:00またぎなのでstart-24:00 までと 0:00-end の重複チェックする
if ($startTime > $endTime) {
$inputRanges = [
[
'start' => $startTime,
'end' => '24:00:00'
],
[
'start' => '00:00:00',
'end' => $endTime
]
];
} else {
$inputRanges = [
[
'start' => $startTime,
'end' => $endTime
]
];
}
// 全時間枠取得
$conditions = [];
if ($id = Hash::get($this->data, 'ReservationTimeframe.id')) {
$conditions = [
'ReservationTimeframe.id !=' => $id,
];
}
$timeframes = $this->find('all', [
'conditions' => $conditions,
'group' => 'ReservationTimeframe.key'
]);
// start > endなデータは24:00またぎなのでstart-24:00 と 0:00-end のデータとして扱う。
$existRanges = [];
foreach ($timeframes as $timeframe) {
if ($timeframe[$this->alias]['start_time'] > $timeframe[$this->alias]['end_time']) {
$existRanges[] = [
'start' => $timeframe[$this->alias]['start_time'],
'end' => '24:00:00'
];
$existRanges[] = [
'start' => '00:00:00',
'end' => $timeframe[$this->alias]['end_time']
];
} else {
$existRanges[] = [
'start' => $timeframe[$this->alias]['start_time'],
'end' => $timeframe[$this->alias]['end_time']
];
}
}
foreach ($inputRanges as $inputRange) {
foreach ($existRanges as $existRange) {
// 既にある時間枠のStartが指定範囲のENDより前で既存時間枠のENDが指定STARTよりも後なら重複
if ($existRange['start'] < $inputRange['end'] &&
$existRange['end'] > $inputRange['start']
) {
return false;
}
}
}
return true;
}
/**
* 時間枠の範囲(表示用)を返す
*
* @param array $timeframe 施設データ
* @return string
*/
protected function _openText($timeframe) {
$ret = '';
//時間
$startTime = $timeframe[$this->alias]['start_time'];
$timeframeTimeZone = new DateTimeZone($timeframe[$this->alias]['timezone']);
$startDate = new DateTime($startTime, new DateTimeZone('UTC'));
$startDate->setTimezone($timeframeTimeZone);
$timeframe[$this->alias]['start_time'] = $startDate->format('H:i');
$endTime = $timeframe[$this->alias]['end_time'];
$endDate = new DateTime($endTime, new DateTimeZone('UTC'));
$endDate->setTimezone($timeframeTimeZone);
$timeframe[$this->alias]['end_time'] = $endDate->format('H:i');
$ret = sprintf('%s %s - %s',
$ret,
$timeframe[$this->alias]['start_time'],
$timeframe[$this->alias]['end_time']
);
if (AuthComponent::user('timezone') != $timeframe[$this->alias]['timezone']) {
$SiteSetting = new SiteSetting();
$SiteSetting->prepare();
$ret .= ' ';
$ret .= $SiteSetting->defaultTimezones[$timeframe[$this->alias]['timezone']];
}
return $ret;
}
}