-
Notifications
You must be signed in to change notification settings - Fork 5
/
EDateCompare.php
186 lines (169 loc) · 6.69 KB
/
EDateCompare.php
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
<?php
/**
* DateCompare Validation Extension
*
* Validator to compare two dates, works similarly to CCompareValidator.
*
* @copyright © Digitick <www.digitick.net> 2012
* @license GNU Lesser General Public License v3.0
* @author Ianaré Sévi
*/
class EDateCompare extends CValidator
{
/**
* @var string the name of the attribute to be compared with
*/
public $compareAttribute;
/**
* @var string a constant date to be compared with
*/
public $compareValue;
/**
* @var boolean whether the attribute value can be null or empty. Defaults to false.
* If this is true, it means the attribute is considered valid when it is empty.
*/
public $allowEmpty = false;
/**
* @var string the date format used to compare the values.
*/
public $dateFormat = 'Y-m-d H:i:s';
/**
* @var boolean whether this validation rule should be skipped when there is already a validation
* error for the current attribute. Defaults to true.
*/
public $skipOnError = true;
/**
* @var string the operator for comparison. Defaults to '='.
* The followings are valid operators:
* <ul>
* <li>'=' or '==': validates to see if the two values are equal.</li>
* <li>'!=': validates to see if the two values are NOT equal.</li>
* <li>'>': validates to see if the value being validated is greater than the value being compared with.</li>
* <li>'>=': validates to see if the value being validated is greater than or equal to the value being compared with.</li>
* <li>'<': validates to see if the value being validated is less than the value being compared with.</li>
* <li>'<=': validates to see if the value being validated is less than or equal to the value being compared with.</li>
* </ul>
*/
public $operator = '=';
protected function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
if ($this->allowEmpty && $this->isEmpty($value)) {
return;
}
if ($this->compareValue !== null) {
$compareTo = $compareValue = $this->compareValue;
}
else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = $object->$compareAttribute;
$compareTo = $object->getAttributeLabel($compareAttribute);
}
$compareDate = DateTime::createFromFormat($this->dateFormat, $compareValue);
$date = DateTime::createFromFormat($this->dateFormat, $value);
// make sure we have two dates
if ($date instanceof DateTime && $compareDate instanceof DateTime)
$diff = ((integer) $date->diff($compareDate)->format('%r%a%H%I%S')) * -1;
else
return; // Perhaps not the best way of handling this. Possibly add an error message.
switch ($this->operator) {
case '=':
case '==':
if ($diff != 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be repeated exactly.');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo));
}
break;
case '!=':
if ($diff == 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '>':
if ($diff <= 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '>=':
if ($diff < 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '<':
if ($diff >= 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than "{compareValue}".');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
case '<=':
if ($diff > 0) {
$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareTo, '{compareValue}' => $compareValue));
}
break;
default:
throw new CException(Yii::t('yii', 'Invalid operator "{operator}".', array('{operator}' => $this->operator)));
}
}
public function clientValidateAttribute($object, $attribute)
{
if ($this->compareValue !== null) {
$compareTo = $this->compareValue;
$compareValue = CJSON::encode($this->compareValue);
}
else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue = "new Date(\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val().replace(' ', 'T')).getTime()";
$compareTo = $object->getAttributeLabel($compareAttribute);
}
$message = $this->message;
$jsDate = 'new Date(value.replace(" ", "T")).getTime()';
switch ($this->operator) {
case '=':
case '==':
if ($message === null)
$message = Yii::t('yii', '{attribute} must be repeated exactly.');
$condition = "{$jsDate}!={$compareValue}";
break;
case '!=':
if ($message === null)
$message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
$condition = "{$jsDate}=={$compareValue}";
break;
case '>':
if ($message === null)
$message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
$condition = "{$jsDate}<={$compareValue}";
break;
case '>=':
if ($message === null)
$message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
$condition = "{$jsDate}<{$compareValue}";
break;
case '<':
if ($message === null)
$message = Yii::t('yii', '{attribute} must be less than "{compareValue}".');
$condition = "{$jsDate}>={$compareValue}";
break;
case '<=':
if ($message === null)
$message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
$condition = "{$jsDate}>{$compareValue}";
break;
default:
throw new CException(Yii::t('yii', 'Invalid operator "{operator}".', array('{operator}' => $this->operator)));
}
$message = strtr($message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{compareValue}' => $compareTo,
));
return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . CJSON::encode($message) . ");
}
";
}
}