<?php 

class DefaultContactType {
    
// const VIP =         0;
// const VIF =         1;
// const Office =      2;
// const Family=       3;
// const Friends=      4;
// const Customers =   5;
// const none=         6;

const VIP =         6;
const VIF =         5;
const Office =      4;
const Family=       3;
const Friends=      2;
const Customers =   1;
const none=         0;


public static function getAllTypes() {

  $defaults = array();
  $defaults[DefaultContactType::VIP] = __('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_VIP');
  $defaults[DefaultContactType::VIF] = __('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_VIF');
  $defaults[DefaultContactType::Office] =__('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_OFFICE');
  $defaults[DefaultContactType::Family] =__('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_FAMILY');
  $defaults[DefaultContactType::Friends] =__('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_FRIENDS');
  $defaults[DefaultContactType::Customers] =__('CONTACT_TYPE_MODEL_ADDRESSES_CONTACT_TYPE_CUSTOMERS');
  $defaults[DefaultContactType::none] ='-';
  
  return $defaults;
}
   
}

class ContactType 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 'contactType';
    }

    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'=> __('CONTACT_TYPE_MODEL_ID'),
            'name'=>__('CONTACT_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 = DefaultContactType::getAllTypes();
//
//        foreach ($defaults as $key => $default) {
//	    if ($this->findByAttributes(array('name'=>$default)) ) {
//		continue;
//	    } 
//            $defaultModel = new ContactType;
//            $defaultModel->id = $key;
//            $defaultModel->name = $default;
//            $models[] = $defaultModel;
//        }
//
//        return array_reverse($models);
//    }

    public function beforeSave() {

	$defaults = DefaultContactType::getAllTypes();
	if (in_array($this->name,$defaults)) {
	  return FALSE;
	} 

	return parent::beforeSave();
    }
    
    public function deleteContactType($contactTypeId)
    {
        $contactTypeOwner = Addresses::model()->countAllByAttributes(array('contact_type'=>$contactTypeId));

        if ($contactTypeOwner == 0) {
            $typeToDelete = ContactType::model()->findByPk($contactTypeId);
            if ($typeToDelete) {
                $typeToDelete->delete();
            }
        }
    }
    
    public static function getDefinedContactTypes()
    {
        $defaults = DefaultContactType::getAllTypes();
        
        $defaultModels = array();
        foreach (array_reverse($defaults) as $id => $name) {
            $defaultModel = new ContactType();
            $defaultModel->id = $id;
            $defaultModel->name = $name;
            $defaultModels[] = $defaultModel;
        }
        
        $userDefinedTypeModels = $defaultModel->getUserDefinedmodels();
        $result = array_merge($defaultModels, $userDefinedTypeModels);
        return $result;
    }
    
    public function getUserDefinedmodels() 
    {
        $limit = 100000;
        $offset = 0;
        $userDefinedTypeModels = array();
        while ($result = ContactType::model()->find(array(),"id",$limit, $offset)) {
            $userDefinedTypeModels = array_merge($userDefinedTypeModels, $result);
            $offset = $offset + $limit;
        }
        return $userDefinedTypeModels;
    }
}