2018-12-01 22:40:16 +01:00
< ? php
/* Copyright ( C ) 2018 SuperAdmin
*
* 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 < http :// www . gnu . org / licenses />.
*/
/**
2019-08-03 19:01:22 +02:00
* \file sendrecurringinvoicebymail / class / actions_sendrecurringinvoicebymail . class . php
* \ingroup sendrecurringinvoicebymail
* \brief Hook overload for cron / afterCreationOfRecurringInvoice ()
2018-12-01 22:40:16 +01:00
*
* Put detailed description here .
*/
/**
2019-08-03 19:01:22 +02:00
* Class Actionssendrecurringinvoicebymail
2018-12-01 22:40:16 +01:00
*/
2019-08-03 19:01:22 +02:00
class Actionssendrecurringinvoicebymail
2018-12-01 22:40:16 +01:00
{
2018-12-02 00:33:14 +01:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-12-01 22:40:16 +01:00
2018-12-02 00:33:14 +01:00
/**
* @ var string Error code ( or message )
*/
public $error = '' ;
2018-12-01 22:40:16 +01:00
2018-12-02 00:33:14 +01:00
/**
* @ var array Errors
*/
public $errors = array ();
2018-12-01 22:40:16 +01:00
/**
* @ var array Hook results . Propagated to $hookmanager -> resArray for later reuse
*/
public $results = array ();
/**
* @ var string String displayed by executeHook () immediately after return
*/
public $resprints ;
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
/**
* Overloading the doActions function : replacing the parent ' s function with the one below
*
* @ param array $parameters Hook metadatas ( context , etc ... )
* @ param CommonObject $object The object to process ( an invoice if you are in invoice module , a propale in propale ' s module , etc ... )
* @ param string $action Current action ( if set ) . Generally create or edit or null
* @ param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @ return int < 0 on error , 0 on success , 1 to replace standard code
*/
2019-02-28 02:05:20 +01:00
public function afterCreationOfRecurringInvoice ( $parameters , & $object , & $action , $hookmanager )
2018-12-01 22:40:16 +01:00
{
global $conf , $user , $langs ;
2018-12-03 23:39:40 +01:00
$langs -> load ( 'mails' );
2018-12-01 22:40:16 +01:00
$error = 0 ; // Error counter
2018-12-02 01:18:02 +01:00
$facturerec = $parameters [ 'facturerec' ];
2021-02-20 00:32:15 +01:00
// We only send the mail when the invoice is not a draft
2018-12-03 23:39:40 +01:00
if ( $object -> brouillon ) {
return 0 ;
2018-12-01 22:40:16 +01:00
}
2021-02-20 00:32:15 +01:00
// Fetch the mail template
2018-12-02 01:18:02 +01:00
// (pas très précise mais je commence à en avoir marre de creuser tout dolibarr pour trouver les bonnes fonctions...)
2019-08-03 19:01:22 +02:00
$result = $this -> db -> query ( " SELECT * FROM " . MAIN_DB_PREFIX . " c_email_templates WHERE module = 'sendrecurringinvoicebymail' AND active = 1 AND enabled = '1' ORDER BY tms DESC LIMIT 1 " );
2018-12-02 01:18:02 +01:00
if ( ! $result or ! ( $template = $this -> db -> fetch_object ( $result ))) {
2019-08-03 19:01:22 +02:00
$this -> error = " Can't find mail template for sendrecurringinvoicebymail " ;
2018-12-03 23:39:40 +01:00
$this -> errors [] = $this -> error ;
$error ++ ;
return - 1 ;
2018-12-02 01:18:02 +01:00
}
2021-02-20 00:32:15 +01:00
// Prepare the substitions for mail's subject and body
2018-12-03 23:39:40 +01:00
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , null , $object );
complete_substitutions_array ( $substitutionarray , $langs , $object ); // lourd et n'a rien ajouté lors de mes tests
2021-02-20 00:32:15 +01:00
// Adding some useful substitions of our own...
2018-12-03 23:39:40 +01:00
if ( ! empty ( $object -> linkedObjects [ 'contrat' ])) {
2021-02-20 00:32:15 +01:00
$contrat = reset ( $object -> linkedObjects [ 'contrat' ]); // no deep search, we take the first linked contract
2018-12-02 01:18:02 +01:00
$substitutionarray [ '__CONTRACT_REF__' ] = $contrat -> ref ;
}
2021-02-20 00:32:15 +01:00
// Initialisations
$mail_data = array (
'sendto' => $object -> thirdparty -> name . ' <' . $object -> thirdparty -> email . '>' ,
'from' => $conf -> global -> MAIN_MAIL_EMAIL_FROM ,
'errorsTo' => $conf -> global -> MAIN_MAIL_ERRORS_TO ,
'replyTo' => $conf -> global -> MAIN_MAIL_ERRORS_TO ,
'subject' => $template -> topic ,
'body' => $template -> content ,
);
// If the invoice has some custom parameters (subject, body, sendto, ...)
$mail_data = array_merge ( $mail_data , $this -> getCustomFieldsMail ( $object ));
// Check that we have a recipient, to avoid some frequent error...
if ( empty ( $mail_data [ 'sendto' ])) {
dol_syslog ( " Empty recipient for thirdparty " . $object -> thirdparty -> id . " . Not sending facturerec " . $facturerec -> ref . " (id: " . $facturerec -> id . " ). " );
return 0 ;
}
// Make the substitutions
foreach ( array ( 'subject' , 'body' ) as $key ) {
$mail_data [ $key ] = make_substitutions ( $mail_data [ $key ], $substitutionarray , $langs );
if ( method_exists ( $object , 'makeSubstitution' )) {
$mail_data [ $key ] = $object -> makeSubstitution ( $mail_data [ $key ]);
}
2018-12-03 23:39:40 +01:00
}
2018-12-02 01:18:02 +01:00
2021-02-20 00:32:15 +01:00
// Check if we have to attach the file
2018-12-02 01:18:02 +01:00
$filePath = array ();
$fileMime = array ();
$fileName = array ();
if ( $template -> joinfiles ) {
2018-12-03 23:39:40 +01:00
$filePath = array ( DOL_DATA_ROOT . '/' . $object -> last_main_doc );
2018-12-02 01:18:02 +01:00
$fileMime = array ( 'application/pdf' ); // FIXME: à rendre dynamique, même si ce sera toujours du PDF ?
2018-12-03 23:39:40 +01:00
$fileName = array ( basename ( $object -> last_main_doc ));
2018-12-02 01:18:02 +01:00
}
2018-12-01 22:40:16 +01:00
2021-02-20 00:32:15 +01:00
// At last, send the mail
2018-12-02 01:18:02 +01:00
$mailfile = new CMailFile (
2021-02-20 00:32:15 +01:00
$mail_data [ 'subject' ],
$mail_data [ 'sendto' ],
$mail_data [ 'from' ],
$mail_data [ 'body' ],
2018-12-02 01:18:02 +01:00
$filePath ,
$fileMime ,
$fileName ,
'' , // CC
'' , // BCC
0 , //deliveryreceipt
0 , //msgishtml
2021-02-20 00:32:15 +01:00
$mail_data [ 'errorsTo' ],
2018-12-02 01:18:02 +01:00
'' , // css
'' , // trackid
'' , // moreinheader
'standard' , // sendcontext
2021-02-20 00:32:15 +01:00
$mail_data [ 'replyTo' ]);
2018-12-03 23:39:40 +01:00
if ( $mailfile -> sendfile ()) {
dol_syslog ( " Success sending email for " . $facturerec -> ref . " (id: " . $facturerec -> id . " ). " );
// Adds info to object for trigger
// (maybe make a copy of the object instead of modifying it directly ?)
$object -> email_msgid = $mailfile -> msgid ;
2021-02-20 00:32:15 +01:00
$object -> email_from = $mail_data [ 'from' ];
$object -> email_subject = $mail_data [ 'subject' ];
$object -> email_to = $mail_data [ 'sendto' ];
2018-12-03 23:39:40 +01:00
//$object->email_tocc = $sendtocc;
//$object->email_tobcc = $sendtobcc;
$object -> actiontypecode = 'AC_OTH_AUTO' ;
2021-02-20 00:32:15 +01:00
$object -> actionmsg2 = $langs -> transnoentities ( 'MailSentBy' ) . ' ' . CMailFile :: getValidAddress ( $mail_data [ 'from' ], 4 , 0 , 1 ) . ' ' . $langs -> transnoentities ( 'To' ) . ' ' . CMailFile :: getValidAddress ( $mail_data [ 'sendto' ], 4 , 0 , 1 );
$object -> actionmsg = $langs -> transnoentities ( 'MailFrom' ) . ': ' . dol_escape_htmltag ( $mail_data [ 'from' ]);
$object -> actionmsg = dol_concatdesc ( $object -> actionmsg , $langs -> transnoentities ( 'MailTo' ) . ': ' . dol_escape_htmltag ( $mail_data [ 'sendto' ]));
$object -> actionmsg = dol_concatdesc ( $object -> actionmsg , $langs -> transnoentities ( 'MailTopic' ) . " : " . $mail_data [ 'subject' ]);
2018-12-03 23:39:40 +01:00
$object -> actionmsg = dol_concatdesc ( $object -> actionmsg , $langs -> transnoentities ( 'TextUsedInTheMessageBody' ) . " : " );
2021-02-20 00:32:15 +01:00
$object -> actionmsg = dol_concatdesc ( $object -> actionmsg , $mail_data [ 'body' ]);
2018-12-03 23:39:40 +01:00
// Launch triggers
$interface = new Interfaces ( $this -> db );
$resultTrigger = $interface -> run_triggers ( 'BILL_SENTBYMAIL' , $object , $user , $langs , $conf );
} else {
$this -> error = " Error sending email for " . $facturerec -> ref . " (id: " . $facturerec -> id . " ). " ;
$this -> errors [] = $this -> error ;
dol_syslog ( $this -> error );
$error ++ ;
}
2018-12-02 01:18:02 +01:00
2018-12-03 23:39:40 +01:00
return ( $error ? - 1 : 0 );
2018-12-01 22:40:16 +01:00
}
2018-12-03 23:39:40 +01:00
2021-02-20 00:32:15 +01:00
/**
* FIXME : For the time being , we abuse of note_private to store our customizations
*/
public function getCustomFieldsMail ( $object )
{
return $this -> parseCustomFieldsMail ( $object -> note_private );
}
/**
* FIXME : For the time being , we abuse of note_private to store our customizations
*
* This expect something like this in note_private :
* This is a good client ... ( other private infos )
* %%% sendrecurringinvoicebymail :: subject
* New invoice __REF__
* %%%
* %%% sendrecurringinvoicebymail :: sendto
* recipient1 @ example . org , recipient2 @ example . org
* %%%
* %%% sendrecurringinvoicebymail :: body
* Hello dear client ,
* Please find attached ...
* %%%
*/
public function parseCustomFieldsMail ( $data )
{
$output = [];
// Remove eventual windows' "\r"
$data = str_replace ( " \r " , " " , $data );
$regexps = array (
'subject' => '/(^|\n)%%% sendrecurringinvoicebymail::subject\n(?<subject>.*)%%%(\n|$)/sU' ,
'body' => '/(^|\n)%%% sendrecurringinvoicebymail::body\n(?<body>.*)%%%(\n|$)/sU' ,
'sendto' => '/(^|\n)%%% sendrecurringinvoicebymail::sendto\n(?<sendto>.*)%%%(\n|$)/sU' ,
);
foreach ( $regexps as $key => $r ) {
$result_regexp = [];
if ( preg_match_all ( $r , $data , $result_regexp )) {
$output [ $key ] = trim ( $result_regexp [ $key ][ 0 ]);
}
}
return $output ;
}
2018-12-01 22:40:16 +01:00
}