<?php

class uniqueExtension extends CValidator
{

    /**
     * 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 $pattern = '/^[1-9]/';

    protected function validateAttribute($object, $attribute)
    {
        // extract the attribute value from it's model object
        $value = $object->$attribute;
        if ($value === '' || $value === null) {
            return TRUE;
        }
        if ($value != '') {
            if (!preg_match($this->pattern, $value)) {
                $this->addError($object, $attribute, __('USER_MODEL_SHOULD_NOT_START_WITH_ZERO_ERROR_MESSAGE'));
                return false;
            }
        }
        if (!$this->isOwnExtension($object, $attribute, $value)) {
            if ($this->isEmergencyNumber($value)) {
                $this->addError($object, $attribute, __('EXTENSION_IS_EMERGENCY_NUMBER_ERROR_MESSAGE'));
                return FALSE;
            } else if ($this->isExistingExtension($attribute, $value)) {
                $this->addError($object, $attribute, __('EXTENSION_IS_UNIQUE_ERROR_MESSAGE'));
                return FALSE;
            }
        }
        return TRUE;
    }

    protected function isExistingExtension($attribute, $value)
    {
        $fCodes = array(
            cfg('DP_VOICEMAIL_LISTENING_PREFIX'), // "mailbox extension" / feature code
            cfg('DP_CONFERENCE_PREFIX'), // featurecode for conference
        );
        if (in_array($value, $fCodes)) {
            return TRUE;
        }
        if ($this->userExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->groupExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->queueExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->conferenceExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->ivrExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->callGroupExtensionExists($attribute, $value)) {
            return TRUE;
        }
        if ($this->scriptExtensionExists($attribute, $value)) {
            return TRUE;
        }
        return FALSE;
    }
    
    protected function isEmergencyNumber($value)
    {
        $emergencyNumber = cfg('DP_EMERGENCY_NUMBERS');
        $emergNumReplaced = str_replace(' ', '', $emergencyNumber);
        $exlodedEmergencyNumbers = explode(',',$emergNumReplaced);
        
        foreach($exlodedEmergencyNumbers as $exlodedEmergencyNumber) {
            if ($exlodedEmergencyNumber == $value) {
                return true;
            }
        }
        return false;
    }

    protected function userExtensionExists($attribute, $value)
    {
        $model = User::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function groupExtensionExists($attribute, $value)
    {
        $model = Groups::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function conferenceExtensionExists($attribute, $value)
    {
        $model = Conference::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function queueExtensionExists($attribute, $value)
    {
        $model = Queue::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function ivrExtensionExists($attribute, $value)
    {
        include_once(__DIR__ . '/../../modules/routingIVR/models/IVR.php');
        $model = IVR::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function callGroupExtensionExists($attribute, $value)
    {
        $model = CallGroup::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }

    protected function scriptExtensionExists($attribute, $value)
    {
        $model = ScriptExtension::model()->findByAttributes(array($attribute => $value));
        if ($model) {
            return true;
        }
        return false;
    }
    
    protected function isOwnExtension($object, $attribute, $value)
    {        
        
        if (method_exists($object, 'extensionOwner')) {
            $object = $object->extensionOwner();
        }
        
        if (method_exists($object, 'isNewRecord')) {
            if ($object->isNewRecord()) {
                return false;
            }
        } else {
            if ($object->isNewRecord) {
                return false;
            }
        }        
        $result = $object->findAllByAttributes(array($attribute => $value));
        if (empty($result)) {
            return false;
        } else {
            $result = $result[0];
            if ($result->id == $object->id) {
                return true;
            }
        }
        return false;
    }
}
