Mailer.php 4.28 KB
<?php
namespace MyMailer;

class Mailer extends \core{



    public $from_name;
    public $from_email;
    public $to_email    = '';
    public $title       = '';
    public $mailMessage = '';
    public $to_name     = '';
    public $mailer;
    public $deliveryService;

    public $public_key = '5xaiqb1pnuu8jsun4rwxhow4shj11e55zdhjf5do';
    public $list_id = '4940006';




    public function __construct(){
        $this->from_email = 'andrey.belyy@gmail.com';
        $this->mailer = new sender\unisender($this->public_key, $this->list_id);
    }

    public function reloadSender(){
        $this->mailer = new sender\unisender($this->public_key,$this->list_id);
    }

    public function changePublicKey($key){
        $this->public_key = $key;
    }

    public function changeListId($list_id){
        $this->list_id  = $list_id;
    }

    function setFromName($name)
    {
        return $this->from_name = $name;
    }

    function setFromEmail($email)
    {
        return $this->from_email = $email;
    }

    function setToMail($email = array(), $string = false)
    {
        if($string) {
            $sendTo = array();

            foreach($email as $user){

                $sendTo[] = $user['email'];
            }

            $email =implode(",", $sendTo);

            return $this->to_email = $email;
        } else {

            return $this->to_email = $email;
        }


    }

    function setTitle($title)
    {
        return $this->title = $title;
    }

    function setMailMessage($mailMessage)
    {
        return $this->mailMessage = $mailMessage;
    }

    function setToName($to_name)
    {
        return $this->to_name = $to_name;
    }



    public function SendEmail()
    {
        $this->mailer->SendMail($this->to_email, '', $this->from_email, $this->from_name, $this->title, $this->mailMessage);
    }



    public function getDeliveryStatus( $campaign_id)
    {
        return $this->mailer->getCampaignStatus($campaign_id);

    }


    public function getCampaignDeliveryStats($campaign_id)
    {
        $status = $this->mailer->getCampaignDeliveryStats($campaign_id);
        return $status;
    }

    public function getVisitedLinks($campaign_id)
    {
        $status = $this->mailer->getVisitedLinks($campaign_id);

    }


    public function updateDeliveryData($campaign_id)
    {
        return $this->getDelivService()->save($campaign_id);
    }




    public function getDelivService()
    {
        $delivery_db = $this->getDi()->get('models')->getDelivery();
        $delivery_email_db = $this->getDi()->get('models')->getDeliveryEmail();
        if($this->deliveryService instanceof \deliveryServ) {
            return $this->deliveryService;
        } else {
            return $this->deliveryService = new \deliveryServ($delivery_db,$delivery_email_db);
        }
    }





    function getUsers($id='')
    {
        $model = new \customersEmailList();
        if(!empty($id)){
            $id = implode(",", $id);
            $model = array($model->findFirst("id = '$id'")->toArray());
        } else {
            $model = $model->find()->toArray();
        }

        return $model;
    }


    public function SendForSelect($template, $users)
    {
        foreach($users as $user){

            $this->setToMail($user['email']);
            $this->setMailMessage($template->text);
            $this->setTitle($template->title);
            $this->SendEmail();
        }
    }

    /**
     * @param $template
     * @param $data
     * @param array $users
     */

    public function SendDelivery($template, $data, $users =array() )
    {
        $model = new \delivery();

        if(!$users) {

            $users = $this->getUsers($data['users_id']);

        }


        $this->setToMail($users,true);

        $this->setMailMessage($template['text']);
        $this->setTitle($template['title']);
        $campaign_id = $this->mailer->createCampaignDelivery($this->to_email,  $this->from_email, $this->from_name, $this->title, $this->mailMessage);


        $delivery = array(
            'campaign_id' => $campaign_id,
            'campaign'    => $data['utm_campaign'],
            'name'        => $data['name'],
            'event_id'    => $data['id'],
            'status'      => '0',
            'project_id'  => $data['project_id']
        );
        $model->save($delivery);
    }

}