Drupal download files: Add confirmation to downloads

This CodeLet adds a confirmation form when a user tries to dowload a file from a Drupal site. The data then saved in the db and also mailed to the administrator. Once the form is submitted, file dowload starts. File download start with Jquery after 3 seconds. Administrator can also view all the form submissions.

CodeLet: 

.install file



/**
 * 
 * 
 * Create scheema for downlo addon module
 * 
 *  DrupalD 
 */


/**
 * Implemenation of hook_schema
 * 
 *  DrupalD 
 */
function download_addon_schema() {
	$schema['download_addon_user_data'] = array(
	  'fields' => array(
	    'id' => array(
	      'type' => 'serial',
	      'lenght' => 11,
	    ),
	    
	    'fid' => array(
	      'type' => 'int',
	      'lenght' => 11,
	    ),
	    
	    'uid' => array(
	      'type' => 'int',
	      'lenght' => 11,
	    ),
	    
	    'user_details' => array(
	      'type' => 'text',
	      'not null' => TRUE,
	    ),
	    
	    'created' => array(
        'type' => 'int',
        'lenght' => 11,
      ),
	  ),
	  'primary key' => array('id'),
	);
	
	return $schema;
}

.module file



/**
 * 
 * 
 * Adds extra fields to file download form.
 * 
 *  DrupalD 
 */


/**
 * Implementation of hook_menu
 * 
 *  DrupalD 
 */
function download_addon_menu() {
	$items = array();
	$items['thankyou'] = array(
	  'type' => MENU_CALLBACK,
	  'page callback' => 'download_addon_thankyou',
	  'access callback' => TRUE,
	);
	
	$items['brochure-request'] = array(
	  'type' => MENU_CALLBACK,
	  'page callback' => 'download_addon_brochure_overview',
	  'access callback' => TRUE,
	);	
	
	$items['brochure-request/%'] = array(
	  'type' => MENU_CALLBACK,
	  'page callback' => 'download_addon_brochure_overview',
	  'page arguments' => array(1),
	  'access callback' => TRUE,
	);	
	
	
	$items['redirect/%'] = array(
	  'type' => MENU_CALLBACK,
	  'page callback' => 'download_addon_get_file',
	  'page arguments' => array(1),
	  'access callback' => TRUE,
	);
	
	$items['confirm/%/%'] = array(
	  'type' => MENU_CALLBACK,
	  'page callback' => 'drupal_get_form',
	  'page arguments' => array('_get_user_form', 1),
	  'access callback' => TRUE,
	);
	return $items;
}



/**
 * Implementation of hook_file_download
 * 
 *  $uri
 *  DrupalD 
 */
function download_addon_file_download($uri) {
	return array('Content-Type' => file_get_mimetype($uri));
}


/**
 * _get_user_form
 * 
 *  $form
 *  $form_status
 *  $sn__fid
 *  $ss__filename
 * 
 *  DrupalD 
 */
function _get_user_form($form, $form_status, $sn__fid) {
	module_load_include('inc', 'popup', 'includes/popup.api');
	$form['form_id'] = 'user_download_confirm';
	$form[''] = "";
	$form['fid'] = array(
	  '' => 'hidden',
	  '' => $sn__fid,
	);
	
	$form['help_text'] = array(
	  '' => '

All demonstrations are conducted from MUMBAI, INDIA having time zone as UTC+5.30.
We are conducting demonstration 24 hours a day and all the 7 days a week. If client needs presentation in KANSAS-USA AT 10 AM ( KANSAN USA time) and following are the time difference :
For conversion of time zone pl use : http://www.timeanddate.com/worldclock/converter.html
Mumbai (India - Maharashtra)
Tuesday, 14 May 2013, 00:00:00
IST
UTC+5:30 hours
Topeka (U.S.A. - Kansas)
Monday, 13 May 2013, 13:30:00
CDT
UTC-5 hours
Date : 15 april 2013
Local time : 10 am or (24 hours time )
TIME ZONE : UTC + OR UTC- ( FOR EXAMPLE For India local time is UTC-5 )
You will get the mail on your e-mail id upon clicking the link you will be connected to the secession where you can listen demonstration and see the live demonstration.
Pre requisite for the demonstration:
Computer/laptop/smart mobile
With internet connection and audio visual facility (Speaker and mike).

', '' => -12, ); $form['abbrivation'] = array( '' => 'select', '' => t('Abbreviation'), '' => TRUE, '' => array( 'Mr.' => t('Mr.'), 'Mrs.' => t('Mrs.'), 'Ms.' => t('Ms.'), ), '' => -11, ); $form['first_name'] = array( '' => 'textfield', '' => t('First name'), '' => TRUE, '' => -10, ); $form['middle_name'] = array( '' => 'textfield', '' => t('Middle name'), '' => TRUE, '' => -9, ); $form['last_name'] = array( '' => 'textfield', '' => t('Last name'), '' => TRUE, '' => -8, ); $form['organization'] = array( '' => 'textfield', '' => t('Organization'), '' => TRUE, '' => -7, ); $form['contact_number'] = array( '' => 'textfield', '' => t('Contact number'), '' => TRUE, '' => -7, ); $form['email'] = array( '' => 'textfield', '' => t('Email'), '' => TRUE, '' => -6, ); $form['brochure_for'] = array( '' => 'checkboxes', '' => t('Need demonstration/Brochure for'), '' => TRUE, '' => array( 'AUTOSPOOL' => t('AUTOSPOOL'), 'SPOOLMAN/ERMAN' => t('SPOOLMAN/ERMAN'), 'ISOMEC' => t('ISOMEC'), 'MATCONTRK/STOREMAN' => t('MATCONTRK/STOREMAN'), ), '' => -5, ); $form['demonstration'] = array( '' => 'fieldset', '' => t('When you would like to see the demonstration?'), '' => TRUE, '' => -4, ); $form['demonstration']['demo_date'] = array( '' => 'textfield', '' => t('Date'), '' => t('Format: dd/mm/YYYY'), '' => TRUE, '' => -10, ); $form['demonstration']['localtime'] = array( '' => 'textfield', '' => t('Local time'), '' => TRUE, '' => -9, ); $form['demonstration']['timezone'] = array( '' => 'radios', '' => t('TIME ZONE'), '' => TRUE, '' => array( 'UTC+' => t('UTC+'), 'UTC-' => t('UTC-'), ), '' => t('For conversion of time zone pl use : ', array('' => 'http://www.timeanddate.com/worldclock/converter.html')), '' => -8, ); $form['city'] = array( '' => 'textfield', '' => t('City/Town'), '' => TRUE, '' => -3, ); $form['country'] = array( '' => 'textfield', '' => t('Country'), '' => TRUE, '' => -2, ); $form['send_product_brochure'] = array( '' => 'checkbox', '' => t('Please send me product brochure'), '' => -1, ); $form['contact_me'] = array( '' => 'checkbox', '' => t('Please contact me on phone/email'), '' => 0, ); $form['your_comments'] = array( '' => 'textarea', '' => t('Any other comments'), '' => 1, ); $form['remark'] = array( '' => 'textarea', '' => t('Remark'), '' => 2, ); $form['submit'] = array( '' => 'submit', '' => t('Submit'), '' => 3 ); return $form; } /** * _get_user_form_submit * * $form * $form_state * DrupalD */ function _get_user_form_submit($form, &$form_state) { global $user; foreach ($form_state['values']['brochure_for'] as $key => $value) { if (is_string($value)) { $as__brochure_for[] = $value; } } $sn__fid = $form_state['values']['fid']; $am__values = array( 'abbrivation' => $form_state['values']['abbrivation'], 'first_name' => $form_state['values']['first_name'], 'middle_name' => $form_state['values']['middle_name'], 'last_name' => $form_state['values']['last_name'], 'organization' => $form_state['values']['organization'], 'contact_number' => $form_state['values']['contact_number'], 'email' => $form_state['values']['email'], 'brochure_for' => implode(', ', $as__brochure_for), 'demo_date' => $form_state['values']['demo_date'], 'local_time' => $form_state['values']['localtime'], 'time_zone' => $form_state['values']['timezone'], 'city' => $form_state['values']['city'], 'country' => $form_state['values']['country'], 'send_product_brochure' => $form_state['values']['send_product_brochure'] != '' ? t('Yes') : t('No'), 'contact_me' => $form_state['values']['contact_me'] != '' ? t('Yes') : t('No'), 'your_comments' => $form_state['values']['your_comments'], 'remark' => $form_state['values']['remark'], ); $sn__id = db_insert('download_addon_user_data') ->fields(array( 'fid' => $sn__fid, 'uid' => $user->uid, 'user_details' => serialize($am__values), 'created' => time('now'), )) ->execute(); $am__values['fid'] = $sn__fid; $ss__to = variable_get('site_mail', ''); //info@eurekadsoft.com $message = drupal_mail('download_addon', 'download_addon_user_data', $ss__to, language_default(), $am__values, $ss__to); if($message['result'] == 'sent') { watchdog('download_addon', 'Request submitted by %sender', array('%sender' => $am__values['email'])); drupal_set_message(t('Your request submitted successfully!')); } drupal_goto('thankyou', array('query' => array('fid' => $sn__fid))); } /** * download_addon_thankyou * * $sn__fid * DrupalD */ function download_addon_thankyou() { global $user; $sn__fid = $_GET['fid']; unset($am__details); $ss__node_title = _get_node_title($sn__fid); $output = t('Thank you for downloading file on ', array('' => $ss__node_title)) .'
';; $output = t('Download should start shortly') .'
';; $output .= t('Here are your submitted details:') .'
'; $result = _get_user_details($sn__fid); $am__details = unserialize($result['user_details']); foreach ($am__details as $key => $value) { $row[] = array( drupal_ucfirst(str_replace('_', ' ', $key)), $value, ); } $attributes = array('width' => '100%'); $output .= theme('table', array('header' => array(),'rows' => $row, 'attributes' => $attributes)); $file = db_query('SELECT filename FROM {file_managed} WHERE fid = :fid', array(':fid' => $sn__fid))->fetchAssoc(); drupal_add_js(array('download_addon' => array('fid' => $sn__fid)), 'setting'); drupal_add_js(array('download_addon' => array('file_name' => $file['filename'])), 'setting'); drupal_add_js(drupal_get_path('module', 'download_addon') .'/download_addon.js', 'file'); return $output; } /** * _get_user_details * * $sn__fid * DrupalD */ function _get_user_details($sn__fid) { global $user; $result = db_select('download_addon_user_data', 'du') ->fields('du') ->condition('fid', $sn__fid, '=') ->condition('uid', $user->uid, '=') ->orderBy('created', 'DESC') ->range(0,1) ->execute() ->fetchAssoc(); return $result; } /** * Implementation of hook_mail * $key * $message * $params * DrupalD */ function download_addon_mail($key, &$message, $params) { switch ($key) { case 'download_addon_user_data': $ss__node_title = _get_node_title($params['fid']); unset($params['fid']); foreach ($params as $key => $value) { if (isset($value)) { $row[] = array( drupal_ucfirst(str_replace('_', ' ', $key)), $value, ); } } $attributes = array('width' => '100%'); $output = theme('table', array('header' => array(),'rows' => $row, 'attributes' => $attributes)); $message['subject'] = t('Inquiry: ', array('' => $ss__node_title)); $message['body'][] = $output; if (isset($params['headers']) && is_array($params['headers'])) { $message['headers'] += $params['headers']; } break; } } /** * _get_node_title * * unknown_type $sn__fid * DrupalD */ function _get_node_title($sn__fid) { $query = db_select('field_data_field_downloads', 'fdd'); $query->join('node', 'n', 'n.nid = fdd.entity_id'); $query->fields('n', array('title')) ->condition('fdd.field_downloads_fid', $sn__fid, '=') ->condition('fdd.entity_type', 'node', '=') ->condition('fdd.bundle', 'products', '='); $result = $query->execute(); $record = $result->fetchAssoc(); return $record['title']; } /** * download_addon_brochure_overview * * DrupalD */ function download_addon_brochure_overview($sn__id = NULL) { if ($sn__id == '') { $header = array(t('Email'), t('Contact number'), t('Name'), t('Product'), t('Demo date')); $result = db_select('download_addon_user_data', 'du') ->fields('du') ->execute(); $row[] = array('data' => array(t('Email'), t('Contact number'), t('Name'), t('Product'), t('Demo date'))); while ($record = $result->fetchAssoc()) { $am__details = unserialize($record['user_details']); $row[] = array( l($am__details['email'], 'brochure-request/'. $record['id']), $am__details['contact_number'], $am__details['first_name'] .' '. $am__details['last_name'], _get_node_title($record['fid']), $am__details['demo_date'], ); } $attributes = array('width' => '100%'); return theme('table', array('header' => array(), 'rows' => $row, 'attributes' => $attributes)); } else { $result = db_select('download_addon_user_data', 'du') ->fields('du') ->condition('id', $sn__id, '=') ->execute() ->fetchAssoc(); $am__details = unserialize($result['user_details']); foreach ($am__details as $key => $value) { $row[] = array( drupal_ucfirst(str_replace('_', ' ', $key)), $value, ); } $attributes = array('width' => '100%'); return l(t(''. theme('table', array('header' => array(),'rows' => $row, 'attributes' => $attributes)); } } /** * download_addon_get_file * * unknown_type $sn__fid * DrupalD */ function download_addon_get_file($sn__fid) { $file = db_query('SELECT filename FROM {file_managed} WHERE fid = :fid', array(':fid' => $sn__fid))->fetchAssoc(); $headers = array( 'Pragma' => 'hack', 'Cache-Control' => 'public, must-revalidate', 'Content-Type' => $file['filemime'], 'Content-Disposition' => 'attachment', 'Content-Transfer-Encoding' => 'binary', ); file_transfer($file['filename'], $headers); }

.js file

jQuery(document).ready(function (){
var myDown = setInterval(function(){get_file()},3000);
var file_id = Drupal.settings.download_addon.fid;
var file_name = Drupal.settings.download_addon.file_name;
function get_file() {
$.ajax({
type: "POST",
url: "resource/" + file_id,
//data: wireIdList,
cache: false,
success: function(response)
{
window.open(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('Error occurred while downlodng file');
}
});
clearInterval(myDown);
}
});

Drupal download files: Add confirmation to downloads | Drupal Developer

By DrupalD on Mon, 10/06/2013 - 5:17pm

Add comment

Log in or register to post comments

Try GSuite and get 20% discount for first year

Feedback