Saturday, January 29, 2011

Uploading Files in Drupal

  1. function invoice_perm()
  2. {
  3. return array('access content invoice');
  4. }

  5. function invoice_menu(
  6. {
  7. $items['invoice'] = array(
  8. 'title' => 'Invoice Entry',
  9. 'page callback' => 'invoice_editpage',
  10. 'access callback' =>'user_access',
  11. 'access arguments' => array('access content invoice'),
  12. 'type' => MENU_NORMAL_ITEM,
  13. );
  14. return $items;
  15. }

  16. function invoice_editpage()
  17. {
  18. $output = t('Invoice Entry Form.');

  19. // Return the HTML generated from the $form data structure.
  20. $output .= drupal_get_form('invoice_nameform');
  21. return $output;
  22. }

  23. function invoice_nameform($form_id, $form_state = NULL)
  24. {

  25. //File Refernce name

  26. $form['invoice_title'] = array(
  27. '#type' => 'textfield',
  28. '#title'=>t('Invoice Title'),
  29. '#size'=>20,
  30. );

  31. $form['#attributes'] = array('enctype' => "multipart/form-data");
  32. $form['invoicefileattachment'] = array(
  33. '#type' => 'file',
  34. '#title' => t('Browse the Scan copy of the invoice'),
  35. '#required' => FALSE,
  36. '#size'=>20,
  37. '#default_value' => $form_state['values']['invoicefileattachment'],
  38. );
  39. $form['submit1'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Register'),
  42. );
  43. return $form;
  44. }

  45. function invoice_nameform_submit($form_id, $form_state)
  46. {

  47. global $user;
  48. profile_load_profile($user);
  49. $uid=$user->uid;
  50. $empid=$user->employee_code;
  51. $userip = $_SERVER['REMOTE_ADDR'];
  52. $dt=date('Y-m-d H:i:s');
  53. $state_code=$user->state_code;

  54. if($state_code==30)
  55. $abc='TR';
  56. else
  57. $abc='TN';

  58. $limits = array () ;
  59. $invoice_title = $form_state['values']['invoice_title'];
  60. $po_title=$form_state['values']['po_title'];
  61. $path123=file_directory_path();


  62. $file = file_save_upload('invoicefileattachment', $validators, file_directory_path());

  63. $filename = $file->filename;
  64. $src = file_directory_path() . '/' . $filename;
  65. $dest = $abc.'/'.$filename;
  66. $path_file=$path123.'/'.$abc.'/'.$filename;

  67. if(isset($filename))
  68. {
  69. file_copy($src, $dest, FILE_EXISTS_REPLACE);
  70. file_delete($path123.'/'.$filename);
  71. drupal_set_message (" Invoice File saved", "status");
  72. file_set_status ($file, FILE_STATUS_PERMANENT);
  73. db_query("INSERT into drupal_txn_file_upload(uid ,
  74. filename,
  75. filepath,
  76. filesize,
  77. state_code,
  78. category,
  79. timestamp,title)
  80. VALUES('%d','%s','%s','%d','%d','%s','%d','%s')",$uid,$filename,$path_file,$file_size,$state_code,$category,time(),$invoice_title);
  81. }

Simple Form in Drupal

  1. function transport_services_perm() // used for setting permission
  2. {
  3. return array('access content transport_services');
  4. }

  5. function transport_services_menu() // creating menu
  6. {
  7. $items['transport_services'] = array(
  8. 'page callback' => 'transport_services_page',
  9. 'access arguments' => array('access content transport_services'),
  10. 'access callback' =>'user_access',
  11. ); return $items;
  12. }

  13. function transport_services_page()
  14. {
  15. $output='Welcome to R';
  16. $output .= drupal_get_form('transport_services_form1');
  17. return $output;
  18. }

  19. // Actual form being defined here....

  20. function transport_services_form2($form_id, $form_state = NULL)
  21. {
  22. $form['reg_no'] = array(
  23. '#title' => t('Registration Number:'),
  24. '#type' => 'textfield',
  25. '#size' => '25',
  26. '#required' => TRUE,
  27. '#prefix' => '
    ',
  28. '#suffix' => '',
  29. );
  30. $form['reg_name'] = array(
  31. '#title' => t('Registration Name:'),
  32. '#type' => 'textfield',
  33. '#size' => '25',
  34. '#required' => TRUE,
  35. '#prefix' => '
    ',
  36. '#suffix' => '',
  37. );
  38. $form['submit'] = array(
  39. '#type' => 'button',
  40. '#value' => t('Submit'),
  41. '#validate' => array('transport_services_renewal_output')
  42. );
  43. return $form;
  44. }
  45. // Submit Function is defined....

  46. function reusable_services_form1_submit($form_id,$form_state)
  47. {
  48. $reg_no = $form_state['values']['reg_no'];
  49. $reg_name = $form_state['values']['reg_name'];
  50. db_query("INSERT INTO {registration} (registration_no) VALUES('%d','%s')",$reg_no,$reg_name);
  51. drupal_set_message(t(“No==>”.$reg_no));
  52. drupal_set_message(t(“Name==>”.$reg_name));
  53. drupal_set_message(t(“Successfully inserted values”));
  54. }