You are here

pardot_webform.admin.inc in Pardot Integration 7.2

Webform admin file.

File

pardot_webform/pardot_webform.admin.inc
View source
<?php

/**
 * @file
 *
 * Webform admin file.
 */

/**
 * Form constructor for the pardot webform management form.
 *
 * @param array $form
 * @param array $form_state
 * @param node $webform_node
 *   The webform node pardot information is attached to.
 *
 * @see pardot_webform_components_form_validate()
 * @see pardot_webform_components_form_submit()
 * @see pardot_webform_components_form_delete_submit()
 *
 * @ingroup forms
 *
 * @return array
 */
function pardot_webform_components_form($form, $form_state, $webform_node) {
  $form = array();
  $form['#tree'] = TRUE;
  $form['#node'] = $webform_node;
  $nid = $webform_node->nid;
  $record = pardot_webform_load($nid);
  $form['#record'] = $record;
  if (!isset($record->is_active)) {
    $record = new stdClass();
    $record->is_active = "";
    $record->url = "";
  }
  $form['details'] = array(
    '#type' => 'fieldset',
    '#title' => t('General'),
  );
  $form['details']['is_active'] = array(
    '#title' => 'Is active',
    '#type' => 'checkbox',
    '#default_value' => $record->is_active,
  );
  $form['details']['url'] = array(
    '#title' => 'Post url',
    '#type' => 'textfield',
    '#default_value' => $record->url,
    '#description' => t('Visit your "Form Handlers" page in Pardot. Click on a form link and then copy the "Endpoint URL" value here.<br /><br />!example', array(
      '!example' => theme('image', array(
        'path' => drupal_get_path('module', 'pardot') . '/pardot_webform/form-handler-url.jpg',
      )),
    )),
  );
  $form['components'] = array(
    '#tree' => TRUE,
  );
  foreach ($webform_node->webform['components'] as $k => $component) {
    $form['components'][$k] = array(
      '#component' => $component,
      'key' => array(
        '#title' => 'Field name',
        '#type' => 'textfield',
        '#size' => 25,
        '#default_value' => isset($record->data[$component['form_key']]['key']) ? $record->data[$component['form_key']]['key'] : '',
      ),
    );
  }
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#submit' => array(
      'pardot_webform_components_form_delete_submit',
    ),
  );
  return $form;
}

/**
 * Theme callback for pardot_webform_components_form().
 */
function theme_pardot_webform_components_form($form) {
  $form = $form['form'];
  $rows = array();
  $output = '';
  $header = array(
    t('Name'),
    t('Type'),
    t('Pardot key'),
  );
  foreach (element_children($form['components']) as $k) {
    $row = array();

    // Name.
    $row[] = $form['#node']->webform['components'][$k]['name'];

    // Type.
    $row[] = $form['#node']->webform['components'][$k]['type'];

    // Pardot key.
    unset($form['components'][$k]['key']['#title']);
    $row[] = drupal_render($form['components'][$k]['key']);
    $rows[] = $row;
  }
  $output .= drupal_render($form['details']);
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Form validation handler for pardot_webform_components_form().
 *
 * @see pardot_webform_components_form_submit()
 */
function pardot_webform_components_form_validate($form, $form_state) {

  // Check if we should validate the form handler.
  if (variable_get('pardot_validate_url', 1)) {

    // Just check if the URL is active. No validation done if it is not.
    if ($form_state['values']['details']['is_active'] == 1) {

      // If so, check if it's a valid url.
      $check_url = $form_state['values']['details']['url'];

      // Send data.
      $result = drupal_http_request($check_url, array(
        'timeout' => 5,
      ));
      if ($result->code != 200) {
        form_set_error('url', t('This is not a valid Pardot Post url.'));
      }
      else {

        // Check to see if the Pardot cookie exists.
        if (!strstr($result->headers['set-cookie'], 'pardot')) {
          form_set_error('url', t('No cookie set, this is not a Pardot Post url.'));
        }
        else {
          drupal_set_message(t('Valid Pardot Post url.'));
        }
      }
    }
    else {

      // Print out message.
      drupal_set_message(t('No validation done for none active Post URL'), 'status');
    }
  }
}

/**
 * Form submission handler for pardot_webform_components_form().
 *
 * Saves the pardot post url and webform information to the database.
 *
 * @see pardot_webform_components_form_validate()
 */
function pardot_webform_components_form_submit($form, $form_state) {
  $node = $form['#node'];
  $record = $form['#record'];
  if ($record) {
    $update = array(
      'nid',
    );
  }
  else {
    $record = new stdClass();
    $update = array();
  }
  $data = array();
  foreach (element_children($form['components']) as $k) {
    $component = $form['components'][$k]['#component'];
    $data[$component['form_key']]['key'] = $form_state['values']['components'][$k]['key'];
  }

  // Will will replace any previous entries.
  db_delete('pardot_webform')
    ->condition('nid', $node->nid)
    ->execute();
  db_insert('pardot_webform')
    ->fields(array(
    'nid' => $node->nid,
    'url' => $form_state['values']['details']['url'],
    'is_active' => (bool) $form_state['values']['details']['is_active'],
    'data' => serialize($data),
  ))
    ->execute();
}

/**
 * Form submission handler for pardot_webform_components_form().
 *
 * Deletes the pardot post url from the database for the node.
 *
 * @see pardot_webform_components_form_validate()
 */
function pardot_webform_components_form_delete_submit($form, &$form_state) {
  $node = $form['#node'];
  db_delete('pardot_webform')
    ->condition('nid', $node->nid)
    ->execute();
  drupal_set_message(t('Pardot settings for this webform deleted.'), 'status');
}