<?php

class Alert extends PhonalisaRecord
{
    const LEVEL_INFO = 1;
    const LEVEL_WARNING = 2;
    const LEVEL_ERROR = 3;
    const LEVEL_CRITICAL = 4;
    const LEVEL_FATAL = 5;

    const COMPONENT_CALLROUTER = 'Callrouter';
    const COMPONENT_MSGD = 'Msgd';
    const COMPONENT_SYSTEM = 'System';
    const COMPONENT_VOIPD = 'Voipd';
    const COMPONENT_WEBGUI = 'WebGUI';

    public $level;
    public $component;
    public $shortDescr;
    public $descr;
    public $created;

    public static function myAttributes()
    {
        return array(
            'level' => 'level',
            'component' => 'component',
            'shortDescr' => 'shortDescr',
            'descr' => 'descr',
            'created' => 'created'
        );
    }

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

    /**
     * @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(
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('level, component, shortDescr, descr, created', 'safe'),
            array('level, component, shortDescr, descr, created', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'level' => __('ALERT_MODEL_LEVEL'),
            'component' => __('ALERT_MODEL_COMPONENT'),
            'shortDescr' => __('ALERT_MODEL_SHORT_DESCRIPTION'),
            'descr' => __('ALERT_MODEL_DESCRIPTION'),
            'created' => __('ALERT_MODEL_CREATED')
        );
    }

    public function search($order = 'created DESC')
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $filter = $this->getFilter($criteria, $order);


        $filter['pagination'] = array(
            'pageSize' => getGridPageSize('alerts-grid'),
        );

        if ($filter != null) {
            return new CPhonalisaDataProvider($this, $filter
            );
        }
    }

    public function levelToString($level)
    {
        $result = 'undefined';
        switch ($level) {
            case Alert::LEVEL_CRITICAL:
                $result = __('ALERT_MODEL_LEVEL_CRITICAL');
                break;
            case Alert::LEVEL_ERROR:
                $result = __('ALERT_MODEL_LEVEL_ERROR');
                break;
            case Alert::LEVEL_FATAL:
                $result = __('ALERT_MODEL_LEVEL_FATAL');
                break;
            case Alert::LEVEL_INFO:
                $result = __('ALERT_MODEL_LEVEL_INFO');
                break;
            case Alert::LEVEL_WARNING:
                $result = __('ALERT_MODEL_LEVEL_WARNING');
                break;
        }
        return $result;
    }

    public function getLevelType()
    {
        $result = '';
        switch ($this->level) {
            case Alert::LEVEL_CRITICAL:
            case Alert::LEVEL_ERROR:
            case Alert::LEVEL_FATAL:
                $result = 'important';
                break;
            case Alert::LEVEL_INFO:
                $result = 'info';
                break;
            case Alert::LEVEL_WARNING:
                $result = 'warning';
                break;
        }
        return $result;
    }

    public function getAlertDataProviderForAdminUser($model)
    {
        $components = $this->getAllComponents();
        $listData = array('' => __('ALERT_MODEL_LEVEL_DEAKTIVATED'));
        $listData = array_merge($listData, array( __("ALERT_MODEL_LEVEL_DROPDOWN_SEPERATOR") => $this->getAllLevels()));

        $data = array();
        $id = 0;
        foreach ($components as $component) {
            $data[$id]['id'] = $id;
            $data[$id]['component'] = $component;
            $data[$id]['level'] = CHtml::dropDownList(
                    'User[alert][alert_' . $component . ']',
                    $model->valueForKey('alert_' . $component),
                    $listData,
                    array("data-translation-id"=> "ALERT_MODEL_LEVEL_DROPDOWN_SEPERATOR"));
            $id++;
        }

        $dataProvider = new CArrayDataProvider($data, array(
            'pagination' => false
        ));
        return $dataProvider;
    }

    protected function getAllComponents()
    {
        return array(
            Alert::COMPONENT_CALLROUTER,
            Alert::COMPONENT_MSGD,
            Alert::COMPONENT_SYSTEM,
            Alert::COMPONENT_VOIPD,
            Alert::COMPONENT_WEBGUI,
        );
    }

    protected function getAllLevels()
    {
        return array(
            Alert::LEVEL_INFO => $this->levelToString(Alert::LEVEL_INFO),
            Alert::LEVEL_WARNING => $this->levelToString(Alert::LEVEL_WARNING),
            Alert::LEVEL_ERROR => $this->levelToString(Alert::LEVEL_ERROR),
            Alert::LEVEL_CRITICAL => $this->levelToString(Alert::LEVEL_CRITICAL),
            Alert::LEVEL_FATAL => $this->levelToString(Alert::LEVEL_FATAL),
        );
    }
}
