I'm trying to pass a DataObjectSet and using <% control MyDataObjectSet %> inside my EmailTemplate.SS but it's not working. I've also tried passing an Array and using also control blocks but still no luck. any suggestions?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Could you post the code you have currently?
<?php
class Notification extends DailyTask {
function process() {
$expiringDocs = new DataObjectSet();
$newDOSet = new DataObjectSet();
foreach( $expiringDocs as $docs ) {
$member = DataObject::get_by_id('Member', (int)$docs->PersonID );
$name = $member->Surname .', '. $member->FirstName;
$record = array(
'FullName' => $name,
'Type' => $docs->Type,
);
$newDOSet ->push(new ArrayData($record));
}
$this->sendEmail($newDOSet);
}
function sendEmail($data) {
$from = 'email@mydomain.com';
$to = 'customer@yahoo.com';
$subject = 'Documents Notification';
$email = new Email($from, $to, $subject);
$email->setTemplate('NotificationTemplate');
$email->populateTemplate($data);
$email->send();
}
}
NotificationTemplate.ss
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en">
<head>
<title></title>
</head>
<body>
<% control data %>
<p>$FullName</p>
<p>$Type</p>
<% end_control %>
</body>
</html>
I think the issue is that you're using <% control data %> - $data was just the variable passed to the populate template. You should pass the populateTemplate() function an array in the format of key => value like so..
$email->populateTemplate(array('MyData' => $data));
Then use <% control MyData %> in the template since that is now the name of $data.
thanks Willr. I'll try that
This post has really helped
the below works, but I'm now thinking what about if i wanted to use the user_forms module.
Will I need to figure out how to extend user_forms in order that i can populate extra data to the userforms SubmittedFormEmail.ss template?
I'm asking just in case there is an alternative way of accessing page_controller functions in email templates, that I'm missing.
mysite > code > Pag.php
So if i have a function in my Page_Controller
public function RegisteredOffice(){
return Office::get()->filter(array('MainOffice' => 1));
//old return DataObject::get("Office", "MainOffice = 1");
}
themes > templates > Page.ss
Then on my Page.ss i can say
<% loop RegisteredOffice %> $Address1 <% end_loop %>
That works
------------
My question is how should I access this RegisteredOffice fuction in my Email Template ContactEmail.ss
Do I have to add it to my $emaildata and then populateTemplate (see code below 'RegisteredOffice' => Page_Controller::RegisteredOffice(), )
ContactPage > code > ContactPage.php
function SendContactForm($data, $form)
{
$DataContactSubmission = new ContactSubmission();
$DataContactSubmission->SiteTreeID = $this->ID;
//echo "<p>email: " . $DataContactSubmission->getField('Email') . "</p>";
// die ('<pre>' . print_r($DataContactSubmission, true) . '</pre>');
$form->saveInto($DataContactSubmission);
//$form->saveInto($Member);
//why do i need this but dont on other example
// $DataContactSubmission->write();
//$Member->login();
if ($DataContactSubmission->write()) {
//Set data
$From = $data['Email'];
$To = $this->Mailto;
$Subject = "CCSW: Website Contact message";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//Put form data into an array
//Clean up returns
$emaildata = array(
'Prefix' => $data['Prefix'],
'FirstName' => $data['FirstName'],
'Surname' => $data['Surname'],
'Company' => $data['Company'],
'DaytimeTel' => $data['DaytimeTel'],
'EveningTel' => $data['EveningTel'],
'Email' => $data['Email'],
'ServiceEnquiry' => $data['ServiceEnquiry'],
'Comments' => str_replace("\n", "<br>", $data['Comments']),
'RegisteredOffice' => Page_Controller::RegisteredOffice(),
// 'SiteConfig' => SiteConfig::current_site_config()
);
//populate template
$email->populateTemplate($emaildata);
//send mail
$email->send();
} else {
$form->sessionMessage(_t('VALUATION_REQUEST_SUBMIT_ERROR', 'There was an error'), 'error');
}
//return to submitted message
//return Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
return $this->redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
}
themes > templates > Email > ContactEmail.ss
<% loop RegisteredOffice %>
$Address1
<% end_loop %>
I'm after a tutorial on how to extend userforms rather than edit line 985 of UserDefinedForm.php
..sometime later
Final Answer:
In case it helps anyone else
mysite >_config.php
Object::add_extension('UserDefinedForm_Controller', 'MyUserDefinedFormExtension');
mysite > code > MysUserDefinedFormExtension.php
<?php
/**
* An Extension to the UserDefinedForm submission process
*
* @package userforms
* Reasoning:
* 1. do not touch source code of userforms module.
* 2. I want to pass data which is captured via the functions in my Page_Controller into the SubmittedFormEmail.ss template
eg <% loop RegisteredOffice %> $Adddress1 <% end_loop %>
*/
/*
Mysite > _config.php contains the following to register this Extended Class
Object::add_extension('UserDefinedForm_Controller', 'MyUserDefinedFormExtension');
*/
class MyUserDefinedFormExtension extends Extension {
private static $allowed_actions = array(
'updateEmailData'
);
public function updateEmailData($emailData, $attachments){
$attachments = null;
$registeredoffices = Controller::curr()->RegisteredOffice();
$footericons = Controller::curr()->getAllFooterIcons();
$NewData = $emailData;
$NewData["RegisteredOffice"] = $registeredoffices;
$NewData["AllFooterIcons"] = $footericons;
$emailData = $NewData;
$this->emailData = $NewData;
return $this->emailData; //return emailData
}
public function updateEmail($email, $recipient, $emailData){
$emailData = $this->emailData;
$email->populateTemplate($emailData);
return;
}
}
also at http://www.sspaste.com/paste/show/53a82f953bf20