Created
April 26, 2013 21:04
-
-
Save sandeepone/5470444 to your computer and use it in GitHub Desktop.
Contact controller
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
<?php defined('SYSPATH') or die('No direct script access.'); | |
class Controller_Contact extends Template { | |
public function action_index() | |
{ | |
$this->title = __('Contact'); | |
// Mail type | |
$types = array( | |
'' => __('Please Choose'), | |
'advertise' => __('Advertise'), | |
'feedback' => __('Feedback'), | |
'info' => __('Info'), | |
'privacy' => __('Privacy'), | |
'other' => __('Other'), | |
); | |
$form = array('name' => '', 'email' => '', 'subject' => '', 'category' => '', 'body' => ''); | |
$post = Validation::factory( empty($_POST) ? $form : $_POST ) | |
->rule('name', 'not_empty') | |
->rule('name', 'min_length', array(':value', 4)) | |
->rule('email', 'not_empty') | |
->rule('email', 'min_length', array(':value', 8)) | |
->rule('email', 'email', array(':value', TRUE)) | |
->rule('email', 'email_domain') | |
->rule('subject', 'not_empty') | |
->rule('category', 'not_empty') | |
->rule('category', 'in_array', array(':value', array_keys($types)) ) | |
->rule('body', 'not_empty'); | |
if( $this->valid_post('contact') ) | |
{ | |
if ($post->check()) | |
{ | |
// Create the email subject | |
$subject = __('[!category] !subject', | |
array( | |
'!category' => $types[$post['category']], | |
'!subject' => Text::plain($post['subject']) | |
)); | |
// Create the email body | |
$body = View::factory('email/contact') | |
->set('name', $post['name']) | |
->set('type', $types[$post['category']]) | |
->set('body', $post['body']) | |
->render(); | |
// Create an email message | |
$email = Email::factory() | |
->subject($subject) | |
->from( Text::plain($post['email']), Text::plain($post['name']) ) | |
->reply_to( Text::plain($post['email']), Text::plain($post['name']) ) | |
->to('sandeepone@gmail.com', 'Webmaster Gleez') | |
->message($body, 'text/html'); | |
// Send the message | |
$email->send(); | |
Message::success( __('Your message has been sent.') ); | |
Kohana::$log->add(LOG::INFO, ':name-from sent an e-mail regarding :category', array( | |
':name-from' => "{$post['name']} [{$post['email']}]", | |
':category' => $types[$post['category']]) ); | |
// Always redirect after a successful POST to prevent refresh warnings | |
$this->request->redirect('contact'); | |
} | |
else | |
{ | |
$errors = $post->errors('forms/contact'); | |
} | |
} | |
$this->response->body( View::factory('contact/form') | |
->bind('post', $post) | |
->bind('errors', $errors) | |
->bind('types', $types) | |
); | |
} | |
public function action_thanks() | |
{ | |
$view = View::factory('contact/thanks'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment