Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TNotify.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library;

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Mail\MailerFactoryInterface;
use Joomla\CMS\Mail\MailTemplate;

class TNotify
{  
  	// send_email, notify_admin, notify_user, notify
	
	public $mailer;

    public function __construct()
    {
        $this->mailer = Factory::getContainer()->get(MailerFactoryInterface::class)->createMailer();
    }
    
    // 1. Send email - Supports Dynamic Content
  	public function send_email($email = [], $data = [])
    {
     	// $email - from_email, from_name, to_email, reply_to_email, subject, message, cc, bcc, attachments

        $dynamic = new TDynamic();
      
      	// Reply to email
      	if (isset($email['reply_to_email']) && $email['reply_to_email'])
        {
        	$email['reply_to_email'] = $dynamic->replace_site($email['reply_to_email']);
            $email['reply_to_email'] = $dynamic->replace_fields($email['reply_to_email'], $data);

            $this->mailer->addReplyTo($email['reply_to_email']);
        }
      
      	// From Name and Email
      	$email['from_email'] = $dynamic->replace_site($email['from_email']);
      	$email['from_name'] = $dynamic->replace_site($email['from_name']);
      
      	$this->mailer->setFrom($email['from_email'], $email['from_name']);
      
      	// Recipient (To)
      	$email['to_email'] = $dynamic->replace_site($email['to_email']);
        $email['to_email'] = $dynamic->replace_fields($email['to_email'], $data);
      
      	$this->mailer->addRecipient($email['to_email']);
      	
      	// Subject
      	$email['subject'] = $dynamic->replace_fields($email['subject'], $data);
      
	    $this->mailer->setSubject($email['subject']);
      
      	// Message
      	$email['message'] = $dynamic->replace_dates($email['message']);
      	$email['message'] = $dynamic->replace_site($email['message']);
      	$email['message'] = $dynamic->replace_fields($email['message'], $data);
      
      	$this->mailer->setBody($email['message']);

        // Attachments
        if (isset($email['attachments']) && $email['attachments'])
        {
            foreach ($email['attachments'] as $attachment)
            {
                if (file_exists($attachment))
                {
                    $this->mailer->addAttachment($attachment);
                }
            }
        }

        // Cc
        if (isset($email['cc']) && $email['cc'])
        {
            $this->mailer->addCc($email['cc']);
        }

        // Bcc
        if (isset($email['bcc']) && $email['bcc'])
        {
            $this->mailer->addCc($email['bcc']);
        }
	    
	    $this->mailer->isHtml(true);
        $this->mailer->Encoding = 'base64';
	    
	    $this->mailer->send();
    }
  
  	// 2. Send email notification to admin
    public function notify_admin($subject, $body, $options = [])
	{
	    // $body can be string, array or object
	    
	    if (\is_object($body))
	    {
	        $body = (array) $body;
	    }
		
		// Recipient
		if (isset($options['to_address']) && $options['to_address'])
		{
		    $this->mailer->addRecipient($options['to_address']);
		}
		else
		{
		    $config = Factory::getConfig();
	        $to_name = $config->get('fromname');
	        $to_address = $config->get('mailfrom');

		    $this->mailer->addRecipient($to_address, $to_name);
		}
	    
	    // Subject
	    $this->mailer->setSubject($subject);
	    
	    // Reply to email
	    if (isset($options['reply_to']) && $options['reply_to'])
	    {
	        $this->mailer->addReplyTo($options['reply_to']);
	    }
	    
	    // Body
	    $email_body = ''; 
	    if (\is_array($body))
	    {
	        foreach ($body as $k => $v)
	        {
	            $email_body .= '<p><strong>' . $k . '</strong>: ' . $v . '</p>';
	        }
	    }
	    else
	    {
	        $email_body = $body;
	    }
	    
	    $this->mailer->setBody($email_body);
	    
	    $this->mailer->isHtml(true);
        $this->mailer->Encoding = 'base64';
	    
	    $this->mailer->send();
	}
  
  	// 3. Send email using TF Mail Component
  	public function notify_user($user_id, $mail_id, $replaces = [], $date = '', $priority = '')
    {
    	if (!ComponentHelper::isEnabled('com_tfmail'))
        {
        	return;  
        }
      
      	$qModel = Factory::getApplication()->bootComponent('com_tfmail')->getMVCFactory()->createModel('Queue', 'Administrator', ['ignore_request' => true]);
      
      	$data = array(
          'user_id' => $user_id,
          'mail_id' => $mail_id,
          'replaces' => $replaces,
    	);
      
      	if (!empty($priority))
        {
        	$data['priority'] = $priority;
        }
      
      	if (!empty($date))
        {
        	$data['send_date'] = Factory::getDate($date)->toSql();
        }
		
      	$qModel->save($data);
      
      	return true;
    }

	// 4. Send email using mail template
	public static function notify($template_id)
	{
		$app = Factory::getApplication();

		$mailer = new MailTemplate($template_id, $app->getLanguage()->getTag());
		
		$mailer->addTemplateData($userdata);		
		$mailer->addRecipient($userdata['email']);
		
        /*
            addRecipient($mail, $name = null, $type = 'to') // cc, bcc
            setReplyTo($mail, $name = '')
            addAttachment($name, $file)
            addTemplateData($data, $plain = false)

            // static methods:
            getTemplate($key, $language)
            createTemplate($key, $subject, $body, $tags, $htmlbody = '')
            updateTemplate($key, $subject, $body, $tags, $htmlbody = '')
            deleteTemplate($key)
        */

		$return = $mailer->send();

		return $return;
	}
}