Proyectos de Subversion LeadersLinked - Services

Rev

Rev 182 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Library;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use LeadersLinked\Model\EmailTemplate;
10
use LeadersLinked\Model\Email;
11
use LeadersLinked\Mapper\EmailMapper;
12
 
13
class QueueEmail
14
{
15
 
16
    /**
17
     *
18
     * @var AdapterInterface
19
     */
20
    private $adapter;
21
 
22
 
23
    /**
24
     *
25
     * @param AdapterInterface $adapter
26
     */
27
    public function __construct($adapter)
28
    {
29
        $this->adapter = $adapter;
30
    }
31
 
32
    /**
33
     *
34
     * @param EmailTemplate $emailTemplate
35
     * @param array $arrayCont
36
     * @param string $to_address
37
     * @param string $to_name
38
     * @return boolean
39
     */
40
    public function processEmailTemplate($emailTemplate, $arrayCont, $to_address, $to_name)
41
    {
42
        $subject = trim(stripslashes($emailTemplate->subject));
43
        $message = trim(stripslashes($emailTemplate->content));
44
 
45
        foreach($arrayCont as $key => $val)
46
        {
156 efrain 47
            if('link' == $key) {
48
                $from_hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
182 efrain 49
                $from_hostname = str_replace('https://', '', $from_hostname);
50
 
51
 
156 efrain 52
                $to_hostname = empty($_SERVER['HTTP_ORIGIN']) ? '' : $_SERVER['HTTP_ORIGIN'];
182 efrain 53
 
156 efrain 54
 
55
                if(empty($to_hostname)) {
56
 
57
                    $to_hostname = empty($_SERVER['HTTP_REFERER']) ?  '' : $_SERVER['HTTP_REFERER'];
58
 
59
                }
182 efrain 60
                $to_hostname = str_replace('https://', '', $to_hostname);
156 efrain 61
 
62
                if($from_hostname && $to_hostname) {
63
                    $val = str_replace($from_hostname, $to_hostname, $val);
64
                }
65
 
66
 
182 efrain 67
 
156 efrain 68
            }
69
 
1 efrain 70
            $message = str_replace('[' . $key .']', $val, $message);
71
            $subject = str_replace('[' . $key .']', $val, $subject);
72
        }
73
 
74
        $email = new Email();
75
        $email->content = json_encode([
76
            'to_address' => $to_address,
77
            'to_name' => $to_name,
78
            'cc' => [],
79
            'bcc' => [],
80
            'subject' => $subject,
81
            'message' => $message,
82
        ]);
83
        $email->status = Email::STATUS_PENDING;
84
        $email->tried = 0;
85
 
86
        $emailMapper = EmailMapper::getInstance($this->adapter);
87
        return $emailMapper->insert($email);
88
    }
89
}