Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334908
  • 博文数量: 89
  • 博客积分: 5152
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 15:12
文章分类

全部博文(89)

文章存档

2012年(1)

2011年(5)

2010年(14)

2009年(69)

我的朋友

分类: 系统运维

2009-07-17 11:01:13

效果如图:


点delete 后




We can easily create an action confirm form using confirm_form() function in Drupal. Normally we need this form when user want to delete something and we want user to confirm his/her action. I am showing here an example in which we are listing all the Doctors information with Edit and Delete option. When user click the delete link we will show him the action confirm form with “Delete” and “Cancel” options. If user click the “Delete” button we will delete that Doctor record from database and if user click on “Cancel” we simply show the doctors list again.

Here is the code. I am using the module name “doctor” in this example.

Step 1. First implement hook_menu() to register new path in Drupal.



function doctor_menu() {
  $items = array();
 
  $items['doctors'] = array(
    'title' => t('Doctors'),
    'page callback' => 'doctors_list',
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  $items['doctors/delete/%doctor_user'] = array(
    'title' => t('Delete doctor'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doctor_delete_confirm', 2),
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  return $items;
}



Step 2. Implementing the doctors_list() function to display the list of doctors.



function doctors_list() {
    $header = array(t('Doctor Name'), t('Gender'), t('Phone No.'), t('Status'), t('Action'));
 
    $query = "SELECT * FROM {doctor}";
    $rs = db_query($query);
 
    $row = array();
 
    if ($rs) {
        while ($data = db_fetch_object($rs)) {
            $gender = $data->gender == 'M' ? t('Male') : t('Female');
            $status = $doctor->status ? t('active') : t('inactive');
            $row[] = array(stripslashes($data->firstname) . ' ' . stripslashes($data->lastname), $gender, stripslashes($data->phoneno), $status,
            "" . t('Edit') . " | "
. t('Delete') . "");
        }
    }
 
    $str .= theme_table($header, $row);
 
    return $str;
}



Step 3. Create a wildcard loader function doctor_user_load() which we used in “doctors/delete/%doctor_user” path. This function checks the given “ID” in path is valid or not. If the given doctor id is not exists in database then “Page not found.” message will display.


function doctor_user_load($doctorid) {
    $query = "SELECT * FROM {doctor} WHERE doctorid = %d";
    $rs = db_query($query, $doctorid);
 
    if ($rs) {
        while ($data = db_fetch_object($rs)) {
            return $data;
        }
    }
 
    return FALSE;
}


Step 4. Create the doctor_delete_confirm() function. We are using the “confirm_form()” function in this function to show action confirm form.


function doctor_delete_confirm(&$form_state, $doctor) {
    $form['_doctor'] = array(
        '#type' => 'value',
        '#value' => $doctor,
    );
 
    return confirm_form($form,
        t('Are you sure you want to delete this doctor?'),
        isset($_GET['destination']) ? $_GET['destination'] : "doctors",
        t('This action cannot be undone.'),
        t('Delete'),
        t('Cancel'));
}


Step 5. Create the Form API submit function for doctor_delete_confirm form.


function doctor_delete_confirm_submit($form, &$form_state) {
    $form_values = $form_state['values'];
 
    if ($form_state['values']['confirm']) {
        $doctor = $form_state['values']['_doctor'];
 
        doctor_delete($form_state['values'], $doctor->doctorid);            
 
        drupal_set_message(t('Doctor has been deleted successfully.'));
      }
 
    drupal_goto("doctors");
}

阅读(1142) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~