<?php
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*      Copyright (c) 2015-2017 LLC Alt-team. All rights reserved.           *
*                                                                           *
*  This is commercial software, only users who have purchased a valid       *
*  license and accept to the terms of the License Agreement can install     *
*  and use this program.                                                    *
*                                                                           *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  PLEASE READ THE FULL TEXT OF THE SOFTWARE LICENSE AGREEMENT IN THE       *
*  "pd_copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.         *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

use Tygh\Registry;

if ( !defined('BOOTSTRAP') ) { die('Access denied'); }

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($mode == 'update') {
    	$sender_data = $_REQUEST['sender_data'];

        if (!empty($sender_data)){
            foreach ($sender_data as $company_id => $company) {
                db_query("UPDATE ?:companies SET sms_sender_name = ?s WHERE company_id = ?i", $company['sms_sender_name'], $company_id);
            }
        }
    }
}

if ($mode == 'send_sms') {
    if (defined('AJAX_REQUEST')) { 
    	$phones = explode(",", $_REQUEST['phones']);

        if (!empty($phones)){
            foreach ($phones as $key => $phone) {
                fn_targetsms_send_sms($phone, $_REQUEST['text'], $_REQUEST['senders'], "sendmanualsms");
            }
        }

    	return true;
    }
}

if ($mode == 'send_massive_sms') {
    if (defined('AJAX_REQUEST')) { 
        $orders_ids = $_REQUEST['orders_ids'];
        if (!empty($orders_ids)){
            fn_targetsms_massive_sms($orders_ids);
        }
    }
}

if ($mode == 'search_orders_by_date') {
    if (defined('AJAX_REQUEST')) { 
        $params['period'] = 'C';
        $params['time_from'] = date("m/d/Y", strtotime($_REQUEST['from']));
        $params['time_to'] = date("m/d/Y", strtotime($_REQUEST['to']));
        if (!empty($_REQUEST['is_reset'])){
            $orders = array();
        }else{
            list($orders, $search) = fn_get_orders($params, 0);
            if (empty($orders)){
            	fn_set_notification("N", __('notice'), __("no_orders_in_range"));
            }
        }
        Registry::get('view')->assign('alt_orders', $orders);
        Registry::get('view')->display('addons/targetsms/components/orders_list.tpl');
        exit;
    }
}