<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_SETUPCOMPANY';
$path_to_root="../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Send SMS"));

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/class.sms.inc");

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



function can_process() {
    $errors = 0;
    
    
    if (!empty($_POST['sms_recepient']) ) {
        $mobile_nos = $_POST['sms_recepient'];
        $tos = explode(',', $mobile_nos);

        if (count($tos) > 0) {
            foreach ($tos as $to) { //loop over values
                if (strlen($to) < 9) {
                    $errors++;
                    display_error(_("Mobile number " . $to . " is not a valid number. Please correct to continue."));
                }
            }
        }
    } else {
        $errors++;
	display_error(_("Recepients must be set."));
    }

    if (empty($_POST['sms_body']) ) {
	$errors++;
	display_error(_("Sms Body cannot be blunk."));
    }
    
    return ($errors == 0);

}


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

if (isset($_POST['submit']) && can_process()) {
	$sms = new SMS();
        $res_s = $sms->send($_POST['sms_recepient'], $_POST['sms_body']);
        if($res_s != FALSE){
            foreach ($res_s as $res){
                if($res->status == 'Success'){
                    display_notification(_("SMS sent to $res->number. Status: $res->status. Cost: $res->cost."));
                } else {
                    display_warning(_("Mobile Nomber: $res->number. Status: $res->status. Cost: $res->cost."));
                }
            }
        }
}  



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


start_form();


div_start('details');

start_table(TABLESTYLE2);
text_row(_("To:"), 'sms_recepient', '', 50, 52);
textarea_row(_("MSG:"), 'sms_body', '', 50, 6);
end_table(1);
div_end();

submit_center('submit', _("Send SMS"), true, '', 'default');

end_form(2);

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

end_page();

?>
