Call us on : 1-305-260-6455
Placeholder

Customer Forum

User-contributed Drupal module using web2lead to create leads

Workbooks Support Posted: 2011-11-09 11:01

Thanks to James Poyser for this contribution which I post here.  I haven't used this myself however it is in use by a customer. To quote, it "saves files attached to form in Drupal DB and then submits form and file URL to Workbooks via web to lead".

 

.info file

 

name = Workbooks file uploader

description = Saves files attached to form in Drupal DB and then submits form and file URL to Workbooks via web to lead

core = 6.x

version = "6.x-1.0"

 

.module file

 

<?php
/*
	This module allows you to submit a Workbooks web to lead form with file attachments.
	The module will save the files using the Drupal upload module then inject the file
	URLs into the Workbooks form, before posting to the Workbooks web app.

	Usage:
		*	Create a web form (static html) and point it to /workbooks_processor (don't forget to set the form type to multipart/form-data)
		* Edit workbooks_processor (below) to include you file fields and the URL for success
		
*/


 /**
 * Implementation of hook_menu().
 */
function workbooks_upload_menu() {

	$items['workbooks_processor'] = array(
		'page callback' => 'workbooks_processor',
		'type' => MENU_CALLBACK,
		'access arguments' => array('access content'),
	);	

  return $items;
}



/* Main function - saves files and submits to workbooks */
function workbooks_processor(){
	global $base_url;
	
	// If files have been uploaded, save them to the files collection
	// and get the public URL for them:
	
	$upload_url = "";
	
	if ($_FILES['files']['error']['upload'] == 0 ) {
		$upload = file_save_upload('upload', array(), file_directory_path() . '/workbooks', FILE_EXISTS_RENAME);
		file_set_status($upload, FILE_STATUS_PERMANENT);		// Save it
		$upload_url = $base_url . "/" . $upload->filepath;	// Get the full URL
	}
	
	// Get the form string
	$post_string = form_walker($_POST, 0);
	
	// Inject the URLS of the files
	if ($upload_url != "") {
		$post_string .= "&INSERT_WORKBOOKS_FIELD_NAME_HERE=" . urlencode($upload_url);
	}
	
	// You can repeat the above if you have more than one file upload to handle.
	
	//create cURL connection
	$curl_connection = curl_init('https://secure.workbooks.com/crm/web_leads');

	//set options
	curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
	curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 0);

	//set data to be posted
	curl_setopt($curl_connection, CURLOPT_POST, 2);
	curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

	//perform our request
	$result = curl_exec($curl_connection);
	
	// Check if we've succeeded
	if(stristr($result, 'failed') === FALSE) {
		// Success: redirect them.
		drupal_goto('ENTER_RETURN_URL_HERE');	// e.g. 'success' would redirect to http://www.yousite.com/success
	} else {
		// Failed: return the response from Workbooks
		return $result;
	}

}


// Flattens a multidimension form array into a string
// e.g. item1[a][b]=c
function form_walker($value, $level, $name = "") {
	$out = "";
	if ($level == 0) {
		foreach ($value as $key => $value) {
			if ($key != 'files') {
				$out .= form_walker($value, 1, $key);				
			}
		}
	} else {
		if (gettype($value) != 'array') {
				$out .= $name . "=" . urlencode(get_magic_quotes_gpc() ? stripslashes($value) : $value) . "&";
		} else {
			foreach ($value as $key => $value) {
				$out .= form_walker($value, $level + 1, $name . urlencode("[" . $key . "]"));
			}
		}
		
	}
	
	return $out;
	
}