<?php 

class DefaultEventType {

  const call 		=	4; 
  const meeting 	=	3;
  const vacation 	=	2;
  const birthday	=	1;
  
  const calenderDefault =	0;


  public static function getAllTypes() {

    $defaults = array();
    $defaults[DefaultEventType::call] = __('EVENT_TYPE_MODEL_CALL');
    $defaults[DefaultEventType::meeting] = __('EVENT_TYPE_MODEL_MEETING');
    $defaults[DefaultEventType::vacation] =__('EVENT_TYPE_MODEL_VACATION');
    $defaults[DefaultEventType::birthday] =__('EVENT_TYPE_MODEL_BIRTHDAY');
        
    return $defaults;
  }
}

class EventType extends PhonalisaRecord
{
    public $id;
    public $name;
    
    public static function myAttributes() {
      return array(
        'id'=>'id',
        'name'=>'name',
        );
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'eventType';
    }

    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'required'),
            array('name,id', 'safe', 'on'=>'search'),
        );
    }
    
    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(
        );
    }

    public function attributeLabels()
    {
        return array(
            'id'=> __('EVENT_TYPE_MODEL_ID'),
            'name'=>__('EVENT_TYPE_MODEL_NAME'),
        );
    }
        
    public function search() {
        //FIXME !! 
        $models = $this->findAll();

        return new CArrayDataProvider($models);
    }
        
    public function findAll($criteria = array(), $params = array()) {

    
        $models= parent::findAll($criteria, $params);
        $defaults = DefaultEventType::getAllTypes();

         
	foreach ($defaults as $key =>  $name) {
	    $defaultModel = new EventType;
	    $defaultModel->name = $name;
	    $defaultModel->id = $key;
	    $models[] = $defaultModel;
	}

        return array_reverse($models);
    }
    
    public function findByAttributes($attributes) {
      $defaults = DefaultEventType::getAllTypes();
      foreach ($attributes as $key => $value ) {
      
	if ($key == 'name' && in_array($value, $defaults)) {
	
	  $defaultModel = new EventType();
	  $defaultModel->name  =  $value; 
	  $defaults = array_flip($defaults);
	  $defaultModel->id = $defaults[$value];
	  return $defaultModel;  
	} else if ($key == 'id' && in_array($value, $defaults)) {

	  $defaultModel = new EventType();
	  $defaultModel->id  =  $value; 
	  $defaultModel->name = $defaults[$value];
	  return $defaultModel;	  
	} 
	
      } 
      
      
      return parent::findByAttributes($attributes);
      
    }
    
    public function beforeSave() {
        
	// check if Custom Type is defaultType 
	$defaultTypes = DefaultEventType::getAllTypes();
	if (in_array($this->name,$defaultTypes)) {
	  return false;
	}
	return parent::beforeSave();
    }
    
}
?>