summaryrefslogtreecommitdiff
path: root/protected/components
diff options
context:
space:
mode:
Diffstat (limited to 'protected/components')
-rw-r--r--protected/components/DateCompareValidator.php57
-rw-r--r--protected/components/DateRangeValidator.php32
-rw-r--r--protected/components/Format.php17
3 files changed, 106 insertions, 0 deletions
diff --git a/protected/components/DateCompareValidator.php b/protected/components/DateCompareValidator.php
new file mode 100644
index 0000000..c013382
--- /dev/null
+++ b/protected/components/DateCompareValidator.php
@@ -0,0 +1,57 @@
+<?php
+class DateCompareValidator extends CValidator {
+ /**
+ * @var mixed the format pattern that the date value should follow.
+ * This can be either a string or an array representing multiple formats.
+ * Defaults to 'MM/dd/yyyy'. Please see {@link CDateTimeParser} for details
+ * about how to specify a date format.
+ */
+ public $format='MM/dd/yyyy';
+ /**
+ * @var boolean whether the attribute value can be null or empty. Defaults to true,
+ * meaning that if the attribute is empty, it is considered valid.
+ */
+ public $allowEmpty=true;
+ /**
+ * @var string the name of the attribute to receive the parsing result.
+ * When this property is not null and the validation is successful, the named attribute will
+ * receive the parsing result.
+ */
+ public $timestampAttribute;
+ public $operator = '>';
+ public $compareAttribute;
+ public $compareValue;
+
+ /**
+ * Validates the attribute of the object.
+ * If there is any error, the error message is added to the object.
+ * @param CModel $object the object being validated
+ * @param string $attribute the attribute being validated
+ */
+ protected function validateAttribute($object, $attribute) {
+ if ((empty($this->compareAttribute) && empty($this->compareValue)) || empty($this->operator)) {
+ $this->addError($attribute, 'Invalid Parameters to dateCompare');
+ }
+
+ $compareAttribute = $this->compareAttribute;
+ $this->compareValue = empty($compareAttribute) ? $this->compareValue : $object->$compareAttribute;;
+
+ if ($this->allowEmpty && empty($this->compareValue)) {
+ return;
+ }
+
+ $start = CDateTimeParser::parse($object->$attribute, $this->format);
+
+ $end = CDateTimeParser::parse($this->compareValue, $this->format);
+
+ //a little php trick - safe than eval and easier than a big switch statement
+
+ if (version_compare($start, $end, $this->operator)) {
+ return;
+ } else {
+ $message = $this->message !== null ? $this->message : Yii::t('astaf', 'The value of {attribute} ({value}) is not {operator} {compareAttribute} ({compareValue}).');
+ $this->addError($object, $attribute, $message, array('{operator}'=>$this->operator, '{compareValue}'=>$this->compareValue, '{value}'=>$object->$attribute, '{compareAttribute}'=>($object->getAttributeLabel($this->compareAttribute))));
+ }
+
+ }
+} \ No newline at end of file
diff --git a/protected/components/DateRangeValidator.php b/protected/components/DateRangeValidator.php
new file mode 100644
index 0000000..8b963a8
--- /dev/null
+++ b/protected/components/DateRangeValidator.php
@@ -0,0 +1,32 @@
+<?php
+class DateRangeValidator extends DateCompareValidator {
+ public $minDate;
+ public $maxDate;
+
+ protected function validateAttribute($object, $attribute) {
+ $errors = null;
+ if ($object->hasErrors($attribute)) {
+ $errors = $object->getErrors($attribute);
+ $object->clearErrors($attribute);
+ }
+ $this->compareValue = $this->minDate;
+ $this->operator = '>';
+ parent::validateAttribute($object, $attribute);
+ $this->compareValue = $this->maxDate;
+ $this->operator = '<';
+ parent::validateAttribute($object, $attribute);
+
+ $addRangeError = false;
+ if ($object->hasErrors($attribute)) {
+ $object->clearErrors($attribute);
+ $addRangeError = true;
+ }
+ if (!empty($errors)) {
+ $object->addErrors($errors);
+ }
+ if ($addRangeError) {
+ $message = $this->message !== null ? $this->message : Yii::t('astaf', 'The value of {attribute} ({value}) is not between {minDate} and {maxDate}.');
+ $this->addError($object, $attribute, $message, array('{operator}'=>$this->operator, '{minDate}'=>$this->minDate, '{value}'=>$object->$attribute, '{maxDate}'=>$this->maxDate));
+ }
+ }
+} \ No newline at end of file
diff --git a/protected/components/Format.php b/protected/components/Format.php
index 129a292..1be259f 100644
--- a/protected/components/Format.php
+++ b/protected/components/Format.php
@@ -15,4 +15,21 @@ class Format {
public static function percentage($value) {
return Yii::app()->locale->numberFormatter->formatPercentage($value);
}
+
+ public static function dbDateTime($value) {
+ return Yii::app()->dateFormatter->format('yyyy-MM-dd HH:mm', CDateTimeParser::parse($value, 'dd.MM.yyyy HH:mm'));
+ }
+
+ public static function displayDateTime($value) {
+ $checkvalue = CDateTimeParser::parse($value, 'dd.MM.yyyy HH:mm');
+ if (!empty($checkvalue)) {
+ return $value;
+ }
+ $timestamp = CDateTimeParser::parse($value, 'yyyy-MM-dd HH:mm:ss');
+ Yii::trace('Input value: '.$value. ' -> output timestamp: '.$timestamp, 'ccwn.astaf.format');
+ if ($timestamp) {
+ return Yii::app()->dateFormatter->format('dd.MM.yyyy HH:mm', $timestamp);
+ }
+ return null;
+ }
} \ No newline at end of file