Skip to content

Contact Action Template

Darryl Hein edited this page Mar 25, 2015 · 7 revisions

Template action to display and process a contact form.

Includes thankyou=1 query parameter to help tracking in Google Analytics.

<?php
	/**
	 * Displays and processes the contact form.
	 *
	 * @return void
	 */
	public function action_contact() {
		$contact = ORM::factory('Contact')
			->set_table_columns('date_submitted', 'edit_flag', FALSE)
			->set_table_columns('ip_address', 'edit_flag', FALSE);

		if ( ! empty($_POST)) {
			$verification_email = trim($this->request->post('email'));
			if ( ! empty($verification_email)) {
				Message::add('Thank you for filling out the contact form.', Message::$error);
				Kohana::$log->add(Kohana_Log::INFO, 'Spam Contact submission received: hidden email field filled out')->write();
			} else {
				try {
					$contact->save_values()
						->set('ip_address', $_SERVER['REMOTE_ADDR'])
						->save();

					$mail = new Mail();
					$mail->AddAddress('[email protected]', 'Person Name');
					$mail->Subject = 'Contact Us Submission - ' . date('Y-m-d H:m');
					$mail->IsHTML(TRUE);
					if ( ! empty($contact->email) && Valid::email($contact->email)) $mail->AddReplyTo($contact->email, $contact->name);

					$mail->Body = '<p>The following submission was received from the website Contact Us form:</p>';
					$mail->Body .= $contact->set_mode('view')
						->set_view_options_for_email()
						->set_table_columns('ip_address', 'view_flag', FALSE)
						->get_view();

					$mail->Send();

					Message::add(Kohana::message('contact', 'sent'), Message::$notice);
					$this->redirect(Route::get('public')->uri(array('action' => 'contact')) . '?thankyou=1');

				} catch (ORM_Validation_Exception $e) {
					Message::add('Please fix the following: ' . Message::add_validation_errors($e, ''), Message::$error);
				}
			}
		}

		$contact->set_mode('add');

		$this->template->page_title = 'Contact Us | ' . $this->page_title_append;
		$this->template->body_html = View::factory('pages/contact')
			->set('message', Message::display())
			->bind('contact', $contact);
	}