Search This Blog

May 19, 2011

Contact Form Module for Drupal - Part 2

Next on the list is the contact.module. In this one you probably know the basic hooks. So let me break down it by hooks

contact.module


hook help - Provide online user help.
<?php
// $Id: nvr.module,v 1.297.2.4 2009/02/25 12:21:53 traxbarn Exp $

function contact_help($path, $arg){
switch ($path) {
case 'admin/help#contact':
return t('Place your instruction here.');
}
}

hook init - Perform setup tasks. See also, hook_boot.
function contact_init(){
drupal_add_css(drupal_get_path('module','contact') .'/contact.css', 'module');
drupal_add_js(drupal_get_path('module','contact') .'/contact.js', 'module');
}

hook perm - Define user permissions.
function contact_perm(){
return array('view contact');
}

hook access - Control access to a node.
function contact_access($op, $node) {
if (user_access('view contact')) {
return TRUE;
}
}

hook theme - Register a module (or theme's) theme implementations.
function contact_theme(){
return array(
'contact_block' => array(
'arguments' => array('form' => NULL),
'template' => 'contact-block',
),
);
}

hook_block - allows you to specify any custom configuration settings, and how to display the block.
function contact_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('nvr contact');
$blocks[0]['cache'] = BLOCK_NO_CACHE;

return $blocks;
}
else if($op == 'view' && $delta == 0 && user_access('view contact')){

$block['content'] = theme('contact_block', NULL);

return $block;
}
}

hook menu - Define menu items and page callbacks.
function contact_menu(){
$items['contact'] = array(
'page callback' => 'contact_form_validate',
'access arguments' => array('view contact'),
'type' => MENU_CALLBACK,
); 

return $items;
}

hook_form - Display a node editing form.

function contact_form(&$form_state){
$form['name'] = array(
'#title' => 'Name',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin-left:6px;width:194px;'),
'#maxlength' => 50,
'#required' => TRUE,
);

$form['email'] = array(
'#title' => 'Email',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin:5px 0px 0px 6px;width:194px;'),
'#required' => TRUE,
);

$form['subject'] = array(
'#title' => 'Subject',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin:5px 0px 0px 6px;width:315px;'),
'#required' => TRUE,
);

$form['message'] = array(
'#title' => 'Message',
'#type' => 'textarea',
'#attributes' => array('style' => 'margin:5px 0px 0px 0px; width:315px; cursor: text;'),
'#maxlength' => 200,
'#required' => TRUE,
);
$form['join_list']['join'] = array(
'#type' => 'radios',
'#title' => t('Join our Mailing List'),
'#size' => 20,
'#default_value' => variable_get('join', 1),
'#options' => array(t('Yes'), t('No')),
'#required' => TRUE
); 

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Email'),
'#attributes' => array('style' => 'width:120px;padding-bottom:3px;'),
'#suffix' => '</div><div style="clear: both;"></div>',
'#prefix' => '<div class="form-item" style="margin-left:78px;margin-top:15px;">',
'#ahah' => array(
'event' => 'click',
'path' => 'contact',
'wrapper' => 'contact-message',
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'throbber',
),  
),  
);

return $form;
}

hook_validate - Perform node validation before a node is created or updated.

function contact_form_validate(){

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];

$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];

$form_id = array_shift($form_state);

$form['#redirect'] = FALSE;
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;

$form_state['name']   = $_POST['name'];
$form_state['email']   = $_POST['email'];
$form_state['subject'] = $_POST['subject'];
$form_state['message'] = $_POST['message'];
$form_state['join']   = $_POST['join'];


if(empty($form_state['name'])){
form_set_error('name', t('Name field is Required.'));
}


$email = isValidEmail($form_state['email']);

if(empty($form_state['subject'])){
form_set_error('subject', t('Subject field is Required.'));
}
if(empty($form_state['message'])){
form_set_error('message', t('Message field is Required.'));
}


//rechecking for the validation

if ($email[1] == TRUE && !empty($form_state['name'])&& !empty($form_state['subject'])&& !empty($form_state['message'])){

$params['name']   = $form_state['name'];
$params['email']  = $form_state['email'];
$params['subject'] = $form_state['subject'];
$params['message'] = $form_state['message'];
$params['join'] = $form_state['join'];
$params['ae_mail'] = 'info@naturesvillageresort.net';

drupal_mail('contact_send_acknowledgment1', $key = NULL, $params['ae_mail'], $language, $params, $params['email'], $send = TRUE);
drupal_mail('contact_send_acknowledgment' , $key = NULL, $email[2], $language, $params, $params['ae_mail'], $send = TRUE);

// Insert the name and email if yes option is choosen 
if ($params['join']==0){
db_query("INSERT INTO {contact} (id, name, email) VALUES (%d, '%s', '%s')", $id, $params['name'] ,$params['email']);
drupal_set_message(t('Message sent. You have successfully joined our mailing list.'));
}else{
drupal_set_message(t('Message Sent.'));  
}

drupal_process_form($form_id, $form, $form_state);
$output = theme('status_messages');
drupal_json(array('status' => FALSE, 'data' => $output));

} 
else{
drupal_process_form($form_id, $form, $form_state);
$output = theme('status_messages');
drupal_json(array('status' => FALSE, 'data' => $output));
} 
}
function template_preprocess_contact_block(&$variables) {
$variables['contact_form'] = drupal_get_form('contact_form');
}

function isValidEmail($email){
if(empty($email)){
$emails[] = form_set_error('mail', t('Email is Required.'));
$emails[] = FALSE;
return $emails;
}
else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$emails[] =  form_set_error('mail', t('The Email Address you specified is not Valid.'));
$emails[] = FALSE;
return $emails;
}
else{
$emails[] = '';
$emails[] = TRUE;
$emails[] = $email;
return $emails;
}
}


hook_mail - Prepare a message based on parameters; called from drupal_mail().
function contact_send_acknowledgment1_mail($key, &$message, $params){
$message['subject'] = t("Webdosh.net:")."   ".$params['subject'];
$message['body']  =  t("\n\r\n\r Message From The Webdosh.net \n\r");
$message['body'] .=  t("\n\r\n\r------------------------------------------\n\r\n\r");
$message['body'] .=  $params['message']. t("\n\r\n\r");
$message['body'] .=  t("\n\r(63) (34) 495-0808 / 4953368 to 69 / 712-1272");  
$message['body'] .=  t("\n\r 6115 Talisay City");
$message['body'] .=  t("\n\r Negros Occidental, Philippines");
$message['body'] .=  t("\n\r------------------------------------------");
$message['body'] .=  t("\n\r\n\r(c) 2010 Webdosh.net. All Rights Reserved. Webdosh.net");
}
function contact_send_acknowledgment_mail($key, &$message, $params){
$message['subject'] = t("Webdosh.net- Auto Responding Email");
$message['body']  =   t("\n\r\n\r Your Email has been successfully sent to ". $params['ae_mail'] .".\n\r");
$message['body'] .=  t("\n\r\n\r We will check our availability and send you an email response within 24 hours.");
$message['body'] .=  t("\n\r\n\r------------------------------------------");
$message['body'] .=  t("\n\r(63) (34) 495-0808 / 4953368 to 69 / 712-1272");
$message['body'] .=  t("\n\r 6115 Talisay City");
$message['body'] .=  t("\n\r Address 2, Philippines");
$message['body'] .=  t("\n\r------------------------------------------");
$message['body'] .=  t("\n\r\n\r(c) 2010 Webdosh.net. All Rights Reserved. Webdosh.net");
}


Continue to Part 3

0 comments: