This topic is locked
[SOLVED]

 Formatting Documents, Converting to PDF and Emailing

12/2/2013 6:52:25 PM
PHPRunner General questions
C
CWDATA author

I have the following code in the On Server Event of a Button inserted into a list page.
The object of the code is to extract records from tables, place them into 3 documents, convert them to PDF and them email them.
I need someone who is really hot in these matters to assist me in getting this to work. I have used this code previously and it worked ok but now it isn't.

If you are a wiz kid at these things and think you could help me, could you please let me know.
// ATTACHING PDF CONVERTER

global $conn;
// REMOVING TIME LIMIT
ini_set("display_errors","1");
@set_time_limit(1000);
// GETTING RECORD DATA
// TRYING TO GET CURRENT RECORD DATA. WORKS IF BUTTON IS ON RECORD DETAILS PAGE
$data = $button->getCurrentRecord();
// GETTING RECORD DATA FROM LIST PAGE
if ( !$data ) { $data = $button->getNextSelectedRecord(); }
// UNCOMMENT LINE BELLOW TO GET CUSTOM RECORD DATA. !!! FOR TESTING PURPOSES ONLY
// $data = db_fetch_array( CustomQuery('SELECT FROM client WHERE ClientID = 8') );
$query = 'SELECT ContractDocsSent FROM tblprospectdataV2 WHERE Unique ID = ' . intval( $data['Unique ID'] ) ;
$qR = CustomQuery( $query );
$str = db_fetch_array( $qR );
if ( intval($str['ContractDocsSent']) ) {
$result = array( "txt" => "Documents already sent. Please refer to Client Document Directory.");
}

else {
// FOLLOWING CODE RUNS ONLY IF DOCUMENTS WERE NOT SENT EARLIER
include("plugins/MPDF57/mpdf.php");
// FORMING RANDOM NAME OF THE TEMPORARY FILE
$fname = "";
for ( $i=0; $i<15; $i++) { $fname .= chr(rand(97, 122)); }
// GETTING CONTENTS OF FILES TO SEND
$html[1] = str_replace( Array(chr(10), chr(13), chr(9)), "", file_get_contents( getabspath('docs/cover-letter.html') ));
$html[2] = str_replace( Array(chr(10), chr(13), chr(9)), "", file_get_contents( getabspath('docs/terms-conditions.html') ));
$html[3] = str_replace( Array(chr(10), chr(13), chr(9)), "", file_get_contents( getabspath('docs/areas.html') ));
// CREATING NAMES OF FILES TO BE SAVED IN DB clientdocumentsupload
$temp_name = array( 1 => ( $data['Company Name'] . "CL"), 2 => ( $data['Company Name'] . "T&C"), 3=> ( $data['Company Name'] . "Areas") ); // to be save to DB
// PREPARING DATA TO SAVE IN DB
$temp_sql_cmd = Array();
$temp_sql_where = Array();
// CHECKING WHAT FILES FROM STATED ABOVE ALREADY EXIST. FOR THESE FILES WE STATE AN SQL COMMAND "UPDATE" AND "WHERE" CLAUSE
$query = 'SELECT
FROM tblcontractdocumentsV2 WHERE Unique ID = ' . intval( $data['Unique ID'] ) . ' AND filename IN ("' . implode('","', $temp_name) . '")';
$qR = CustomQuery($query);
while ($str = db_fetch_array($qR) ){
$temp_k = array_search( $str['filename'], $temp_name);
if ( $temp_k ) {
$temp_sql_cmd[ $temp_k ] = 'UPDATE tblcontractdocumentsV2 SET ';
$temp_sql_where[ $temp_k ] = ' WHERE DocumentID= ' . $str['DocumentID'];
}

}
// FOR FILLES THAT WERE NOT FOUND IN DB WE STATE AN SQL COMMAND "INSERT INTO"
foreach( $temp_name as $k=>$v ) {
if ( !isset( $temp_sql_cmd[ $k ] ) ) {
$temp_sql_cmd[ $k ] = 'INSERT INTO tblcontractdocumentsV2 SET Unique ID=' . intval( $data['Unique ID'] ) . ',';
}
}
// LOOP THROUGH FILES TO CONVERT THEM TO PDF
for ($i=1; $i<=3; $i++) {
// PLACING CLIENT DATA INTO PLACES
$html[$i] = str_replace( '%Client Name%', $data['Client Name'], $html[$i]);
$html[$i] = str_replace( '%Mailing Address Line 1%', ( $data['Mailing Address Line 1'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Address Line 1'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%Mailing Address Line 2%', ( $data['Mailing Address Line 2'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Address Line 2'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%Mailing Address Line 3%', ( $data['Mailing Address Line 3'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Address Line 3'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%Mailing Address Line 4%', ( $data['Mailing Address Line 4'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Address Line 4'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%Mailing Address Line 5%', ( $data['Mailing Address Line 5'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Address Line 5'] . "<br />"): ""), $html[$i]);
$html[$i] = str_replace( '%Mailing Town%', ( $data['Mailing Town'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Town'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%County%', ( $data['County'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['County'] . "<br />"): ""), $html[$i]);

$html[$i] = str_replace( '%Mailing Postcode%', ( $data['Mailing Postcode'] ? ("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $data['Mailing Postcode'] . "<br />"): ""), $html[$i]);
$html[$i] = str_replace( '%date%', str_replace(Array("st ", "nd ", "rd ", "th "), Array("<sup>st</sup> ", "<sup>nd</sup> ", "<sup>rd</sup> ", "<sup>th</sup> "), date('jS F Y') ), $html[$i]);

$html[$i] = str_replace( '%fdate%', ( str_replace(Array("st ", "nd ", "rd ", "th "), Array("<sup>st</sup> ", "<sup>nd</sup> ", "<sup>rd</sup> ", "<sup>th</sup> "), date('jS') ) . " day of " . date(' F Y')), $html[$i]);
$html[$i] = str_replace( '%Contact Prefix%', $data['Contact Prefix'],$html[$i]);

$html[$i] = str_replace( '%Contact First Name%', $data['Contact First Name'], $html[$i]);

$html[$i] = str_replace( '%Contact Surname%', $data['Contact Surname'], $html[$i]);
$html[$i] = strreplace( '%Meeting Date%', $data['Meeting Date'], $html[$i]);
// CREATING PDF
if ( $i == 3 ) $mpdf=new mPDF('c','A4','','',7,7,5,5,5,5); / this is for "Areas we cover" with smaller margins, cause no headers/

else $mpdf=new mPDF('c','A4','','',15,10,32,15,5,5);
$mpdf->SetDisplayMode('fullpage');

$mpdf->useActiveForms = true;

// $mpdf->SetHTMLHeader($header); // this was disabled because headers were embedded in html

// $mpdf->SetHTMLFooter($footer); // this was disabled because headers were embedded in html

$mpdf->WriteHTML($html[$i]);

$mpdf->Output(getabspath( getabspath('docs/' . $fname . '
' . $i . '.pdf'), 'F'));
// SAVING FILES TO DATABASE
if ( $data['Unique ID'] ) {
$temp_pdf = file_getcontents( getabspath('docs/' . $fname . '' . $i . '.pdf') );
$query = $temp_sql_cmd[ $i ] . 'filename = "' . $conn->real_escape_string($temp_name[$i]) . '", document="' . $conn->real_escape_string($temp_pdf) . '"' . ( $temp_sql_where[ $i ] ? $temp_sql_where[ $i ] : "" );
$temp_pdf = null; // free memory
CustomQuery($query);
$query = null; // free memory

}
unset( $html[$i] ); // free memory

}
// SENDING EMAIL
$from = "simonu@procurementgroup.co.uk"; // here must be your email
$to = ($data['Contract Signatory Email']?$data['Contract Signatory Email']:"simonu@procurementgroup.co.uk"); // HERE MUST BE USER eMAIL
$msg = "<h1>Hi %user%</h1><p>Please find attached your contract documents.</p><p>Best regards</p><p>Simon Unger</p>";
$msg = str_replace( '%user%', $data['Contact First Name'], $msg);
$subject="Contract Documents";
$attachments = array(

array('path' => getabspath('docs/' . $fname . '_1.pdf'), 'name' => 'Cover-letter.pdf' ),

array('path' => getabspath('docs/' . $fname . '_2.pdf'), 'name' => 'Terms-n-Conditions.pdf' ),

array('path' => getabspath('docs/' . $fname . '_3.pdf'), 'name' => 'Areas that we cover.pdf' )

) ;
$ret = runner_mail(array('from' => $from, 'to' => $to, 'subject' => $subject, 'htmlbody' => $msg, 'charset' => 'UTF-8', 'attachments' => $attachments));
if(!$ret["mailed"]){

$result = array("txt" => "FAILED: " . $conn->real_escape_string( $ret["message"]) );

}

else {

unlink(getabspath('docs/' . $fname . '_1.pdf'));

unlink(getabspath('docs/' . $fname . '_2.pdf'));

unlink(getabspath('docs/' . $fname . '_3.pdf'));
$query = 'UPDATE tblprospectdataV2 SET ContractDocsSent = 1 WHERE Unique ID = ' . intval( $data['Unique ID'] ) ;

CustomQuery($query );

$query = 'UPDATE tblprospectdataV2 SET ContractDocsSentDate = CURDATE() WHERE Unique ID = ' . intval( $data['Unique ID'] ) ;

CustomQuery($query );
$result = array( "txt" => "SENT");

}

}