Thursday, March 17, 2011

Drupal 7 Ajax Pagination Update Delete Insert Form theme_table

Drupal 7 Ajax Pagination Update Delete Insert Form  theme_table  - Implemented below

deductible.module

<?php
//;$Id$

/**
 * @file
 * Enables a single blog for an individual or multiple users.
 */
// After you learn Form API in Chapter 5, you'll be able to
// make these settings configurable.

function deductible_help($section) {
    switch ($section) {
        case 'admin/help#deductible':
            return t('This module stores deductible for given make an Model');
    }
}

function deductible_menu(){
  
  
  $items['deductible/add_deductible'] = array(
    'title' => '',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('adddeductible'),
    'access callback' => TRUE,
     'file'=>'deductible.inc',
    'weight' => 0,
  );
  

    //watchdog('Deductibe', '1', array(), WATCHDOG_DEBUG);
    $items['deductible/display_deductibles'] = array(
    //    'title'=>'Calculate Deductible',
        'access arguments' => array('administer user'),
        'page callback' => 'deductible_menu_page',
        'page arguments' => array(t('Display Deductible')),
        'access callback' => TRUE,
        'file'=> 'deductible.inc',
    //'type' => MENU_CALLBACK,
    );
  
    $items['deductible/deductible/delete/%'] = array(
    //    'title'=>'Calculate Deductible',
        'access arguments' => array('administer user'),
        'page callback' => 'delete_deductible',
        'access callback' => TRUE,
        'page arguments' => array(0,1,2,3),
        'file'=> 'deductible.inc',
    //'type' => MENU_LOCAL_TASK,
    );
  
    //watchdog('Deductibe', '2', array(), WATCHDOG_DEBUG);
    return $items;
}



/**
 * Implementation of hook_perm().
 */
function deductible_perm() {
  return array('administer user');
}



function theme_deductible($form) {
    watchdog('Deductibe', 'abf', array(), WATCHDOG_DEBUG);
    $output = drupal_render($form['Title']);
    return $output;
}

function deductible_theme($existing, $type, $theme, $path) {
    return array(
    'deductible' => array(
      'arguments' => array('form' => NULL),
        'template' => 'deductible',  
        'render element' => 'form',  
    //  'file'=> 'deductibleadd.inc',  
    ),
    'list' => array(
      'arguments' => array('form' => NULL),
        'template' => 'list',  
        'render element' => 'form',      
          ),
      'carrier' => array(
      'arguments' => array('form' => NULL),
        'template' => 'carrier',  
        'render element' => 'form',      
          ),  
    );
}

function deductible_block_info() {
    $blocks = array();
    // The array key defines the $delta parameter used in all
    // other block hooks.
    $blocks['list'] = array(
       // The name of the block on the blocks administration page.
       'info' => t('List'),
       'file'=>'list.inc',
       'access arguments' => array('administer user'),
    );
    return $blocks;
}

function deductible_block_view($delta = '') {
 $block = array();
 switch ($delta) {
 case "list":
 $form = drupal_get_form('display_list');
 $block["content"] = $form;
 break;
 }
 return $block;
 }

deductible.inc

 <?php

function adddeductible($form, &$form_state) {
      $form = array();
    $path = drupal_get_path('module', 'Deductible');
    //$form_state['redirect'] = 'deductilbe/display_results';
    $result = db_query("SELECT carrier_name FROM {drup_carrier}");
    $form['#prefix']='<div id="ooo" class="dedwrapper">';
    $form['#suffix']='</div>';
    $carrier_names[] = array();
    $carrier_names[0]='--Select--';
   // $manufacture_names[]='Select';
          foreach ($result as $carrier) {
              $carrier_names[$carrier->carrier_name] = $carrier->carrier_name;
          }
  $selectedcarrier = isset($form_state['values']['carrier_select']) ? $form_state['values']['carrier_select'] : NULL;        
  $selectedmanufacture = isset($form_state['values']['manufacture_select']) ? $form_state['values']['manufacture_select'] : NULL;
  $selectedmodel = isset($form_state['values']['model_select']) ? $form_state['values']['model_select'] : NULL;
 
    $form['carrier_select'] = array(
    '#type' => 'select',
    '#title' => t('Carrier'),
    '#options' => $carrier_names,
      '#default_value' => $carrier_names[0],
    '#ajax' => array(
     'callback' => 'deductible_callback_formanufacture',
      'wrapper' => 'manufacturediv',
    ),
  );
 
  $form['manufacture_select'] = array(
    '#type' => 'select',
    '#title' => t('Manufacture'),
    '#options' => getManufactureList($selectedcarrier),
      //'#default_value' => $manufacture_names[0],
       '#prefix' => '<div id="manufacturediv">',
    '#suffix' => '</div>',
    '#ajax' => array(
     'callback' => 'deductible_callback_formodel',
      'wrapper' => 'modeldiv',
    ),
  );
 
  $form['model_select'] = array(
    '#type' => 'select',
    '#title' => t('Model'),
    '#prefix' => '<div id="modeldiv">',
    '#suffix' => '</div>',
   // '#default_value' => isset($form_state['values']['model_select']) ? $form_state['values']['model_select'] : 'Select',
    '#options' => getModelList($selectedmanufacture),
  '#ajax' => array(
     'callback' => 'deductible_callback_fortier',
      'wrapper' => 'tierdiv',
    ),
   );
 
   $form['tier_select'] = array(
    '#type' => 'select',
    '#title' => t('Tier'),
    '#prefix' => '<div id="tierdiv">',
    '#suffix' => '</div>',
  //  '#default_value' => isset($form_state['values']['tier_select']) ? $form_state['values']['tier_select'] : NULL,
    '#options' => getTierList($selectedmodel),
   );
 
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#submit' => array('deductible_submit'),
    );
 //$form['#theme'] = 'deductible';
  return $form;
}

function deductible_callback_formanufacture($form, $form_state) {
     watchdog('Deductibe', 'In ajax', array(), WATCHDOG_DEBUG);
  return $form['manufacture_select'];
}

function deductible_callback_formodel($form, $form_state) {
     watchdog('Deductibe', 'In ajax', array(), WATCHDOG_DEBUG);
  return $form['model_select'];
}

function deductible_callback_fortier($form, $form_state) {
     watchdog('Deductibe', 'In ajax', array(), WATCHDOG_DEBUG);
  return $form['tier_select'];
}


function getManufactureList($selectedCarrier = NULL){
    $manufacture_names = array();
        if(isset($selectedCarrier)){
             $manufacture_names[0]='--Select--';
    watchdog('Deductibe', 'In select'.$selectedCarrier, array(), WATCHDOG_DEBUG);  
        $result = db_query("SELECT manufacture_name FROM {drup_manufacture}");
          foreach ($result as $manufacture_name) {
              $manufacture_names[$manufacture_name->manufacture_name] = $manufacture_name->manufacture_name;
          }
        }
          return $manufacture_names;
}

function getTierList($selectedModel = NULL){
    $tiers = array();
  
        if(isset($selectedModel)){
             $tiers[0]='--Select--';
    watchdog('Deductibe', 'In select'.$selectedModel, array(), WATCHDOG_DEBUG);  
        $result = db_query("SELECT tier_amount FROM {drup_tier} ");
  
  
          foreach ($result as $tier) {
              $tiers[$tier->tier_amount] = $tier->tier_amount;
          }
        }
          return $tiers;
}

function getModelList($selectedManufacture = NULL){
//    watchdog('Deductibe', 'FUNNY CATCH -JACK'.$selectedManufacture, array(), WATCHDOG_DEBUG);  
  
    $model_names = array();
        if(isset($selectedManufacture)){
    watchdog('Deductibe', 'In select'.$selectedManufacture, array(), WATCHDOG_DEBUG);  
        $result = db_query("SELECT model_name FROM {drup_model} WHERE model_manufacture  = :contents", array(':contents' => trim($selectedManufacture)));
  
    $model_names = array();
 
          foreach ($result as $model) {
               $model_names[0]='--Select--';
              $model_names[$model->model_name] = $model->model_name;
          }
        }
          return $model_names;
}

function deductible_submit($form, &$form_state){

  $carselect = isset($form_state['values']['carrier_select'])?$form_state['values']['carrier_select']:NULL;
  $manselect=isset($form_state['values']['manufacture_select'])?$form_state['values']['manufacture_select']:NULL;
  $modselect = isset($form_state['values']['model_select'])?$form_state['values']['model_select']:NULL;
  $tierselect = isset($form_state['values']['tier_select'])?$form_state['values']['tier_select']:NULL;
 // watchdog('Deductibe', 'Some Jacky'.$manselect, array(), WATCHDOG_DEBUG);  
 
  if(isset($carselect)&&isset($manselect)&&isset($modselect)&&isset($tierselect)){
      if(strlen($carselect)>0&&strlen($manselect)>0&&strlen($modselect)>0&&strlen($tierselect)>0){
         watchdog('Deductibe', 'Some Jacky'.strlen($carselect), array(), WATCHDOG_DEBUG);  
        $insertquery = db_insert('drup_deductible')->fields(array(
      'carrier_name'=>$carselect,
       'manufacture_name'=>$manselect,
       'model_name'=>$modselect,
         'tier_name'=>$tierselect
      ))->execute();
      if($insertquery!==false){
           $message = 'Deductible Added';
         drupal_set_message(t($message));
         $form_state['redirect'] = 'deductible/display_deductibles';
      }else{
          drupal_set_message(t('Error-Contact Administrator- Database issue'));
      }
      }else{
          watchdog('Deductibe', 'Some Jacky'.$carselect.$manselect.$modselect.$tierselect, array(), WATCHDOG_DEBUG);  
          drupal_set_message(t('Please select all entries'));
      }
  }else{
      drupal_set_message(t('Error-Contact Administrator'));
  }
}

function deductible_menu_page($content = NULL, $arg1 = NULL, $arg2 = NULL){
  /*$tablesort = tablesort_sql($header);
    $result = pager_query($sql . $tablesort, $limit);
    $rows = array();
     // Then you can pass the data to the theme functions
    $output .= theme('table', $header, $rows);
    $output .= theme('pager', NULL, $limit, 0);
    */
  
    $headervariable =  array(t('Carrier Name'),t('Manufacture Name'),t('Model Name'),t('Tier Name'),t('Delete'),t('Update'));
    //$result = db_query("SELECT deductible_id,carrier_name,manufacture_name,model_name,tier_name FROM {drup_deductible}");
    $entries_per_page=3;
    $select = db_select('drup_deductible', 'pv')->extend('PagerDefault')->extend('TableSort');
    $result = $select
    ->fields('pv', array('deductible_id','carrier_name', 'manufacture_name', 'model_name', 'tier_name'))
    ->limit($entries_per_page)
    ->orderByHeader($headervariable)
    ->execute();
  
    $listitems=array();
    $bigitems = array();
    foreach ($result as $item) {
        watchdog('Deductibe', 'hi', array(), WATCHDOG_DEBUG);
        $listitems=array($item->carrier_name,
        $item->manufacture_name,
        $item->model_name,
        $item->tier_name,
        l('Delete','deductible/deductible/delete/'.$item->deductible_id),
        l('Update','deductible/deductible/update/'.$item->deductible_id)
        );
        $bigitems[]=$listitems;
    }

    //   return print_r($bigitems);
    if (count($listitems) == 0)
    $make[] = array("No Models Entered Yet");
    else{
        watchdog('Deductibe', 'a', array(), WATCHDOG_DEBUG);
    }

    $link_to_add_form = l('Add More Deductible','deductible/add_deductible');
    $variable=array(
             'header' =>  $headervariable,
             'rows'  =>  $bigitems,
             'attributes' => array(),
             'caption' => NULL,
             'colgroups' => NULL,
             'sticky' => NULL,
             'empty' => NULL,
    );
    // return print_r($items);
    $output= '<div id="car">'.theme_table($variable).'<br/>'.$link_to_add_form.'</div>';
    $output .= theme('pager');
    $output .= theme('sort');
    return $output;
  
}

function delete_deductible($content = NULL, $arg1 = NULL, $arg2 = NULL,$arg3=NULL)
{

    global $base_root;
    $url=$base_root . request_uri();
    $output =  '<div>' . $url . '</div>';
    watchdog('Deductibe', 'In delete'.$arg3, array(), WATCHDOG_DEBUG);
    if(isset($arg3)){
        delete_entry($arg3);
    }
    drupal_goto($path = 'deductible/display_deductibles',  array(), $http_response_code = 302);
    //return $output.$arg2;
}

function delete_entry($arg){

    try{
        $num_deleted = db_delete('drup_deductible')
        ->condition('deductible_id', $arg)
        ->execute();
    }catch(Exception $e){
    }
    if($num_deleted!==false){
        drupal_set_message('<div id="msg">'.t('The deductible has been deleted').'</div>');
    //    $form_state['redirect'] = 'deductible/display_deductibles';
    }else{
        drupal_set_message(t('Some issue- Unable to delete'));
    //    $form_state['redirect'] = 'deductible/display_results';
    }
}


/*function adddeductible_form($form, &$form_state){
    $form = array();
    $path = drupal_get_path('module', 'Deductible');
    //$form_state['redirect'] = 'deductilbe/display_results';
    $result = db_query("SELECT manufacture_name FROM {drup_manufacture}");
  
    $manufacture_names = array();
          foreach ($result as $manufacture) {
              $manufacture_names[] = $manufacture->manufacture_name;
          }
  $selected = isset($form_state['values']['manufacture_select']) ? $form_state['values']['manufacture_select'] : key($manufacture_names);
 
  $form['manufacture_select'] = array(
    '#type' => 'select',
    '#title' => t('Manufacture'),
    '#options' => $manufacture_names,
      '#default_value' => $selected,
    '#ajax' => array(
     'callback' => 'deductible_dependent_dropdown_callback',
      'wrapper' => 'dropdown-second-replace',
    ),
  );

  $form['model_select'] = array(
    '#type' => 'select',
    '#title' => t('Model'),
    '#prefix' => '<div id="dropdown-second-replace">',
    '#suffix' => '</div>',
    '#default_value' => isset($form_state['values']['model_select']) ? $form_state['values']['model_select'] : 'Select',
    '#options' => getModelList($selected),
      '#ajax' => array(
     'callback' => 'deductible_dependent_dropdown_callback2',
      'wrapper' => 'dropdown-third-replace',
    ),
   );
 


  
$form['#attached'] = array
    (
    'css' => array
    (
        'type' => 'file',
        'data' => $path . '/root.css',
    ),
    'js' => array
    (
        'type' => 'file',
        'data' => $path . '/root.js',
    ),
    );
  
    $form['#theme'] = 'deductible';
    return $form;
}
*/
/*function deductible_dependent_dropdown_callback($form, $form_state) {
     watchdog('Deductibe', 'In ajax', array(), WATCHDOG_DEBUG);
  return $form['manufacture_select'];
}


*/

/*function deductible_submit($form, &$form_state){
  watchdog('Deductibe', 'In add', array(), WATCHDOG_DEBUG);
  global $user;
  $username = $user->name;
  watchdog('Deductibe', $form_state['values']['manufacture_select'], array(), WATCHDOG_DEBUG);
   $form_state['redirect'] = 'deductible/add_manufacture';
  /*$Make = $form_state['values']['Make'];
  $Model = $form_state['values']['Model'];
  $Deductible = $form_state['values']['Deductible'];
      $insertquery = db_insert('Deductible')->fields(array(
      'username' => $username,
      'Make'=>$form_state['values']['Make'],
       'model'=>$form_state['values']['Model'],
       'deductible'=>$form_state['values']['Deductible']
      ))->execute();
      if($insertquery!==false){
           $message = 'You have submitted the '  . ' form which contains the following data:<pre>' . print_r($form_state['values']['Make'],true) . '</pre>';
         drupal_set_message(t($message));
         $form_state['redirect'] = 'deductible/display_results';
      }else{
          drupal_set_message(t('Error-Contact Administrator'));
      }*/
//}


/*function deductible_menu_page($content = NULL, $arg1 = NULL, $arg2 = NULL,$arg3=NULL)
{
    $result = db_query("SELECT itemid,make,model,deductible FROM {Deductible}");
    $listitems=array();
    $bigitems = array();
    foreach ($result as $item) {
        watchdog('Deductibe', 'hi', array(), WATCHDOG_DEBUG);
        $listitems=array($item->make,
        $item->model,
        $item->deductible,
        l('Delete','deductible/delete/'.$item->itemid),
        l('Update','deductible/update/'.$item->itemid)
        );
        $bigitems[]=$listitems;
    }

    //   return print_r($bigitems);
    if (count($listitems) == 0)
    $make[] = array("No Models Entered Yet");
    else{
        watchdog('Deductibe', 'a', array(), WATCHDOG_DEBUG);
    }

    $link_to_add_form = l('Add More Models','deductible/calculate');
    $variable=array(
             'header' =>  array(t('Make'), t('Model'),t('Deductible'),t('Delete'),t('Update')),
             'rows'  =>  $bigitems,
             'attributes' => array(),
             'caption' => NULL,
             'colgroups' => NULL,
             'sticky' => NULL,
             'empty' => NULL,
    );
    // return print_r($items);
    return theme_table($variable).'<br/>'.$link_to_add_form;
}*/
/*  $form['justtext'] = array(
    '#type' => 'textfield',
    '#title' => t("Who"),
    '#prefix' => '<div id="replace_textfield_div1">',
    '#suffix' => '</div>',
  );*/
/*  if (!empty($form_state['values']['manufacture_select'])) {
    $form['justtext']['#description'] = t("Say why you chose") .  " '{$form_state['values']['manufacture_select']}'";
   /* $selected=$form_state['values']['manufacture_select'];
     $form['model_select']['#options'] = getModelList($selected);*/
 // }

 /*     $form['fs'] = array(
      '#type'=>'fieldset',
      '#attributes'=>array('style'=>'width:50%;'),
      '#weight'=>$weight,
    );
  */


function display_list(){
    $form = array();
    $path = drupal_get_path('module', 'Deductible');
    $form['#attached'] = array
    (
    'css' => array
    (
        'type' => 'file',
        'data' => $path . '/root.css',
    ),
    'js' => array
    (
        'type' => 'file',
        'data' => $path . '/root.js',
    ),
    );
    $form['#theme'] = 'list';
    return $form;
}



2 comments:

  1. hi!,Ι lοve уouг wгiting very much!
    shaге ωe communіcate morе apprοximately your аrtiсle on AOL?
    Ι need an exрert on this area to solѵe mу ρrοblem.

    May be that is уоu! Τakіng
    а looκ аheаd to peеr you.


    Feel free to visit my site - raspberry ketone

    ReplyDelete
  2. Wоw, marvelous blog layout! How long have you bееn blogging
    for? уou made blogging loοk easy. The overall
    lοoκ of youг website iѕ wonԁerful, let
    alоne the сontеnt!

    Look at my web-ѕite ... debt reduction services

    ReplyDelete