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

246 lines
5.9 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
* Copyright (C) DevCode s.n.c.
*
* 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\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
{
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 = parent::build();
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;
$value = $module->replacePlaceholders($this->id_record, $value);
}
$this->attributes['subject'] = $value;
}
public function setContentAttribute($value)
{
if (isset($this->template)) {
$module = $this->template->module;
$value = $module->replacePlaceholders($this->id_record, $value);
}
$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
if (!empty($template['reply_to'])) {
$this->options['reply_to'] = $template['reply_to'];
}
// 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
}
}
}