<?php
/**
 * Created by PhpStorm.
 * User: IRIG
 * Date: 28/08/2017
 * Time: 05:01 PM
 */

class SendMessage
{

    private $sms_number = '';
    private $sms_api = '';

    private $Client = null;
    private $DAL = null;

    public function __construct($sms_number = '10001000300082', $sms_api = 'B59DE2F3-E235-4729-9A1C-7D8E852B5289')
    {
        $this->Construct($sms_number, $sms_api);
    }

//    public function SendMessage($sms_number = '10001398', $sms_api = 'B59DE2F3-E235-4729-9A1C-7D8E852B5289')
//    {
//        $this->Construct($sms_number, $sms_api);
//    }

    protected function Construct($sms_number, $sms_api)
    {
        //$client = new SoapClient('http//xxx.xxx.xxx?wsdl', array());
        try {
            if (!class_exists("DAL")) require_once '../DAL.php';
            $this->DAL = new DAL('constant.php');
            $this->sms_api = $sms_api;
            $this->sms_number = $sms_number;
            ini_set("soap.wsdl_cache_enabled", "0");
            $this->Client = new SoapClient(
                "http://sms.api.ir/Api/SendSMS.asmx?WSDL",
                array('encoding' => 'UTF-8', 'soap_version' => SOAP_1_1, 'trace' => 1, 'exceptions' => true)
            );
        } catch (Exception $e) {
            $e->getTrace();
        }
    }

    public function SendOneMessage($number, $message)
    {
        try {
            $parameters = array(
                'signature' => $this->sms_api,
                'from' => $this->sms_number,
                'to' => (is_array($number) ? $number : array($number)),
                'texts' => (is_array($message) ? $message : array($message)),
                'isFlash' => false,
                'udh' => '',
            );
            $outArray = (array)$this->Client->SendGroupSMSP2P($parameters);
            $outArray = json_encode($outArray, JSON_UNESCAPED_UNICODE);
        } catch (SoapFault $ex) {
            $outArray = $ex->faultstring;
        }

        $number = (is_array($number) ? json_encode($number, JSON_UNESCAPED_UNICODE) : $number);
        $message = (is_array($message) ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message);
        $this->DAL->DoCommand(
            "INSERT INTO `app_sms_log` (`from`, `to`, `message`, `result`)VALUES ('{$this->sms_number}','{$number}','{$message}','{$outArray}')");
        return $this->sms_number;
    }


    public function SendMultiMessage($TextAndNumber = array(array("message" => "", "number" => "")))
    {
        $numbers = array();
        $messages = array();
        foreach ($TextAndNumber as $client) {
            if (isset($client['message']) AND isset($client['number'])) {
                $numbers[] = $client['number'];
                $messages[] = $client['message'];
            }
        }

        if (count($numbers) > 0 AND count($messages) > 0 AND count($numbers) === count($messages))
            return $this->SendOneMessage($numbers, $messages);
        return 0;
    }
}
//$a = new SendMessage();
//$a->SendOneMessage("09367153842", "ss");
//$array = array(
//    array('message' => 'salam mame', 'number' => '09362758507'),
//    array('message' => 'salam kavand', 'number' => '09367153842')
//);
//$a->SendMultiMessage($array);