<?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".
 *
 * The followings are the available columns in table 'person':
 * @property integer $id
 * @property string $number
 * @property string $name
 * @property string $alias
 * @property integer $type_id
 * @property string $mobile
 * @property string $email
 * @property string $manager
 * @property integer $department_id
 * @property integer $is_manager
 * @property integer $is_food_manager
 * @property integer $receive_emails
 * @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 Badge[] $badges
 * @property Department $department
 * @property PersonType $type
 * @property PersonPos[] $personPoses
 * @property Ticket[] $tickets
 */
class NonLDAPPerson extends LsmsActiveRecord
{


    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Person 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';
    }

    /**
     * @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('number, name, type_id,is_manager,is_food_manager,receive_emails, status', 'required'),
            array('manager', 'required', 'message' => 'The Manager person didn\'t exist'),
            array('type_id, department_id, is_manager, is_food_manager,receive_emails', 'numerical', 'integerOnly' => true),
            array('number, name, alias, mobile, email', 'length', 'max' => 255),
            array('manager', 'length', 'max' => 45),
            array('status', 'length', 'max' => 11),
            array('email', 'email'),
            array('number', 'unique'),
            // Check If This Email is Unique when add new Non-LDAP Person
            array('email', 'emailValidate'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, number, name, alias, type_id, mobile, email, manager, department_id, is_manager, is_food_manager,receive_emails, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @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(
            'badges' => array(self::HAS_MANY, 'Badge', 'person_id'),
            'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
            'type' => array(self::BELONGS_TO, 'PersonType', 'type_id'),
            'personPoses' => array(self::HAS_MANY, 'PersonPos', 'person_id'),
            'tickets' => array(self::HAS_MANY, 'Ticket', 'person_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'number' => yii::t('default', 'ID Number'),
            'name' => yii::t('default', 'Full Name'),
            'alias' => yii::t('default', 'Alias'),
            'type_id' => yii::t('default', 'Type'),
            'mobile' => yii::t('default', 'Mobile'),
            'email' => yii::t('default', 'Email'),
            'manager' => yii::t('default', 'Manager'),
            'department_id' => yii::t('default', 'Department'),
            'is_manager' => yii::t('default', 'Is Manager'),
            'is_food_manager' => yii::t('default', 'Is Food Manager'),
            'receive_emails' => yii::t('default', 'Receive Emails'),
            'status' => yii::t('default', 'Status'),
        );
    }

    /**
     * @return array customized attribute labels Tooltips (name=>label tooltips)
     */
    public function attributeLabelsPopover()
    {
        return array(
            'number' => yii::t('default', 'ID Number'),
            'name' => yii::t('default', 'Full Name'),
            'alias' => yii::t('default', 'Alias'),
            'type_id' => yii::t('default', 'Type'),
            'mobile' => yii::t('default', 'Mobile'),
            'email' => yii::t('default', 'Email'),
            'manager' => yii::t('default', 'Manager'),
            'department_id' => yii::t('default', 'Department'),
            'is_manager' => yii::t('default', 'Is Manager'),
            'is_food_manager' => yii::t('default', 'Is Food Manager'),
            'receive_emails' => yii::t('default', 'Receive Emails'),
            'status' => yii::t('default', '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('number', $this->number, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('alias', $this->alias, true);
        $criteria->compare('type_id', $this->type_id);
        $criteria->compare('mobile', $this->mobile, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('manager', $this->manager, true);
        $criteria->compare('department_id', $this->department_id);
        $criteria->compare('is_manager', $this->is_manager);
        $criteria->compare('is_food_manager', $this->is_food_manager);
        $criteria->compare('receive_emails', $this->receive_emails);
        $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 NonLDAPPerson::model()->active()->findAll(array('order' => 'name'));
        }
        else
        {
            return NonLDAPPerson::model()->findAll(array('order' => 'name'));
        }
    }

    /*
     * Check If This Email is Unique
     */

    public function emailValidate($attribute, $params)
    {
        if ($this->isNewRecord)
        {
            if (isset($this->email) && ($this->email != ""))
            {
                $person_model = NonLDAPPerson::model()->findByAttributes(array('email' => $this->email));
                if ($person_model != null)
                {
                    $this->addError($attribute, Yii::t('default', '{n} already exists', $this->email));
                }
            }
        }
    }

}