<?php

/**
 * This is the model class for table "phone_key".
 *
 * The followings are the available columns in table 'phone_key':
 * @property integer $id
 * @property integer $user_id
 * @property integer $profile_id
 * @property integer $group_id
 * @property string $label
 * @property string $name
 * @property string $phone
 * @property string $key_idx
 * @property string $level
 * @property boolean $locked
 * @property string $default
 * @property string $function
 * @property string $number_data
 * @property array $availableFunctions
 */
class PhoneKey extends PhonalisaRecord
{

    public $id;
    public $user_id;
    public $profile_id;
    public $group_id;
    public $label;
    public $name;
    public $phone;
    public $key_idx;
    public $level;
    public $locked;
    public $default;
    public $function;
    public $number_data;
    public $availableFunctions = array();
    public $snomKeyLabel;

    /**
     * @return array customized attribute
     */
    public static function myAttributes()
    {
        return array(
            'user_id' => 'user_id',
            'profile_id' => 'profile_id',
            'group_id' => 'group_id',
            'label' => 'label',
            'name' => 'name',
            'phone' => 'phone',
            'key_idx' => 'key_idx',
            'level' => 'level',
            'locked' => 'locked',
            'default' => 'default',
            'function' => 'function',
            'number_data' => 'number_data',
            'snomKeyLabel' => 'snomKeyLabel'
        );
    }

    /**
     * @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, phone, key_idx', 'required'),
            array('profile_id', 'required', 'on' => 'phoneKeyProfile'),
            array('user_id', 'required', 'on' => 'phoneSettings'),
            array('default,group_id, function, number_data, label, locked, snomKeyLabel', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array(
                'id,'
                . 'user_id,'
                . 'group_id,'
                . 'label,'
                . 'name,'
                . 'phone,'
                . 'key_idx,'
                . 'level,'
                . 'locked,'
                . 'default,'
                . 'function,'
                . 'number_data',
                '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(
        );
    }

    /**
     * @return array customized attribute labels (name=>label).
     */
    public function attributeLabels()
    {
        return array(
            'id' => __('PHONEKEYS_MODEL_ID'),
            'user_id' => __('PHONEKEYS_MODEL_USER_ID'),
            'profile_id' => __('PHONEKEYS_MODEL_PROFILE_ID'),
            'group_id' => __('PHONEKEYS_MODEL_GROUP_ID'),
            'label' => __('PHONEKEYS_MODEL_LABEL'),
            'name' => __('PHONEKEYS_MODEL_NAME'),
            'phone' => __('PHONEKEYS_MODEL_PHONE'),
            'key_idx' => __('PHONEKEYS_MODEL_KEY_IDX'),
            'shift' => __('PHONEKEYS_MODEL_SHIFT'),
            'locked' => __('PHONEKEYS_MODEL_LOCKED'),
            'default' => __('PHONEKEYS_MODEL_DEFAULT'),
            'function' => __('PHONEKEYS_MODEL_FUNCTION'),
            'number_data' => __('PHONEKEYS_MODEL_NUMBER_DATA'),
            'level' => __('PHONEKEYS_MODEL_NUMBER_LEVEL'),
        );
    }

    /**
     * @return array functions for the key.
     */
    public function getFunctionsforKey()
    {
        $phone = new Phones();
        return $phone->getOptions();
    }

    /**
     * @return boolean whether the profile is defined (true) or not (false).
     */
    public function isProfileDefined()
    {

        if ($this->profile_id) {
            $profileKey = $this->findByAttributes(array(
                'profile_id' => $this->profile_id,
                'label' => $this->label,
                'phone' => $this->phone,
                'function' => $this->function,
                'number_data' => $this->number_data,
            ));
            if ($profileKey != null) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return boolean whether the button is empty (true) or not (false).
     */
    public function isButtonEmpty()
    {
        $phone = strtolower($this->phone);
        if ($this->function !== '' || (substr($phone, 0, 6) === 'tiptel' || substr($phone, 0, 7) === 'yealink')) {
            return false;
        }
        return true;
    }

    public static function getKeyNameLabel($data)
    {
        $keyName = $data->name;
        if (substr($data->phone, 0, 4) === 'snom') {
            $keyName = $data->snomKeyLabel;
            if ($data->level === 'memory' || $data->level === 'line') {
                $keyName = 'P' . $keyName;
            } else if (startsWith($data->level, 'ext')) {
                $keyName = 'EK' . $keyName;
            }
        }
        return $keyName;
    }
}
