This topic is locked
[SOLVED]

Email PDF from view page issue

9/15/2021 12:28:58 AM
PHPRunner General questions
D
david22585 author

So I've tried about 30 differant ways to make this work, and I just can't figure it out. I am trying to email a PDF with the information on the view page, but based on the format of the print page using a custom button. On my event codes -> Before Display, I used:
$pageObject->setProxyValue("id", $values["id"]);

This is used to set a proxy value in the button code. On the button code, I use the following on Client Before:

var params = {
pageType: 'print',
selection: proxy['id'],
keys: proxy['id'],
scope: '2'
}

ajax.addPDF( 'pdf', params, function() {
return ctrl.dialog( {
title: 'Email this page',
fields: [{
name: 'email',
value: ''
},
{
name: 'filename',
value: proxy['id']
}]
});
});
return false;

I based this on the following manual info: https://xlinesoft.com/phprunner/docs/pdf_parameters.htm

For scope, I've tried 0 and 2. For keys and selection, I am getting the proper ID of the record, as I used it in the filename area and it shows the proper record ID. The problem is that it is emailing all the records and not just the inidividual record on the view page. It is basing it upon the print page, but just not the 1 record on the view page. I was wondring if anyone had any suggestions on why it's printing all the records and not just the record specified by the selection, keys, or scope parameter?

D
david22585 author 9/15/2021

This is also with PHPRunner 10.5.37251

D
david22585 author 9/15/2021

After trying about 500 ways, I got it to work with a bit more to it as well. This is used to submit a work order to a contractor. I wanted to make it as easy and as interactive as possible. On the view page events, Before Display, I have the following code:

$pageObject->setProxyValue("id", $values["id"]);

if ($values["contractor"] != ""){
$contractoremail = DBLookup("SELECT email FROM contractors WHERE company_id = ".$values['contractor']."");
$pageObject->setProxyValue("contractoremail", "".$contractoremail."");
$companyname = DBLookup("SELECT companyname FROM contractors WHERE company_id = ".$values['contractor']."");
$pageObject->setProxyValue("companyname", "".$companyname."");
} else {
$pageObject->hideItem("woemail");
}

The first line sets a proxy value of the id for the work order to be used in the PDF creation. The next part looks to see if a contractor has ben assigned to the work order, and if there hasn't been, hides the button. If a contractor was assigned, it sets a proxy value for their email address and name.

Next up is the button code. On Client Before, I used the following code:

var params = {
page: 'print',
pageType: 'print',
selection: [[proxy["id"]]]
}

ajax.addPDF( 'pdf', params, function() {
return ctrl.dialog( {
title: 'Email Work Order to Contractor',
fields: [{
name: 'email',
label: 'Email address',
value: proxy["contractoremail"]
},
{
name: 'filename',
label: 'File Name',
value: 'Work Order '+proxy["id"]
}],
ok: 'Send Work Order'
});
});
return false;

The first part sets the parameters for the addPDF API. The first 2 lines set the page to use as the print page layout. The "selection" is the array creation from the proxy set from the ID on the view page. Since it's being email from the view page, we want to use the print page layout, but we only need the 1 ID for the array.

Within the addPDF section for the email, using the proxy automatically fills in their email address from their record obtain in the before display event.

On the server event, I use SendGrid instead of the default PHPRunner stuff, so my stuff within there is custom done for that.

$path = $button->saveTempFile( $params["pdf"] );

$email = new \SendGrid\Mail\Mail();
$email->setFrom("Email@Address.com", "Email Name");
$email->addTo($params["email"]);
$email->setSubject("Work Order Request");
$email->addContent("text/plain", "work order request.");
$email->addContent("text/html", "work order request.");
$file_encoded = base64_encode(file_get_contents($path));
$email->addAttachment(
$file_encoded,
"application/pdf",
"".$params["filename"]."",
"attachment"
);
$sendgrid = new \SendGrid('send_grid_key_here);
try {
$response = $sendgrid->send($email);
//print $response->statusCode() . "\n";
//print_r($response->headers());
//print $response->body() . "\n";
$result["success"] = true;
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."\n";
$result["message"] = $ret["message"];
$result["success"] = false;
}

For the Client After, I wanted a nice sweet alert that it was sent

if( result.success ) {
ctrl.setMessage("Work order has been emailed to "+proxy["companyname"]);
swal({
text: "Work order has been sent to "+proxy["companyname"],
icon: "success",
timer: 4000
});
} else {
ctrl.setMessage("error sending " + result.message );
}

Now you get a nice sweet alert saying it was sent to the companies name, and disappears after 4 seconds.