Last active
February 8, 2016 00:02
-
-
Save trentsnippets/5794064 to your computer and use it in GitHub Desktop.
Create a contact form in Silverstripe where submissions are emailed to admin and saved to database. There are 3 Silverstripe files included in this gist.
This file contains hidden or 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
<?php | |
/** Important: When adding a second form to a website the second ModalAdmin extention would not function until I added it's | |
* Tested: 3.0 | |
* name into the same $managed_models array as the first. I was then able to take it out and have it work on it's own. | |
* ContactPage.php | |
* This is all the logic for the Contact page. | |
*/ | |
class ContactPage extends Page | |
{ | |
//Set relationship | |
public static $has_many = array( | |
'ContactFormSubmissions' => 'ContactFormSubmission' | |
); | |
} | |
/** | |
* Contact Page Controller | |
*/ | |
class ContactPage_Controller extends Page_Controller | |
{ | |
static $allowed_actions = array('ContactForm'); | |
//Create Contact Form | |
public function ContactForm(){ | |
//Add form fields | |
$fields = new FieldList( | |
new TextField('Name'), | |
new EmailField('Email'), | |
new NumericField('Phone'), | |
new TextField('Book', 'Book A Mobile Advisor'), | |
new TextField('Info', 'Information Request A Product Type'), | |
new TextField('Question', 'Ask A Question'), | |
new CheckboxField('Join', 'Join Our Mailing List') | |
); | |
// Create actions EG. Submit | |
$actions = new FieldList( | |
new FormAction('doContactForm', 'Submit') | |
); | |
//Create Form Validation required fields | |
$validator = new RequiredFields('Name', 'Email'); | |
//Return form as an Object | |
return new Form($this, 'ContactForm', $fields, $actions, $validator); | |
} | |
//Process the contact form | |
public function doContactForm($data, $form){ | |
//Create submission object | |
$submission = new ContactFormSubmission(); | |
//save submission object into form object this function is needed so the submission values will be available from the $submission object. | |
$form->saveInto($submission); | |
$submission->write(); | |
//Email form submission to user. | |
$from = '[email protected]'; | |
//Get SiteConfig to get the email setting to send an email to them. | |
$config = SiteConfig::current_site_config(); | |
$to = $config->ContactFormEmailAddress; | |
$subject = 'Legacy Life Contact Form'; | |
print_r($config->ContactFormEmailAddress); | |
$body = 'Name: '.$submission->Name.'<br />'; | |
$body .= 'Email: '.$submission->Email.'<br />'; | |
$body .= 'Phone: '.$submission->Phone.'<br />'; | |
$body .= 'Book A Mobile Advisor: '.$submission->Book.'<br />'; | |
$body .= 'Information Request A Product Type: '.$submission->Info.'<br />'; | |
$body .= 'Ask A Question: '.$submission->Question.'<br />'; | |
$email = new Email($from, $to, $subject, $body); | |
$email->send(); | |
//Once finished processing form redirect back to contact page. | |
//return $this->redirectBack(); | |
} | |
} | |
?> | |
<?php | |
/** | |
* ContactFormSubmission.php | |
* ContactFormSubmission Controller | |
* | |
*/ | |
class ContactFormSubmission extends DataObject | |
{ | |
//Add new columns to the database | |
static $db = array( | |
'Name' => 'Varchar(100)', | |
'Email' => 'Varchar(100)', | |
'Phone' => 'Varchar(100)', | |
'Book' => 'Varchar(100)', | |
'Info' => 'Varchar(100)', | |
'Question' => 'Varchar(100)', | |
'Join' => 'Varchar(100)' | |
); | |
public static $has_one = array( | |
'ContactPage' => 'ContactPage' | |
); | |
public static $summary_fields = array( | |
'Name' => 'Name', | |
'Email' => 'Email', | |
'Phone' => 'Phone', | |
'Book' => 'Book A Mobile Advisor', | |
'Info' => 'Information Request A Product Type', | |
'Question' => 'Ask A Question', | |
'Join' => 'Join Our Mailing List', | |
); | |
} | |
?> | |
<?php | |
/** | |
* ContactFormAdmin.php | |
* This file will creat the menu in the CMS which displays all contact form entries. | |
*/ | |
class MyAdmin extends ModelAdmin { | |
public static $managed_models = array('ContactFormSubmission'); // Can manage multiple models | |
static $url_segment = 'contact-form'; // Linked as /admin/products/ | |
static $menu_title = 'Contact Form Submissions'; | |
static $menu_icon = 'mysite/images/contact-icon.jpg'; | |
//This function is used to overload the modal admin form and change it. | |
public function getEditForm($id = null, $fields = null) { | |
$form = parent::getEditForm($id, $fields); | |
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass)); | |
$gridField->getConfig()->removeComponentsByType('GridFieldAddNewButton'); | |
$gridField->getConfig()->removeComponentsByType('GridFieldEditButton'); | |
return $form; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment