<?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/ui.inc");
include_once($path_to_root . "/includes/data_checks.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;

//-------------------------------------------------------------------------------------------------
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 --"),
    '10' => _("INVOICE CREATED"),
    '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();
?>
