-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuestionnaireValidateBehavior.php
More file actions
162 lines (155 loc) · 4.33 KB
/
QuestionnaireValidateBehavior.php
File metadata and controls
162 lines (155 loc) · 4.33 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
<?php
/**
* QuestionnaireValidate 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');
/**
* QuestionnaireValidate Behavior
*
* @package Questionnaires\Questionnaires\Model\Befavior
* @author Allcreator <info@allcreator.net>
*/
class QuestionnaireValidateBehavior 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['Questionnaire'][$compare])) {
return true;
}
$check2 = strtotime($model->data['Questionnaire'][$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;
}
/**
* 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 = QuestionnairesComponent::QUESTIONNAIRE_PERIOD_STAT_IN;
if ($check == QuestionnairesComponent::USES_USE) {
$nowTime = (new NetCommonsTime())->getNowDatetime();
$nowTime = strtotime($nowTime);
if ($nowTime < strtotime($startTime)) {
$ret = QuestionnairesComponent::QUESTIONNAIRE_PERIOD_STAT_BEFORE;
}
if ($nowTime > strtotime($endTime)) {
$ret = QuestionnairesComponent::QUESTIONNAIRE_PERIOD_STAT_END;
}
}
return $ret;
}
}