<?php
/**
* @file
* 方便用户联系我们.
*/
/**
* 实现钩子hook_menu().
*/
function contactus_menu() {
$items = array();
//联系我们菜单项
$items['contactus'] = array(
'tilte' => '联系我们',
'page callback' => 'contactus_page',
'type' => MENU_CALLBACK,
'access callback' =>TRUE,
'file' => 'contactus.pages.inc',
);
//确认页面菜单项
$items['contactus/confirm'] = array(
'page callback' => 'contactus_confirm_page',
'type' => MENU_CALLBACK,
'access callback' =>TRUE,
'file' => 'contactus.pages.inc',
);
//致谢页面菜单项
$items['contactus/thanks'] = array(
'page callback' => 'contactus_thanks_page',
'type' => MENU_CALLBACK,
'access callback' =>TRUE,
'file' => 'contactus.pages.inc',
);
return $items;
}
/**
* 实现钩子hook_mail().
*/
function contactus_mail($key, &$message, $params){
$language = $message['language'];
switch ($key) {
case 'contact':
//邮件的标题
$message['subject'] = '联系我们';
//邮件正文,这里面包含:姓名、单位名称、电子邮件、电话号码、邮件正文、访问来源
$message['body'][] = '姓名:'.$params['name'];
$message['body'][] = '单位名称:'.$params['company_name'];
$message['body'][] = '电子邮件:'.$params['mail'];
$message['body'][] = '电话号码:'.$params['phone'];
$message['body'][] = '邮件正文:'.$params['contact'];
//对于访问来源,如果visit的值我们选择了“其它”,那么此时我们取$params['other'],否则取$params['visit']
$visit = "";
if($params['visit'] == 'other'){
$visit = $params['other'];
}else{
$visit = $params['visit'];
}
$message['body'][] = '访问来源:'. $visit;
}
}
/**
* “确认”页面的回调函数
*/
function contactus_confirm_page(){
//我们为这个页面设置标题
drupal_set_title('联系我们');
//这里首先作了判断,如果会话中没有设置contactus_form,返回contactus
if(empty($_SESSION['contactus_form'])){
drupal_goto('contactus');
}else{
}
$render_array = array(
'#markup' => '',
);
//该页面的正文为一个表单,注意对于表单,这里需要使用drupal_render呈现一下。
$render_array['#markup'] .= drupal_render(drupal_get_form('contactus_confirm_form'));
//Drupal7的页面回调,返回的应该是一个数组
return $render_array;
}
/**
* “确认”表单的构建函数
*/
function contactus_confirm_form(){
//添加我们自己的CSS,用来控制表单的样式
drupal_add_css(drupal_get_path('module', 'contactus').'/contactus.css');
//drupal_set_message(print_r($_SESSION['contactus_form']));
//表单元素“姓名”
$form['name'] = array(
'#title' => t('姓名'),
//表单元素的类型,这里为item
'#type' => 'item',
//'#default_value' => $_SESSION['contactus_form']['name'],
//表单元素的#markup,在Drupal6下面,我用的是#value,在7下面就无法工作,改为了#default_value,还是不行,
//最后改为#markup,才可以了
'#markup' => isset($_SESSION['contactus_form']['name'])?$_SESSION['contactus_form']['name']:"",
);
//表单元素“单位名称”
$form['company_name'] = array(
'#title' => t('单位名称'),
'#type' => 'item',
//这是我在调试的时候,使用#value、#default_value、#description分别测试时的代码,这里保留了。
//'#value' => $_SESSION['contactus_form']['company_name'],
//'#value' => '123456',
'#markup' => isset($_SESSION['contactus_form']['company_name'])?$_SESSION['contactus_form']['company_name']:"",
//'#description' => '123456',
);
//表单元素“电子邮件”
$form['mail'] = array(
'#title' => t('电子邮件'),
'#type' => 'item',
'#markup' => isset($_SESSION['contactus_form']['mail'])?$_SESSION['contactus_form']['mail']:"",
);
//表单元素“电话号码”
$form['phone'] = array(
'#title' => t('电话号码'),
'#type' => 'item',
'#markup' => isset($_SESSION['contactus_form']['phone'])?$_SESSION['contactus_form']['phone']:"",
);
//表单元素“邮件正文”
$form['contact'] = array(
'#title' => t('邮件正文'),
'#type' => 'item',
'#markup' => isset($_SESSION['contactus_form']['contact'])?$_SESSION['contactus_form']['contact']:"",
);
//表单元素“访问来源”
$form['visit'] = array(
'#title' => t('访问来源'),
'#type' => 'item',
'#markup' => isset($_SESSION['contactus_form']['visit'])?$_SESSION['contactus_form']['visit']:"",
);
//如果访问来源,我们选择了“其它”,此时使用other表单元素的值来替换$form['visit']['#markup']。
if( isset($_SESSION['contactus_form']['visit']) && $_SESSION['contactus_form']['visit'] == 'other'){
$form['visit']['#markup'] = isset($_SESSION['contactus_form']['other'])?$_SESSION['contactus_form']['other']:"";
}
/*
//表单元素“返回”按钮
$form['back'] = array(
'#type' => 'submit',
'#value' => t('返回'),
'#submit' => array('contactus_confirm_form_back'),
);
//表单元素“提交”按钮
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('提交'),
);
*/
//表单元素“返回”图片按钮
$form['image_back'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module','contactus').'/images/button2-1.jpg',
//使用这个按钮提交时,对应的提交处理函数为contactus_confirm_form_back
'#submit' => array('contactus_confirm_form_back'),
'#executes_submit_callback' => TRUE,
//为表单元素添加两个属性,onmouseout、onmouseover,为了在鼠标移到按钮上时,显示不同的图片效果
'#attributes' =>array(
'onmouseout' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button2-1.jpg'",
'onmouseover' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button2-2.jpg'",
),
);
//表单元素“提交”图片按钮
$form['image_submit'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module','contactus').'/images/button3-1.jpg',
'#executes_submit_callback' => TRUE,
//使用这个按钮提交时,对应的提交处理函数为contactus_confirm_form_submit
'#submit' => array('contactus_confirm_form_submit'),
'#attributes' =>array(
'onmouseout' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button3-1.jpg'",
'onmouseover' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button3-2.jpg'",
),
);
return $form;
}
/**
* 返回按钮对应的提交函数
*/
function contactus_confirm_form_back($form, &$form_state){
//简单的重定向到contactus页面
$form_state['redirect'] = 'contactus';
}
/**
* 提交按钮对应的提交函数
*/
function contactus_confirm_form_submit($form, &$form_state){
$values = NULL;
//从会话中获取用户最初提交的值,并将$_SESSION['contactus_form']置为空。
if(empty($_SESSION['contactus_form'])){
drupal_goto('contact');
}else{
$values = $_SESSION['contactus_form'];
unset($_SESSION['contactus_form']);
}
//收件人地址,这里为作者的邮箱
$to = 'g089h515r806@gmail.com';
//用户填写的邮箱地址
$from = $values['mail'];
//发送邮件
drupal_mail('contactus', 'contact', $to, language_default(), $values, $from);
//简单的重定向到致谢页面
$form_state['redirect'] = 'contactus/thanks';
}
/**
* “致谢”页面的回调函数
*/
function contactus_thanks_page(){
//我们为这个页面设置标题
drupal_set_title('联系我们');
$render_array = array(
'#markup' => '',
);
//Drupal7的页面回调,返回的应该是一个数组
$render_array['#markup'] .= '<div id="contactus-thanks">';
$render_array['#markup'] .= t('感谢您的来信,我们会在第一时间给您回复。');
$render_array['#markup'] .= '</div>';
return $render_array;
}
<?php
/**
* @file
* 各种页面的回调函数.
*/
/**
* “联系我们”页面的回调函数
*/
function contactus_page(){
//我们为这个页面设置标题
drupal_set_title('联系我们');
$render_array = array(
'#markup' => '',
);
//该页面的正文为一个表单,注意对于表单,这里需要使用drupal_render呈现一下。
$render_array['#markup'] .= drupal_render(drupal_get_form('contactus_form'));
//Drupal7的页面回调,返回的应该是一个数组
return $render_array;
}
/**
* “联系我们”表单的构建函数
*/
function contactus_form(){
//添加我们自己的CSS,用来控制表单的样式
drupal_add_css(drupal_get_path('module', 'contactus').'/contactus.css');
//提示信息,默认为markup类型。
$form['tips'] = array(
'#prefix' =>'<div id="tips">',
'#markup' => t('<span class="form-required">*</span> 号为必填项。'),
'#suffix' =>'</div>',
);
//表单元素“姓名”
$form['name'] = array(
//表单元素的#title属性,对应于实际输出中的label
'#title' => t('姓名'),
//表单元素的类型,这里为textfield
'#type' => 'textfield',
//这个表单元素是必填的
'#required' => TRUE,
//表单元素的默认值,这里使用了三位运算符和isset进行判定
'#default_value' =>isset($_SESSION['contactus_form']['name'])?$_SESSION['contactus_form']['name']:"",
//表单元素的描述,
'#description' => t('例如:周星驰'),
);
//表单元素“单位名称”
$form['company_name'] = array(
'#title' => t('单位名称'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' =>isset($_SESSION['contactus_form']['company_name'])?$_SESSION['contactus_form']['company_name']:"",
'#description' => t('例如:北京无名信息技术公司'),
);
//表单元素“电子邮件”
$form['mail'] = array(
'#title' => t('电子邮件'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' =>isset($_SESSION['contactus_form']['mail'])?$_SESSION['contactus_form']['mail']:"",
'#description' => t('例如:info@example.com'),
);
//表单元素“电话号码”
$form['phone'] = array(
'#title' => t('电话号码'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' =>isset($_SESSION['contactus_form']['phone'])?$_SESSION['contactus_form']['phone']:"",
'#description' => t('例如:010-88888888'),
);
//表单元素“邮件正文”
$form['contact'] = array(
'#title' => t('邮件正文'),
//表单元素的类型,这里为textarea
'#type' => 'textarea',
'#required' => TRUE,
'#default_value' =>isset($_SESSION['contactus_form']['contact'])?$_SESSION['contactus_form']['contact']:"",
);
//访问来源的可选项
$visit_options = array(
'baidu' =>t('百度'),
'google' =>t('谷歌'),
'sohu' =>t('搜狐'),
'other' =>t('其它'),
);
//表单元素“访问来源”
$form['visit'] = array(
'#title' => t('访问来源'),
//表单元素的类型,这里为radios
'#type' => 'radios',
//单选按钮的可选项。
'#options' => $visit_options,
'#default_value' =>isset($_SESSION['contactus_form']['visit'])?$_SESSION['contactus_form']['visit']:"",
//为了便于控制radios的外观,我们使用#prefix、#suffix为其添加了一个带有ID的div
'#prefix' => '<div id="visit-radios">',
'#suffix' => '</div>',
);
//表单元素“其它”,它依赖于表单元素“访问来源”
$form['other'] = array(
'#title' => t('其它'),
'#type' => 'textfield',
'#default_value' =>isset($_SESSION['contactus_form']['other'])?$_SESSION['contactus_form']['other']:"",
//这里的意思是,当表单元素“访问来源”的值为“other”时,这个表单元素才显示出来
'#states' => array(
'visible' => array(
':input[name="visit"]' => array('value' => 'other'),
),
),
);
/*
//表单元素“确认”提交按钮
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('确认'),
);
*/
//表单元素“确认”提交按钮,这里采用了图片的形式
$form['image_submit'] = array(
//表单元素的类型,这里为image_button
'#type' => 'image_button',
//图片按钮特有的#src属性,
'#src' => drupal_get_path('module','contactus').'/images/button1-1.jpg',
'#value' => 'image_sub',
//为表单元素添加两个属性,onmouseout、onmouseover,为了在鼠标移到按钮上时,显示不同的图片效果
'#attributes' =>array(
'onmouseout' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button1-1.jpg'",
'onmouseover' => "this.src='".base_path().drupal_get_path('module','contactus')."/images/button1-2.jpg'",
),
//为了便于控制image_button的外观,我们使用#prefix、#suffix为其添加了一个带有ID的div
'#prefix' => '<div id="image-submit">',
'#suffix' => '</div>',
);
return $form;
}
/**
* contactus_form表单的验证函数
*/
function contactus_form_validate($form, &$form_state){
//这里使用正则表达式,来验证邮箱的有效性,注意,这里的正则表达式,包围在"/...../"之间。
if(!preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $form_state['values']['mail'])){
form_set_error('mail',t('您输入的电子邮件地址格式不正确'));
}
}
/**
* contactus_form表单的提交函数
*/
function contactus_form_submit($form, &$form_state){
//把表单的值存放在会话中去,由于这里涉及到了两个不同的表单之间传值。
$_SESSION['contactus_form'] = $form_state['values'];
//表单重定向到确认页面
$form_state['redirect'] = 'contactus/confirm';
}