function to loop through a browse from sending the appropriate email depending on the values of the current row
This function starts at row 0
Determines the email that should be sent (using sdass and sdimm and other values)
The procedure sends the email (using the values found in the setting table and the template
updates the required tabel information
Increments drow
Calls the JS function testcase(drow) using callback function
Here is how I retrive and use the data need for my email
browse custom code
Code: Select all
//>>>JS
function testcase(drow){[
// Get the value of PK from cell 0
let idvtt=$('#nucell_'+drow+'_0').html(); //drow is the current row in the browse form
//sdass and sdimm are variables to determine which emails to send depending on certain values (works)
// drow is used to determine the next row to be use with the jscallback
nuRunPHPHiddenWithParams('Test_RHWP', 'param',{idvtt:idvtt,drow:drow,sdass:sdass,sdimm:sdimm},0);
}
Proccedure Test_RHWP
Code: Select all
//>>>>PHP 'Test_RHWP' Procedure
function decodeEncodedString($encodedString) {
$decodedString = base64_decode($encodedString);
$decodedArray = json_decode($decodedString, true);
return $decodedArray;
}
$result = decodeEncodedString('#param#');
if ($result != null) {
// Retrieve values
$idvtt = $result['idvtt'];
$drow=$result['drow'];
$sdass=$result['sdass'];
$sdimm=$result['sdimm'];
// Retrive the data from a settings table that contains setting for sending email
$setting_sql="SELECT ID_setting,vttemail_from,vttemail_fromname,vttemail_attfile,vttemail_html,vttemail_replyadd FROM tbl_vttsettings WHERE ID_setting=1";
$setting_sql_r=nuRunQuery($setting_sql);
if(db_num_rows($setting_sql_r)<1) {
nuDisplayError('Enregistrement introuvable!code 001');
}else{
$set_result=db_fetch_row($setting_sql_r);
$emailfrom=$set_result[1]; // email from address
$emailfromname=$set_result[2]; //email from name
$emailatt=$set_result[3]; // email att file name
$emailhtml=$set_result[4]; // email html
$emailreply=$set_result[5]; // email reply to address
}
//Retrive data from table useing PK &idvtt
$sql_select="
SELECT tbl_vtt.ID_vtt,
tbl_vtt.PQ_vtt,
tbl_vtt.AX_vtt,
tbl_vtt.WR_vttass30,
tbl_vtt.WR_asspd,
tbl_vtt.WR_vttimm30,
tbl_vtt.WR_immpd,
tbl_vtt.DE_vttass,
tbl_vtt.DE_vttimm,
datediff(tbl_vtt.DE_vttass,now()) as jrass,
datediff(tbl_vtt.DE_vttimm,now()) as jrimm,
tbl_membre.NC_membre,
tbl_membre.CR_membre
FROM tbl_vtt
JOIN tbl_membre ON tbl_vtt.ID_mbvtt = tbl_membre.ID_membre
WHERE ID_vtt=".$idvtt;
$stmt = nuRunQuery($sql_select);
$num_rows = db_num_rows($stmt);
//===================================
if($num_rows>0){
$row=db_fetch_row($stmt);
}else{
nuDisplayError('Enregistrement introuvable! code 002'); // end if($num_rows>0)
}
// Retrieve the template data
$templateR = nuGetEmailTemplateData($template,"","vtt_reminder");
if ($templateR == false) {
nuDisplayError('Unknown email template! code 003');
return;
//Set the email params
$email_params = array(
'to' => $row[12], // email addresse from tbl_vtt
'cc' => $templateR['cc'], // cc from template
'bcc' => $templateR['bcc'], //bcc from template
'body' => nl2br($templateR['body']),//body from template
'subject' => $templateR['subject'] //subject from template
);
// set properties of other data needed for email fields
nuSetProperty('ID_vtt',$row[0]);
nuSetProperty('PQ_vtt',$row[1]);
nuSetProperty('AX_vtt',$row[2]);
nuSetProperty('WR_vttass30',$row[3]);
nuSetProperty('WR_asspd',$row[4]);
nuSetProperty('WR_vttimm30',$row[5]);
nuSetProperty('WR_immpd',$row[6]);
nuSetProperty('DE_vttass',$row[7]);
nuSetProperty('DEvttimm',$row[8]);
nuSetProperty('jrass',$row[9]);
nuSetProperty('jrimm',$row[10]);
nuSetProperty('NC_membre',$row[11]);
foreach ($email_params as $key => $value) { // replace params value
$email_params[$key] = nuReplaceHashVariables($value);
}
// Set the tag text
$tag_text='VTT '.nuGetProperty('ID_vtt');
//>>>>>>send email
$r=nuSendEmail([
'to' =>$email_params['to'], //
'cc' => $email_params['cc'],
'bcc' =>$email_params['bcc'],
'body' => $email_params['body'],
'subject' =>$email_params['subject'],
'from'=> $emailfrom, // from addresse setting table
'fromname'=> $emailfromname,
'attachments'=>[$filename=>$file],
'html'=>$emailhtml,
'reply_to_addresses'=>$emailreply
],$logOptions = [
'tag' => $tag_text]);
}
//if successful I update my tables
//increment drow
$drow=$drow+1;
//use the callback to restart the function on the next row
$js = "testcase($drow);";
nuJavaScriptCallback($js);