<?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 "badge_type".
  *
  * The followings are the available columns in table 'badge_type':
  * @property integer $id
  * @property string $name
  * @property integer $activation_period
  * @property integer $non_manager_meals
  * @property integer $manager_meals
  * @property integer $food_manager_meals
  * @property integer $allow_reassign
  * @property integer $notify_before_badge_expire
  * @property integer $start_notify_from_x_days_before_expiry
  * @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 BadgeAssignmentRule[] $badgeAssignmentRules
  */
 class BadgeType extends LsmsActiveRecord
     {


     /**
      * Returns the static model of the specified AR class.
      * @param string $className active record class name.
      * @return BadgeType 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 'badge_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, activation_period, non_manager_meals, manager_meals, food_manager_meals, allow_reassign, status,notify_before_badge_expire', 'required', 'except' => 'search'),
             array('activation_period, non_manager_meals, manager_meals, food_manager_meals, allow_reassign, notify_before_badge_expire, start_notify_from_x_days_before_expiry', 'numerical', 'integerOnly' => true),
             array('name', 'length', 'max' => 255),
             array('name', 'unique', 'message' => yii::t('dafault', 'Badge Type name ({value}) already exists.')),
             array('status', 'length', 'max' => 8),
             //Check If start_notify_from_x_days_before_expiry value is greater than zero and not blank when notify_before_badge_expire value is true
             array('start_notify_from_x_days_before_expiry', 'StartNotifyFromTypeValidate'),
             // The following rule is used by search().
// Please remove those attributes that should not be searched.
             array('id, name, activation_period, non_manager_meals, manager_meals, food_manager_meals, allow_reassign, notify_before_badge_expire, start_notify_from_x_days_before_expiry, status, created_by, created_date, last_edited_by, last_edited_date', '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', 'type_id'),
             'badgeAssignmentRules' => array(self::HAS_MANY, 'BadgeAssignmentRule', 'badge_type_id'),
         );
     }


//Check If start_notify_from_x_days_before_expiry value is greater than zero and not blank when notify_before_badge_expire value is true
     public function StartNotifyFromTypeValidate($attribute, $params)
     {


         if ($this->notify_before_badge_expire == 1)
         {
             if ((isset($this->start_notify_from_x_days_before_expiry) && empty($this->start_notify_from_x_days_before_expiry))
                     || (isset($this->start_notify_from_x_days_before_expiry) && ($this->start_notify_from_x_days_before_expiry < 0))
                     || ( ! isset($this->start_notify_from_x_days_before_expiry)))
             {
                 $this->addError($attribute, Yii::t('default', 'The Start Notification Before can’t be blank or less than one day'));
             }
         }
     }


     /**
      * @return array customized attribute labels (name=>label)
      */
     public function attributeLabels()
     {
         return array(
             'name' => yii::t('default', 'Name'),
             'activation_period' => yii::t('default', 'Activation Period'),
             'non_manager_meals' => yii::t('default', 'Non Manager Meals'),
             'manager_meals' => yii::t('default', 'Manager Meals'),
             'food_manager_meals' => yii::t('default', 'Food Manager Meals'),
             'allow_reassign' => yii::t('default', 'Allow Reassign'),
             'notify_before_badge_expire' => yii::t('default', 'Notify Before Badge Expire'),
             'start_notify_from_x_days_before_expiry' => yii::t('default', 'Start Notification Before'),
             'status' => yii::t('default', 'Status'),
         );
     }


     /**
      * @return array customized attribute labels Tooltips (name=>label tooltips)
      */
     public function attributeLabelsPopover()
     {
         return array(
             'name' => '',
             'activation_period' => yii::t('default', 'tooltip_activation_period'),
             'non_manager_meals' => yii::t('default', 'tooltip_non_manager_meals'),
             'manager_meals' => yii::t('default', 'tooltip_manager_meals'),
             'food_manager_meals' => yii::t('default', 'tooltip_food_manager_meals'),
             'allow_reassign' => yii::t('default', 'tooltip_allow_reassign'),
             'notify_before_badge_expire' => yii::t('default', 'tooltip_notify_before_badge_expire'),
             'start_notify_from_x_days_before_expiry' => yii::t('default', 'tooltip_notification_before'),
             '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('activation_period', $this->activation_period);
         $criteria->compare('non_manager_meals', $this->non_manager_meals);
         $criteria->compare('manager_meals', $this->manager_meals);
         $criteria->compare('food_manager_meals', $this->food_manager_meals);
         $criteria->compare('allow_reassign', $this->allow_reassign);
         $criteria->compare('notify_before_badge_expire', $this->notify_before_badge_expire);
         $criteria->compare('start_notify_from_x_days_before_expiry', $this->start_notify_from_x_days_before_expiry);
         $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 BadgeType::model()->active()->findAll(array('order' => 'name'));
         }
         else
         {
             return BadgeType::model()->findAll(array('order' => 'name'));
         }
     }


     public static function getID($name=null)
     {
         if (($name != '') && ($name != null))
         {
             $badgeType_model = BadgeType::model()->findByAttributes(array('name' => $name));
             if ($badgeType_model != null)
             {
                 return $badgeType_model->id;
             }
             else
             {
                 return -1;
             }
         }
         else
         {
             return -1;
         }
     }
     }