<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class emailmng_config
{

    protected $conf;
    protected $CI;

    public function __construct()
    {
        $this->CI = &get_instance();
        $this->CI->load->library('ciext/configurator');
        $this->conf = new configurator('emailmng');
    }

    public function render()
    {
        $this->CI->load->library('gettext_language');
        $this->CI->gettext_language->load(__DIR__ . '/../language/' . $this->CI->config->item('language') . '.po');
        
        if (!$this->CI->rt_user->isAllowed('emailmng', 'can_mng_conf')) {
            $this->CI->admin_lib->show_denied();
        }
        $this->callbacks()->display_as();

        return $this->conf->render();
    }

    protected function callbacks()
    {
        $this->CI->load->model('rtgrid/rtgrid_field_callback');
        $this->CI->rtgrid_field_callback->buffer = [
            'mailtype' => $this->CI->config->item('mailtype'),
            'protocol' => $this->CI->config->item('mail_protocol'),
            'priority' => $this->CI->config->item('priority'),
        ];

        $this->conf->add_input('sender_email');
        $this->conf->add_input('sender_name');
        $this->conf->add_item('mailtype', array($this->CI->rtgrid_field_callback, 'dropdown'));
        $this->conf->add_item('protocol', array($this->CI->rtgrid_field_callback, 'dropdown'));
        $this->conf->add_input('mailpath');
        $this->conf->add_input('smtp_host');
        $this->conf->add_input('smtp_user');
        $this->conf->add_input('smtp_pass');
        $this->conf->add_input('smtp_port');
        $this->conf->add_radio('wordwrap');
        $this->conf->add_item('wrapchars', array($this->CI->rtgrid_field_callback, 'spinner'));
        $this->conf->add_radio('validate');
        $this->conf->add_item('priority', array($this->CI->rtgrid_field_callback, 'dropdown'));

        return $this;
    }

    protected function display_as()
    {
        $this->conf->display_as('sender_email', $this->CI->lang->line('From(e-mail)'));
        $this->conf->display_as('sender_name', $this->CI->lang->line('From(name)'));
        $this->conf->display_as('mailtype', $this->CI->lang->line('Type of mail'));
        $this->conf->display_as('protocol', $this->CI->lang->line('The mail sending protocol'));
        $this->conf->display_as('mailpath', $this->CI->lang->line('The server path to Sendmail'));
        $this->conf->display_as('smtp_host', $this->CI->lang->line('SMTP host'));
        $this->conf->display_as('smtp_user', $this->CI->lang->line('SMTP user'));
        $this->conf->display_as('smtp_pass', $this->CI->lang->line('SMTP password'));
        $this->conf->display_as('smtp_port', $this->CI->lang->line('SMTP port<br/>default: 25'));
        $this->conf->display_as('wordwrap', $this->CI->lang->line('Enable word-wrap'));
        $this->conf->display_as('wrapchars', $this->CI->lang->line('Character count to wrap at<br/>default: 76'));
        $this->conf->display_as('validate', $this->CI->lang->line('Validate e-mail'));
        $this->conf->display_as('priority', $this->CI->lang->line(
                        'Email Priority<br/>'
                        . ' 1 = highest<br/>'
                        . ' (default)3 = normal<br/>'
                        . ' 5 = lowest'
                )
        );

        return $this;
    }

}
