<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 

/**
 * This is the model class for table "person_type".
 *
 * The followings are the available columns in table 'person_type':
 * @property integer $id
 * @property string $name
 * @property integer $is_ldap
 * @property string $equivalent_ldap_type
 * @property string $status
 * @property integer $created_by
 * @property string $created_date
 * @property integer $last_edited_by
 * @property string $last_edited_date
 *
 * The followings are the available model relations:
 * @property BadgeAssignmentRule[] $badgeAssignmentRules
 * @property Person[] $people
 */
class PersonType extends LsmsActiveRecord
    {

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

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'person_type';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name ,status, is_ldap', 'required'),
            array('name', 'unique', 'message' => yii::t('dafault', 'Person Type name ({value}) already exists.')),
            array('is_ldap', 'numerical', 'integerOnly' => true),
            array('name, equivalent_ldap_type', 'length', 'max' => 255),
            array('status', 'length', 'max' => 8),
            //Check If equivalent LDAP type is not blank when is LDAP true
            array('equivalent_ldap_type', 'EquivalentLDAPTypeValidate'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, is_ldap, equivalent_ldap_type, created_by, status, created_date, last_edited_by, last_edited_date', 'safe', 'on' => 'search'),
        );
    }

    /*
     * Check If equivalent LDAP type is not blank when is LDAP true
     */

    public function EquivalentLDAPTypeValidate( $attribute, $params )
    {
        if ($this->is_ldap == 1)
        {
            if ((isset($this->equivalent_ldap_type) && empty($this->equivalent_ldap_type)) || (!isset($this->equivalent_ldap_type)))
            {
                $this->addError($attribute, Yii::t('default', 'The Equivalent LDAP Type can’t be blank when the person type is LDAP'));
            }
        }
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'badgeAssignmentRules' => array(self::HAS_MANY, 'BadgeAssignmentRule', 'person_type_id'),
            'people' => array(self::HAS_MANY, 'Person', 'type_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'name' => yii::t('default', 'Name'),
            'is_ldap' => yii::t('default', 'Is LDAP'),
            'equivalent_ldap_type' => yii::t('default', 'Equivalent LDAP Type'),
            'status' => yii::t('default', 'Status'),
        );
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover()
    {
        return array(
            'name' => yii::t('default', 'tootip_name'),
            'is_ldap' => yii::t('default', 'tootip_is_ldap'),
            'equivalent_ldap_type' => yii::t('default', 'tootip_equivalent_ldap_type'),
            'status' => '',);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('name', $this->name, true);
        $criteria->compare('is_ldap', $this->is_ldap);
        $criteria->compare('equivalent_ldap_type', $this->equivalent_ldap_type, true);
        $criteria->compare('status', $this->status, false);

        $pageSize = $this->yiiparam('pageSize');
        return new CActiveDataProvider($this, array(
            'pagination' => array('pageSize' => $pageSize,),
            'criteria' => $criteria,
        ));
    }

    public static function getAll( $status=null )
    {
        if ($status == "Active")
        {
            return PersonType::model()->active()->findAll(array('order' => 'name'));
        }
        else
        {
            return PersonType::model()->findAll(array('order' => 'name'));
        }
    }

    // the Following function return all Non-LDAP types
    public static function getAllNonLDAP( $status=null )
    {
        if ($status == "Active")
        {
            return PersonType::model()->active()->findAllByAttributes(array('is_ldap' => 0));
        }
        else
        {
            return PersonType::model()->findAllByAttributes(array('is_ldap' => 0));
        }
    }

    // The Following Function take LDAP type as parameter and return the ID as Return Value and -1 if not found
    public static function getLDAPTypeID( $LDAP_type=null )
    {
        try
        {
            if (isset($LDAP_type) && ($LDAP_type != ""))
            {
                $model = PersonType::model()->findByAttributes(array("equivalent_ldap_type" => $LDAP_type));
                if ($model != null)
                {
                    return $model->id;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                return -1;
            }
        }
        catch (Exception $exc)
        {
            return -1;
        }
    }

    }