<?php

class isUnique extends CValidator
{

    public $allowEmpty = false;
    public $aditionalComparePropertys;
    /**
     * 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)
    {
        //FIXME: Check with the ExtensionManager if this extension already exists (e.g. Queues, IVR's, Conference Rooms etc)

        $attributes = array($attribute => $object->$attribute);
        
        if (is_array($this->aditionalComparePropertys)) {
            foreach ($this->aditionalComparePropertys as $aditionalAttribute) {
                $attributes[$aditionalAttribute] = $object->$aditionalAttribute;
            }
        } 

        $model = $object->findByAttributes($attributes);

        if (!$model) {
            return true;
        }
        
        if ($this->allowEmpty == true && empty($object->$attribute)) {
            return true;
        }

        if ($object->isNewRecord() && $object->$attribute != $model->$attribute) {
            return true;
        } else if (!$object->isNewRecord() && $model->id == $object->id) {
            return true;
        }
        $object->addError($attribute, ucfirst($attribute) . __('PHONALISA_VALIDATORS_ISUNIQUE_ERROR_MESSAGE'));
        return false;
    }
}
