<?php

class UserActivedValidator extends CValidator
{
    protected $className, $idAttribute,$idValue, $allowEmpty,$attributeName,$criteria = array(),$caseSensitive = false;
    protected function validateAttribute($object, $attribute)
    {
        $value = $object->$attribute;

        if ($this->allowEmpty && $this->isEmpty($value)) {
            return;
        }
        if ($this->idValue !== null) {
            $compareTo = $compareValue = $this->idValue;
        } else {
            $idAttribute = $this->idAttribute === null ? $attribute.'_identify' : $this->idAttribute;
            $idValue = $object->$idAttribute;
            $compareTo = $object->getAttributeLabel($idAttribute);
        }
        if (is_array($value)) {
            $this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));

            return;
        }
        $className = $this->className === null ? get_class($object) : Yii::import($this->className);
        $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
        $finder = $this->getModel($className);
        $table = $finder->getTableSchema();
        if (($column = $table->getColumn($attributeName)) === null) {
            throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".',

        array('{column}' => $attributeName, '{table}' => $table->name)));
        }
        if (($idColumn = $table->getColumn($idAttribute)) === null) {
            throw new CException(Yii::t('yii', 'Table "{table}" does not have a column named "{column}".',

        array('{column}' => $idAttribute, '{table}' => $table->name)));
        }
        $columnName = $column->rawName;
        $idcolumnName = $idColumn->rawName;
        $criteria = new CDbCriteria();
        if ($this->criteria !== array()) {
            $criteria->mergeWith($this->criteria);
        }
        $tableAlias = empty($criteria->alias) ? $finder->getTableAlias(true) : $criteria->alias;
        $valueParamName = CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++;
        $criteria->addCondition($this->caseSensitive ? "{$tableAlias}.{$idcolumnName}={$valueParamName}" : "LOWER({$tableAlias}.{$idcolumnName})=LOWER({$valueParamName})");
        $criteria->params[$valueParamName] = $idValue;
        $find_object = $finder->find($criteria);
        if (!$find_object) {
            $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is wrong.');
            $this->addError($object, $attribute, $message, array('{value}' => CHtml::encode($value)));
        } else {
            $find_value = $find_object[$attributeName];
            if (crypt($value, $find_value) !== $find_value) {
                $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} "{value}" is wrong.');
                $this->addError($object, $attribute, $message, array('{value}' => CHtml::encode($value)));
            }
        }
    }
    protected function getModel($className)
    {
        return CActiveRecord::model($className);
    }
}
