<?php

class DefaultController extends Controller
{

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                //'users'=>array('@'),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('create', 'update'),
                //'users'=>array('@'),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete'),
                //'users'=>array('@'),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate()
    {
        $model = app()->user->sudoUserInstance;
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            if ($model->save()) {
                $job = new Job();
                $job->type = "provisioning";
                $job->user_id = $model->id;
                $job->save();

                $this->redirect(array('index'));
            }
        }
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $model = app()->user->sudoUserInstance;
        if (!$model) {
            app()->user->logout();
            app()->user->loginRequired();
        }
        $this->render('phonesettingsNotifications', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model = User::model()->findByPk($id);
        if ($model === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'phonesettings-user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function phonesettingsNotificationsSearch()
    {

        $phonesettings = $this->renderPhonesettingsNotifications();

        $dataProvider = new CArrayDataProvider($phonesettings, array('pagination' => false));

        return $dataProvider;
    }

    public function renderPhonesettingsNotifications()
    {

        $userSettings = array();
        
        if (PhonalisaLicenseManager::faxGUIEnabled()) {
            $userSettings[] = array(
                'id' => '10',
                'key' => 'email_fax',
                'short'=>$this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_FAX_NOTIFICATION_SHORT"
                ),
                'value' => '',
                'descr'=>$this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_FAX_NOTIFICATION_DESCRIPTION"
                ),
                'type' => 'bool',
            );
        }
        if (PhonalisaLicenseManager::vmail2MailEnabled()) {
            $userSettings[] = array(
                'id' => '11',
                'key' => 'email_vmail',
                'short'=>$this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_VOICEMAIL_NOTIFICATION_SHORT"
                ),
                'value' => '',
                'descr'=>$this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_VOICEMAIL_NOTIFICATION_DESCRIPTION"
                ),
                'type' => 'bool',
            );
            $userSettings[] = array(
                'id' => '16',
                'key' => 'email_vmail_delete',
                'short' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EMAIL_VMAIL_DELETE_SHORT"
                ),
                'value' => '',
                'descr' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EMAIL_VMAIL_DELETE_DESCRIPTION"
                ),
                'type' => 'bool',
            );
        }
        if (PhonalisaLicenseManager::missedCall2MailEnabled()) {
            $userSettings[] = array(
                'id' => '13',
                'key' => 'email_external_missed_call',
                'short' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EXTERNAL_MISSED_CALL_NOTIFICATION_SHORT"
                ),
                'value' => '',
                'descr' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EXTERNAL_MISSED_CALL_NOTIFICATION_DESCRIPTION"
                ),
                'type' => 'bool',
            );

            $userSettings[] = array(
                'id' => '13',
                'key' => 'email_internal_missed_call',
                'short' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_INTERNAL_MISSED_CALL_NOTIFICATION_SHORT"
                ),
                'value' => '',
                'descr' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_INTERNAL_MISSED_CALL_NOTIFICATION_DESCRIPTION"
                ),
                'type' => 'bool',
            );
        }
        
        
        if (app()->user->sudoUserInstance->isSystemAdmin && PhonalisaLicenseManager::sipAccountOffline2MailEnabled()) {
            $userSettings[] = array(
                'id' => '18',
                'key' => 'email_sip_account_offline',
                'short' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EXTERNAL_SIPACCOUNT_OFFLINE_NOTIFICATION_SHORT"
                ),
                'value' => '',
                'descr' => $this->getTextCellContent(
                    "PHONESETTINGS_USER_MODEL_EXTERNAL_SIPACCOUNT_OFFLINE_NOTIFICATION_DESCRIPTION"
                ),
                'type' => 'bool',
       	    );
	}

        return $userSettings;
    }

    public function getTextCellContent($translationKey)
    {
        $content = "";
        $content .= "<p data-translation-id='$translationKey'>" . __($translationKey) . "</p>";
        return $content;
    }

    public function renderValue($data)
    {
        $model = app()->user->sudoUserInstance;

        if ($data['type'] == 'bool') {
            return CHtml::checkBox(
                'User[' . $data['key'] . ']',
                $model->{$data['key']},
                array('name' => '.$value.',
                    'uncheckValue' => '0'
                )
            );
        } elseif ($data['type'] == 'text') {
            return CHtml::textField(
                'User[' . $data['key'] . ']',
                $model->{$data['key']}
            );
        } elseif ($data['type'] == 'dropdown') {
            $stdRingtones = array(
                'ringtone-1' => __('BELLCORE') . ' 1',
                'ringtone-2' => __('BELLCORE') . ' 2',
                'ringtone-3' => __('BELLCORE') . ' 3',
                'ringtone-4' => __('BELLCORE') . ' 4',
                'ringtone-5' => __('BELLCORE') . ' 5',
                'ringtone-6' => __('BELLCORE') . ' 6',
                'ringtone-7' => __('BELLCORE') . ' 7',
                'ringtone-8' => __('BELLCORE') . ' 8',
                'ringtone-9' => __('BELLCORE') . ' 9',
                'ringtone-10' => __('BELLCORE') . ' 10',
                'mute' => __('BELLCORE_MUTE'),
            );

            $data = Ringtone::model()->findAllByAttributes(
                array(
                    'userId' => $model->id
                )
            );
            $userRingtones = CHtml::listData($data, 'id', 'fileName');

            $availableRingTones = array(__('CHOOSE_BELLCHOR') => array_merge($stdRingtones, $userRingtones));
            return CHtml::dropDownList('User[' . $data['key'] . ']', $model->{$data['key']}, $availableRingTones);
        }
    }
}
