-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from monkeyfeet/feature/contact
Feature/contact
- Loading branch information
Showing
15 changed files
with
522 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
class ContactPage extends Page { | ||
|
||
private static $db = array( | ||
'ToEmail'=> 'Varchar(255)', | ||
'FromEmail'=> 'Varchar(255)', | ||
'FromName'=> 'Varchar(255)', | ||
'ContactDetails'=> 'HTMLText', | ||
'OnCompletionMessage'=> 'HTMLText'/*, | ||
'LatLong'=> 'Varchar(255)'*/ | ||
); | ||
|
||
private static $has_many = array( | ||
'Submissions' => 'FormSubmission' | ||
); | ||
|
||
static $description = 'Basic contact page'; | ||
static $icon = 'contact/images/contact-page'; | ||
|
||
function getCMSFields(){ | ||
|
||
$fields = parent::getCMSFields(); | ||
|
||
// ContactDetails tab | ||
//$fields->addFieldToTab('Root.ContactDetails', new TextField('LatLong', 'Lat/Long coordinates<br/><em>For Google map</em>')); | ||
$fields->addFieldToTab('Root.ContactDetails', new HTMLEditorField('ContactDetails', 'Contact details')); | ||
|
||
// Emails tab | ||
$fields->addFieldToTab('Root.Emails', new TextareaField('ToEmail', '"To" Email<br/><em>Email addresses to deliver form submissions to. Can be comma-separated list.</em>')); | ||
$fields->addFieldToTab('Root.Emails', new TextField('FromEmail', '"From" & "Reply-to" Email<br/><em>Displayed in form submission email.</em>')); | ||
$fields->addFieldToTab('Root.Emails', new TextField('FromName', 'From name<br/><em>Displayed in form submission email. Defaults to "'.SiteConfig::current_site_config()->Title.' contact form".</em>')); | ||
|
||
// Submissions tab | ||
$GridFieldConfig = GridFieldConfig_RecordEditor::create(); | ||
$SubmissionsField = new GridField( | ||
'Submissions', | ||
'Submissions', | ||
$this->Submissions(), | ||
$GridFieldConfig | ||
); | ||
$fields->addFieldToTab('Root.Submissions', $SubmissionsField); | ||
|
||
// OnCompletion tab | ||
$fields->addFieldToTab('Root.OnCompletion', new HTMLEditorField('OnCompletionMessage', 'Message to show after form submission')); | ||
|
||
return $fields; | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
class ContactPage_Controller extends Page_Controller { | ||
|
||
private static $allowed_actions = array( | ||
'ContactForm', | ||
'submitted' | ||
); | ||
|
||
public function init(){ | ||
parent::init(); | ||
} | ||
|
||
function ContactForm(){ | ||
|
||
$params = $this->request->params(); | ||
|
||
if($params['Action'] == 'submitted'){ | ||
|
||
return $this->OnCompletionMessage; | ||
|
||
}else{ | ||
|
||
$fields = new FieldList( | ||
new TextField('Name', 'Name'), | ||
new EmailField('Email', 'Email'), | ||
//new TextField('Phone', 'Phone'), | ||
new TextareaField('Message', 'Message'), | ||
new HiddenField('ContactPageID', null, $this->ID) | ||
); | ||
|
||
$actions = new FieldList( | ||
new FormAction('doContactForm', 'Send') | ||
); | ||
|
||
// Validate required fields | ||
$validator = new RequiredFields('Name', 'Email', 'Message'); | ||
|
||
$form = Form::create($this, 'ContactForm', $fields, $actions, $validator)->addExtraClass('contact-form'); | ||
|
||
return $form; | ||
|
||
} | ||
|
||
} | ||
|
||
function doContactForm($data, $form) { | ||
|
||
// create form submission record | ||
$Submission = FormSubmission::create(); | ||
$Submission->URL = $this->Link(); | ||
$Submission->Payload = json_encode($data); | ||
$Submission->OriginID = $this->ID; | ||
$Submission->OriginClass = $this->ClassName; | ||
$Submission->write(); | ||
$Submission->SendEmails(); | ||
|
||
$this->redirect($this->Link().'submitted'); | ||
} | ||
|
||
/*** | ||
* Send email | ||
***/ | ||
function EmailAdmin($submission){ | ||
|
||
if( !empty($this->FromName) ){ | ||
$FromName = $this->FromName; | ||
} | ||
else{ | ||
$FromName = $this->SiteConfig()->Title.' contact form'; | ||
} | ||
|
||
$from = $FromName . ' <' . $this->FromEmail . '>'; | ||
$to = $this->ToEmail; | ||
//$to = Email::setAdminEmail(); | ||
$subject = 'A new submission has been received from '.$FromName; | ||
$body = ''; | ||
|
||
$email = new Email($from, $to, $subject, $body); | ||
|
||
//set template | ||
$email->setTemplate('AdminEmail'); | ||
|
||
//populate template | ||
$email->populateTemplate($submission); | ||
|
||
//send mail | ||
$email->send(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,185 @@ | ||
<?php | ||
class FormSubmission extends DataObject { | ||
|
||
static private $db = array( | ||
'FormData' => 'Text', | ||
'Name' => 'Varchar(255)', | ||
'Email' => 'Varchar(255)' | ||
); | ||
|
||
static private $has_one = array( | ||
'Page' => 'Page' | ||
); | ||
class FormSubmission extends DataObject { | ||
|
||
static $summary_fields = array( | ||
'Created' => 'Date', | ||
'Name' => 'Name', | ||
'Email' => 'Email' | ||
private static $singular_name = 'Form submission'; | ||
private static $plural_name = 'Form submissions'; | ||
private static $description = 'The payload for all contact form submissions'; | ||
private static $default_sort = 'Created DESC'; | ||
|
||
private static $db = array( | ||
'URL' => 'Varchar', | ||
'Payload' => 'Text', | ||
'IPAddress' => 'Varchar(18)', | ||
'UniqueID' => 'Text' | ||
); | ||
|
||
static $default_sort = 'Created DESC'; | ||
private static $has_one = array( | ||
'Origin' => 'DataObject' | ||
); | ||
|
||
public static $searchable_fields = array( | ||
'Created', | ||
'Name', | ||
'Email' | ||
private static $summary_fields = array( | ||
'Created' => 'Created', | ||
'URL' => 'URL', | ||
'OriginType' => 'Origin', | ||
'IPAddress' => 'IP Address', | ||
'UniqueID' => 'UniqueID' | ||
); | ||
|
||
/** | ||
* CMS Fields | ||
**/ | ||
public function getCMSFields(){ | ||
$fields = FieldList::create(); | ||
|
||
$fields->push(TextField::create('URL','URL')); | ||
$fields->push(TextField::create('IPAddress','IPAddress')); | ||
$fields->push(TextField::create('UniqueID','UniqueID')); | ||
|
||
$fields->push(LiteralField::create('html','<br /><h3>Submission data</h3>')); | ||
$data = json_decode($this->Payload, true); | ||
foreach ($data as $key => $value){ | ||
if ($key == 'AttendeesData'){ | ||
$fields->push(ReadonlyField::create('Playload_'.$key,$key,json_encode($value))); | ||
} else { | ||
$fields->push(ReadonlyField::create('Playload_'.$key,$key,(string)$value)); | ||
} | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
function onBeforeWrite(){ | ||
$this->UniqueID = $this->CreateUniqueID($this->Created); | ||
parent::onBeforeWrite(); | ||
} | ||
|
||
public function OriginType(){ | ||
if( $this->OriginClass ) return $this->OriginClass; | ||
if( $this->OriginID <= 0 ) return '-'; | ||
return $this->Origin()->ClassName; | ||
} | ||
|
||
|
||
/** | ||
* Send emails responding to this submission | ||
**/ | ||
public function SendEmails(){ | ||
|
||
// convert payload to array for template | ||
$data = json_decode($this->Payload); | ||
|
||
// set up email "from" vars | ||
$config = SiteConfig::current_site_config(); | ||
if( $config->SendEmailsFrom_Name && $config->SendEmailsFrom_Email ){ | ||
$from = '"'.$config->SendEmailsFrom_Name.'" <'.$config->SendEmailsFrom_Email.'>'; | ||
}else{ | ||
$from = 'Divine Laziness <[email protected]>'; | ||
} | ||
|
||
// define time var to use in template | ||
$body = ''; | ||
$data->TimeSent = date('Y-m-d H:i:s'); | ||
$data->UniqueID = $this->UniqueID; | ||
|
||
// different sources require different handling | ||
switch ($this->OriginType()){ | ||
|
||
case 'ContactPage': | ||
// --- ADMIN EMAIL --- | ||
if( isset($this->Origin()->AdminEmail) ){ | ||
$to = $this->Origin()->AdminEmail; | ||
}else{ | ||
$to = $config->SendEmailsTo_Email; | ||
} | ||
$subject = $config->Title . ' Website contact form submission'; | ||
$data->Title = $data->Name . ' has made an enquiry.'; | ||
$data->URL = Director::absoluteBaseURL(); | ||
$data->Data = ArrayList::create(array( | ||
ArrayData::create(array( | ||
'IsHeading' => true, | ||
'Value' => 'Details' | ||
)), | ||
ArrayData::create(array( | ||
'Title' => 'Name', | ||
'Value' => $data->Name | ||
)), | ||
ArrayData::create(array( | ||
'Title' => 'Email', | ||
'Value' => $data->Email | ||
)), | ||
ArrayData::create(array( | ||
'Title' => 'Message', | ||
'Value' => $data->Message | ||
)) | ||
)); | ||
$email = Email::create($from, $to, $subject, $body); | ||
$email->replyTo($data->Email); | ||
$email->setTemplate('Emails/FormSubmission'); | ||
$email->populateTemplate( ArrayData::create($data) ); | ||
$email->send(); | ||
|
||
// --- CUSTOMER EMAIL --- | ||
$to = '"'.$data->Name.'" <'.$data->Email.'>'; | ||
$data->Title = 'Thanks for your message, we\'ll be in touch soon. The details you submitted are included below for your own records.'; | ||
$email = Email::create($from, $to, $subject, $body); | ||
$email->setTemplate('Emails/FormSubmission'); | ||
$email->populateTemplate( ArrayData::create($data) ); | ||
$email->send(); | ||
|
||
break; | ||
|
||
default: | ||
|
||
// --- ADMIN EMAIL --- | ||
$to = $config->SendEmailsTo_Email; | ||
$subject = $config->Title . ' Website form submission'; | ||
$data->Title = $data->Name . ' sent you a message through the website.'; | ||
$email = Email::create($from, $to, $subject, $body); | ||
$email->replyTo($data->Email); | ||
$email->setTemplate('Emails/FormSubmission'); | ||
$email->populateTemplate( ArrayData::create($data) ); | ||
$email->send(); | ||
|
||
// --- CUSTOMER EMAIL --- | ||
$to = '"'.$data->Name.'" <'.$data->Email.'>'; | ||
$data->Title = 'Thanks for your message, we\'ll be in touch soon. The details you submitted are included below for your own records.'; | ||
$email = Email::create($from, $to, $subject, $body); | ||
$email->setTemplate('Emails/FormSubmission'); | ||
$email->populateTemplate( ArrayData::create($data) ); | ||
$email->send(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
/** | ||
* Create unique ID for submission | ||
* | ||
* @param $str string | string to hash | ||
* | ||
* @return string | hashed string | ||
**/ | ||
function CreateUniqueID($str){ | ||
return md5($str . microtime()); | ||
} | ||
|
||
function EditLink(){ | ||
return Director::absoluteBaseURL() . 'admin/form-submissions/FormSubmission/EditForm/field/FormSubmission/item/'.$this->ID.'/edit'; | ||
} | ||
|
||
function DataToHTMLList($data){ | ||
$str = '<ul>'; | ||
foreach($data as $key => $val){ | ||
switch($key){ | ||
case 'SecurityID': | ||
// don't include | ||
break; | ||
default: | ||
$str .= '<li><strong>'.$key.':</strong> '.$val.'</li>'; | ||
} | ||
} | ||
$str .= '</ul>'; | ||
return $str; | ||
} | ||
|
||
} |
Oops, something went wrong.