Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.86 KB | None | 0 0
  1. <?php
  2.  
  3. class ContactPage extends Page {
  4.  
  5.     private static $icon = 'Boilerplate/code/Modules/ContactForm/images/envelope-at-sign.png';
  6.  
  7.     public static $db = array(
  8.         'MailTo' => 'Varchar(100)',
  9.         'SubmitText' => 'Text',
  10.         'GoogleAPI' => 'Varchar(255)',
  11.         'Latitude' => 'Varchar(255)',
  12.         'Longitude' => 'Varchar(255)',
  13.         'MapColor' => 'Varchar(255)',
  14.         'WaterColor' => 'Varchar(255)',
  15.         'MapMarker' => 'Boolean(1)'
  16.     );
  17.  
  18.     public static $defaults = array(
  19.         'SubmitText' => 'Thank you for contacting us, we will get back to you as soon as possible.'
  20.     );
  21.  
  22.     private static $description = 'Contact form page';
  23.  
  24.     public function getCMSFields() {
  25.  
  26.         $fields = parent::getCMSFields();
  27.  
  28.         Requirements::javascript('Boilerplate/javascript/colorpicker.min.js');
  29.         Requirements::css('Boilerplate/css/colorpicker.css');
  30.  
  31.         $fields->addFieldToTab('Root.Main', new LiteralField('js',
  32.         '<script type="text/javascript">
  33.             (function($) {
  34.                 $(document).ready(function() {
  35.                     $(\'.color-picker\').on(\'click\', function(){
  36.                         $(this).iris({
  37.                             hide: false,
  38.                             change: function(event, ui) {
  39.                                 var $c, $r, $g, $b, $mid;
  40.                                 $(this).css(\'backgroundColor\', ui.color.toString());
  41.                             }
  42.                         });
  43.                     });
  44.                 });
  45.             })(jQuery);
  46.         </script>'
  47.         ));
  48.  
  49.         /* =========================================
  50.          * Settings
  51.          =========================================*/
  52.  
  53.         /* -----------------------------------------
  54.          * Contact Page
  55.         ------------------------------------------*/
  56.  
  57.         $fields->addFieldToTab("Root.Main", new HeaderField("Contact Form"), 'Content');
  58.         $fields->addFieldToTab("Root.Main", new TextField("MailTo", "Choose an email address for the contact page to send to"), 'Content');
  59.         $fields->addFieldToTab("Root.Main", new TextareaField("SubmitText", "Text for contact form submission"), 'Content');
  60.  
  61.         /* -----------------------------------------
  62.          * Google Map
  63.         ------------------------------------------*/
  64.  
  65.         $toggleFields = ToggleCompositeField::create(
  66.             'GoogleMap',
  67.             'Google Map',
  68.             array(
  69.                 new Textfield('GoogleAPI', 'Maps API (Optional)'),
  70.                 new Textfield('Latitude', 'Latitude'),
  71.                 new Textfield('Longitude', 'Longitude'),
  72.                 new ColorField('MapColor', 'Map Colour (Optional)'),
  73.                 new ColorField('WaterColor', 'Water Colour (Optional)'),
  74.                 new CheckboxField('MapMarker', 'Map Marker (Optional)'),
  75.             )
  76.         )->setHeadingLevel(4)->setStartClosed(true);
  77.         $fields->addFieldToTab('Root.Main', $toggleFields, 'Content');
  78.  
  79.         return $fields;
  80.  
  81.     }
  82.  
  83. }
  84.  
  85. class ContactPage_Controller extends Page_Controller {
  86.  
  87.     private static $allowed_actions = array('ContactForm');
  88.  
  89.     public function init() {
  90.  
  91.         parent::init();
  92.  
  93.         if($this->Latitude && $this->Longitude){
  94.  
  95.             if($this->MapColor != ''){
  96.                 $mapColor = "'".$this->MapColor."'";
  97.             }else{
  98.                 $mapColor = 'false';
  99.             }
  100.             if($this->WaterColor != ''){
  101.                 $waterColor = "'".$this->WaterColor."'";
  102.             }else{
  103.                 $waterColor = 'false';
  104.             }
  105.             Requirements::javascript('https://maps.googleapis.com/maps/api/js?key='.$this->GoogleAPI.'&sensor=false');
  106.             Requirements::javascript('Boilerplate/code/Modules/ContactForm/js/mapScript.js');
  107.             Requirements::customScript(<<<JS
  108. (function($) {
  109.     $(document).ready(function(){
  110.         getMap($this->Latitude, $this->Longitude, $mapColor, $waterColor, $this->MapMarker)
  111.     })
  112. })(jQuery);
  113. JS
  114. );
  115.  
  116.         }
  117.  
  118.  
  119.     }
  120.  
  121.     /*
  122.      * Create Contact Form
  123.      * @returns Form
  124.      * */
  125.     public function ContactForm() {
  126.  
  127.         $name = new TextField('Name');
  128.         $name->setAttribute('placeholder', 'Enter your name');
  129.         $name->setAttribute('required', 'required');
  130.         $name->addExtraClass('form-control');
  131.         if($nameVal = Session::get('Form_ContactForm_Name.data.Name')){
  132.             $name->setValue($nameVal);
  133.         }
  134.  
  135.         $email = new EmailField('Email');
  136.         $email->setAttribute('placeholder', 'Enter your email address');
  137.         $email->setAttribute('required', 'required');
  138.         $email->addExtraClass('form-control');
  139.         if($emailVal = Session::get('Form_ContactForm_Name.data.Email')){
  140.             $email->setValue($emailVal);
  141.         }
  142.  
  143.         $message = new TextareaField('Message');
  144.         $message->setAttribute('placeholder', 'Enter your message');
  145.         $message->setAttribute('required', 'required');
  146.         $message->addExtraClass('form-control');
  147.         if($messageVal = Session::get('Form_ContactForm_Name.data.Message')){
  148.             $message->setValue($messageVal);
  149.         }
  150.  
  151.         $question = new TextField('Question');
  152.         $question->setTitle('3 &plus; 7 &equals; ?');
  153.         $question->setAttribute('required', 'required');
  154.         $question->addExtraClass('form-control');
  155.         if($questionVal = Session::get('Form_ContactForm_Name.data.Question')){
  156.             $question->setValue($questionVal);
  157.         }
  158.  
  159.         $fields = new FieldList(
  160.             $name,
  161.             $email,
  162.             $message,
  163.             $question
  164.         );
  165.  
  166.         $action = new FormAction('SendContactForm', 'Submit Form');
  167.         $action->addExtraClass('btn btn-primary btn-lg');
  168.  
  169.         $actions = new FieldList(
  170.             $action
  171.         );
  172.  
  173.         $validator = new RequiredFields(
  174.             'Name',
  175.             'Email',
  176.             'Message'
  177.         );
  178.  
  179.         return new Form($this, 'ContactForm', $fields, $actions, $validator);
  180.     }
  181.  
  182.     /*
  183.      * Function to send contact form email
  184.      * @returns Redirection
  185.      * */
  186.     function SendContactForm($data, $form) {
  187.  
  188.         Session::set('Form_ContactForm_Name.data', $data);
  189.  
  190.         if((int)$data['Question']!=10){
  191.             $form->AddErrorMessage('Question', 'Sorry, the question was answered incorrectly', 'validation');
  192.             return $this->redirectBack();
  193.         }
  194.  
  195.         $From = $data['Email'];
  196.         $To = $this->MailTo;
  197.         $Subject = "Website Contact message";
  198.         $email = new Email($From, $To, $Subject);
  199.         $email->setTemplate('ContactEmail');
  200.         $email->populateTemplate($data);
  201.         $email->send();
  202.         if($this->SubmitText){
  203.             $submitText = $this->SubmitText;
  204.         }else{
  205.             $submitText = 'Thank you for contacting us, we will get back to you as soon as possible.';
  206.         }
  207.         $this->setFlash($submitText, 'success');
  208.         return $this->redirect($this->Link("?success=1"));
  209.  
  210.     }
  211.  
  212.     public function Success() {
  213.         return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
  214.     }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement