2004-10-20 23:06:45 +02:00
< ? php
2005-03-15 21:25:19 +01:00
/* Copyright ( C ) 2003 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2011-09-03 02:14:27 +02:00
* Copyright ( C ) 2004 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2014-11-06 11:20:47 +01:00
* Copyright ( C ) 2014 Juanjo Menent < jmenent @ 2 byte . es >
2018-09-29 14:51:52 +02:00
* Copyright ( C ) 2018 Philippe Grand < philippe . grand @ atoo - net . com >
2021-03-19 11:50:15 +01:00
* Copyright ( C ) 2021 Thibault FOUCART < support @ ptibogxiv . net >
2003-06-30 19:33:53 +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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2003-06-30 19:33:53 +02:00
* ( 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2003-06-30 19:33:53 +02:00
*/
2005-03-15 21:25:19 +01:00
/**
2010-11-22 10:18:53 +01:00
* \file htdocs / core / class / notify . class . php
2011-06-19 16:35:16 +02:00
* \ingroup notification
* \brief File of class to manage notifications
2010-11-22 10:18:53 +01:00
*/
2019-12-03 11:17:29 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2005-01-09 19:30:34 +01:00
2005-07-14 15:03:20 +02:00
/**
2012-01-27 16:12:11 +01:00
* Class to manage notifications
2010-11-22 10:18:53 +01:00
*/
2005-01-09 19:30:34 +01:00
class Notify
2003-06-30 19:33:53 +02:00
{
2018-10-01 17:14:28 +02:00
/**
* @ var int ID
*/
public $id ;
/**
2020-10-31 14:32:18 +01:00
* @ var DoliDB Database handler .
*/
public $db ;
2018-10-01 17:14:28 +02:00
/**
* @ var string Error code ( or message )
*/
2019-12-03 11:17:29 +01:00
public $error = '' ;
2018-10-01 17:14:28 +02:00
/**
* @ var string [] Error codes ( or messages )
*/
public $errors = array ();
2017-10-07 13:09:31 +02:00
2018-10-06 12:34:51 +02:00
public $author ;
public $ref ;
public $date ;
public $duree ;
public $note ;
2018-10-01 17:14:28 +02:00
/**
2020-10-31 14:32:18 +01:00
* @ var int Project ID
*/
public $fk_project ;
2005-07-14 15:03:20 +02:00
2009-06-06 04:50:59 +02:00
// Les codes actions sont definis dans la table llx_notify_def
2005-07-14 15:03:20 +02:00
2017-10-07 13:09:31 +02:00
// codes actions supported are
2019-12-11 19:11:13 +01:00
// @todo defined also into interface_50_modNotificiation_Notificiation.class.php
2017-10-07 13:09:31 +02:00
public $arrayofnotifsupported = array (
'BILL_VALIDATE' ,
'BILL_PAYED' ,
'ORDER_VALIDATE' ,
'PROPAL_VALIDATE' ,
'PROPAL_CLOSE_SIGNED' ,
'FICHINTER_VALIDATE' ,
'FICHINTER_ADD_CONTACT' ,
'ORDER_SUPPLIER_VALIDATE' ,
'ORDER_SUPPLIER_APPROVE' ,
'ORDER_SUPPLIER_REFUSE' ,
2018-10-25 12:45:13 +02:00
'SHIPPING_VALIDATE' ,
'EXPENSE_REPORT_VALIDATE' ,
'EXPENSE_REPORT_APPROVE' ,
2021-03-19 11:53:06 +01:00
'HOLIDAY_VALIDATE' ,
'HOLIDAY_APPROVE' ,
2021-02-23 22:03:23 +01:00
'ACTION_CREATE'
2017-10-07 13:09:31 +02:00
);
/**
2011-09-03 02:14:27 +02:00
* Constructor
*
2011-11-06 15:12:18 +01:00
* @ param DoliDB $db Database handler
2017-10-07 13:09:31 +02:00
*/
2019-02-26 22:42:19 +01:00
public function __construct ( $db )
2017-10-07 13:09:31 +02:00
{
$this -> db = $db ;
}
/**
* Return message that say how many notification ( and to which email ) will occurs on requested event .
* This is to show confirmation messages before event is recorded .
*
* @ param string $action Id of action in llx_c_action_trigger
* @ param int $socid Id of third party
* @ param Object $object Object the notification is about
* @ return string Message
*/
2019-02-26 22:42:19 +01:00
public function confirmMessage ( $action , $socid , $object )
2006-08-05 18:44:43 +02:00
{
global $langs ;
$langs -> load ( " mails " );
2009-06-30 13:59:46 +02:00
2019-12-03 11:17:29 +01:00
$listofnotiftodo = $this -> getNotificationsArray ( $action , $socid , $object , 0 );
2017-10-06 14:05:13 +02:00
2020-12-24 02:59:00 +01:00
$texte = '' ;
2019-12-03 11:17:29 +01:00
$nb = - 1 ;
2020-12-20 11:04:21 +01:00
if ( is_array ( $listofnotiftodo )) {
$nb = count ( $listofnotiftodo );
}
if ( $nb < 0 ) {
$texte = img_object ( $langs -> trans ( " Notifications " ), 'email' ) . ' ' . $langs -> trans ( " ErrorFailedToGetListOfNotificationsToSend " );
} elseif ( $nb == 0 ) {
$texte = img_object ( $langs -> trans ( " Notifications " ), 'email' ) . ' ' . $langs -> trans ( " NoNotificationsWillBeSent " );
} elseif ( $nb == 1 ) {
$texte = img_object ( $langs -> trans ( " Notifications " ), 'email' ) . ' ' . $langs -> trans ( " ANotificationsWillBeSent " );
} elseif ( $nb >= 2 ) {
$texte = img_object ( $langs -> trans ( " Notifications " ), 'email' ) . ' ' . $langs -> trans ( " SomeNotificationsWillBeSent " , $nb );
}
if ( is_array ( $listofnotiftodo )) {
2019-12-03 11:17:29 +01:00
$i = 0 ;
2020-12-28 12:38:33 +01:00
foreach ( $listofnotiftodo as $val ) {
2020-12-20 11:04:21 +01:00
if ( $i ) {
$texte .= ', ' ;
} else {
$texte .= ' (' ;
}
if ( $val [ 'isemailvalid' ]) {
$texte .= $val [ 'email' ];
} else {
$texte .= $val [ 'emaildesc' ];
}
2017-10-07 13:09:31 +02:00
$i ++ ;
}
2020-12-20 11:04:21 +01:00
if ( $i ) {
$texte .= ')' ;
}
}
2017-10-06 14:05:13 +02:00
2006-08-05 18:44:43 +02:00
return $texte ;
}
2009-06-30 13:59:46 +02:00
2017-10-07 13:09:31 +02:00
/**
* Return number of notifications activated for action code ( and third party )
*
* @ param string $notifcode Code of action in llx_c_action_trigger ( new usage ) or Id of action in llx_c_action_trigger ( old usage )
* @ param int $socid Id of third party or 0 for all thirdparties or - 1 for no thirdparties
* @ param Object $object Object the notification is about ( need it to check threshold value of some notifications )
* @ param int $userid Id of user or 0 for all users or - 1 for no users
* @ param array $scope Scope where to search
* @ return array | int < 0 if KO , array of notifications to send if OK
*/
2019-02-26 22:42:19 +01:00
public function getNotificationsArray ( $notifcode , $socid = 0 , $object = null , $userid = 0 , $scope = array ( 'thirdparty' , 'user' , 'global' ))
2006-08-05 18:44:43 +02:00
{
2015-04-13 17:04:18 +02:00
global $conf , $user ;
2011-06-19 16:35:16 +02:00
2019-12-03 11:17:29 +01:00
$error = 0 ;
$resarray = array ();
2017-10-07 13:09:31 +02:00
$valueforthreshold = 0 ;
2020-12-20 11:04:21 +01:00
if ( is_object ( $object )) {
$valueforthreshold = $object -> total_ht ;
}
2020-12-24 02:59:00 +01:00
$sqlnotifcode = '' ;
2020-12-20 11:04:21 +01:00
if ( $notifcode ) {
if ( is_numeric ( $notifcode )) {
2021-06-09 15:36:47 +02:00
$sqlnotifcode = " AND n.fk_action = " . (( int ) $notifcode ); // Old usage
2020-12-20 11:04:21 +01:00
} else {
$sqlnotifcode = " AND a.code = ' " . $this -> db -> escape ( $notifcode ) . " ' " ; // New usage
}
}
2014-10-25 00:42:52 +02:00
2020-12-20 11:04:21 +01:00
if ( ! $error ) {
if ( $socid >= 0 && in_array ( 'thirdparty' , $scope )) {
2017-10-07 13:09:31 +02:00
$sql = " SELECT a.code, c.email, c.rowid " ;
2019-12-03 11:17:29 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " notify_def as n, " ;
$sql .= " " . MAIN_DB_PREFIX . " socpeople as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_action_trigger as a, " ;
$sql .= " " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " WHERE n.fk_contact = c.rowid " ;
$sql .= " AND a.rowid = n.fk_action " ;
$sql .= " AND n.fk_soc = s.rowid " ;
2020-12-20 11:04:21 +01:00
$sql .= $sqlnotifcode ;
2019-12-03 11:17:29 +01:00
$sql .= " AND s.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2020-12-20 11:04:21 +01:00
if ( $socid > 0 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND s.rowid = " . (( int ) $socid );
2020-12-20 11:04:21 +01:00
}
2017-10-07 13:09:31 +02:00
dol_syslog ( __METHOD__ . " " . $notifcode . " , " . $socid . " " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2020-12-20 11:04:21 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
2019-12-03 11:17:29 +01:00
$i = 0 ;
2020-12-20 11:04:21 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-12-20 11:04:21 +01:00
if ( $obj ) {
2019-12-03 11:17:29 +01:00
$newval2 = trim ( $obj -> email );
$isvalid = isValidEmail ( $newval2 );
2020-12-20 11:04:21 +01:00
if ( empty ( $resarray [ $newval2 ])) {
$resarray [ $newval2 ] = array ( 'type' => 'tocontact' , 'code' => trim ( $obj -> code ), 'emaildesc' => 'Contact id ' . $obj -> rowid , 'email' => $newval2 , 'contactid' => $obj -> rowid , 'isemailvalid' => $isvalid );
}
2017-10-07 13:09:31 +02:00
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-12-03 11:17:29 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
}
}
2020-12-20 11:04:21 +01:00
if ( ! $error ) {
if ( $userid >= 0 && in_array ( 'user' , $scope )) {
2017-10-07 13:09:31 +02:00
$sql = " SELECT a.code, c.email, c.rowid " ;
2019-12-03 11:17:29 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " notify_def as n, " ;
$sql .= " " . MAIN_DB_PREFIX . " user as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_action_trigger as a " ;
$sql .= " WHERE n.fk_user = c.rowid " ;
$sql .= " AND a.rowid = n.fk_action " ;
2020-12-20 11:04:21 +01:00
$sql .= $sqlnotifcode ;
2019-12-03 11:17:29 +01:00
$sql .= " AND c.entity IN ( " . getEntity ( 'user' ) . " ) " ;
2020-12-20 11:04:21 +01:00
if ( $userid > 0 ) {
2021-06-09 15:36:47 +02:00
$sql .= " AND c.rowid = " . (( int ) $userid );
2020-12-20 11:04:21 +01:00
}
2017-10-07 13:09:31 +02:00
dol_syslog ( __METHOD__ . " " . $notifcode . " , " . $socid . " " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
2020-12-20 11:04:21 +01:00
if ( $resql ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $resql );
2019-12-03 11:17:29 +01:00
$i = 0 ;
2020-12-20 11:04:21 +01:00
while ( $i < $num ) {
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2020-12-20 11:04:21 +01:00
if ( $obj ) {
2019-12-03 11:17:29 +01:00
$newval2 = trim ( $obj -> email );
$isvalid = isValidEmail ( $newval2 );
2020-12-20 11:04:21 +01:00
if ( empty ( $resarray [ $newval2 ])) {
$resarray [ $newval2 ] = array ( 'type' => 'touser' , 'code' => trim ( $obj -> code ), 'emaildesc' => 'User id ' . $obj -> rowid , 'email' => $newval2 , 'userid' => $obj -> rowid , 'isemailvalid' => $isvalid );
}
2017-10-07 13:09:31 +02:00
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-12-03 11:17:29 +01:00
$this -> error = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
}
}
}
2020-12-20 11:04:21 +01:00
if ( ! $error ) {
if ( in_array ( 'global' , $scope )) {
2017-10-07 13:09:31 +02:00
// List of notifications enabled for fixed email
2020-12-20 11:04:21 +01:00
foreach ( $conf -> global as $key => $val ) {
if ( $notifcode ) {
if ( $val == '' || ! preg_match ( '/^NOTIFICATION_FIXEDEMAIL_' . $notifcode . '_THRESHOLD_HIGHER_(.*)$/' , $key , $reg )) {
continue ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-12-20 11:04:21 +01:00
if ( $val == '' || ! preg_match ( '/^NOTIFICATION_FIXEDEMAIL_.*_THRESHOLD_HIGHER_(.*)$/' , $key , $reg )) {
continue ;
}
2017-10-07 13:09:31 +02:00
}
$threshold = ( float ) $reg [ 1 ];
2020-12-20 11:04:21 +01:00
if ( $valueforthreshold < $threshold ) {
continue ;
}
2017-10-07 13:09:31 +02:00
2019-12-03 11:17:29 +01:00
$tmpemail = explode ( ',' , $val );
2020-12-20 11:04:21 +01:00
foreach ( $tmpemail as $key2 => $val2 ) {
2019-12-03 11:17:29 +01:00
$newval2 = trim ( $val2 );
2020-12-20 11:04:21 +01:00
if ( $newval2 == '__SUPERVISOREMAIL__' ) {
if ( $user -> fk_user > 0 ) {
2019-12-03 11:17:29 +01:00
$tmpuser = new User ( $this -> db );
2017-10-07 13:09:31 +02:00
$tmpuser -> fetch ( $user -> fk_user );
2020-12-20 11:04:21 +01:00
if ( $tmpuser -> email ) {
$newval2 = trim ( $tmpuser -> email );
} else {
$newval2 = '' ;
}
} else {
$newval2 = '' ;
}
2017-10-07 13:09:31 +02:00
}
2020-12-20 11:04:21 +01:00
if ( $newval2 ) {
2019-12-03 11:17:29 +01:00
$isvalid = isValidEmail ( $newval2 , 0 );
2020-12-20 11:04:21 +01:00
if ( empty ( $resarray [ $newval2 ])) {
$resarray [ $newval2 ] = array ( 'type' => 'tofixedemail' , 'code' => trim ( $key ), 'emaildesc' => trim ( $val2 ), 'email' => $newval2 , 'isemailvalid' => $isvalid );
}
2017-10-07 13:09:31 +02:00
}
}
}
}
2006-08-05 18:44:43 +02:00
}
2009-06-30 13:59:46 +02:00
2020-12-20 11:04:21 +01:00
if ( $error ) {
return - 1 ;
}
2015-04-13 17:04:18 +02:00
//var_dump($resarray);
return $resarray ;
2006-08-05 18:44:43 +02:00
}
2017-10-07 13:09:31 +02:00
/**
* Check if notification are active for couple action / company .
* If yes , send mail and save trace into llx_notify .
*
2018-07-16 09:57:45 +02:00
* @ param string $notifcode Code of action in llx_c_action_trigger ( new usage ) or Id of action in llx_c_action_trigger ( old usage )
* @ param Object $object Object the notification deals on
* @ param array $filename_list List of files to attach ( full path of filename on file system )
* @ param array $mimetype_list List of MIME type of attached files
* @ param array $mimefilename_list List of attached file name in message
* @ return int < 0 if KO , or number of changes if OK
2017-10-07 13:09:31 +02:00
*/
2019-02-26 22:42:19 +01:00
public function send ( $notifcode , $object , $filename_list = array (), $mimetype_list = array (), $mimefilename_list = array ())
2017-10-07 13:09:31 +02:00
{
2019-12-03 11:17:29 +01:00
global $user , $conf , $langs , $mysoc ;
2017-10-07 13:09:31 +02:00
global $hookmanager ;
global $dolibarr_main_url_root ;
2020-04-27 17:13:23 +02:00
global $action ;
2005-07-14 15:03:20 +02:00
2020-12-20 11:04:21 +01:00
if ( ! in_array ( $notifcode , $this -> arrayofnotifsupported )) {
return 0 ;
}
2014-10-25 00:42:52 +02:00
2017-10-07 13:09:31 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2020-12-20 11:04:21 +01:00
if ( ! is_object ( $hookmanager )) {
2017-10-07 13:09:31 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php' ;
2019-12-03 11:17:29 +01:00
$hookmanager = new HookManager ( $this -> db );
2017-10-07 13:09:31 +02:00
}
$hookmanager -> initHooks ( array ( 'notification' ));
2017-10-06 14:05:13 +02:00
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::send notifcode= " . $notifcode . " , object= " . $object -> id );
2014-10-25 00:42:52 +02:00
2017-10-07 13:09:31 +02:00
$langs -> load ( " other " );
2014-10-25 00:42:52 +02:00
// Define $urlwithroot
2020-04-10 10:59:32 +02:00
$urlwithouturlroot = preg_replace ( '/' . preg_quote ( DOL_URL_ROOT , '/' ) . '$/i' , '' , trim ( $dolibarr_main_url_root ));
$urlwithroot = $urlwithouturlroot . DOL_URL_ROOT ; // This is to use external domain name found into config file
2014-10-25 00:42:52 +02:00
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2009-07-30 00:03:20 +02:00
2014-10-25 00:42:52 +02:00
// Define some vars
2017-10-07 13:09:31 +02:00
$application = 'Dolibarr' ;
2020-12-20 11:04:21 +01:00
if ( ! empty ( $conf -> global -> MAIN_APPLICATION_TITLE )) {
$application = $conf -> global -> MAIN_APPLICATION_TITLE ;
}
2017-10-07 13:09:31 +02:00
$replyto = $conf -> notification -> email_from ;
2014-10-25 00:42:52 +02:00
$object_type = '' ;
2017-10-07 13:09:31 +02:00
$link = '' ;
2014-10-25 00:42:52 +02:00
$num = 0 ;
2020-10-31 14:32:18 +01:00
$error = 0 ;
2006-08-05 18:44:43 +02:00
2020-04-10 10:59:32 +02:00
$oldref = ( empty ( $object -> oldref ) ? $object -> ref : $object -> oldref );
$newref = ( empty ( $object -> newref ) ? $object -> ref : $object -> newref );
2018-10-01 17:05:52 +02:00
2018-10-25 12:45:13 +02:00
$sql = '' ;
2014-10-25 00:42:52 +02:00
// Check notification per third party
2020-12-20 11:04:21 +01:00
if ( ! empty ( $object -> socid ) && $object -> socid > 0 ) {
2020-04-10 10:59:32 +02:00
$sql .= " SELECT 'tocontactid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.default_lang, " ;
$sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_action_trigger as a, " ;
$sql .= " " . MAIN_DB_PREFIX . " notify_def as n, " ;
$sql .= " " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " WHERE n.fk_contact = c.rowid AND a.rowid = n.fk_action " ;
$sql .= " AND n.fk_soc = s.rowid " ;
$sql .= " AND c.statut = 1 " ;
2020-12-20 11:04:21 +01:00
if ( is_numeric ( $notifcode )) {
2021-06-09 15:36:47 +02:00
$sql .= " AND n.fk_action = " . (( int ) $notifcode ); // Old usage
2020-12-20 11:04:21 +01:00
} else {
$sql .= " AND a.code = ' " . $this -> db -> escape ( $notifcode ) . " ' " ; // New usage
}
2021-06-09 15:36:47 +02:00
$sql .= " AND s.rowid = " . (( int ) $object -> socid );
2018-10-25 12:45:13 +02:00
2020-04-10 10:59:32 +02:00
$sql .= " \n UNION \n " ;
2018-10-25 12:45:13 +02:00
}
2010-01-09 15:48:27 +01:00
2016-03-13 00:20:29 +01:00
// Check notification per user
2020-04-10 10:59:32 +02:00
$sql .= " SELECT 'touserid' as type_target, c.email, c.rowid as cid, c.lastname, c.firstname, c.lang as default_lang, " ;
$sql .= " a.rowid as adid, a.label, a.code, n.rowid, n.type " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as c, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_action_trigger as a, " ;
$sql .= " " . MAIN_DB_PREFIX . " notify_def as n " ;
$sql .= " WHERE n.fk_user = c.rowid AND a.rowid = n.fk_action " ;
$sql .= " AND c.statut = 1 " ;
2020-12-20 11:04:21 +01:00
if ( is_numeric ( $notifcode )) {
$sql .= " AND n.fk_action = " . $notifcode ; // Old usage
} else {
$sql .= " AND a.code = ' " . $this -> db -> escape ( $notifcode ) . " ' " ; // New usage
}
2017-10-07 13:09:31 +02:00
$result = $this -> db -> query ( $sql );
2020-12-20 11:04:21 +01:00
if ( $result ) {
2017-10-07 13:09:31 +02:00
$num = $this -> db -> num_rows ( $result );
2020-04-10 10:59:32 +02:00
$projtitle = '' ;
2020-12-20 11:04:21 +01:00
if ( ! empty ( $object -> fk_project )) {
2018-10-04 19:30:03 +02:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2018-10-01 17:05:52 +02:00
$proj = new Project ( $this -> db );
$proj -> fetch ( $object -> fk_project );
2020-04-10 10:59:32 +02:00
$projtitle = '(' . $proj -> title . ')' ;
2018-10-01 17:05:52 +02:00
}
2017-10-07 13:09:31 +02:00
2020-12-20 11:04:21 +01:00
if ( $num > 0 ) {
2017-10-07 13:09:31 +02:00
$i = 0 ;
2020-12-20 11:04:21 +01:00
while ( $i < $num && ! $error ) { // For each notification couple defined (third party/actioncode)
2017-10-07 13:09:31 +02:00
$obj = $this -> db -> fetch_object ( $result );
2020-04-10 10:59:32 +02:00
$sendto = dolGetFirstLastname ( $obj -> firstname , $obj -> lastname ) . " < " . $obj -> email . " > " ;
2015-03-18 21:44:57 +01:00
$notifcodedefid = $obj -> adid ;
2020-05-07 11:59:13 +02:00
$trackid = '' ;
2020-12-20 11:04:21 +01:00
if ( $obj -> type_target == 'tocontactid' ) {
$trackid = 'con' . $obj -> id ;
}
if ( $obj -> type_target == 'touserid' ) {
$trackid = 'use' . $obj -> id ;
}
2014-09-04 14:38:30 +02:00
2020-12-20 11:04:21 +01:00
if ( dol_strlen ( $obj -> email )) {
2017-10-07 13:09:31 +02:00
// Set output language
$outputlangs = $langs ;
2020-12-20 11:04:21 +01:00
if ( $obj -> default_lang && $obj -> default_lang != $langs -> defaultlang ) {
2017-10-07 13:09:31 +02:00
$outputlangs = new Translate ( '' , $conf );
$outputlangs -> setDefaultLang ( $obj -> default_lang );
2019-12-03 11:17:29 +01:00
$outputlangs -> loadLangs ( array ( " main " , " other " ));
2017-10-07 13:09:31 +02:00
}
2014-10-25 00:42:52 +02:00
2019-12-03 11:17:29 +01:00
$subject = '[' . $mysoc -> name . '] ' . $outputlangs -> transnoentitiesnoconv ( " DolibarrNotification " ) . ( $projtitle ? ' ' . $projtitle : '' );
2021-03-19 11:53:06 +01:00
2017-10-07 13:09:31 +02:00
switch ( $notifcode ) {
2014-10-25 00:42:52 +02:00
case 'BILL_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> facture -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'invoice' );
2014-10-25 00:42:52 +02:00
$object_type = 'facture' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> BILL_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextInvoiceValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2016-02-22 12:52:01 +01:00
case 'BILL_PAYED' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> facture -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'invoice' );
2016-02-22 12:52:01 +01:00
$object_type = 'facture' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> BILL_PAYED_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextInvoicePayed " , $link );
2016-02-22 12:52:01 +01:00
break ;
2014-10-25 00:42:52 +02:00
case 'ORDER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> commande -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'commande' );
2014-10-25 00:42:52 +02:00
$object_type = 'order' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> ORDER_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextOrderValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
case 'PROPAL_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> propal -> multidir_output [ $object -> entity ] . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'propal' );
2014-10-25 00:42:52 +02:00
$object_type = 'propal' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> PROPAL_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextProposalValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2017-10-07 13:09:31 +02:00
case 'PROPAL_CLOSE_SIGNED' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> propal -> multidir_output [ $object -> entity ] . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'propal' );
2017-10-07 13:09:31 +02:00
$object_type = 'propal' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> PROPAL_CLOSE_SIGNED_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextProposalClosedSigned " , $link );
2017-10-07 13:09:31 +02:00
break ;
2016-03-11 21:07:38 +01:00
case 'FICHINTER_ADD_CONTACT' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-01-29 20:36:40 +01:00
$dir_output = $conf -> ficheinter -> dir_output ;
2016-03-11 21:07:38 +01:00
$object_type = 'ficheinter' ;
2020-05-07 11:59:13 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextInterventionAddedContact " , $link );
2016-03-11 21:07:38 +01:00
break ;
2014-10-25 00:42:52 +02:00
case 'FICHINTER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-01-29 20:36:40 +01:00
$dir_output = $conf -> ficheinter -> dir_output ;
2014-10-25 00:42:52 +02:00
$object_type = 'ficheinter' ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextInterventionValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2015-03-18 21:44:57 +01:00
case 'ORDER_SUPPLIER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-06 14:05:13 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
2015-03-18 21:44:57 +01:00
$object_type = 'order_supplier' ;
2018-10-16 03:30:17 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2020-05-07 11:59:13 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " EMailTextOrderValidatedBy " , $link , $user -> getFullName ( $outputlangs ));
2019-12-03 11:17:29 +01:00
$mesg .= " \n \n " . $outputlangs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2015-03-18 21:44:57 +01:00
break ;
2014-10-25 00:42:52 +02:00
case 'ORDER_SUPPLIER_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-06 14:05:13 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
2014-10-25 00:42:52 +02:00
$object_type = 'order_supplier' ;
2018-10-16 03:30:17 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2020-05-07 11:59:13 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " EMailTextOrderApprovedBy " , $link , $user -> getFullName ( $outputlangs ));
2019-12-03 11:17:29 +01:00
$mesg .= " \n \n " . $outputlangs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2014-10-25 00:42:52 +02:00
break ;
case 'ORDER_SUPPLIER_REFUSE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-06 14:05:13 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
2014-10-25 00:42:52 +02:00
$object_type = 'order_supplier' ;
2018-10-16 03:30:17 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2020-05-07 11:59:13 +02:00
$mesg .= $outputlangs -> transnoentitiesnoconv ( " EMailTextOrderRefusedBy " , $link , $user -> getFullName ( $outputlangs ));
2019-12-03 11:17:29 +01:00
$mesg .= " \n \n " . $outputlangs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2014-10-25 00:42:52 +02:00
break ;
case 'SHIPPING_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expedition/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> expedition -> dir_output . " /sending/ " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'shipment' );
2020-04-29 18:03:28 +02:00
$object_type = 'expedition' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> SHIPPING_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextExpeditionValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2018-10-25 12:45:13 +02:00
case 'EXPENSE_REPORT_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> expensereport -> dir_output ;
$object_type = 'expensereport' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> EXPENSE_REPORT_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextExpenseReportValidated " , $link );
2018-10-25 12:45:13 +02:00
break ;
case 'EXPENSE_REPORT_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> expensereport -> dir_output ;
$object_type = 'expensereport' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> EXPENSE_REPORT_APPROVE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextExpenseReportApproved " , $link );
2018-10-25 12:45:13 +02:00
break ;
case 'HOLIDAY_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> holiday -> dir_output ;
$object_type = 'holiday' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> HOLIDAY_VALIDATE_TEMPLATE ;
2020-05-07 11:39:32 +02:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextHolidayValidated " , $link );
2018-10-25 12:45:13 +02:00
break ;
2021-02-23 22:03:23 +01:00
case 'HOLIDAY_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2021-02-23 22:03:23 +01:00
$dir_output = $conf -> holiday -> dir_output ;
$object_type = 'holiday' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> HOLIDAY_APPROVE_TEMPLATE ;
2021-02-23 22:03:23 +01:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextHolidayApproved " , $link );
2021-03-19 11:53:06 +01:00
break ;
2021-02-23 22:03:23 +01:00
case 'ACTION_CREATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/action/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2021-02-23 22:03:23 +01:00
$dir_output = $conf -> agenda -> dir_output ;
$object_type = 'action' ;
2021-03-19 11:53:06 +01:00
$labeltouse = $conf -> global -> ACTION_CREATE_TEMPLATE ;
2021-02-23 22:03:23 +01:00
$mesg = $outputlangs -> transnoentitiesnoconv ( " EMailTextActionAdded " , $link );
break ;
2014-10-25 00:42:52 +02:00
}
2021-03-19 11:50:15 +01:00
2021-03-19 11:53:06 +01:00
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
$formmail = new FormMail ( $this -> db );
$arraydefaultmessage = null ;
2021-03-19 11:50:15 +01:00
if ( ! empty ( $labeltouse )) $arraydefaultmessage = $formmail -> getEMailTemplate ( $this -> db , $object_type . '_send' , $user , $outputlangs , 0 , 1 , $labeltouse );
2021-03-19 11:53:06 +01:00
if ( ! empty ( $labeltouse ) && is_object ( $arraydefaultmessage ) && $arraydefaultmessage -> id > 0 ) {
$substitutionarray = getCommonSubstitutionArray ( $outputlangs , 0 , null , $object );
complete_substitutions_array ( $substitutionarray , $outputlangs , $object );
$subject = make_substitutions ( $arraydefaultmessage -> topic , $substitutionarray , $outputlangs );
$message = make_substitutions ( $arraydefaultmessage -> content , $substitutionarray , $outputlangs );
} else {
$message = $outputlangs -> transnoentities ( " YouReceiveMailBecauseOfNotification " , $application , $mysoc -> name ) . " \n " ;
$message .= $outputlangs -> transnoentities ( " YouReceiveMailBecauseOfNotification2 " , $application , $mysoc -> name ) . " \n " ;
$message .= " \n " ;
$message .= $mesg ;
}
2021-03-19 11:50:15 +01:00
2017-10-07 13:09:31 +02:00
$ref = dol_sanitizeFileName ( $newref );
2021-03-19 11:50:15 +01:00
$pdf_path = $dir_output . " / " . $ref . " .pdf " ;
2020-12-20 11:04:21 +01:00
if ( ! dol_is_file ( $pdf_path )) {
2014-10-25 00:42:52 +02:00
// We can't add PDF as it is not generated yet.
$filepdf = '' ;
2020-05-21 15:05:19 +02:00
} else {
2014-10-25 00:42:52 +02:00
$filepdf = $pdf_path ;
2021-03-19 11:53:06 +01:00
$filename_list [] = $filepdf ;
$mimetype_list [] = mime_content_type ( $filepdf );
$mimefilename_list [] = $ref . " .pdf " ;
2014-10-25 00:42:52 +02:00
}
2019-12-03 11:17:29 +01:00
$parameters = array ( 'notifcode' => $notifcode , 'sendto' => $sendto , 'replyto' => $replyto , 'file' => $filename_list , 'mimefile' => $mimetype_list , 'filename' => $mimefilename_list );
2020-12-20 11:04:21 +01:00
if ( ! isset ( $action )) {
$action = '' ;
}
2020-04-27 17:13:23 +02:00
2019-12-03 11:17:29 +01:00
$reshook = $hookmanager -> executeHooks ( 'formatNotificationMessage' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2020-12-20 11:04:21 +01:00
if ( empty ( $reshook )) {
if ( ! empty ( $hookmanager -> resArray [ 'subject' ])) {
$subject .= $hookmanager -> resArray [ 'subject' ];
}
if ( ! empty ( $hookmanager -> resArray [ 'message' ])) {
$message .= $hookmanager -> resArray [ 'message' ];
}
2017-10-07 13:09:31 +02:00
}
2020-10-31 14:32:18 +01:00
$mailfile = new CMailFile (
2017-10-07 13:09:31 +02:00
$subject ,
$sendto ,
$replyto ,
$message ,
2018-07-16 09:57:45 +02:00
$filename_list ,
$mimetype_list ,
$mimefilename_list ,
2017-10-07 13:09:31 +02:00
'' ,
'' ,
0 ,
2019-09-28 10:25:00 +02:00
- 1 ,
2020-10-31 14:32:18 +01:00
'' ,
'' ,
$trackid ,
'' ,
'notification'
);
2017-10-07 13:09:31 +02:00
2020-12-20 11:04:21 +01:00
if ( $mailfile -> sendfile ()) {
2017-10-07 13:09:31 +02:00
if ( $obj -> type_target == 'touserid' ) {
2020-12-20 11:04:21 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " notify (daten, fk_action, fk_soc, fk_user, type, objet_type, type_target, objet_id, email) " ;
2019-12-03 11:17:29 +01:00
$sql .= " VALUES (' " . $this -> db -> idate ( dol_now ()) . " ', " . $notifcodedefid . " , " . ( $object -> socid ? $object -> socid : 'null' ) . " , " . $obj -> cid . " , ' " . $obj -> type . " ', ' " . $object_type . " ', ' " . $obj -> type_target . " ', " . $object -> id . " , ' " . $this -> db -> escape ( $obj -> email ) . " ') " ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " notify (daten, fk_action, fk_soc, fk_contact, type, objet_type, type_target, objet_id, email) " ;
2019-12-03 11:17:29 +01:00
$sql .= " VALUES (' " . $this -> db -> idate ( dol_now ()) . " ', " . $notifcodedefid . " , " . ( $object -> socid ? $object -> socid : 'null' ) . " , " . $obj -> cid . " , ' " . $obj -> type . " ', ' " . $object_type . " ', ' " . $obj -> type_target . " ', " . $object -> id . " , ' " . $this -> db -> escape ( $obj -> email ) . " ') " ;
2017-10-07 13:09:31 +02:00
}
2020-12-20 11:04:21 +01:00
if ( ! $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
2020-05-21 15:05:19 +02:00
} else {
2014-10-25 00:42:52 +02:00
$error ++ ;
2019-12-03 11:17:29 +01:00
$this -> errors [] = $mailfile -> error ;
2017-10-07 13:09:31 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
dol_syslog ( " No notification sent for " . $sendto . " because email is empty " );
}
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-12-12 19:49:13 +01:00
dol_syslog ( " No notification to thirdparty sent, nothing into notification setup for the thirdparty socid = " . ( empty ( $object -> socid ) ? '' : $object -> socid ));
2014-09-04 14:38:30 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2020-12-20 11:04:21 +01:00
$error ++ ;
2019-12-03 11:17:29 +01:00
$this -> errors [] = $this -> db -> lasterror ();
2017-10-07 13:09:31 +02:00
dol_syslog ( " Failed to get list of notification to send " . $this -> db -> lasterror (), LOG_ERR );
2020-12-20 11:04:21 +01:00
return - 1 ;
2017-10-07 13:09:31 +02:00
}
// Check notification using fixed email
2020-12-20 11:04:21 +01:00
if ( ! $error ) {
foreach ( $conf -> global as $key => $val ) {
2020-05-04 19:08:31 +02:00
$reg = array ();
2020-12-20 11:04:21 +01:00
if ( $val == '' || ! preg_match ( '/^NOTIFICATION_FIXEDEMAIL_' . $notifcode . '_THRESHOLD_HIGHER_(.*)$/' , $key , $reg )) {
continue ;
}
2017-10-07 13:09:31 +02:00
$threshold = ( float ) $reg [ 1 ];
2020-12-20 11:04:21 +01:00
if ( ! empty ( $object -> total_ht ) && $object -> total_ht <= $threshold ) {
2017-10-07 13:09:31 +02:00
dol_syslog ( " A notification is requested for notifcode = " . $notifcode . " but amount = " . $object -> total_ht . " so lower than threshold = " . $threshold . " . We discard this notification " );
continue ;
}
2019-12-03 11:17:29 +01:00
$param = 'NOTIFICATION_FIXEDEMAIL_' . $notifcode . '_THRESHOLD_HIGHER_' . $reg [ 1 ];
2017-10-07 13:09:31 +02:00
$sendto = $conf -> global -> $param ;
$notifcodedefid = dol_getIdFromCode ( $this -> db , $notifcode , 'c_action_trigger' , 'code' , 'rowid' );
2020-12-20 11:04:21 +01:00
if ( $notifcodedefid <= 0 ) {
dol_print_error ( $this -> db , 'Failed to get id from code' );
}
2020-05-07 11:59:13 +02:00
$trackid = '' ;
2017-10-07 13:09:31 +02:00
$object_type = '' ;
$link = '' ;
$num ++ ;
2019-12-03 11:17:29 +01:00
$subject = '[' . $mysoc -> name . '] ' . $langs -> transnoentitiesnoconv ( " DolibarrNotification " ) . ( $projtitle ? ' ' . $projtitle : '' );
2017-10-07 13:09:31 +02:00
switch ( $notifcode ) {
2014-10-25 00:42:52 +02:00
case 'BILL_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> facture -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'invoice' );
2014-10-25 00:42:52 +02:00
$object_type = 'facture' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextInvoiceValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2016-02-22 12:52:01 +01:00
case 'BILL_PAYED' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/compta/facture/card.php?facid=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $$conf -> facture -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'invoice' );
2016-02-22 12:52:01 +01:00
$object_type = 'facture' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextInvoicePayed " , $link );
2016-02-22 12:52:01 +01:00
break ;
2014-10-25 00:42:52 +02:00
case 'ORDER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> commande -> dir_output . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'commande' );
2014-10-25 00:42:52 +02:00
$object_type = 'order' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextOrderValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
case 'PROPAL_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> propal -> multidir_output [ $object -> entity ] . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'propal' );
2014-10-25 00:42:52 +02:00
$object_type = 'propal' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextProposalValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2017-10-07 13:09:31 +02:00
case 'PROPAL_CLOSE_SIGNED' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/propal/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> propal -> multidir_output [ $object -> entity ] . " / " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'propal' );
2017-10-07 13:09:31 +02:00
$object_type = 'propal' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextProposalClosedSigned " , $link );
2017-10-07 13:09:31 +02:00
break ;
case 'FICHINTER_ADD_CONTACT' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2020-05-07 11:39:32 +02:00
$dir_output = $conf -> ficheinter -> dir_output ;
2017-10-07 13:09:31 +02:00
$object_type = 'ficheinter' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextInterventionAddedContact " , $link );
2017-10-07 13:09:31 +02:00
break ;
2014-10-25 00:42:52 +02:00
case 'FICHINTER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fichinter/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2014-10-25 00:42:52 +02:00
$dir_output = $conf -> facture -> dir_output ;
$object_type = 'ficheinter' ;
2019-01-27 11:55:16 +01:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextInterventionValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2017-10-07 13:09:31 +02:00
case 'ORDER_SUPPLIER_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-07 13:09:31 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
$object_type = 'order_supplier' ;
$mesg = $langs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2019-12-03 11:17:29 +01:00
$mesg .= $langs -> transnoentitiesnoconv ( " EMailTextOrderValidatedBy " , $link , $user -> getFullName ( $langs ));
$mesg .= " \n \n " . $langs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2017-10-07 13:09:31 +02:00
break ;
2014-10-25 00:42:52 +02:00
case 'ORDER_SUPPLIER_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-06 14:05:13 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
2014-10-25 00:42:52 +02:00
$object_type = 'order_supplier' ;
$mesg = $langs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2019-12-03 11:17:29 +01:00
$mesg .= $langs -> transnoentitiesnoconv ( " EMailTextOrderApprovedBy " , $link , $user -> getFullName ( $langs ));
$mesg .= " \n \n " . $langs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2014-10-25 00:42:52 +02:00
break ;
2015-04-01 16:33:39 +02:00
case 'ORDER_SUPPLIER_APPROVE2' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2017-10-06 14:05:13 +02:00
$dir_output = $conf -> fournisseur -> commande -> dir_output ;
2015-04-01 16:33:39 +02:00
$object_type = 'order_supplier' ;
$mesg = $langs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2019-12-03 11:17:29 +01:00
$mesg .= $langs -> transnoentitiesnoconv ( " EMailTextOrderApprovedBy " , $link , $user -> getFullName ( $langs ));
$mesg .= " \n \n " . $langs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2015-04-01 16:33:39 +02:00
break ;
2014-10-25 00:42:52 +02:00
case 'ORDER_SUPPLIER_REFUSE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/fourn/commande/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2014-10-25 00:42:52 +02:00
$dir_output = $conf -> fournisseur -> dir_output . '/commande/' ;
$object_type = 'order_supplier' ;
$mesg = $langs -> transnoentitiesnoconv ( " Hello " ) . " , \n \n " ;
2019-12-03 11:17:29 +01:00
$mesg .= $langs -> transnoentitiesnoconv ( " EMailTextOrderRefusedBy " , $link , $user -> getFullName ( $langs ));
$mesg .= " \n \n " . $langs -> transnoentitiesnoconv ( " Sincerely " ) . " . \n \n " ;
2014-10-25 00:42:52 +02:00
break ;
case 'SHIPPING_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expedition/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
$dir_output = $conf -> expedition -> dir_output . " /sending/ " . get_exdir ( 0 , 0 , 0 , 1 , $object , 'shipment' );
2014-10-25 00:42:52 +02:00
$object_type = 'order_supplier' ;
2020-05-07 11:39:32 +02:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextExpeditionValidated " , $link );
2014-10-25 00:42:52 +02:00
break ;
2018-10-25 12:45:13 +02:00
case 'EXPENSE_REPORT_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> expensereport -> dir_output ;
$object_type = 'expensereport' ;
2020-05-07 11:39:32 +02:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextExpenseReportValidated " , $link );
2018-10-25 12:45:13 +02:00
break ;
case 'EXPENSE_REPORT_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/expensereport/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> expensereport -> dir_output ;
$object_type = 'expensereport' ;
2020-05-07 11:39:32 +02:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextExpenseReportApproved " , $link );
2018-10-25 12:45:13 +02:00
break ;
case 'HOLIDAY_VALIDATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> holiday -> dir_output ;
$object_type = 'holiday' ;
2020-05-07 11:39:32 +02:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextHolidayValidated " , $link );
2018-10-25 12:45:13 +02:00
break ;
case 'HOLIDAY_APPROVE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/holiday/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2018-10-25 12:45:13 +02:00
$dir_output = $conf -> holiday -> dir_output ;
$object_type = 'holiday' ;
2020-05-07 11:39:32 +02:00
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextHolidayApproved " , $link );
2021-03-19 11:53:06 +01:00
break ;
2021-02-23 22:03:23 +01:00
case 'ACTION_CREATE' :
2021-03-19 11:50:15 +01:00
$link = '<a href="' . $urlwithroot . '/comm/action/card.php?id=' . $object -> id . '&entity=' . $object -> entity . '">' . $newref . '</a>' ;
2021-02-23 22:03:23 +01:00
$dir_output = $conf -> agenda -> dir_output ;
$object_type = 'action' ;
$mesg = $langs -> transnoentitiesnoconv ( " EMailTextActionAdded " , $link );
break ;
2021-03-19 11:53:06 +01:00
}
2015-04-01 11:30:48 +02:00
$ref = dol_sanitizeFileName ( $newref );
2014-10-25 00:42:52 +02:00
$pdf_path = $dir_output . " / " . $ref . " / " . $ref . " .pdf " ;
2020-12-20 11:04:21 +01:00
if ( ! dol_is_file ( $pdf_path )) {
2014-10-25 00:42:52 +02:00
// We can't add PDF as it is not generated yet.
$filepdf = '' ;
2020-05-21 15:05:19 +02:00
} else {
2014-10-25 00:42:52 +02:00
$filepdf = $pdf_path ;
2021-03-19 11:53:06 +01:00
$filename_list [] = $pdf_path ;
$mimetype_list [] = mime_content_type ( $filepdf );
$mimefilename_list [] = $ref . " .pdf " ;
2014-10-25 00:42:52 +02:00
}
2015-03-19 11:47:30 +01:00
2019-12-03 11:17:29 +01:00
$message .= $langs -> transnoentities ( " YouReceiveMailBecauseOfNotification2 " , $application , $mysoc -> name ) . " \n " ;
$message .= " \n " ;
$message .= $mesg ;
2018-10-16 03:30:17 +02:00
2018-01-21 15:55:56 +01:00
$message = nl2br ( $message );
2017-10-07 13:09:31 +02:00
// Replace keyword __SUPERVISOREMAIL__
2020-12-20 11:04:21 +01:00
if ( preg_match ( '/__SUPERVISOREMAIL__/' , $sendto )) {
2019-12-03 11:17:29 +01:00
$newval = '' ;
2020-12-20 11:04:21 +01:00
if ( $user -> fk_user > 0 ) {
2019-12-03 11:17:29 +01:00
$supervisoruser = new User ( $this -> db );
2017-10-07 13:09:31 +02:00
$supervisoruser -> fetch ( $user -> fk_user );
2020-12-20 11:04:21 +01:00
if ( $supervisoruser -> email ) {
$newval = trim ( dolGetFirstLastname ( $supervisoruser -> firstname , $supervisoruser -> lastname ) . ' <' . $supervisoruser -> email . '>' );
}
2017-10-07 13:09:31 +02:00
}
dol_syslog ( " Replace the __SUPERVISOREMAIL__ key into recipient email string with " . $newval );
$sendto = preg_replace ( '/__SUPERVISOREMAIL__/' , $newval , $sendto );
2019-12-03 11:17:29 +01:00
$sendto = preg_replace ( '/,\s*,/' , ',' , $sendto ); // in some case you can have $sendto like "email, __SUPERVISOREMAIL__ , otheremail" then you have "email, , othermail" and it's not valid
$sendto = preg_replace ( '/^[\s,]+/' , '' , $sendto ); // Clean start of string
$sendto = preg_replace ( '/[\s,]+$/' , '' , $sendto ); // Clean end of string
2017-10-07 13:09:31 +02:00
}
2020-12-20 11:04:21 +01:00
if ( $sendto ) {
2019-12-03 11:17:29 +01:00
$parameters = array ( 'notifcode' => $notifcode , 'sendto' => $sendto , 'replyto' => $replyto , 'file' => $filename_list , 'mimefile' => $mimetype_list , 'filename' => $mimefilename_list );
$reshook = $hookmanager -> executeHooks ( 'formatNotificationMessage' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2020-12-20 11:04:21 +01:00
if ( empty ( $reshook )) {
if ( ! empty ( $hookmanager -> resArray [ 'subject' ])) {
$subject .= $hookmanager -> resArray [ 'subject' ];
}
if ( ! empty ( $hookmanager -> resArray [ 'message' ])) {
$message .= $hookmanager -> resArray [ 'message' ];
}
2017-10-07 13:09:31 +02:00
}
2020-10-31 14:32:18 +01:00
$mailfile = new CMailFile (
2017-10-07 13:09:31 +02:00
$subject ,
$sendto ,
$replyto ,
$message ,
2018-07-16 09:57:45 +02:00
$filename_list ,
$mimetype_list ,
$mimefilename_list ,
2017-10-07 13:09:31 +02:00
'' ,
'' ,
0 ,
2019-09-28 10:25:00 +02:00
1 ,
2020-10-31 14:32:18 +01:00
'' ,
$trackid ,
'' ,
'' ,
'notification'
2017-10-07 13:09:31 +02:00
);
2020-12-20 11:04:21 +01:00
if ( $mailfile -> sendfile ()) {
2017-10-07 13:09:31 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " notify (daten, fk_action, fk_soc, fk_contact, type, type_target, objet_type, objet_id, email) " ;
2019-12-03 11:17:29 +01:00
$sql .= " VALUES (' " . $this -> db -> idate ( dol_now ()) . " ', " . $notifcodedefid . " , " . ( $object -> socid ? $object -> socid : 'null' ) . " , null, 'email', 'tofixedemail', ' " . $object_type . " ', " . $object -> id . " , ' " . $this -> db -> escape ( $conf -> global -> $param ) . " ') " ;
2020-12-20 11:04:21 +01:00
if ( ! $this -> db -> query ( $sql )) {
2017-10-07 13:09:31 +02:00
dol_print_error ( $this -> db );
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-07 13:09:31 +02:00
$error ++ ;
2019-12-03 11:17:29 +01:00
$this -> errors [] = $mailfile -> error ;
2017-10-07 13:09:31 +02:00
}
}
}
}
2014-10-25 00:42:52 +02:00
2020-12-20 11:04:21 +01:00
if ( ! $error ) {
return $num ;
} else {
return - 1 * $error ;
}
2017-10-07 13:09:31 +02:00
}
2018-09-29 14:51:52 +02:00
}