<?php

class RoutingOutbound extends PhonalisaRecord
{

    public $id;
    public $targetType;
    public $usergroup;
    public $pattern;
    public $gw_id;
    public $sort;
    public $overwriteCallerId;
    public $failover_1;
    public $failover_2;
    public $active = 1;
    public $dateTimeProfile;
    public $lcrPrefix;
    public $desc;

    public static function myAttributes()
    {
        return array(
            'overwriteCallerId' => 'overwriteCallerId',
            'targetType' => 'targetType',
            'usergroup' => 'usergroup',
            'pattern' => 'pattern',
            'gw_id' => 'gw_id',
            'failover_1' => 'failover_1',
            'failover_2' => 'failover_2',
            'sort' => 'sort',
            'active' => 'active',
            'dateTimeProfile' => 'dateTimeProfile',
            'lcrPrefix' => 'lcrPrefix',
            'desc' => 'desc'
        );
    }

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

    /**
     * @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('pattern, targetType, gw_id, sort', 'required'),
            array(' sort', 'numerical', 'integerOnly' => true),
            array('overwriteCallerId', 'length', 'max' => 32),
            array("desc", 'length', 'max' => 255),
            array('usergroup, active, lcrPrefix, failover_1, dateTimeProfile, failover_2', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, usergroup, pattern, gw_id, sort', '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' => __('ROUTING_OUTBOUND_MODEL_ID'),
            'usergroup' => __('ROUTING_OUTBOUND_MODEL_USERGROUP'),
            'pattern' => __('ROUTING_OUTBOUND_MODEL_PATTERN'),
            'gw_id' => __('ROUTING_OUTBOUND_MODEL_GW_ID'),
            'sort' => __('ROUTING_OUTBOUND_MODEL_SORT'),
            'overwriteCallerId' => __('ROUTING_OUTBOUND_MODEL_OVERWRITE_CALLER_ID'),
            'active' => __('ROUTING_OUTBOUND_MODEL_ACTIVE'),
            'dateTimeProfile' => __('ROUTING_OUTBOUND_MODEL_DATE_TIME_PROFILE'),
            'lcrPrefix' => __('ROUTING_OUTBOUND_MODEL_LCR_PREFIX'),
            "desc" => __("ROUTING_OUTBOUND_MODEL_DESC")
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CPhonalisaDataProvider 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.
        $arrayModels = array();
        $newmodel = new RoutingOutbound();

        $arrayModels = RoutingOutbound::model()->findAll();
        //default values for a new inbound Routing:

        $newmodel->id = "new";
        $newmodel->sort = "new";
        $arrayModels[] = $newmodel;

        $count = count($arrayModels);
        $dataProvider = new CArrayDataProvider($arrayModels, array(
            'id' => 'key',
            'sort' => array('defaultOrder' => 'sort ASC',
            ),
            'pagination' => array('pageSize' => $count
            ),
        ));
        return $dataProvider;
    }

//    public static function csvRelevantAttributes($modelName)
//    {
//        return ImportHelper::getCsvRelevantModelAttributes($modelName);
//    }

    public function getExportNotRelevantAttributes()
    {
        return array(
            'gw_id',
            'dateTimeProfile',
            'usergroup'
        );
    }

    public function sortedGateways($gateways)
    {
        $sortedGateways["active"] = array();
        $sortedGateways["disabled"] = array();
        foreach ($gateways as $gateway) {
            if ($gateway->active === "1") {
                $sortedGateways["active"][] = $gateway;
            } else {
                $sortedGateways["disabled"][] = $gateway;
            }
        }
        return $sortedGateways;
    }
}
