<?php

class LdapInterface extends PhonalisaRecord
{

    public $id;
    public $server;
    public $username;
    public $password;
    public $connectedModel;
    public $addressBookID;
    public $baseDN;
    public $filter = '(|(objectClass=contact)(objectClass=user))';
    public $selectedItems;
    public $regEx;
    public $ldapData;
    public $ldapKeys;
    public $connected;
    public $positivSearchResult;
    public $cronJobEnabled;
    public $cronRule = '15 * * * *';
    public $owner; // required for address import

    public static function myAttributes()
    {
        return array(
            'server' => 'server',
            'username' => 'username',
            'password' => 'password',
            'connectedModel' => 'connectedModel',
            'addressBookID' => 'addressBookID',
            'baseDN' => 'baseDN',
            'filter' => 'filter',
            'selectedItems' => 'selectedItems',
            'regEx' => 'regEx',
            'ldapData' => 'ldapData',
            'ldapKeys' => 'ldapKeys',
            'connected' => 'connected',
            'positivSearchResult' => 'positivSearchResult',
            'cronJobEnabled' => 'cronJobEnabled',
            'cronRule' => 'cronRule',
            'owner' => 'owner'
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return RoutingInbound the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('server, connectedModel, baseDN, filter', 'required'),
            array('addressBookID, owner', 'required', 'on' => 'Addresses'),
            array('connected', 'required', 'on' => 'Addresses', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_BIND')),
            array('positivSearchResult', 'required', 'on' => 'Addresses', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_FILTER')),
            array('connected', 'required', 'on' => 'update', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_BIND')),
            array('positivSearchResult', 'required', 'on' => 'update', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_FILTER')),
            array('connected', 'required', 'on' => 'create', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_BIND')),
            array('positivSearchResult', 'required', 'on' => 'create', 'message' => __('ADMIN_LDAP_INTERFACE_ERROR_SERVER_FILTER')),
            array('cronJobEnabled, cronRule, username, password', 'safe'),
            array(
                'server, '
                . 'username, '
                . 'password, '
                . 'connectedModel, '
                . 'addressBookID, '
                . 'baseDN, '
                . 'filter, '
                . 'selectedItems, '
                . 'regEx, '
                . 'ldapData, '
                . 'ldapKeys, '
                . 'connected, '
                . 'positivSearchResult,'
                . 'owner',
                'safe',
                'on' => 'update'
            ),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'server' => __('ADMIN_LDAP_INTERFACE_SERVER'),
            'username' => __('ADMIN_LDAP_INTERFACE_USERNAME'),
            'password' => __('ADMIN_LDAP_INTERFACE_PASSWORD'),
            'connectedModel' => __('ADMIN_LDAP_INTERFACE_CONNECTED_MODEL'),
            'addressBookID' => __('ADMIN_LDAP_INTERFACE_ADDRESS_BOOK_ID'),
            'baseDN' => __('ADMIN_LDAP_INTERFACE_BASE_DN'),
            'filter' => __('ADMIN_LDAP_INTERFACE_FILTER'),
            'selectedItems' => __('ADMIN_LDAP_INTERFACE_ASSIGNMENTS'),
            'regEx' => __('ADMIN_LDAP_INTERFACE_REGEX'),
            'ldapData' => __('ADMIN_LDAP_INTERFACE_LDAP_DATA'),
            'ldapKeys' => __('ADMIN_LDAP_INTERFACE_LDAP_KEYS'),
            'connected' => __('ADMIN_LDAP_INTERFACE_LDAP_CONNECTED'),
            'positivSearchResult' => __('ADMIN_LDAP_INTERFACE_LDAP_SEARCH_RESULT'),
            'cronJobEnabled' => __('ADMIN_LDAP_INTERFACE_LDAP_CRON_JOB_ENABLED'),
            'cronRule' => __('ADMIN_LDAP_INTERFACE_LDAP_CRON_RULE'),
            'owner' => __('ADMIN_LDAP_INTERFACE_LDAP_OWNER')
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CPhonalisaDataProvider the data provider
     * that can return the models based on the search/filter conditions.
     */
    public function search($order = 'title')
    {
        $criteria = new CDbCriteria;
        $dataProvider = new CPhonalisaDataProvider($this, array(
            'criteria' => $criteria,
        ));
        return $dataProvider;
    }

    public function getAvailableModelTypes()
    {
        return array(
            get_class(User::model()) => __('ADMIN_LDAP_INTERFACE_USER_MODEL'),
            get_class(Addresses::model()) => __('ADMIN_LDAP_INTERFACE_ADDRESSES_MODEL')
        );
    }
    
    public function beforeSave()
    {
        if ($this->connectedModel == 'Addresses') {
            $this->owner = Yii::app()->User->sudoUser;
        }else {
            $this->owner = '';
        }
        return parent::beforeSave();
    }
}
