1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-14 01:15:16 +01:00
openstamanager/modules/emails/src/Mail.php

259 lines
6.4 KiB
PHP
Raw Normal View History

2019-08-26 18:02:05 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-08-26 18:02:05 +02:00
2019-08-29 10:25:14 +02:00
namespace Modules\Emails;
2019-08-26 18:02:05 +02:00
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
2019-08-29 10:25:14 +02:00
use Models\PrintTemplate;
use Models\Upload;
use Models\User;
2019-08-28 16:58:47 +02:00
use Modules\Newsletter\Newsletter;
2019-08-26 18:02:05 +02:00
class Mail extends Model
{
use SimpleModelTrait;
2019-08-26 18:02:05 +02:00
protected $table = 'em_emails';
2019-08-27 15:42:13 +02:00
protected $options = null;
2019-08-26 18:02:05 +02:00
2019-08-27 15:42:13 +02:00
public static function build(User $user, $template = null, $id_record = null, $account = null)
2019-08-26 18:02:05 +02:00
{
$model = new static();
2019-08-26 18:02:05 +02:00
2019-08-27 15:42:13 +02:00
$model->created_by = $user->id;
2019-08-29 10:25:14 +02:00
if (!empty($template)) {
$model->id_template = $template->id;
$model->id_account = $template->account->id;
}
2019-08-26 18:02:05 +02:00
$model->id_record = $id_record;
2019-08-29 10:25:14 +02:00
$model->save();
2019-08-26 18:02:05 +02:00
if (!empty($template)) {
$model->resetFromTemplate();
}
if (!empty($account)) {
$model->id_account = $account->id;
}
$model->save();
return $model;
}
/**
* Aggiunge un allegato del gestionale alla notifica.
*
* @param string $file_id
*/
2019-08-29 10:25:14 +02:00
public function addUpload($file_id, $name = null)
2019-08-26 18:02:05 +02:00
{
2019-08-29 10:25:14 +02:00
$this->uploads()->attach($file_id, ['id_email' => $this->id, 'name' => $name]);
2019-08-26 18:02:05 +02:00
}
/**
* Aggiunge una stampa alla notifica.
*
* @param string|int $print
* @param string $name
*/
public function addPrint($print_id, $name = null)
{
2019-08-29 10:25:14 +02:00
$this->prints()->attach($print_id, ['id_email' => $this->id, 'name' => $name]);
2019-08-26 18:02:05 +02:00
}
public function resetPrints()
{
2020-03-31 20:31:03 +02:00
$this->prints()->detach();
2019-08-26 18:02:05 +02:00
}
/**
* Aggiunge un destinatario alla notifica.
*
* @param string $value
* @param string $type
*/
public function addReceiver($value, $type = null)
{
if (empty($value)) {
return;
}
$list = explode(';', $value);
2019-08-29 10:25:14 +02:00
foreach ($list as $address) {
if (!empty($address)) {
$receiver = Receiver::build($this, $address, $type);
}
2019-08-26 18:02:05 +02:00
}
}
public function save(array $options = [])
{
2019-08-27 15:42:13 +02:00
if (isset($this->options)) {
$this->setOptionsAttribute($this->options);
}
2019-08-26 18:02:05 +02:00
2019-08-28 16:58:47 +02:00
$newsletter = $this->newsletter;
if (!empty($newsletter)) {
$newsletter->fixStato();
}
2019-08-26 18:02:05 +02:00
return parent::save($options);
}
// Attributi Eloquent
public function setOptionsAttribute($value)
{
$this->attributes['options'] = json_encode($value);
}
public function getOptionsAttribute()
{
return json_decode($this->attributes['options'], true);
}
/**
* Restituisce il titolo della notifica.
*
* @return bool
*/
public function getReadNotifyAttribute()
{
return $this->options['read-notify'];
}
2019-08-29 10:25:14 +02:00
/**
* Imposta il titolo della notifica.
*
* @param bool $value
*/
public function setReadNotifyAttribute($value)
{
$this->options['read-notify'] = boolval($value);
}
public function setSubjectAttribute($value)
2019-08-27 17:25:52 +02:00
{
if (isset($this->template)) {
$module = $this->template->module;
2021-01-28 21:13:48 +01:00
$value = $module->replacePlaceholders($this->id_record, $value, ['is_pec' => intval($this->account->pec)]);
2019-08-27 17:25:52 +02:00
}
$this->attributes['subject'] = $value;
}
public function setContentAttribute($value)
{
if (isset($this->template)) {
$module = $this->template->module;
2021-01-28 21:13:48 +01:00
$value = $module->replacePlaceholders($this->id_record, $value, ['is_pec' => intval($this->account->pec)]);
2019-08-27 17:25:52 +02:00
}
$this->attributes['content'] = $value;
}
2019-08-26 18:02:05 +02:00
/* Relazioni Eloquent */
public function account()
{
2019-08-29 10:25:14 +02:00
return $this->belongsTo(Account::class, 'id_account')->withTrashed();
2019-08-26 18:02:05 +02:00
}
public function template()
{
2019-08-29 10:25:14 +02:00
return $this->belongsTo(Template::class, 'id_template')->withTrashed();
2019-08-26 18:02:05 +02:00
}
2019-08-27 17:25:52 +02:00
public function user()
{
return $this->belongsTo(User::class, 'created_by');
}
2019-08-28 16:58:47 +02:00
public function newsletter()
{
2019-08-29 10:25:14 +02:00
return $this->belongsTo(Newsletter::class, 'id_newsletter');
}
public function receivers()
{
return $this->hasMany(Receiver::class, 'id_email');
}
public function uploads()
{
return $this->belongsToMany(Upload::class, 'em_email_upload', 'id_email', 'id_file')->withPivot('name');
}
public function prints()
{
return $this->belongsToMany(PrintTemplate::class, 'em_email_print', 'id_email', 'id_print')->withPivot('name');
2019-08-28 16:58:47 +02:00
}
2019-08-26 18:02:05 +02:00
protected function resetFromTemplate()
{
$template = $this->template;
$this->id_account = $template->account->id;
$this->read_notify = $template->read_notify;
// Contentuto e oggetto
2019-08-27 17:25:52 +02:00
$this->content = $template->body;
$this->subject = $template->subject;
2019-08-26 18:02:05 +02:00
// Reply To
2023-07-05 15:16:22 +02:00
if (!empty($template['tipo_reply_to'])) {
$reply_to = '';
if ($template['tipo_reply_to'] == 'email_fissa') {
$reply_to = $template['reply_to'];
} else {
$user = \Auth::user();
$reply_to = $user->email;
}
if (!empty($reply_to)) {
$this->options['reply_to'] = $reply_to;
}
2019-08-26 18:02:05 +02:00
}
// CC
if (!empty($template['cc'])) {
$this->addReceiver($template['cc'], 'cc');
}
// BCC
if (!empty($template['bcc'])) {
$this->addReceiver($template['bcc'], 'bcc');
}
// Incluesione stampe predefinite
2019-08-29 10:25:14 +02:00
$prints = $template->prints;
2019-08-26 18:02:05 +02:00
foreach ($prints as $print) {
2019-08-29 10:25:14 +02:00
$this->addPrint($print['id']);
2019-08-26 18:02:05 +02:00
}
}
}