openstamanager/src/Notifications/EmailNotification.php

246 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace Notifications;
use Mail;
use Prints;
class EmailNotification extends Notification
{
2018-09-20 14:41:01 +02:00
protected $subject = null;
protected $readNotify = false;
protected $template = null;
protected $account = null;
protected $attachments = null;
/**
* Restituisce l'account email della notifica.
*
* @return array
*/
public function getAccount()
{
return Mail::get($this->account);
}
/**
* Imposta l'account email della notifica.
*
* @param string|int $value
*/
public function setAccount($value)
{
$this->account = $value;
}
/**
* Restituisce il template della notifica.
*
* @return array
*/
public function getTemplate()
{
return Mail::getTemplate($this->template);
}
/**
* Imposta il template della notifica.
*
* @param string|int $value
2018-09-25 16:47:44 +02:00
* @param int $id_record
*/
2018-09-20 14:41:01 +02:00
public function setTemplate($value, $id_record = null)
{
$this->template = $value;
$template = $this->getTemplate();
2018-09-20 14:41:01 +02:00
$this->setReadNotify($template['read_notify']);
$this->setAccount($template['id_smtp']);
2018-09-20 14:41:01 +02:00
if (!empty($id_record)) {
$variables = Mail::getTemplateVariables($template['id'], $id_record);
// Sostituzione delle variabili di base
$replaces = [];
foreach ($variables as $key => $value) {
$replaces['{'.$key.'}'] = $value;
}
$body = replace($template['body'], $replaces);
$subject = replace($template['subject'], $replaces);
$this->setContent($body);
$this->setSubject($subject);
}
}
/**
* Include le stampe selezionate dal template.
*
* @param int $id_record
*/
public function includeTemplatePrints($id_record)
{
$template = $this->getTemplate();
$prints = $dbo->fetchArray('SELECT id_print FROM zz_email_print WHERE id_email = '.prepare($template['id']));
foreach ($prints as $print) {
$this->addPrint($print['id_print'], $id_record);
2018-09-25 16:47:44 +02:00
}
}
/**
* Restituisce gli allegati della notifica.
*
* @return array
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* Imposta gli allegati della notifica.
*
* @param array $values
*/
public function setAttachments(array $values)
{
$this->attachments = [];
foreach ($values as $value) {
$path = is_array($value) ? $value['path'] : $value;
$name = is_array($value) ? $value['name'] : null;
$this->addAttachment($path, $name);
}
}
/**
* Aggiunge un allegato alla notifica.
*
* @param string $value
*/
public function addAttachment($path, $name = null)
{
$this->attachments[] = [
'path' => $path,
'name' => $name ?: basename($path),
];
}
/**
* Aggiunge una stampa alla notifica.
*
* @param string|int $print
2018-09-25 16:47:44 +02:00
* @param int $id_record
* @param string $name
*/
public function addPrint($print, $id_record, $name = null)
{
$print = Prints::get($print);
if (empty($name)) {
$name = $print['title'].'.pdf';
}
// Utilizzo di una cartella particolare per il salvataggio temporaneo degli allegati
$path = DOCROOT.'/files/notifications/'.$print['title'].' - '.$id_record.'.pdf';
Prints::render($print['id'], $id_record, $path);
$this->addAttachment($path, $name);
}
/**
* Restituisce il titolo della notifica.
*
* @return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* Imposta il titolo della notifica.
*
* @param string $value
*/
public function setSubject($value)
{
$this->subject = $value;
}
2018-09-20 14:41:01 +02:00
/**
* Restituisce il titolo della notifica.
*
* @return bool
*/
public function getReadNotify()
{
return $this->readNotify;
}
/**
* Imposta il titolo della notifica.
*
* @param bool $value
*/
public function setReadNotify($value)
{
$this->readNotify = boolval($value);
}
/**
2018-11-12 18:49:51 +01:00
* Aggiunge un destinatario alla notifica.
2018-09-20 14:41:01 +02:00
*
* @param string $value
* @param string $type
*/
public function addReceiver($value, $type = null)
{
$this->receivers[] = [
'email' => $value,
'type' => $type,
];
}
public function send($exceptions = false)
{
$account = $this->getAccount();
2018-09-20 14:41:01 +02:00
$mail = new Mail($account['id'], $exceptions);
// Template
$template = $this->getTemplate();
if (!empty($template)) {
$mail->setTemplate($template);
}
// Destinatari
2018-09-20 14:41:01 +02:00
$receivers = $this->getReceivers();
foreach ($receivers as $receiver) {
$mail->addReceiver($receiver['email'], $receiver['type']);
}
// Allegati
$attachments = $this->getAttachments();
foreach ($attachments as $attachment) {
2018-09-20 14:41:01 +02:00
$mail->AddAttachment($attachment['path'], $attachment['name']);
}
// Conferma di lettura
if (!empty($this->getReadNotify())) {
$mail->ConfirmReadingTo = $mail->From;
}
// Oggetto
$mail->Subject = $this->getSubject();
// Contenuto
$mail->Body = $this->getContent();
return $mail->send();
}
}