2021-04-06 16:53:51 +02:00
< ? php
/* Copyright ( C ) 2021 NextGestion < contact @ nextgestion . com >
2024-03-19 22:02:04 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2021-04-06 16:53:51 +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
2022-09-07 20:21:01 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2021-04-06 16:53:51 +02:00
*/
/**
* \file partnership / class / partnershiputils . class . php
* \ingroup partnership
* \brief Class with utilities
*/
//require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
//require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2021-04-17 14:25:28 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/geturl.lib.php' ;
2021-06-09 23:55:56 +02:00
require_once DOL_DOCUMENT_ROOT . '/partnership/lib/partnership.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/partnership/class/partnership.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2021-04-17 14:25:28 +02:00
require_once DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php' ;
2021-06-09 23:55:56 +02:00
2021-04-06 16:53:51 +02:00
/**
* Class with cron tasks of Partnership module
*/
class PartnershipUtils
{
2024-10-05 18:55:36 +02:00
/**
* @ var DoliDB
*/
2021-06-14 15:37:02 +02:00
public $db ; //!< To store db handler
2024-10-05 18:55:36 +02:00
/**
* @ var string
*/
2021-06-14 15:37:02 +02:00
public $error ; //!< To return error code (or message)
2024-10-05 18:55:36 +02:00
/**
* @ var string []
*/
2021-06-14 15:37:02 +02:00
public $errors = array (); //!< To return several error codes (or messages)
2021-04-06 16:53:51 +02:00
2024-10-05 18:55:36 +02:00
/**
* @ var string To store output of some cron methods
*/
public $output ;
2022-10-03 19:41:04 +02:00
2021-04-06 16:53:51 +02:00
2021-04-06 16:56:23 +02:00
/**
* Constructor
*
2024-01-13 15:50:02 +01:00
* @ param DoliDB $db Database handler
2021-04-06 16:56:23 +02:00
*/
2021-04-08 18:42:59 +02:00
public function __construct ( $db )
2021-04-06 16:56:23 +02:00
{
$this -> db = $db ;
}
2021-04-17 15:30:56 +02:00
2021-04-17 14:25:28 +02:00
/**
* Action executed by scheduler to cancel status of partnership when subscription is expired + x days . ( Max number of action batch per call = $conf -> global -> PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL )
*
* CAN BE A CRON TASK
*
* @ return int 0 if OK , <> 0 if KO ( this function is used also by cron so only 0 is OK )
*/
public function doCancelStatusOfMemberPartnership ()
{
global $conf , $langs , $user ;
2022-05-10 16:19:00 +02:00
$managedfor = getDolGlobalString ( 'PARTNERSHIP_IS_MANAGED_FOR' , 'thirdparty' );
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
if ( $managedfor != 'member' ) {
2021-04-17 14:25:28 +02:00
return 0 ; // If option 'PARTNERSHIP_IS_MANAGED_FOR' = 'thirdparty', this cron job does nothing.
}
2021-04-17 15:30:56 +02:00
$partnership = new Partnership ( $this -> db );
2023-11-27 11:56:32 +01:00
$MAXPERCALL = ( ! getDolGlobalString ( 'PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL' ) ? 25 : $conf -> global -> PARTNERSHIP_MAX_EXPIRATION_CANCEL_PER_CALL ); // Limit to 25 per call
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$langs -> loadLangs ( array ( " partnership " , " member " ));
2021-04-17 14:25:28 +02:00
2021-06-14 15:37:02 +02:00
$error = 0 ;
$erroremail = '' ;
$this -> output = '' ;
$this -> error = '' ;
2021-04-17 15:30:56 +02:00
$partnershipsprocessed = array ();
2021-04-17 14:25:28 +02:00
2024-01-05 04:18:53 +01:00
$gracedelay = getDolGlobalString ( 'PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL' );
2021-04-17 15:30:56 +02:00
if ( $gracedelay < 1 ) {
2021-06-14 15:37:02 +02:00
$this -> error = 'BadValueForDelayBeforeCancelCheckSetup' ;
2021-04-17 15:30:56 +02:00
return - 1 ;
}
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
dol_syslog ( get_class ( $this ) . " ::doCancelStatusOfMemberPartnership cancel expired partnerships with grace delay of " . $gracedelay );
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$now = dol_now ();
2024-03-19 22:02:04 +01:00
$datetotest = dol_time_plus_duree ( $now , - 1 * abs (( float ) $gracedelay ), 'd' );
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$this -> db -> begin ();
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$sql = " SELECT p.rowid, p.fk_member, p.status " ;
$sql .= " , d.datefin, d.fk_adherent_type, dty.subscription " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " partnership as p " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " adherent as d on (d.rowid = p.fk_member) " ;
2021-04-17 14:25:28 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " adherent_type as dty on (dty.rowid = d.fk_adherent_type) " ;
$sql .= " WHERE fk_member > 0 " ;
$sql .= " AND (d.datefin < ' " . $this -> db -> idate ( $datetotest ) . " ' AND dty.subscription = 1) " ;
2021-11-07 16:51:18 +01:00
$sql .= " AND p.status = " . (( int ) $partnership :: STATUS_APPROVED ); // Only accepted not yet canceled
2021-04-17 15:30:56 +02:00
$sql .= $this -> db -> order ( 'd.rowid' , 'ASC' );
// Limit is managed into loop later
2024-10-05 18:55:36 +02:00
$numofexpiredmembers = 0 ;
2021-04-17 15:30:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$numofexpiredmembers = $this -> db -> num_rows ( $resql );
$somethingdoneonpartnership = 0 ;
$ifetchpartner = 0 ;
while ( $ifetchpartner < $numofexpiredmembers ) {
$ifetchpartner ++ ;
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
2023-12-04 13:49:31 +01:00
if ( ! empty ( $partnershipsprocessed [ $obj -> rowid ])) {
continue ;
}
2021-04-17 15:30:56 +02:00
if ( $somethingdoneonpartnership >= $MAXPERCALL ) {
dol_syslog ( " We reach the limit of " . $MAXPERCALL . " partnership processed, so we quit loop for this batch doCancelStatusOfMemberPartnership to avoid to reach email quota. " , LOG_WARNING );
break ;
}
$object = new Partnership ( $this -> db );
$object -> fetch ( $obj -> rowid );
// Get expiration date
$expirationdate = $obj -> datefin ;
if ( $expirationdate && $expirationdate < $now ) { // If contract expired (we already had a test into main select, this is a security)
$somethingdoneonpartnership ++ ;
$result = $object -> cancel ( $user , 0 );
// $conf->global->noapachereload = null;
if ( $result < 0 ) {
$error ++ ;
$this -> error = $object -> error ;
if ( is_array ( $object -> errors ) && count ( $object -> errors )) {
2023-12-04 13:49:31 +01:00
if ( is_array ( $this -> errors )) {
$this -> errors = array_merge ( $this -> errors , $object -> errors );
} else {
$this -> errors = $object -> errors ;
}
2021-04-17 15:30:56 +02:00
}
} else {
2021-06-14 15:37:02 +02:00
$partnershipsprocessed [ $object -> id ] = $object -> ref ;
2021-04-17 15:30:56 +02:00
// Send an email to inform member
2023-09-16 12:10:39 +02:00
$labeltemplate = '(' . getDolGlobalString ( 'PARTNERSHIP_SENDMAIL_IF_AUTO_CANCEL' , 'SendingEmailOnPartnershipCanceled' ) . ')' ;
2021-04-17 15:30:56 +02:00
dol_syslog ( " Now we will send an email to member id= " . $object -> fk_member . " with label " . $labeltemplate );
// Send deployment email
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2021-06-14 15:37:02 +02:00
$formmail = new FormMail ( $this -> db );
2021-04-17 15:30:56 +02:00
// Define output language
$outputlangs = $langs ;
$newlang = '' ;
2023-12-04 13:49:31 +01:00
if ( getDolGlobalInt ( 'MAIN_MULTILANGS' ) && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
2021-06-14 15:37:02 +02:00
if ( ! empty ( $newlang )) {
2021-04-17 15:30:56 +02:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
2021-06-14 15:37:02 +02:00
$outputlangs -> loadLangs ( array ( 'main' , 'member' , 'partnership' ));
2021-04-17 15:30:56 +02:00
}
2021-06-14 15:37:02 +02:00
$arraydefaultmessage = $formmail -> getEMailTemplate ( $this -> db , 'partnership_send' , $user , $outputlangs , 0 , 1 , $labeltemplate );
2021-04-17 15:30:56 +02:00
2021-06-14 15:37:02 +02:00
$substitutionarray = getCommonSubstitutionArray ( $outputlangs , 0 , null , $object );
2021-04-17 15:30:56 +02:00
complete_substitutions_array ( $substitutionarray , $outputlangs , $object );
$subject = make_substitutions ( $arraydefaultmessage -> topic , $substitutionarray , $outputlangs );
$msg = make_substitutions ( $arraydefaultmessage -> content , $substitutionarray , $outputlangs );
2023-10-15 18:39:13 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_INFO_SOCIETE_NOM , ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL' ) . '>' ;
2021-04-17 15:30:56 +02:00
2023-09-16 12:10:39 +02:00
// We are in the case of autocancellation subscription because of missing backlink
$fk_partner = $object -> fk_member ;
2021-04-17 15:30:56 +02:00
$adherent = new Adherent ( $this -> db );
$adherent -> fetch ( $object -> fk_member );
2023-09-16 12:10:39 +02:00
$sendto = $adherent -> email ;
$trackid = 'par' . $object -> id ;
$sendcontext = 'standard' ;
$cmail = new CMailFile ( $subject , $sendto , $from , $msg , array (), array (), array (), '' , '' , 0 , 1 , '' , '' , $trackid , '' , $sendcontext );
2021-04-17 15:30:56 +02:00
$result = $cmail -> sendfile ();
2023-09-16 12:10:39 +02:00
if ( ! $result || ! empty ( $cmail -> error ) || ! empty ( $cmail -> errors )) {
2021-04-17 15:30:56 +02:00
$erroremail .= ( $erroremail ? ', ' : '' ) . $cmail -> error ;
$this -> errors [] = $cmail -> error ;
2023-12-04 13:49:31 +01:00
if ( is_array ( $cmail -> errors ) && count ( $cmail -> errors ) > 0 ) {
$this -> errors += $cmail -> errors ;
}
2023-09-16 12:10:39 +02:00
} else {
// Initialisation of datas of object to call trigger
if ( is_object ( $object )) {
$actiontypecode = 'AC_OTH_AUTO' ; // Event insert into agenda automatically
$attachedfiles = array ();
$object -> actiontypecode = $actiontypecode ; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
$object -> actionmsg = $arraydefaultmessage -> topic . " \n " . $arraydefaultmessage -> content ; // Long text
2023-12-04 13:49:31 +01:00
$object -> actionmsg2 = $langs -> transnoentities ( " PartnershipSentByEMail " , $object -> ref );
2024-03-28 13:31:09 +01:00
; // Short text ($langs->transnoentities('MailSentByTo')...);
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT' )) {
2023-09-16 12:10:39 +02:00
$object -> actionmsg2 = $subject ; // Short text
}
$object -> trackid = $trackid ;
$object -> fk_element = $object -> id ;
$object -> elementtype = $object -> element ;
if ( is_array ( $attachedfiles ) && count ( $attachedfiles ) > 0 ) {
$object -> attachedfiles = $attachedfiles ;
}
$object -> email_from = $from ;
$object -> email_subject = $subject ;
$object -> email_to = $sendto ;
$object -> email_subject = $subject ;
$triggersendname = 'PARTNERSHIP_SENTBYMAIL' ;
// Call of triggers (you should have set $triggersendname to execute trigger)
if ( ! empty ( $triggersendname )) {
$result = $object -> call_trigger ( $triggersendname , $user );
if ( $result < 0 ) {
$error ++ ;
}
}
// End call of triggers
}
2021-04-17 15:30:56 +02:00
}
}
}
}
}
} else {
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
2021-06-14 15:37:02 +02:00
if ( ! $error ) {
2021-04-17 15:30:56 +02:00
$this -> db -> commit ();
$this -> output = $numofexpiredmembers . ' expired partnership members found' . " \n " ;
2023-12-04 13:49:31 +01:00
if ( $erroremail ) {
$this -> output .= '. Got errors when sending some email : ' . $erroremail ;
}
2021-04-17 15:30:56 +02:00
} else {
$this -> db -> rollback ();
$this -> output = " Rollback after error \n " ;
2021-06-14 15:37:02 +02:00
$this -> output .= $numofexpiredmembers . ' expired partnership members found' . " \n " ;
2023-12-04 13:49:31 +01:00
if ( $erroremail ) {
$this -> output .= '. Got errors when sending some email : ' . $erroremail ;
}
2021-04-17 15:30:56 +02:00
}
2021-06-14 15:37:02 +02:00
return ( $error ? 1 : 0 );
2021-04-17 14:25:28 +02:00
}
2021-04-06 16:53:51 +02:00
2021-04-06 16:56:23 +02:00
/**
2021-04-17 14:25:28 +02:00
* Action executed by scheduler to check if Dolibarr backlink not found on partner website . ( Max number of action batch per call = $conf -> global -> PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL )
2021-04-06 16:56:23 +02:00
*
* CAN BE A CRON TASK
*
2023-09-15 18:41:20 +02:00
* @ param int $maxpercall Max per call
* @ return int 0 if OK , <> 0 if KO ( this function is used also by cron so only 0 is OK )
2021-04-06 16:56:23 +02:00
*/
2023-09-15 18:41:20 +02:00
public function doWarningOfPartnershipIfDolibarrBacklinkNotfound ( $maxpercall = 0 )
2021-04-06 16:56:23 +02:00
{
global $conf , $langs , $user ;
2021-04-06 16:53:51 +02:00
2022-05-10 16:19:00 +02:00
$managedfor = getDolGlobalString ( 'PARTNERSHIP_IS_MANAGED_FOR' );
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$partnership = new Partnership ( $this -> db );
2023-09-15 18:41:20 +02:00
if ( empty ( $maxpercall )) {
$maxpercall = getDolGlobalInt ( 'PARTNERSHIP_MAX_WARNING_BACKLINK_PER_CALL' , 10 );
}
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$langs -> loadLangs ( array ( " partnership " , " member " ));
2021-04-17 14:25:28 +02:00
2021-06-14 15:37:02 +02:00
$error = 0 ;
$erroremail = '' ;
$this -> output = '' ;
$this -> error = '' ;
2021-04-17 15:30:56 +02:00
$partnershipsprocessed = array ();
2023-09-03 02:05:43 +02:00
$emailnotfound = '' ;
$websitenotfound = '' ;
2021-04-17 14:25:28 +02:00
2023-09-16 11:41:36 +02:00
/* $gracedelay = getDolGlobalInt ( 'PARTNERSHIP_NBDAYS_AFTER_MEMBER_EXPIRATION_BEFORE_CANCEL' );
2021-04-17 15:30:56 +02:00
if ( $gracedelay < 1 ) {
2021-06-14 15:37:02 +02:00
$this -> error = 'BadValueForDelayBeforeCancelCheckSetup' ;
2021-04-17 15:30:56 +02:00
return - 1 ;
2023-09-16 11:41:36 +02:00
} */
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$fk_partner = ( $managedfor == 'member' ) ? 'fk_member' : 'fk_soc' ;
2021-04-06 16:53:51 +02:00
2021-04-17 15:30:56 +02:00
dol_syslog ( get_class ( $this ) . " ::doWarningOfPartnershipIfDolibarrBacklinkNotfound Warning of partnership " );
2021-04-06 16:53:51 +02:00
2021-04-17 15:30:56 +02:00
$now = dol_now ();
2023-09-16 11:41:36 +02:00
//$datetotest = dol_time_plus_duree($now, -1 * abs($gracedelay), 'd');
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$this -> db -> begin ();
2021-04-17 14:25:28 +02:00
2021-04-17 15:30:56 +02:00
$sql = " SELECT p.rowid, p.status, p. " . $fk_partner ;
2023-09-15 23:18:07 +02:00
$sql .= " , p.url_to_check, p.last_check_backlink " ;
2021-04-17 15:30:56 +02:00
$sql .= ', partner.url, partner.email' ;
$sql .= " FROM " . MAIN_DB_PREFIX . " partnership as p " ;
if ( $managedfor == 'member' ) {
2021-04-17 14:25:28 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " adherent as partner on (partner.rowid = p.fk_member) " ;
} else {
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as partner on (partner.rowid = p.fk_soc) " ;
}
2023-09-03 02:05:43 +02:00
$sql .= " WHERE p. " . $fk_partner . " > 0 " ;
$sql .= " AND p.status = " . (( int ) $partnership :: STATUS_APPROVED ); // Only accepted and not yet canceled
2024-01-12 18:06:50 +01:00
$sql .= " AND (p.last_check_backlink IS NULL OR p.last_check_backlink <= ' " . $this -> db -> idate ( $now - 24 * 3600 ) . " ') " ; // Never more than 1 check every day to check that website contains a referral link.
2021-04-17 15:30:56 +02:00
$sql .= $this -> db -> order ( 'p.rowid' , 'ASC' );
// Limit is managed into loop later
2024-10-05 18:55:36 +02:00
$numofexpiredmembers = 0 ;
2021-04-17 15:30:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
2021-06-14 15:37:02 +02:00
$numofexpiredmembers = $this -> db -> num_rows ( $resql );
2021-04-17 15:30:56 +02:00
$somethingdoneonpartnership = 0 ;
2021-06-14 15:37:02 +02:00
$ifetchpartner = 0 ;
2021-04-17 15:30:56 +02:00
while ( $ifetchpartner < $numofexpiredmembers ) {
$ifetchpartner ++ ;
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) {
2023-09-16 11:41:36 +02:00
if ( ! empty ( $partnershipsprocessed [ $obj -> rowid ])) {
continue ;
}
2021-04-17 15:30:56 +02:00
2023-09-15 18:41:20 +02:00
if ( $somethingdoneonpartnership >= $maxpercall ) {
dol_syslog ( " We reach the limit of " . $maxpercall . " partnership processed, so we quit loop for this batch doWarningOfPartnershipIfDolibarrBacklinkNotfound to avoid to reach email quota. " , LOG_WARNING );
2021-04-17 15:30:56 +02:00
break ;
}
$backlinkfound = 0 ;
$object = new Partnership ( $this -> db );
$object -> fetch ( $obj -> rowid );
if ( $managedfor == 'member' ) {
$fk_partner = $object -> fk_member ;
} else {
$fk_partner = $object -> fk_soc ;
}
2023-09-15 23:18:07 +02:00
$website = ( empty ( $obj -> url_to_check ) ? $obj -> url : $obj -> url_to_check );
2021-04-17 15:30:56 +02:00
if ( empty ( $website )) {
$websitenotfound .= ( $websitenotfound ? ', ' : '' ) . 'Website not found for id="' . $fk_partner . '"' . " \n " ;
} else {
$backlinkfound = $this -> checkDolibarrBacklink ( $website );
}
if ( ! $backlinkfound ) {
$tmpcount = $object -> count_last_url_check_error + 1 ;
2024-03-21 23:36:26 +01:00
$nbminbacklinkerrorforcancel = ( int ) getDolGlobalString ( 'PARTNERSHIP_MIN_BACKLINK_ERROR_FOR_CANCEL' , 3 );
$nbmaxbacklinkerrorforcancel = ( int ) getDolGlobalString ( 'PARTNERSHIP_MAX_BACKLINK_ERROR_FOR_CANCEL' , ( int ) $nbminbacklinkerrorforcancel + 2 );
2023-09-15 16:44:18 +02:00
// If $nbminbacklinkerrorforemail = 0, no autoemail
if ( $nbminbacklinkerrorforcancel > 0 ) {
if ( $tmpcount > $nbminbacklinkerrorforcancel && $tmpcount <= $nbmaxbacklinkerrorforcancel ) { // Send Warning Email
if ( ! empty ( $obj -> email )) {
$emailnotfound .= ( $emailnotfound ? ', ' : '' ) . 'Email not found for id="' . $fk_partner . '"' . " \n " ;
} else {
// Example: 'SendingEmailOnPartnershipWillSoonBeCanceled'
$labeltemplate = '(' . getDolGlobalString ( 'PARTNERSHIP_SENDMAIL_IF_NO_LINK' , 'SendingEmailOnPartnershipWillSoonBeCanceled' ) . ')' ;
dol_syslog ( " Now we will send an email to partner id= " . $fk_partner . " with label " . $labeltemplate );
// Send deployment email
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
$formmail = new FormMail ( $this -> db );
// Define output language
$outputlangs = $langs ;
$newlang = '' ;
2023-12-04 13:49:31 +01:00
if ( getDolGlobalInt ( 'MAIN_MULTILANGS' ) && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
2023-09-15 16:44:18 +02:00
if ( ! empty ( $newlang )) {
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
$outputlangs -> loadLangs ( array ( 'main' , 'member' , 'partnership' ));
}
$arraydefaultmessage = $formmail -> getEMailTemplate ( $this -> db , 'partnership_send' , $user , $outputlangs , 0 , 1 , $labeltemplate );
$substitutionarray = getCommonSubstitutionArray ( $outputlangs , 0 , null , $object );
complete_substitutions_array ( $substitutionarray , $outputlangs , $object );
$subject = make_substitutions ( $arraydefaultmessage -> topic , $substitutionarray , $outputlangs );
$msg = make_substitutions ( $arraydefaultmessage -> content , $substitutionarray , $outputlangs );
2023-10-15 18:39:13 +02:00
$from = dol_string_nospecial ( $conf -> global -> MAIN_INFO_SOCIETE_NOM , ' ' , array ( " , " )) . ' <' . getDolGlobalString ( 'MAIN_INFO_SOCIETE_MAIL' ) . '>' ;
2023-09-15 16:44:18 +02:00
2023-09-16 12:10:39 +02:00
$sendto = $obj -> email ;
$trackid = 'par' . $object -> id ;
$sendcontext = 'standard' ;
$cmail = new CMailFile ( $subject , $sendto , $from , $msg , array (), array (), array (), '' , '' , 0 , 1 , '' , '' , $trackid , '' , $sendcontext );
2023-09-15 16:44:18 +02:00
$result = $cmail -> sendfile ();
2023-09-16 12:10:39 +02:00
if ( ! $result || ! empty ( $cmail -> error ) || ! empty ( $cmail -> errors )) {
2023-09-15 16:44:18 +02:00
$erroremail .= ( $erroremail ? ', ' : '' ) . $cmail -> error ;
$this -> errors [] = $cmail -> error ;
2023-12-04 13:49:31 +01:00
if ( is_array ( $cmail -> errors ) && count ( $cmail -> errors ) > 0 ) {
$this -> errors += $cmail -> errors ;
}
2023-09-16 12:10:39 +02:00
} else {
// Initialisation of datas of object to call trigger
if ( is_object ( $object )) {
$actiontypecode = 'AC_OTH_AUTO' ; // Event insert into agenda automatically
$attachedfiles = array ();
if ( $managedfor != 'member' ) {
$object -> socid = $fk_partner ; // To link to a company
}
$object -> actiontypecode = $actiontypecode ; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...)
$object -> actionmsg = $arraydefaultmessage -> topic . " \n " . $arraydefaultmessage -> content ; // Long text
2023-12-04 13:49:31 +01:00
$object -> actionmsg2 = $langs -> transnoentities ( " PartnershipSentByEMail " , $object -> ref );
2024-03-28 13:31:09 +01:00
; // Short text ($langs->transnoentities('MailSentByTo')...);
2023-11-27 11:56:32 +01:00
if ( getDolGlobalString ( 'MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT' )) {
2023-09-16 12:10:39 +02:00
$object -> actionmsg2 = $subject ; // Short text
}
$object -> trackid = $trackid ;
$object -> fk_element = $object -> id ;
$object -> elementtype = $object -> element ;
if ( is_array ( $attachedfiles ) && count ( $attachedfiles ) > 0 ) {
$object -> attachedfiles = $attachedfiles ;
}
$object -> email_from = $from ;
$object -> email_subject = $subject ;
$object -> email_to = $sendto ;
$object -> email_subject = $subject ;
$triggersendname = 'PARTNERSHIP_SENTBYMAIL' ;
// Call of triggers (you should have set $triggersendname to execute trigger)
if ( ! empty ( $triggersendname )) {
$result = $object -> call_trigger ( $triggersendname , $user );
if ( $result < 0 ) {
$error ++ ;
}
}
// End call of triggers
}
2023-09-15 16:44:18 +02:00
}
2021-04-17 15:30:56 +02:00
}
2023-09-15 16:44:18 +02:00
} elseif ( $tmpcount > $nbmaxbacklinkerrorforcancel ) { // Cancel Partnership
$object -> status = $object :: STATUS_CANCELED ;
$object -> reason_decline_or_cancel = $langs -> trans ( 'BacklinkNotFoundOnPartnerWebsite' );
2021-04-17 15:30:56 +02:00
}
}
$object -> count_last_url_check_error = $tmpcount ;
} else {
$object -> count_last_url_check_error = 0 ;
$object -> reason_decline_or_cancel = '' ;
}
2021-06-14 15:37:02 +02:00
$partnershipsprocessed [ $object -> id ] = $object -> ref ;
2021-04-17 15:30:56 +02:00
2023-09-15 22:41:35 +02:00
$object -> last_check_backlink = $now ;
2021-04-17 15:30:56 +02:00
$object -> update ( $user );
}
}
} else {
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
2021-04-17 14:25:28 +02:00
2021-06-14 15:37:02 +02:00
if ( ! $error ) {
2021-04-17 15:30:56 +02:00
$this -> db -> commit ();
2023-09-03 02:05:43 +02:00
$this -> output = " " ;
2021-04-17 15:30:56 +02:00
} else {
$this -> db -> rollback ();
$this -> output = " Rollback after error \n " ;
}
2023-09-03 02:05:43 +02:00
$this -> output .= $numofexpiredmembers . ' partnership checked' . " \n " ;
2023-12-04 13:49:31 +01:00
if ( $erroremail ) {
$this -> output .= '. Got errors when sending some email : ' . $erroremail . " \n " ;
}
if ( $emailnotfound ) {
$this -> output .= '. Email not found for some partner : ' . $emailnotfound . " \n " ;
}
if ( $websitenotfound ) {
$this -> output .= '. Website not found for some partner : ' . $websitenotfound . " \n " ;
}
2023-09-03 02:05:43 +02:00
$this -> output .= " \n SQL used to find partnerships to scan: " . $sql ;
2021-04-17 15:30:56 +02:00
2021-06-14 15:37:02 +02:00
return ( $error ? 1 : 0 );
2021-04-17 14:25:28 +02:00
}
/**
* Action to check if Dolibarr backlink not found on partner website
*
2022-12-30 18:52:19 +01:00
* @ param string $website Partner ' s website URL
2021-06-09 23:55:56 +02:00
* @ return int 0 if KO , 1 if OK
2021-04-17 14:25:28 +02:00
*/
2021-06-09 23:55:56 +02:00
private function checkDolibarrBacklink ( $website = null )
2021-04-17 14:25:28 +02:00
{
2023-09-15 16:44:18 +02:00
global $conf ;
2021-04-06 16:53:51 +02:00
2021-04-17 14:25:28 +02:00
$found = 0 ;
$error = 0 ;
$webcontent = '' ;
2021-04-06 16:53:51 +02:00
2021-04-17 14:25:28 +02:00
// $website = 'https://nextgestion.com/'; // For Test
2021-06-09 23:55:56 +02:00
$tmpgeturl = getURLContent ( $website , 'GET' , '' , 1 , array (), array ( 'http' , 'https' ), 0 );
2021-04-17 15:30:56 +02:00
if ( $tmpgeturl [ 'curl_error_no' ]) {
2021-04-17 14:25:28 +02:00
$error ++ ;
dol_syslog ( 'Error getting ' . $website . ': ' . $tmpgeturl [ 'curl_error_msg' ]);
} elseif ( $tmpgeturl [ 'http_code' ] != '200' ) {
$error ++ ;
dol_syslog ( 'Error getting ' . $website . ': ' . $tmpgeturl [ 'curl_error_msg' ]);
} else {
$urlContent = $tmpgeturl [ 'content' ];
$dom = new DOMDocument ();
@ $dom -> loadHTML ( $urlContent );
2021-04-06 16:53:51 +02:00
2021-04-17 14:25:28 +02:00
$xpath = new DOMXPath ( $dom );
$hrefs = $xpath -> evaluate ( " //a " );
2024-11-06 20:15:15 +01:00
'@phan-var-force DOMNodeList $hrefs' ;
2021-04-06 16:53:51 +02:00
2021-04-17 15:30:56 +02:00
for ( $i = 0 ; $i < $hrefs -> length ; $i ++ ) {
$href = $hrefs -> item ( $i );
2024-10-05 18:55:36 +02:00
'@phan-var-force DOMElement $href' ;
2021-04-17 15:30:56 +02:00
$url = $href -> getAttribute ( 'href' );
$url = filter_var ( $url , FILTER_SANITIZE_URL );
2024-08-07 03:05:02 +02:00
if ( ! ( ! filter_var ( $url , FILTER_VALIDATE_URL ))) {
2021-04-17 15:30:56 +02:00
$webcontent .= $url ;
}
2021-04-17 14:25:28 +02:00
}
2021-04-17 15:30:56 +02:00
}
2023-11-27 11:56:32 +01:00
if ( $webcontent && getDolGlobalString ( 'PARTNERSHIP_BACKLINKS_TO_CHECK' ) && preg_match ( '/' . getDolGlobalString ( 'PARTNERSHIP_BACKLINKS_TO_CHECK' ) . '/' , $webcontent )) {
2021-04-17 15:30:56 +02:00
$found = 1 ;
2021-04-17 14:25:28 +02:00
}
2021-04-06 16:53:51 +02:00
2021-04-17 14:25:28 +02:00
return $found ;
2021-04-06 16:56:23 +02:00
}
2021-04-06 16:53:51 +02:00
}