View source
<?php
define('PARDOT_STATUS_NEW', 'new');
define('PARDOT_STATUS_FAILED', 'failed');
define('PARDOT_STATUS_UPLOADED', 'uploaded');
function pardot_theme() {
return array(
'pardot_js' => array(
'arguments' => array(),
'template' => 'pardot-js',
),
'pardot_webform_components_form' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'pardot.admin.inc',
),
'pardot_admin_scoring' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'pardot.admin-scoring.inc',
),
'pardot_admin_campaign' => array(
'arguments' => array(
'form' => array(),
),
'file' => 'pardot.admin-campaign.inc',
),
);
}
function pardot_menu() {
$items = array();
$items['admin/settings/pardot'] = array(
'title' => 'Pardot',
'description' => 'Create and edit Pardot settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_form',
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'pardot.admin.inc',
);
$items['admin/settings/pardot/general'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/pardot/scoring'] = array(
'title' => 'Scoring',
'description' => 'Create and edit Pardot scoring settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_scoring',
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_LOCAL_TASK,
'file' => 'pardot.admin-scoring.inc',
);
$items['admin/settings/pardot/scoring/%pardot_scoring/edit'] = array(
'title' => 'Scoring',
'description' => 'Create and edit Pardot scoring settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_scoring_edit',
4,
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_CALLBACK,
'file' => 'pardot.admin-scoring.inc',
);
$items['admin/settings/pardot/scoring/%pardot_scoring/delete'] = array(
'title' => 'Scoring',
'description' => 'Create and edit Pardot scoring settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_scoring_delete',
4,
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_CALLBACK,
'file' => 'pardot.admin-scoring.inc',
);
$items['admin/settings/pardot/campaign'] = array(
'title' => 'Campaigns',
'description' => 'Create and edit Pardot campaign settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_campaign',
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_LOCAL_TASK,
'file' => 'pardot.admin-campaign.inc',
);
$items['admin/settings/pardot/campaign/%pardot_campaign/edit'] = array(
'title' => 'Campaign',
'description' => 'Create and edit Pardot campaign settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_campaign_edit',
4,
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_CALLBACK,
'file' => 'pardot.admin-campaign.inc',
);
$items['admin/settings/pardot/campaign/%pardot_campaign/delete'] = array(
'title' => 'Campaign',
'description' => 'Create and edit Pardot campaign settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_admin_campaign_delete',
4,
),
'access arguments' => array(
'administer pardot',
),
'type' => MENU_CALLBACK,
'file' => 'pardot.admin-campaign.inc',
);
$items['node/%webform_menu/webform/pardot'] = array(
'title' => 'Pardot',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'pardot_webform_components_form',
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'file' => 'pardot.admin.inc',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function pardot_perm() {
return array(
'administer pardot',
);
}
function pardot_help($path, $arg) {
switch ($path) {
case 'admin/settings/pardot':
return '<p>' . t('For help finding tracking code, follow the !instructions found on the Pardot help site', array(
'!instructions' => l('instructions', 'http://help.pardot.com/faqs/campaigns/tracking-code'),
)) . '</p>';
}
}
function pardot_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'webform_client_form') !== 0) {
return;
}
$nid = $form['details']['nid']['#value'];
if (empty($nid)) {
return;
}
$form_settings = pardot_webform_load($nid);
if (empty($form_settings) || !$form_settings->is_active) {
return;
}
$form['#submit'] = is_array($form['#submit']) ? $form['#submit'] : array(
'webform_client_form_submit ',
);
$form['#submit'][] = 'pardot_webform_submit';
}
function pardot_footer() {
drupal_add_js(theme('pardot_js'), 'inline', 'footer');
$output = '';
if (isset($_SESSION['pardot_submission'])) {
$submission = pardot_submission_load($_SESSION['pardot_submission']);
$submission->status = PARDOT_STATUS_UPLOADED;
pardot_submission_save($submission);
$form_settings = pardot_webform_load($submission->form_nid);
$url = $form_settings->url;
$post_fields = $submission->data['post_fields'];
foreach ($post_fields as $key => $value) {
if (!empty($form_settings->data[$key])) {
$result[$form_settings->data[$key]['key']] = $value;
}
else {
$result[$key] = $value;
}
}
$url = url($form_settings->url, array(
'query' => $result,
));
$output .= '<iframe src="' . $url . '" width="1" height="1"></iframe>';
unset($_SESSION['pardot_submission']);
}
return $output;
}
function template_preprocess_pardot_js(&$vars) {
$vars['pardot_a_id'] = variable_get('pardot_a_id', '');
$results = db_query('SELECT campaign_id, paths FROM {pardot_campaign}');
while ($row = db_fetch_object($results)) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $row->paths);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $row->paths);
}
if ($page_match) {
$vars['pardot_c_id'] = $row->campaign_id;
break;
}
}
if (empty($vars['pardot_c_id'])) {
$vars['pardot_c_id'] = variable_get('pardot_c_id', '');
}
$path = drupal_get_path_alias($_GET['q']);
$score = db_result(db_query("SELECT score FROM {pardot_scoring} WHERE path = '%s'", $path));
if (!$score) {
$score = db_result(db_query("SELECT score FROM {pardot_scoring} WHERE path = '%s'", $_GET['q']));
}
$vars['score'] = $score;
}
function pardot_scoring_load($scoring_id) {
return db_fetch_object(db_query('SELECT * FROM {pardot_scoring} WHERE scoring_id = %d', $scoring_id));
}
function pardot_campaign_load($campaign_id) {
return db_fetch_object(db_query('SELECT * FROM {pardot_campaign} WHERE campaign_id = %d', $campaign_id));
}
function pardot_webform_submit($form, &$form_state) {
$values = array();
$values['user_headers'] = _pardot_get_user_headers();
$values['post_fields'] = _pardot_form_collapse($form, $form_state);
$submission = new stdClass();
$submission->form_nid = $form['#node']->nid;
$submission->data = $values;
pardot_submission_save($submission);
if (!$submission->sid) {
$message = 'Unable to save submitted entry. <pre>!data</pre>';
$vars = array(
'!data' => var_export($submission, TRUE),
);
watchdog('pardot', $message, $vars, WATCHDOG_NOTICE);
}
else {
$_SESSION['pardot_submission'] = $submission->sid;
}
}
function _pardot_get_user_headers() {
$headers = array();
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
}
else {
foreach ($_SERVER as $k => $v) {
if (substr($k, 0, 5) == 'HTTP_') {
$k = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($k, 5)))));
$headers[$k] = $v;
}
}
}
if (!$headers) {
return array();
}
foreach ($headers as $key => $value) {
$headers[strtolower($key)] = $value;
}
unset($headers['host']);
unset($headers['cookie']);
return $headers;
}
function pardot_webform_load($nid) {
$record = db_fetch_object(db_query('SELECT * FROM {pardot_webform} WHERE nid = %d', $nid));
if ($record) {
$record->data = unserialize($record->data);
}
return $record;
}
function pardot_submission_save($submission) {
if (!isset($submission->submitted)) {
$submission->submitted = $_SERVER['REQUEST_TIME'];
}
if (!isset($submission->status)) {
$submission->status = PARDOT_STATUS_NEW;
}
if (isset($submission->sid)) {
drupal_write_record('pardot_submissions', $submission, array(
'sid',
));
}
else {
drupal_write_record('pardot_submissions', $submission);
}
}
function pardot_submission_load($sid) {
$object = db_fetch_object(db_query('SELECT * FROM {pardot_submissions} WHERE sid = %d', $sid));
$object->data = unserialize($object->data);
return $object;
}
function pardot_submission_delete($sid) {
db_query('DELETE FROM {pardot_submissions} WHERE sid = %d', $sid);
}
function _pardot_form_collapse($form, $form_state) {
$result = array();
$form_tree = !empty($form_state['values']['submitted_tree']) ? $form_state['values']['submitted_tree'] : $form_state['values']['submitted'];
_pardot_form_collapse_form($form_tree, $form_state['values']['submitted'], $form['submitted'], $result);
return $result;
}
function _pardot_form_collapse_form($tree, $posted_values, $form, &$result) {
foreach ($tree as $name => $value) {
if (is_array($value) && !in_array($value, $posted_values)) {
_pardot_form_collapse_form($value, $posted_values, $form[$name], $result);
}
elseif (is_array($value)) {
if (count($value) == 3 && $value[2] > 1900 && $value[2] < 2100 && checkdate($value[0], $value[1], $value[2])) {
$result[$name] = implode('/', $value);
}
else {
$result[$name] = implode(',', $value);
}
}
elseif ($form[$name]['#type'] == 'select') {
$result[$name] = $form[$name]['#options'][$value];
}
else {
$result[$name] = $value;
}
}
}