-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRegistrationValidateBehavior.php
More file actions
229 lines (216 loc) · 6.68 KB
/
RegistrationValidateBehavior.php
File metadata and controls
229 lines (216 loc) · 6.68 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
<?php
/**
* RegistrationValidate Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator <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('ModelBehavior', 'Model');
/**
* RegistrationValidate Behavior
*
* @package Registrations\Registrations\Model\Befavior
* @author Allcreator <info@allcreator.net>
*/
class RegistrationValidateBehavior extends ModelBehavior {
/**
* Checks if flag is on, required other fields
*
* @param object &$model use model
* @param array $check check data array
* @param mix $requireValue when check data value equal this value, then require other field
* @param array $others require data field names
* @param string $ope require condition AND or OR or XOR
* @return bool
*/
public function requireOtherFields(&$model, $check, $requireValue, $others, $ope) {
$checkPatterns = array(
'AND' => array('midstream' => array('chk' => true, 'ret' => false), 'end' => array('ret' => true)),
'OR' => array('midstream' => array('chk' => false, 'ret' => true), 'end' => array('ret' => false)),
'XOR' => array('midstream' => array('chk' => false, 'ret' => false), 'end' => array('ret' => true)),
);
$ope = strtoupper($ope);
$checkPattern = $checkPatterns[$ope];
$value = array_values($check);
$value = $value[0];
if ($value != $requireValue) {
return true;
}
foreach ($others as $other) {
$checkData = Hash::get($model->data, $other);
$otherFieldsName = explode('.', $other);
// is_系のフィールドの場合、チェックボックスで実装され、OFFでも0という数値が入ってくる
// そうすると「Blank」判定してほしいのに「ある」と判定されてしまう
// なのでis_で始まるフィールドのデータの設定を確認するときだけは == falseで判定する
if (strncmp('is_', $otherFieldsName[count($otherFieldsName) - 1], 3) === 0) {
$ret = ($checkData == false);
} else {
$ret = Validation::blank($checkData);
}
if ($ret == $checkPattern['midstream']['chk']) {
return $checkPattern['midstream']['ret'];
}
}
return $checkPattern['end']['ret'];
}
/**
* Checks datetime null or datetime
*
* @param object &$model use model
* @param array $check check data array
* @return bool
*/
public function checkDateTime(&$model, $check) {
foreach ($check as $val) {
if (Validation::blank($val)) {
continue;
}
$ret = Validation::datetime($val);
if (!$ret) {
return false;
}
}
return true;
}
/**
* Used to compare 2 datetime values.
*
* @param object &$model use model
* @param string|array $check datetime string
* @param string $operator Can be either a word or operand
* is greater >, is less <, greater or equal >=
* less or equal <=, is less <, equal to ==, not equal !=
* @param string $compare compare datetime string
* @return bool Success
*/
public function checkDateComp(&$model, $check, $operator, $compare) {
// 比較対象がないので比較する必要なし
if (Validation::blank($model->data['Registration'][$compare])) {
return true;
}
$check2 = strtotime($model->data['Registration'][$compare]);
foreach ($check as $val) {
if (Validation::blank($val)) {
continue;
}
$check1 = strtotime($val);
$ret = Validation::comparison($check1, $operator, $check2);
if (!$ret) {
return false;
}
}
return true;
}
/**
* checkMinMax
* min and max is require both value
*
* @param object &$model use model
* @param bool $check post data
* @return bool
*/
public function checkMinMax(&$model, $check) {
// 範囲使わない設定のときはチェックしない
if ($model->data['RegistrationQuestion']['is_range'] == RegistrationsComponent::USES_NOT_USE) {
return true;
}
// 最大値、最小値はテキストで「数値型」の場合と、日付け型の「日」「日時」の場合のみ設定可能
if (!$this->__checkMinMaxNumeric($model, $check)) {
return false;
}
if (!$this->__checkMinMaxDate($model, $check)) {
return false;
}
if (!$this->__checkMinMaxDateTime($model, $check)) {
return false;
}
if ($model->data['RegistrationQuestion']['min'] >= $model->data['RegistrationQuestion']['max']) {
return false;
}
return true;
}
/**
* getPeriodStatus
* get period status now and specified time
*
* @param object &$model use model
* @param bool $check flag data
* @param string $startTime start time
* @param string $endTime end time
* @return int
*/
public function getPeriodStatus(&$model, $check, $startTime, $endTime) {
$ret = RegistrationsComponent::REGISTRATION_PERIOD_STAT_IN;
if ($check == WorkflowBehavior::PUBLIC_TYPE_LIMITED) {
$nowTime = (new NetCommonsTime())->getNowDatetime();
$nowTime = strtotime($nowTime);
if ($nowTime < strtotime($startTime)) {
$ret = RegistrationsComponent::REGISTRATION_PERIOD_STAT_BEFORE;
}
if ($nowTime > strtotime($endTime)) {
$ret = RegistrationsComponent::REGISTRATION_PERIOD_STAT_END;
}
}
return $ret;
}
/**
* __checkMinMaxNumeric
* min and max is require both value
*
* @param object &$model use model
* @param bool $check post data
* @return bool
*/
private function __checkMinMaxNumeric(&$model, $check) {
if ($model->data['RegistrationQuestion']['question_type_option'] == RegistrationsComponent::TYPE_OPTION_NUMERIC) {
if (!Validation::numeric($model->data['RegistrationQuestion']['min'])) {
return false;
}
if (!Validation::numeric($model->data['RegistrationQuestion']['max'])) {
return false;
}
}
return true;
}
/**
* __checkMinMaxDate
* min and max is require both value
*
* @param object &$model use model
* @param bool $check post data
* @return bool
*/
private function __checkMinMaxDate(&$model, $check) {
if ($model->data['RegistrationQuestion']['question_type_option'] == RegistrationsComponent::TYPE_OPTION_DATE) {
if (!Validation::date($model->data['RegistrationQuestion']['min'])) {
return false;
}
if (!Validation::date($model->data['RegistrationQuestion']['max'])) {
return false;
}
}
return true;
}
/**
* __checkMinMaxDateTime
* min and max is require both value
*
* @param object &$model use model
* @param bool $check post data
* @return bool
*/
private function __checkMinMaxDateTime(&$model, $check) {
if ($model->data['RegistrationQuestion']['question_type_option'] == RegistrationsComponent::TYPE_OPTION_DATE_TIME) {
if (!Validation::datetime($model->data['RegistrationQuestion']['min'])) {
return false;
}
if (!Validation::datetime($model->data['RegistrationQuestion']['max'])) {
return false;
}
}
return true;
}
}