<?php

$page_security = 'SA_SETUPCOMPANY';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Sms Setup"));

//include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once(dirname(__FILE__). '/includes/tax_regime.inc');

include_once($path_to_root . "/admin/db/company_db.inc");
include_once( $path_to_root . "/modules/external/includes/db/notification_template_db.php");

//-------------------------------------------------------------------------------------------------
// initialize stuff just in case
$init = 0;
if (get_company_pref('sms_api_username') === null) {
    set_company_pref('sms_api_username', 'system.external', 'varchar', 100, '');
    $init++;
}

if (get_company_pref('sms_api_key') === null) {
    set_company_pref('sms_api_key', 'system.external', 'varchar', 250, '');
    $init++;
}

if (get_company_pref('sms_api_active') === null) {
    set_company_pref('sms_api_active', 'system.external', 'varchar', 1, '');
    $init++;
}

if (get_company_pref('sms_api_sender_id') === null) {
    set_company_pref('sms_api_sender_id', 'system.external', 'varchar', 150, '');
    $init++;
}

if (get_company_pref('sms_gateway') === null) {
    set_company_pref('sms_gateway', 'system.external', 'varchar', 250, '');
    $init++;
}

//if ($init)
//    refresh_sys_prefs();

function can_process() {
    $errors = 0;

    if (empty($_POST['sms_api_username'])) {
        $errors++;
        display_error(_("The SMS API Username must be entered."));
    }

    if (empty($_POST['sms_api_key'])) {
        $errors++;
        display_error(_("The SMS API KEY must be entered."));
    }

    return ($errors == 0);
}

//-------------------------------------------------------------------------------------------------

if (isset($_POST['submit']) && can_process()) {
    update_company_prefs(get_post(array('sms_api_key', 'sms_api_username', 'sms_api_active', 'sms_api_sender_id','sms_gateway')));

    display_notification(_("The sms sending settings has been updated."));
}

$prefs = get_company_prefs();
$_POST['sms_api_username'] = $prefs['sms_api_username'];
$_POST['sms_api_key'] = $prefs['sms_api_key'];
$_POST['sms_api_active'] = $prefs['sms_api_active'];
$_POST['sms_api_sender_id'] = $prefs['sms_api_sender_id'];
if(!isset($_POST['sms_gateway']))
    $_POST['sms_gateway'] = $prefs['sms_gateway'];
//-------------------------------------------------------------------------------------------------

start_form();

div_start('details');
start_table(TABLESTYLE2);
start_row();
echo array_selector(
        'sms_gateway', get_post('sms_gateway'), Array(
    '' => _('Select Gateway'),
    'AfricasTalkingGateway' => 'AfricasTalkingGateway',
    'enetonlinesolutions' => 'enetonlinesolutions'), array('select_submit' => TRUE)
);
end_row();
$Ajax->activate('_page_body');
if (get_post('sms_gateway') == 'enetonlinesolutions') {
    text_row(_("USERNAME:"), 'sms_api_username', $_POST['sms_api_username'], 30, 62);
    text_row(_("PASSWORD:"), 'sms_api_key', $_POST['sms_api_key'], 70, 150);
    text_row(_("SENDER ID:"), 'sms_api_sender_id', $_POST['sms_api_sender_id'], 70, 150);
    array_selector_row(_("Active? :"), 'sms_api_active', $_POST['sms_api_active'], array('0' => _("NO"), '1' => _("YES")));
} else if (get_post('sms_gateway') == 'AfricasTalkingGateway') {
    text_row(_("SMS API USERNAME:"), 'sms_api_username', $_POST['sms_api_username'], 30, 62);
    text_row(_("SMS API KEY:"), 'sms_api_key', $_POST['sms_api_key'], 70, 150);
    array_selector_row(_("Active? :"), 'sms_api_active', $_POST['sms_api_active'], array('0' => _("NO"), '1' => _("YES")));
}

end_table(1);

div_end();
if (!empty(get_post('sms_gateway'))) {
    echo '<center>';
    submit('submit', _("Update"), true, '', 'default');
    echo '</center>';
}

end_form(2);
echo "<hr>";

//-------------------------------------------------------------------------------------------------
function can_process_template() {
    $errors = 0;
    if (empty($_POST['template_id'])) {
        $errors++;
        display_error(_("Please Select a Template."));
    }
    return ($errors == 0);
}

if (isset($_POST['submit_template']) && can_process_template()) {
    set_template('sms', $_POST['template_type'], $_POST['active'], $_POST['template_subject'], $_POST['template_body']);
    display_notification(_("The sms sending settings have been updated."));
}


$template_type = '';
if (isset($_POST['template_type'])) {
    $template_type = $_POST['template_type'];
}
start_form();
start_table(TABLESTYLE_NOBORDER);
start_row();
$_selector = array_selector(
        'template_type', $template_type, $allocs = array(
    '-1' => _("Select --"),
    '80' => _("MEMBER CONTRIBUTION"),
        ), array('select_submit' => true)
);
label_cells(_("Select Template:"), $_selector);
end_row();
end_table();

if ($template_type != -1) {
    $Ajax->activate('template_details');
    $temp = get_template('sms', $_POST['template_type']);
} else {
    $temp['body'] = '';
    $temp['id'] = '';
    $temp['subject'] = '';
    $temp['fields'] = '';
    $temp['active'] = '';
}

div_start('template_details');
start_table(TABLESTYLE2);
start_row();
check_row(_('Active ? :'), 'active', $temp['active']);
textarea_row(_("MSG:"), 'template_body', $temp['body'], 50, 5);
$variables = explode(',', $temp['fields']);
$i = 0;
$DisplayFields = '';
foreach ($variables as $variable) {
    $i++;
    $DisplayFields .= $variable . "&nbsp; &nbsp;";
    if ($i == 3) {
        $DisplayFields .= '<br/>';
        $i = 0;
    }
}
label_row('Variables', $DisplayFields);
end_row();
hidden('template_id', $temp['id']);
hidden('template_subject', $temp['subject']);
end_table(1);
div_end();
echo '<center>';
submit('submit_template', _("Update Template"), true, '', 'default');
echo '</center>';

end_form(2);

//-------------------------------------------------------------------------------------------------

end_page();
?>
