diff options
Diffstat (limited to 'protected/components')
| -rw-r--r-- | protected/components/DateCompareValidator.php | 57 | ||||
| -rw-r--r-- | protected/components/DateRangeValidator.php | 32 | ||||
| -rw-r--r-- | protected/components/Format.php | 35 | ||||
| -rw-r--r-- | protected/components/Html.php | 19 |
4 files changed, 141 insertions, 2 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 new file mode 100644 index 0000000..1be259f --- /dev/null +++ b/protected/components/Format.php @@ -0,0 +1,35 @@ +<?php +class Format { + public static function currency($value, $currency = 'EUR') { + return Yii::app()->locale->numberFormatter->formatCurrency($value, $currency); + } + + public static function number($value, $einheit = '') { + return Yii::app()->locale->numberFormatter->formatDecimal($value).' '.$einheit; + } + + public static function decimal($value) { + return Yii::app()->locale->numberFormatter->formatDecimal($value); + } + + 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 diff --git a/protected/components/Html.php b/protected/components/Html.php index c4a246c..6c7df3c 100644 --- a/protected/components/Html.php +++ b/protected/components/Html.php @@ -1,6 +1,5 @@ <?php -class Html extends CHtml -{ +class Html extends CHtml { /** * Makes the given URL relative to the /image directory */ @@ -19,5 +18,21 @@ class Html extends CHtml public static function jsUrl($url) { return Yii::app()->baseUrl.'/js/'.$url; } + + public static function enumItem($model, $attribute) { + $attr = $attribute; + self::resolveName($model, $attr); + preg_match('/\((.*)\)/', $model->tableSchema->columns[$attr]->dbType, $matches); + foreach (explode(',', $matches[1]) as $value) { + $value = str_replace("'", null, $value); + $values[$value] = Yii::t('enumItem', $value); + } + + return $values; + } + + public static function enumDropDownList($model, $attribute, $htmlOptions = array()) { + return CHtml::activeDropDownList($model, $attribute, Html::enumItem($model, $attribute), $htmlOptions); + } } ?>
\ No newline at end of file |
