Advertisement
Guest User

Untitled

a guest
Jul 14th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. // CONTACT FORM
  2. class ContactForm extends Form {
  3.    
  4.     public function __construct( $controller, $name )
  5.     {
  6.         // Fields
  7.         $nameField = TextField::create('Name', 'Name');
  8.         $nameField->setAttribute('placeholder', 'Name');
  9.        
  10.         $emailField = TextField::create('Email', 'E-Mail Address');
  11.         $emailField->setAttribute('placeholder', 'E-Mail address');
  12.        
  13.         $messageField = TextAreaField::create('Message', 'Message');
  14.         $messageField->setAttribute('placeholder', 'Your message');
  15.        
  16.         $fields = new FieldList($nameField, $emailField, $messageField);
  17.        
  18.         // Actions
  19.         $actions = new FieldList(
  20.             FormAction::create('doSubmit', 'Send Message')
  21.         );
  22.        
  23.         // Validator
  24.         $validator = new RequiredFields('Name', 'Email', 'Message');
  25.        
  26.         return parent::__construct( $controller, $name, $fields, $actions, $validator );
  27.     }
  28.    
  29.     public function doSubmit( array $data, Form $form )
  30.     {
  31.         if( Director::is_ajax() )
  32.         {
  33.             die('ajax');
  34.             // IS AJAX
  35.             if( ContactPage::sendEmail( $data ) )
  36.             {
  37.                 return json_encode(array(
  38.                     'success' => true,
  39.                     'message' => 'Your message has been sent! Please allow up to 2-3 business days before resubmitting your request.'
  40.                 ));
  41.             }
  42.             else
  43.             {
  44.                 return json_encode(array(
  45.                     'success' => false,
  46.                     'message' => 'Your message could not be sent. Please contact staff@intellectproductions.com manually'
  47.                 ));
  48.             }
  49.         }
  50.         else
  51.         {
  52.             die('not ajax');
  53.             // NOT AJAX
  54.             if( ContactPage::sendEmail( $data ) )
  55.             {
  56.                 return $this->Controller->curr()->redirectBack();
  57.             }
  58.             else
  59.             {
  60.                 Session::set('FormError', 'Your message could not be sent. Please contact staff@intellectproductions.com manually.');
  61.                 Session::set('FormInfo.' . $form->FormName() . '.data', $data);
  62.                 Session::set('FormInfo.' . $form->FormName() . '.errors', array());
  63.                 return $this->Controller->curr()->redirectBack();
  64.             }
  65.         }
  66.     }
  67.    
  68.     public function forTemplate()
  69.     {
  70.         return $this->customise(array(
  71.             'FormError' => SessionExtension::flash('FormError')
  72.         ))->renderWith(array( $this->class, 'Form' ));
  73.     }
  74.    
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement